← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~jtv/maas/maascli-split-auth into lp:maas

 

Jeroen T. Vermeulen has proposed merging lp:~jtv/maas/maascli-split-auth into lp:maas.

Commit message:
Split auth-related code out of maascli.api.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~jtv/maas/maascli-split-auth/+merge/128419

This was in the way of a structural rearrangement I'm trying to make to the CLI in order to make it more user-friendly.  There's too much code in this module to move about conveniently.  I'll be splitting the CLI commands away from the API commands next.


Jeroen
-- 
https://code.launchpad.net/~jtv/maas/maascli-split-auth/+merge/128419
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~jtv/maas/maascli-split-auth into lp:maas.
=== modified file 'src/maascli/api.py'
--- src/maascli/api.py	2012-10-05 18:44:46 +0000
+++ src/maascli/api.py	2012-10-08 05:49:21 +0000
@@ -16,7 +16,6 @@
     ]
 
 from email.message import Message
-from getpass import getpass
 import httplib
 from itertools import chain
 import json
@@ -26,14 +25,12 @@
     urlparse,
     )
 
-from apiclient.creds import (
-    convert_string_to_tuple,
-    convert_tuple_to_string,
-    )
+from apiclient.creds import convert_tuple_to_string
 from apiclient.maas_client import MAASOAuth
 from apiclient.multipart import encode_multipart_data
 from apiclient.utils import ascii_url
 import httplib2
+from maascli.auth import obtain_credentials
 from maascli.command import (
     Command,
     CommandError,
@@ -48,32 +45,6 @@
     )
 
 
-def try_getpass(prompt):
-    """Call `getpass`, ignoring EOF errors."""
-    try:
-        return getpass(prompt)
-    except EOFError:
-        return None
-
-
-def obtain_credentials(credentials):
-    """Prompt for credentials if possible.
-
-    If the credentials are "-" then read from stdin without interactive
-    prompting.
-    """
-    if credentials == "-":
-        credentials = sys.stdin.readline().strip()
-    elif credentials is None:
-        credentials = try_getpass(
-            "API key (leave empty for anonymous access): ")
-    # Ensure that the credentials have a valid form.
-    if credentials and not credentials.isspace():
-        return convert_string_to_tuple(credentials)
-    else:
-        return None
-
-
 def fetch_api_description(url):
     """Obtain the description of remote API given its base URL."""
     url_describe = urljoin(url, "describe/")

=== added file 'src/maascli/auth.py'
--- src/maascli/auth.py	1970-01-01 00:00:00 +0000
+++ src/maascli/auth.py	2012-10-08 05:49:21 +0000
@@ -0,0 +1,46 @@
+# Copyright 2012 Canonical Ltd.  This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+"""MAAS CLI authentication."""
+
+from __future__ import (
+    absolute_import,
+    print_function,
+    unicode_literals,
+    )
+
+__metaclass__ = type
+__all__ = [
+    'obtain_credentials',
+    ]
+
+from getpass import getpass
+import sys
+
+from apiclient.creds import convert_string_to_tuple
+
+
+def try_getpass(prompt):
+    """Call `getpass`, ignoring EOF errors."""
+    try:
+        return getpass(prompt)
+    except EOFError:
+        return None
+
+
+def obtain_credentials(credentials):
+    """Prompt for credentials if possible.
+
+    If the credentials are "-" then read from stdin without interactive
+    prompting.
+    """
+    if credentials == "-":
+        credentials = sys.stdin.readline().strip()
+    elif credentials is None:
+        credentials = try_getpass(
+            "API key (leave empty for anonymous access): ")
+    # Ensure that the credentials have a valid form.
+    if credentials and not credentials.isspace():
+        return convert_string_to_tuple(credentials)
+    else:
+        return None

=== modified file 'src/maascli/tests/test_api.py'
--- src/maascli/tests/test_api.py	2012-10-05 09:41:33 +0000
+++ src/maascli/tests/test_api.py	2012-10-08 05:49:21 +0000
@@ -16,9 +16,7 @@
 import httplib
 import io
 import json
-import sys
 
