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) fake_name = utils.random_name() fake_email = utils.random_name() fake_username = utils.random_name() fake_uid = utils.random_name() fake_region = utils.random_name() 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_auth_with_token_id(self): for cls in self.id_classes.values(): ident = cls() tok = utils.random_name() tenant_id = utils.random_name() 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) 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_authenticate(self): ident = self.identity_class(username=self.username, api_key=self.api_key) savopen = urllib2.urlopen urllib2.urlopen = Mock(return_value=fakes.FakeIdentityResponse()) ident.authenticate() urllib2.urlopen = savopen
def test_update_user(self): ident = self.rax_identity_class() resp = fakes.FakeIdentityResponse() resp.response_type = "users" ident.method_put = Mock(return_value=resp) fake_name = utils.random_name() fake_email = utils.random_name() fake_username = utils.random_name() fake_uid = utils.random_name() fake_region = utils.random_name() 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())
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) 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) self.assertRaises(exc.TenantNotFound, ident.list_tenants)
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) tenant = ident.get_tenant() self.assert_(isinstance(tenant, base_identity.Tenant))
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_authenticate_fail_creds(self): ident = self.rax_identity_class(username="******", password="******") savrequest = requests.api.request fake_resp = fakes.FakeIdentityResponse() fake_resp.status_code = 401 requests.api.request = Mock(return_value=fake_resp) self.assertRaises(exc.AuthenticationFailed, ident.authenticate) requests.api.request = savrequest
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) self.assertRaises(exc.AuthorizationFailure, ident.check_token)
def test_find_user(self): ident = self.rax_identity_class() resp = fakes.FakeIdentityResponse() resp.response_type = "users" ident.method_get = Mock(return_value=resp) fake_uri = utils.random_name() ret = ident._find_user(fake_uri) self.assert_(isinstance(ret, pyrax.rax_identity.User))
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) fake_uri = utils.random_name() ret = ident._find_user(fake_uri) self.assertIsNone(ret)
def test_list_users(self): ident = self.rax_identity_class() resp = fakes.FakeIdentityResponse() resp.response_type = "users" ident.method_get = Mock(return_value=resp) 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_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) fake_id = utils.random_name() ident.delete_tenant(fake_id) ident.method_delete.assert_called_with("tenants/%s" % fake_id)
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) fake_id = utils.random_name() self.assertRaises(exc.TenantNotFound, ident.delete_tenant, fake_id)
def test_delete_user_fail(self): for cls in self.id_classes.values(): ident = cls() resp = fakes.FakeIdentityResponse() resp.response_type = "users" resp.status_code = 404 ident.method_delete = Mock(return_value=resp) fake_name = utils.random_unicode() self.assertRaises(exc.UserNotFound, ident.delete_user, fake_name)
def test_authenticate(self): savrequest = requests.api.request requests.api.request = Mock(return_value=fakes.FakeIdentityResponse()) 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() requests.api.request = savrequest
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) tokens = ident.list_tokens() ident.method_get.assert_called_with("tokens/%s" % ident.token, admin=True) self.assert_("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) valid = ident.check_token() ident.method_head.assert_called_with("tokens/%s" % ident.token, admin=True) self.assert_(valid)
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) fake_name = utils.random_name() 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_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) 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_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) fake_name = utils.random_name() fake_email = utils.random_name() fake_password = utils.random_name() self.assertRaises(exc.AuthorizationFailure, ident.create_user, fake_name, fake_email, fake_password)
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) 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_auth_with_token_id_auth_fail(self): for cls in self.id_classes.values(): ident = cls() tok = utils.random_name() tenant_id = utils.random_name() resp = fakes.FakeIdentityResponse() resp.status_code = 401 ident.method_post = Mock(return_value=resp) self.assertRaises(exc.AuthenticationFailed, ident.auth_with_token, tok, tenant_id=tenant_id)
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 = 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_auth_with_token_id_auth_fail_general(self): for cls in self.id_classes.values(): ident = cls() tok = utils.random_name() tenant_id = utils.random_name() resp = fakes.FakeIdentityResponse() resp.status_code = 499 resp.reason = "fake" resp.json = Mock(return_value={"error": {"message": "fake"}}) ident.method_post = Mock(return_value=resp) self.assertRaises(exc.AuthenticationFailed, ident.auth_with_token, tok, tenant_id=tenant_id)
def test_has_valid_token(self): ident = self.identity_class(username=self.username, api_key=self.api_key) savopen = urllib2.urlopen urllib2.urlopen = Mock(return_value=fakes.FakeIdentityResponse()) 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) urllib2.urlopen = savopen
def test_authenticate_fail_other(self): ident = self.rax_identity_class(username="******", password="******") savrequest = requests.api.request fake_resp = fakes.FakeIdentityResponse() fake_resp.status_code = 500 fake_resp.json = Mock( return_value={ u'unauthorized': { u'message': u'Username or api key is invalid', u'code': 500 } }) requests.api.request = Mock(return_value=fake_resp) self.assertRaises(exc.AuthenticationFailed, ident.authenticate) requests.api.request = savrequest
def test_create_user(self): ident = self.rax_identity_class() resp = fakes.FakeIdentityResponse() resp.response_type = "users" ident.method_post = Mock(return_value=resp) fake_name = utils.random_name() fake_email = utils.random_name() fake_password = utils.random_name() 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) fake_name = utils.random_name() fake_desc = utils.random_name() 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)