From 1efd3a466453c2e8fbfebc27044f8f6304246edb Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Thu, 28 May 2015 15:57:57 -0400 Subject: [PATCH] Add unit tests for basic auth methods in token clients This commit adds the first missing unit tests to the token clients. Previously there was no coverage at all for these clients so there was no in-tree method for verifying the functionality of these classes. The coverage of this commit is limited and only covers 2 basic uses of the auth() method in both the v2 and v3 token clients. It will be expanded in future patches. As part of enabling this coverage the fake_http class had to be modified to return a user provided body and response code. Change-Id: I1888b31550e5db4343003b05c0f6865444756789 --- tempest_lib/tests/fake_http.py | 2 +- tempest_lib/tests/services/__init__.py | 0 .../tests/services/identity/v2/__init__.py | 0 .../services/identity/v2/test_token_client.py | 66 +++++++++++++++ .../tests/services/identity/v3/__init__.py | 0 .../services/identity/v3/test_token_client.py | 81 +++++++++++++++++++ 6 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 tempest_lib/tests/services/__init__.py create mode 100644 tempest_lib/tests/services/identity/v2/__init__.py create mode 100644 tempest_lib/tests/services/identity/v2/test_token_client.py create mode 100644 tempest_lib/tests/services/identity/v3/__init__.py create mode 100644 tempest_lib/tests/services/identity/v3/test_token_client.py diff --git a/tempest_lib/tests/fake_http.py b/tempest_lib/tests/fake_http.py index 002f16b..eda202d 100644 --- a/tempest_lib/tests/fake_http.py +++ b/tempest_lib/tests/fake_http.py @@ -34,7 +34,7 @@ class fake_httplib2(object): } return (fake_headers, return_obj) elif isinstance(self.return_type, int): - body = "fake_body" + body = body or "fake_body" header_info = { 'content-type': 'text/plain', 'status': str(self.return_type), diff --git a/tempest_lib/tests/services/__init__.py b/tempest_lib/tests/services/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tempest_lib/tests/services/identity/v2/__init__.py b/tempest_lib/tests/services/identity/v2/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tempest_lib/tests/services/identity/v2/test_token_client.py b/tempest_lib/tests/services/identity/v2/test_token_client.py new file mode 100644 index 0000000..81943dc --- /dev/null +++ b/tempest_lib/tests/services/identity/v2/test_token_client.py @@ -0,0 +1,66 @@ +# Copyright 2015 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import json + +from oslotest import mockpatch + +from tempest_lib.common import rest_client +from tempest_lib.services.identity.v2 import token_client +from tempest_lib.tests import base +from tempest_lib.tests import fake_http + + +class TestTokenClientV2(base.TestCase): + + def setUp(self): + super(TestTokenClientV2, self).setUp() + self.fake_200_http = fake_http.fake_httplib2(return_type=200) + + def test_auth(self): + token_client_v2 = token_client.TokenClientJSON('fake_url') + post_mock = self.useFixture(mockpatch.PatchObject( + token_client_v2, 'post', return_value=self.fake_200_http.request( + 'fake_url', body={'access': {'token': 'fake_token'}}))) + resp = token_client_v2.auth('fake_user', 'fake_pass') + self.assertIsInstance(resp, rest_client.ResponseBody) + req_dict = json.dumps({ + 'auth': { + 'passwordCredentials': { + 'username': 'fake_user', + 'password': 'fake_pass', + }, + } + }) + post_mock.mock.assert_called_once_with('fake_url/tokens', + body=req_dict) + + def test_auth_with_tenant(self): + token_client_v2 = token_client.TokenClientJSON('fake_url') + post_mock = self.useFixture(mockpatch.PatchObject( + token_client_v2, 'post', return_value=self.fake_200_http.request( + 'fake_url', body={'access': {'token': 'fake_token'}}))) + resp = token_client_v2.auth('fake_user', 'fake_pass', 'fake_tenant') + self.assertIsInstance(resp, rest_client.ResponseBody) + req_dict = json.dumps({ + 'auth': { + 'tenantName': 'fake_tenant', + 'passwordCredentials': { + 'username': 'fake_user', + 'password': 'fake_pass', + }, + } + }) + post_mock.mock.assert_called_once_with('fake_url/tokens', + body=req_dict) diff --git a/tempest_lib/tests/services/identity/v3/__init__.py b/tempest_lib/tests/services/identity/v3/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tempest_lib/tests/services/identity/v3/test_token_client.py b/tempest_lib/tests/services/identity/v3/test_token_client.py new file mode 100644 index 0000000..9b81784 --- /dev/null +++ b/tempest_lib/tests/services/identity/v3/test_token_client.py @@ -0,0 +1,81 @@ +# Copyright 2015 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import json + +from oslotest import mockpatch + +from tempest_lib.common import rest_client +from tempest_lib.services.identity.v3 import token_client +from tempest_lib.tests import base +from tempest_lib.tests import fake_http + + +class TestTokenClientV2(base.TestCase): + + def setUp(self): + super(TestTokenClientV2, self).setUp() + self.fake_201_http = fake_http.fake_httplib2(return_type=201) + + def test_auth(self): + token_client_v3 = token_client.V3TokenClientJSON('fake_url') + post_mock = self.useFixture(mockpatch.PatchObject( + token_client_v3, 'post', return_value=self.fake_201_http.request( + 'fake_url', body={'access': {'token': 'fake_token'}}))) + resp = token_client_v3.auth(username='fake_user', password='fake_pass') + self.assertIsInstance(resp, rest_client.ResponseBody) + req_dict = json.dumps({ + 'auth': { + 'identity': { + 'methods': ['password'], + 'password': { + 'user': { + 'name': 'fake_user', + 'password': 'fake_pass', + } + } + }, + } + }) + post_mock.mock.assert_called_once_with('fake_url/auth/tokens', + body=req_dict) + + def test_auth_with_tenant(self): + token_client_v2 = token_client.V3TokenClientJSON('fake_url') + post_mock = self.useFixture(mockpatch.PatchObject( + token_client_v2, 'post', return_value=self.fake_201_http.request( + 'fake_url', body={'access': {'token': 'fake_token'}}))) + resp = token_client_v2.auth(username='fake_user', password='fake_pass', + project_name='fake_tenant') + self.assertIsInstance(resp, rest_client.ResponseBody) + req_dict = json.dumps({ + 'auth': { + 'identity': { + 'methods': ['password'], + 'password': { + 'user': { + 'name': 'fake_user', + 'password': 'fake_pass', + } + }}, + 'scope': { + 'project': { + 'name': 'fake_tenant' + } + }, + } + }) + + post_mock.mock.assert_called_once_with('fake_url/auth/tokens', + body=req_dict)