-from apiclient.creds import convert_tuple_to_string
 import httplib2
 from maascli import (
     api,
@@ -102,53 +100,6 @@
 class TestFunctions(TestCase):
     """Test for miscellaneous functions in `maascli.api`."""
 
-    def test_try_getpass(self):
-        getpass = self.patch(api, "getpass")
-        getpass.return_value = sentinel.credentials
-        self.assertIs(sentinel.credentials, api.try_getpass(sentinel.prompt))
-        getpass.assert_called_once_with(sentinel.prompt)
-
-    def test_try_getpass_eof(self):
-        getpass = self.patch(api, "getpass")
-        getpass.side_effect = EOFError
-        self.assertIsNone(api.try_getpass(sentinel.prompt))
-        getpass.assert_called_once_with(sentinel.prompt)
-
-    @staticmethod
-    def make_credentials():
-        return (
-            factory.make_name("cred"),
-            factory.make_name("cred"),
-            factory.make_name("cred"),
-            )
-
-    def test_obtain_credentials_from_stdin(self):
-        # When "-" is passed to obtain_credentials, it reads credentials from
-        # stdin, trims whitespace, and converts it into a 3-tuple of creds.
-        credentials = self.make_credentials()
-        stdin = self.patch(sys, "stdin")
-        stdin.readline.return_value = (
-            convert_tuple_to_string(credentials) + "\n")
-        self.assertEqual(credentials, api.obtain_credentials("-"))
-        stdin.readline.assert_called_once()
-
-    def test_obtain_credentials_via_getpass(self):
-        # When None is passed to obtain_credentials, it attempts to obtain
-        # credentials via getpass, then converts it into a 3-tuple of creds.
-        credentials = self.make_credentials()
-        getpass = self.patch(api, "getpass")
-        getpass.return_value = convert_tuple_to_string(credentials)
-        self.assertEqual(credentials, api.obtain_credentials(None))
-        getpass.assert_called_once()
-
-    def test_obtain_credentials_empty(self):
-        # If the entered credentials are empty or only whitespace,
-        # obtain_credentials returns None.
-        getpass = self.patch(api, "getpass")
-        getpass.return_value = None
-        self.assertEqual(None, api.obtain_credentials(None))
-        getpass.assert_called_once()
-
     def test_fetch_api_description(self):
         content = factory.make_name("content")
         request = self.patch(httplib2.Http, "request")

=== added file 'src/maascli/tests/test_auth.py'
--- src/maascli/tests/test_auth.py	1970-01-01 00:00:00 +0000
+++ src/maascli/tests/test_auth.py	2012-10-08 05:49:21 +0000
@@ -0,0 +1,71 @@
+# Copyright 2012 Canonical Ltd.  This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+"""Tests for `maascli.auth`."""
+
+from __future__ import (
+    absolute_import,
+    print_function,
+    unicode_literals,
+    )
+
+__metaclass__ = type
+__all__ = []
+
+import sys
+
+from apiclient.creds import convert_tuple_to_string
+from maascli import auth
+from maastesting.factory import factory
+from maastesting.testcase import TestCase
+from mock import sentinel
+
+
+class TestAuth(TestCase):
+
+    def test_try_getpass(self):
+        getpass = self.patch(auth, "getpass")
+        getpass.return_value = sentinel.credentials
+        self.assertIs(sentinel.credentials, auth.try_getpass(sentinel.prompt))
+        getpass.assert_called_once_with(sentinel.prompt)
+
+    def test_try_getpass_eof(self):
+        getpass = self.patch(auth, "getpass")
+        getpass.side_effect = EOFError
+        self.assertIsNone(auth.try_getpass(sentinel.prompt))
+        getpass.assert_called_once_with(sentinel.prompt)
+
+    @staticmethod
+    def make_credentials():
+        return (
+            factory.make_name("cred"),
+            factory.make_name("cred"),
+            factory.make_name("cred"),
+            )
+
+    def test_obtain_credentials_from_stdin(self):
+        # When "-" is passed to obtain_credentials, it reads credentials from
+        # stdin, trims whitespace, and converts it into a 3-tuple of creds.
+        credentials = self.make_credentials()
+        stdin = self.patch(sys, "stdin")
+        stdin.readline.return_value = (
+            convert_tuple_to_string(credentials) + "\n")
+        self.assertEqual(credentials, auth.obtain_credentials("-"))
+        stdin.readline.assert_called_once()
+
+    def test_obtain_credentials_via_getpass(self):
+        # When None is passed to obtain_credentials, it attempts to obtain
+        # credentials via getpass, then converts it into a 3-tuple of creds.
+        credentials = self.make_credentials()
+        getpass = self.patch(auth, "getpass")
+        getpass.return_value = convert_tuple_to_string(credentials)
+        self.assertEqual(credentials, auth.obtain_credentials(None))
+        getpass.assert_called_once()
+
+    def test_obtain_credentials_empty(self):
+        # If the entered credentials are empty or only whitespace,
+        # obtain_credentials returns None.
+        getpass = self.patch(auth, "getpass")
+        getpass.return_value = None
+        self.assertEqual(None, auth.obtain_credentials(None))
+        getpass.assert_called_once()


Follow ups