Add tests to check clientmanager auth function 55/55855/7
authorthuva4 <tharma.thuva@gmail.com>
Tue, 17 Apr 2018 05:00:27 +0000 (10:30 +0530)
committerthuva4 <tharma.thuva@gmail.com>
Thu, 26 Apr 2018 13:03:20 +0000 (18:33 +0530)
JIRA: RELENG-379

Change-Id: Ia1b36cabc1b0750f5a9dab8509c1c9b868889e7a
Signed-off-by: thuva4 <tharma.thuva@gmail.com>
testapi/testapi-client/testapiclient/tests/unit/test_app.py [new file with mode: 0644]
testapi/testapi-client/testapiclient/tests/unit/utils.py

diff --git a/testapi/testapi-client/testapiclient/tests/unit/test_app.py b/testapi/testapi-client/testapiclient/tests/unit/test_app.py
new file mode 100644 (file)
index 0000000..c20b27f
--- /dev/null
@@ -0,0 +1,50 @@
+import urllib
+
+from mock import mock
+
+from testapiclient import main
+from testapiclient.tests.unit import utils
+from testapiclient.tests.unit import fakes
+
+
+class MainTest(utils.TestCommand):
+    def setUp(self):
+        super(MainTest, self).setUp()
+        self.app = main.TestAPIClient()
+        self.cas_sever = '{}{}{}'.format(
+            self.env_variables['testapi_cas_auth_url'],
+            urllib.quote(self.env_variables['testapi_url']),
+            self.env_variables['testapi_cas_signin_return'])
+        self.headers = {
+            'Content-type': 'application/json',
+            'Accept': 'text/plain'}
+
+    def test_auth_success(self):
+        self.post_mock.return_value = fakes.FakeResponse(
+            data={'text': "success"})
+        self.app.run(
+            ['-u', 'user', '-p', 'pass', 'pod', 'create',
+             '{"name": "asfad"}'])
+        self.post_mock.assert_called_with(
+            'http://localhost:8000/api/v1/pods',
+            data='{"name": "asfad"}',
+            headers=self.headers)
+
+    def test_auth_failure(self):
+        self.post_mock.return_value = fakes.FakeResponse(
+            data={'text': "login"})
+        self.app.run(
+            ['-u', 'user', '-p', 'pass', 'pod', 'create',
+             '{"name": "asfad"}'])
+        self.post_mock.assert_called_once_with(
+            self.cas_sever,
+            {'pass': 'pass', 'name': 'user', 'form_id': 'user_login'}
+        )
+
+    def test_auth_not_called(self):
+        self.auth_post = mock.patch(
+            'testapiclient.utils.clientmanager.ClientManager.auth').start()
+        self.app.run(['pod', 'get'])
+        self.auth_post.assert_not_called()
+        self.get_mock.assert_called_once_with(
+            'http://localhost:8000/api/v1/pods', headers=self.headers)
index 21f98c4..c59aadd 100644 (file)
@@ -19,7 +19,7 @@ class TestCommand(testtools.TestCase):
 
     def setUp(self):
         super(TestCommand, self).setUp()
-        env_variables = {
+        self.env_variables = {
             'testapi_url': 'http://localhost:8000/api/v1',
             'testapi_cas_auth_url':
             (
@@ -29,7 +29,7 @@ class TestCommand(testtools.TestCase):
             'testapi_cas_signin_return': '/auth/signin_return'
         }
         self.config_mock = mock.patch.dict(
-            'os.environ', env_variables).start()
+            'os.environ', self.env_variables).start()
         self.fake_stdout = fakes.FakeStdout()
         self.fake_log = fakes.FakeLog()
         self.app = fakes.FakeApp(self.fake_stdout, self.fake_log)