def test_api_request_expired(self): clt = self.client id_svc = clt.identity sav_auth = id_svc.authenticate returns = [exc.Unauthorized(""), (fakes.FakeIdentityResponse(), fakes.fake_identity_response)] def auth_resp(*args, **kwargs): result = returns.pop(0) if isinstance(result, Exception): raise result return result id_svc.authenticate = Mock() sav_req = clt.request clt.request = Mock(side_effect=auth_resp) url = DUMMY_URL method = "PUT" clt.unauthenticate() clt.management_url = url id_svc.token = "" id_svc.tenant_id = utils.random_unicode() clt._api_request(url, method) self.assertEqual(id_svc.authenticate.call_count, 2) clt.request = sav_req id_svc.authenticate = sav_auth
def test_get_tenant(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "tenants" ident.method_get = Mock(return_value=(resp, resp.json())) tenant = ident.get_tenant() self.assertTrue(isinstance(tenant, base_identity.Tenant))
def test_list_tenants_auth_fail(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "tenants" resp.status_code = 403 ident.method_get = Mock(return_value=(resp, resp.json())) self.assertRaises(exc.AuthorizationFailure, ident.list_tenants)
def test_check_token_fail_auth(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "tokens" resp.status_code = 403 ident.method_head = Mock(return_value=(resp, resp.json())) self.assertRaises(exc.AuthorizationFailure, ident.check_token)
def test_regions(self): ident = self.base_identity_class() fake_resp = fakes.FakeIdentityResponse() ident._parse_response(fake_resp.json()) expected = ("DFW", "ORD", "SYD", "FAKE") self.assertEqual(len(ident.regions), len(expected)) for rgn in expected: self.assertTrue(rgn in ident.regions)
def test_list_tenants_other_fail(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "tenants" resp.status_code = 404 ident.method_get = Mock(return_value=(resp, resp.json())) self.assertRaises(exc.TenantNotFound, ident.list_tenants)
def test_get_tenant_none(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "tenants" ident._list_tenants = Mock(return_value=[]) tenant = ident.get_tenant() self.assertIsNone(tenant)
def test_authenticate(self): savrequest = pyos.http.request fake_resp = fakes.FakeIdentityResponse() fake_body = fakes.fake_identity_response pyos.http.request = Mock(return_value=(fake_resp, fake_body)) ident = self.identity_class() utils.add_method(ident, lambda self: "", "_get_auth_endpoint") ident.authenticate() pyos.http.request = savrequest
def test_delete_tenantfail(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "tenant" resp.status_code = 404 ident.method_delete = Mock(return_value=(resp, resp.json())) fake_id = utils.random_unicode() self.assertRaises(exc.TenantNotFound, ident.delete_tenant, fake_id)
def test_delete_tenant(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "tenant" ident.method_delete = Mock(return_value=(resp, resp.json())) fake_id = utils.random_unicode() ident.delete_tenant(fake_id) ident.method_delete.assert_called_with("tenants/%s" % fake_id)
def test_authenticate_fail_creds(self): ident = self.identity_class(username="******", password="******") savrequest = pyos.http.request fake_resp = fakes.FakeIdentityResponse() fake_resp.status_code = 401 fake_body = fakes.fake_identity_response pyos.http.request = Mock(return_value=(fake_resp, fake_body)) self.assertRaises(exc.AuthenticationFailed, ident.authenticate) pyos.http.request = savrequest
def test_auth_with_token_id_auth_fail(self): for cls in self.id_classes.values(): ident = cls() tok = utils.random_unicode() tenant_id = utils.random_unicode() resp = fakes.FakeIdentityResponse() resp.status_code = 401 ident.method_post = Mock(return_value=(resp, resp.json())) self.assertRaises(exc.AuthenticationFailed, ident.auth_with_token, tok, tenant_id=tenant_id)
def test_authenticate_fail_other(self): ident = self.identity_class(username="******", password="******") savrequest = pyos.http.request fake_resp = fakes.FakeIdentityResponse() fake_resp.status_code = 500 fake_body = {u'unauthorized': { u'message': u'Username or api key is invalid', u'code': 500}} pyos.http.request = Mock(return_value=(fake_resp, fake_body)) self.assertRaises(exc.InternalServerError, ident.authenticate) pyos.http.request = savrequest
def test_revoke_token_fail(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "tokens" resp.status_code = 401 token = ident.token = utils.random_unicode() ident.method_delete = Mock(return_value=(resp, resp.json())) self.assertRaises(exc.AuthorizationFailure, ident.revoke_token, token)
def test_get_token_endpoints(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "endpoints" ident.method_get = Mock(return_value=(resp, resp.json())) eps = ident.get_token_endpoints() self.assertTrue(isinstance(eps, list)) ident.method_get.assert_called_with("tokens/%s/endpoints" % ident.token, admin=True)
def test_list_token(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "tokens" ident.method_get = Mock(return_value=(resp, resp.json())) tokens = ident.list_tokens() ident.method_get.assert_called_with("tokens/%s" % ident.token, admin=True) self.assertTrue("token" in tokens)
def test_check_token(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "tokens" ident.method_head = Mock(return_value=(resp, resp.json())) valid = ident.check_token() ident.method_head.assert_called_with("tokens/%s" % ident.token, admin=True) self.assertTrue(valid)
def test_authenticate_backwards_compatibility_connect_param(self): savrequest = pyos.http.request fake_resp = fakes.FakeIdentityResponse() fake_body = fakes.fake_identity_response pyos.http.request = Mock(return_value=(fake_resp, fake_body)) ident = self.identity_class() # Necessary for testing to avoid NotImplementedError. utils.add_method(ident, lambda self: "", "_get_auth_endpoint") ident.authenticate(connect=False) pyos.http.request = savrequest
def test_authenticate_fail_gt_299ino_message(self): ident = self.identity_class(username="******", password="******") savrequest = pyos.http.request fake_resp = fakes.FakeIdentityResponse() fake_resp.status_code = 444 fake_body = {u'unauthorized': { u'bogus': u'Username or api key is invalid', u'code': 500}} pyos.http.request = Mock(return_value=(fake_resp, fake_body)) self.assertRaises(exc.AuthenticationFailed, ident.authenticate) pyos.http.request = savrequest
def test_list_tenants(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "tenants" ident.method_get = Mock(return_value=(resp, resp.json())) tenants = ident.list_tenants() self.assertTrue(isinstance(tenants, list)) are_tenants = [isinstance(itm, base_identity.Tenant) for itm in tenants] self.assertTrue(all(are_tenants))
def test_revoke_token(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "tokens" token = ident.token = utils.random_unicode() ident.method_delete = Mock(return_value=(resp, resp.json())) valid = ident.revoke_token(token) ident.method_delete.assert_called_with("tokens/%s" % ident.token, admin=True) self.assertTrue(valid)
def test_create_tenant(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "tenant" ident.method_post = Mock(return_value=(resp, resp.json())) fake_name = utils.random_unicode() fake_desc = utils.random_unicode() tenant = ident.create_tenant(fake_name, description=fake_desc) self.assertTrue(isinstance(tenant, base_identity.Tenant)) cargs = ident.method_post.call_args self.assertEqual(len(cargs), 2) self.assertEqual(cargs[0], ("tenants", )) data = cargs[1]["data"]["tenant"] self.assertEqual(data["name"], fake_name) self.assertEqual(data["description"], fake_desc)
def test_auth_with_token_name(self): for cls in self.id_classes.values(): ident = cls() tok = utils.random_unicode() nm = utils.random_unicode() resp = fakes.FakeIdentityResponse() # Need to stuff this into the standard response sav = resp.content["access"]["user"]["name"] resp.content["access"]["user"]["name"] = nm ident.method_post = Mock(return_value=(resp, resp.json())) ident.auth_with_token(tok, tenant_name=nm) ident.method_post.assert_called_once_with("tokens", headers={'Content-Type': 'application/json', 'Accept': 'application/json'}, std_headers=False, data={'auth': {'token': {'id': tok}, 'tenantName': nm}}) self.assertEqual(ident.username, nm) resp.content["access"]["user"]["name"] = sav
def test_auth_with_token_without_tenant_id(self): for cls in self.id_classes.values(): ident = cls() tok = utils.random_unicode() tenant_id = None resp = fakes.FakeIdentityResponse() # Need to stuff this into the standard response sav = resp.content["access"]["token"]["tenant"]["id"] resp.content["access"]["token"]["tenant"]["id"] = tenant_id ident.method_post = Mock(return_value=(resp, resp.json())) ident.auth_with_token(tok, tenant_id=tenant_id) ident.method_post.assert_called_once_with("tokens", headers={'Content-Type': 'application/json', 'Accept': 'application/json'}, std_headers=False, data={'auth': {'token': {'id': tok}}}) self.assertEqual(ident.tenant_id, tenant_id) resp.content["access"]["token"]["tenant"]["id"] = sav
def test_has_valid_token(self): savrequest = pyos.http.request pyos.http.request = Mock(return_value=(fakes.FakeIdentityResponse(), fakes.fake_identity_response)) for cls in self.id_classes.values(): ident = self.identity_class() # Necessary for testing to avoid NotImplementedError. utils.add_method(ident, lambda self: "", "_get_auth_endpoint") ident.authenticate() valid = ident._has_valid_token() self.assertTrue(valid) ident.expires = datetime.datetime.now() - datetime.timedelta(1) valid = ident._has_valid_token() self.assertFalse(valid) ident = self._get_clean_identity() valid = ident._has_valid_token() self.assertFalse(valid) pyos.http.request = savrequest