def test_auth_login(self): config = "anchor.jsonloader.conf._config" data = {'auth': {'keystone': {'url': 'http://localhost:35357'}}} with mock.patch.dict(config, data): from anchor.auth import keystone from anchor.auth import results keystone_url = data['auth']['keystone']['url'] + '/v3/auth/tokens' keystone_token = uuid.uuid4().hex json_response = { "token": { "roles": [ { "name": "admin" } ], "user": { "name": "priti" }, } } user = json_response['token']['user']['name'] roles = [role['name'] for role in json_response['token']['roles']] expected = results.AuthDetails(username=user, groups=roles) with requests_mock.mock() as m: m.post(keystone_url, json=json_response, status_code=200) requests.post(keystone_url) # Check that it can parse Keystone response when # response has valid json and status code of 200 self.assertEqual(keystone.login(None, keystone_token), expected) # Check that it fails and returns appropriate auth # failure when Keystone authentication fails m.post(keystone_url, status_code=201) self.assertEqual(keystone.login(None, keystone_token), None) # Check that it fails and returns appropriate auth # failure when Keystone response is corrupted m.post(keystone_url, json={}, status_code=200) self.assertEqual(keystone.login(None, keystone_token), None)
def test_parse_keystone_valid_response(self): with mock.patch.dict(self.config, self.data): with requests_mock.mock() as m: m.get(self.keystone_url, json=self.json_response, status_code=200) requests.get(self.keystone_url) self.assertEqual(keystone.login( None, self.keystone_token), self.expected)
def test_auth_login(self): config = "anchor.jsonloader.conf._config" data = {'auth': {'keystone': {'url': 'http://localhost:35357'}}} with mock.patch.dict(config, data): from anchor.auth import keystone from anchor.auth import results keystone_url = data['auth']['keystone']['url'] + '/v3/auth/tokens' keystone_token = uuid.uuid4().hex json_response = { "token": { "roles": [{ "name": "admin" }], "user": { "name": "priti" }, } } user = json_response['token']['user']['name'] roles = [role['name'] for role in json_response['token']['roles']] expected = results.AuthDetails(username=user, groups=roles) with requests_mock.mock() as m: m.post(keystone_url, json=json_response, status_code=200) requests.post(keystone_url) # Check that it can parse Keystone response when # response has valid json and status code of 200 self.assertEqual(keystone.login(None, keystone_token), expected) # Check that it fails and returns appropriate auth # failure when Keystone authentication fails m.post(keystone_url, status_code=201) self.assertEqual(keystone.login(None, keystone_token), None) # Check that it fails and returns appropriate auth # failure when Keystone response is corrupted m.post(keystone_url, json={}, status_code=200) self.assertEqual(keystone.login(None, keystone_token), None)
def test_parse_keystone_ok_but_malformed_response(self): with mock.patch.dict(self.config, self.data): with requests_mock.mock() as m: m.get(self.keystone_url, json={}, status_code=200) self.assertEqual(keystone.login(None, self.keystone_token), None)
def test_parse_keystone_auth_fail(self): with mock.patch.dict(self.config, self.data): with requests_mock.mock() as m: m.get(self.keystone_url, status_code=401) self.assertEqual(keystone.login(None, self.keystone_token), None)
def test_parse_keystone_ok_but_malformed_response(self): with mock.patch.dict(self.config, self.data): with requests_mock.mock() as m: m.get(self.keystone_url, json={}, status_code=200) self.assertEqual(keystone.login( None, self.keystone_token), None)
def test_parse_keystone_auth_fail(self): with mock.patch.dict(self.config, self.data): with requests_mock.mock() as m: m.get(self.keystone_url, status_code=401) self.assertEqual(keystone.login( None, self.keystone_token), None)