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_update_user(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "users" ident.method_put = Mock(return_value=(resp, resp.json())) fake_name = utils.random_unicode() fake_email = utils.random_unicode() fake_username = utils.random_unicode() fake_uid = utils.random_unicode() fake_region = utils.random_unicode() fake_enabled = random.choice((True, False)) kwargs = {"email": fake_email, "username": fake_username, "uid": fake_uid, "enabled": fake_enabled} if isinstance(ident, self.rax_identity_class): kwargs["defaultRegion"] = fake_region ident.update_user(fake_name, **kwargs) cargs = ident.method_put.call_args self.assertEqual(len(cargs), 2) self.assertEqual(cargs[0], ("users/%s" % fake_name, )) data = cargs[1]["data"]["user"] self.assertEqual(data["enabled"], fake_enabled) self.assertEqual(data["username"], fake_username) self.assert_(fake_email in data.values()) if isinstance(ident, self.rax_identity_class): self.assert_(fake_region in data.values())
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.assert_(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_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_find_user(self): ident = self.rax_identity_class() resp = fakes.FakeIdentityResponse() resp.response_type = "users" ident._call = Mock(return_value=(resp, resp.json())) fake_uri = utils.random_unicode() ret = ident._find_user(fake_uri) self.assert_(isinstance(ret, pyrax.rax_identity.User))
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(pyrax.regions), len(expected)) for rgn in expected: self.assert_(rgn in pyrax.regions)
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_find_user_fail(self): ident = self.rax_identity_class() resp = fakes.FakeIdentityResponse() resp.response_type = "users" resp.status_code = 404 ident.method_get = Mock(return_value=(resp, resp.json())) fake_uri = utils.random_unicode() ret = ident._find_user(fake_uri) self.assertIsNone(ret)
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_list_users(self): ident = self.rax_identity_class() resp = fakes.FakeIdentityResponse() resp.response_type = "users" ident.method_get = Mock(return_value=(resp, resp.json())) ret = ident.list_users() self.assert_(isinstance(ret, list)) are_users = [isinstance(itm, pyrax.rax_identity.User) for itm in ret] self.assert_(all(are_users))
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_authenticate_fail_creds(self): ident = self.rax_identity_class(username="******", password="******") savrequest = pyrax.http.request fake_resp = fakes.FakeIdentityResponse() fake_resp.status_code = 401 fake_body = fakes.fake_identity_response pyrax.http.request = Mock(return_value=(fake_resp, fake_body)) self.assertRaises(exc.AuthenticationFailed, ident.authenticate) pyrax.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_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.assert_(valid)
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.assert_(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.assert_("token" in tokens)
def test_authenticate_fail_other(self): ident = self.rax_identity_class(username="******", password="******") savrequest = pyrax.http.request fake_resp = fakes.FakeIdentityResponse() fake_resp.status_code = 500 fake_body = fakes.fake_identity_response fake_resp.json = Mock(return_value={u'unauthorized': { u'message': u'Username or api key is invalid', u'code': 500}}) pyrax.http.request = Mock(return_value=(fake_resp, fake_body)) self.assertRaises(exc.InternalServerError, ident.authenticate) pyrax.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.assert_(isinstance(tenants, list)) are_tenants = [isinstance(itm, base_identity.Tenant) for itm in tenants] self.assert_(all(are_tenants))
def test_delete_user(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "users" ident.method_delete = Mock(return_value=(resp, resp.json())) fake_name = utils.random_unicode() ident.delete_user(fake_name) cargs = ident.method_delete.call_args self.assertEqual(len(cargs), 2) self.assertEqual(cargs[0], ("users/%s" % fake_name, ))
def test_list_credentials(self): ident = self.rax_identity_class() resp = fakes.FakeIdentityResponse() resp.response_type = "users" resp.status_code = 200 ident.method_get = Mock(return_value=(resp, resp.json())) fake_name = utils.random_unicode() ident.list_credentials(fake_name) cargs = ident.method_get.call_args called_uri = cargs[0][0] self.assert_("/credentials" in called_uri) self.assert_("users/%s/" % fake_name in called_uri)
def test_create_user_not_found(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "users" resp.status_code = 404 ident.method_post = Mock(return_value=(resp, resp.json())) fake_name = utils.random_unicode() fake_email = utils.random_unicode() fake_password = utils.random_unicode() self.assertRaises(exc.AuthorizationFailure, ident.create_user, fake_name, fake_email, fake_password)
def test_authenticate(self): savrequest = pyrax.http.request fake_resp = fakes.FakeIdentityResponse() fake_body = fakes.fake_identity_response pyrax.http.request = Mock(return_value=(fake_resp, fake_body)) for cls in self.id_classes.values(): ident = cls() if cls is self.keystone_identity_class: # Necessary for testing to avoid NotImplementedError. utils.add_method(ident, lambda self: "", "_get_auth_endpoint") ident.authenticate() pyrax.http.request = savrequest
def test_list_roles_for_user(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "users" resp.status_code = 200 ident.method_get = Mock(return_value=(resp, resp.json())) resp = ident.list_roles_for_user("fake") self.assertTrue(isinstance(resp, list)) role = resp[0] self.assertTrue("description" in role) self.assertTrue("name" in role) self.assertTrue("id" in role)
def test_create_user_bad_email(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "users" resp.status_code = 400 resp_body = {"badRequest": {"message": "Expecting valid email address"}} ident.method_post = Mock(return_value=(resp, resp_body)) fake_name = utils.random_unicode() fake_email = utils.random_unicode() fake_password = utils.random_unicode() self.assertRaises(exc.InvalidEmail, ident.create_user, fake_name, fake_email, fake_password)
def test_create_user(self): ident = self.rax_identity_class() resp = fakes.FakeIdentityResponse() resp.response_type = "users" ident.method_post = Mock(return_value=(resp, resp.json())) fake_name = utils.random_unicode() fake_email = utils.random_unicode() fake_password = utils.random_unicode() ident.create_user(fake_name, fake_email, fake_password) cargs = ident.method_post.call_args self.assertEqual(len(cargs), 2) self.assertEqual(cargs[0], ("users", )) data = cargs[1]["data"]["user"] self.assertEqual(data["username"], fake_name) self.assert_(fake_password in data.values())
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.assert_(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_id(self): for cls in self.id_classes.values(): ident = cls() tok = utils.random_unicode() tenant_id = utils.random_unicode() 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}, 'tenantId': tenant_id}}) self.assertEqual(ident.tenant_id, tenant_id) resp.content["access"]["token"]["tenant"]["id"] = sav
def test_has_valid_token(self): savrequest = pyrax.http.request pyrax.http.request = Mock(return_value=(fakes.FakeIdentityResponse(), fakes.fake_identity_response)) for cls in self.id_classes.values(): ident = cls() if cls is self.keystone_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.assert_(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) pyrax.http.request = savrequest
def test_update_user(self): ident = self.rax_identity_class() resp = fakes.FakeIdentityResponse() resp.response_type = "users" ident.method_put = Mock(return_value=(resp, resp.json())) fake_name = utils.random_unicode() fake_email = utils.random_unicode() fake_username = utils.random_unicode() fake_uid = utils.random_unicode() fake_region = utils.random_unicode() fake_enabled = random.choice((True, False)) ident.update_user(fake_name, email=fake_email, username=fake_username, uid=fake_uid, defaultRegion=fake_region, enabled=fake_enabled) cargs = ident.method_put.call_args self.assertEqual(len(cargs), 2) self.assertEqual(cargs[0], ("users/%s" % fake_name, )) data = cargs[1]["data"]["user"] self.assertEqual(data["enabled"], fake_enabled) self.assertEqual(data["username"], fake_username) self.assert_(fake_email in data.values()) self.assert_(fake_region in data.values())