def test_get_token_and_catalog_for_token_credentials(self): """ The response returned should include the credentials that were supplied during authentication """ core = MimicCore(Clock(), [ExampleAPI()]) root = MimicRoot(core).app.resource() (response, json_body) = self.successResultOf( json_request( self, root, "POST", "/identity/v2.0/tokens", { "auth": { "tenantId": "12345", "token": { "id": "iuyiuyiuy-uyiuyiuy-1987878" } } })) self.assertEqual(response.code, 200) tenant_id = json_body["access"]["token"]["tenant"]["id"] self.assertEqual(tenant_id, "12345") tenant_name = json_body["access"]["token"]["tenant"]["name"] self.assertEqual(tenant_name, tenant_id) user_name = json_body["access"]["user"]["name"] self.assertTrue(user_name)
def test_api_service_endpoints_are_not_duplicated(self): """ The service catalog should not duplicate endpoints for an entry/endpoints """ regions_and_versions_list = [("ORD", "v1"), ("DFW", "v1"), ("DFW", "v2"), ("IAD", "v3")] core = MimicCore( Clock(), [ExampleAPI(regions_and_versions=regions_and_versions_list)]) root = MimicRoot(core).app.resource() (response, json_body) = self.successResultOf( json_request( self, root, "POST", "/identity/v2.0/tokens", { "auth": { "passwordCredentials": { "username": "******", "password": "******" } } })) self.assertEqual(response.code, 200) service_catalog = json_body["access"]["serviceCatalog"] self.assertEqual(len(service_catalog), 1) endpoints_list = service_catalog[0]["endpoints"] self.assertEqual(len(endpoints_list), 4)
def test_get_token_and_catalog_for_api_credentials(self): """ The response returned should include the credentials that were supplied during authentication """ core = MimicCore(Clock(), [ExampleAPI()]) root = MimicRoot(core).app.resource() (response, json_body) = self.successResultOf( json_request( self, root, "POST", "/identity/v2.0/tokens", { "auth": { "RAX-KSKEY:apiKeyCredentials": { "username": "******", "apiKey": "jhgjhghg-nhghghgh-12222" }, "tenantName": "12345" } })) self.assertEqual(response.code, 200) tenant_id = json_body["access"]["token"]["tenant"]["id"] self.assertEqual(tenant_id, "12345") tenant_name = json_body["access"]["token"]["tenant"]["name"] self.assertEqual(tenant_name, tenant_id) user_name = json_body["access"]["user"]["name"] self.assertEqual(user_name, "demoauthor")
def test_response_service_catalog_has_base_uri(self): """ The JSON response's service catalog whose endpoints all begin with the same base URI as the request. """ core = MimicCore(Clock(), [ExampleAPI()]) root = MimicRoot(core).app.resource() (response, json_body) = self.successResultOf( json_request( self, root, "POST", "http://mybase/identity/v2.0/tokens", { "auth": { "passwordCredentials": { "username": "******", "password": "******" } } })) self.assertEqual(200, response.code) services = json_body['access']['serviceCatalog'] self.assertEqual(1, len(services)) urls = [endpoint['publicURL'] for endpoint in services[0]['endpoints']] self.assertEqual(1, len(urls)) self.assertTrue( urls[0].startswith('http://mybase/'), '{0} does not start with "http://mybase"'.format(urls[0]))
def test_response_for_get_username(self): """ Test to verify :func: `get_username`. """ core = MimicCore(Clock(), [ExampleAPI()]) root = MimicRoot(core).app.resource() (response, json_body) = self.successResultOf( json_request(self, root, "GET", "http://mybase/identity/v1.1/mosso/123456")) self.assertEqual(301, response.code) self.assertTrue(json_body['user']['id'])
def make_request_to_site(self): """ Make a request and return the response. """ core = MimicCore(Clock(), [ExampleAPI('response!')]) root = MimicRoot(core).app.resource() # get the region and service id registered for the example API (region, service_id) = one_api(self, core) url = "/mimicking/{0}/{1}".format(service_id, region) response = self.successResultOf( request(self, root, "GET", url, headers={"one": ["two"]})) return (response, url)
def test_service_endpoint_returns_service_resource(self): """ When the URI used to access a real service has the right service ID and right region, the service's resource is used to respond to the request. """ core = MimicCore(Clock(), [ExampleAPI('response!')]) root = MimicRoot(core).app.resource() # get the region and service id registered for the example API (region, service_id) = one_api(self, core) (response, content) = self.successResultOf(request_with_content( self, root, "GET", "http://mybase/mimicking/{0}/{1}".format(service_id, region) )) self.assertEqual(200, response.code) self.assertEqual('response!', content)
def test_get_token_and_catalog_for_invalid_json_request_body(self): """ :func: `get_token_and_service_catalog` returns response code 400, when an invalid json request body is used to authenticate. """ core = MimicCore(Clock(), [ExampleAPI()]) root = MimicRoot(core).app.resource() (response, json_body) = self.successResultOf( json_request( self, root, "POST", "/identity/v2.0/tokens", {"auth": { "token": { "id": "iuyiuyiuy-uyiuyiuy-1987878" } }})) self.assertEqual(response.code, 400) self.assertEqual(json_body["message"], "Invalid JSON request body")
def test_service_endpoint_returns_404_if_wrong_region(self): """ When the URI used to access a real service has the right service ID but wrong service ID, a 404 is returned and the resource for the service is accessed. """ example = ExampleAPI() core = MimicCore(Clock(), [example]) root = MimicRoot(core).app.resource() # get the region and service id registered for the example API (region, service_id) = one_api(self, core) response = self.successResultOf(request( self, root, "GET", "http://mybase/mimicking/not_{0}/{1}".format(service_id, region) )) self.assertEqual(404, response.code) self.assertEqual([], example.store.keys())
def test_response_for_impersonation(self): """ Test to verify :func: `get_impersonation_token`. """ core = MimicCore(Clock(), [ExampleAPI()]) root = MimicRoot(core).app.resource() (response, json_body) = self.successResultOf( json_request( self, root, "POST", "http://mybase/identity/v2.0/RAX-AUTH/impersonation-tokens", { "RAX-AUTH:impersonation": { "expire-in-seconds": 1, "user": { "username": "******" } } })) self.assertEqual(200, response.code) self.assertTrue(json_body['access']['token']['id'])
def test_response_service_catalog_has_base_uri(self): """ The JSON response's service catalog whose endpoints all begin with the same base URI as the request. """ core = MimicCore(Clock(), [ExampleAPI()]) root = MimicRoot(core).app.resource() (response, json_body) = self.successResultOf( json_request( self, root, "GET", "http://mybase/identity/v2.0/tokens/1234567890/endpoints")) self.assertEqual(200, response.code) urls = [endpoint['publicURL'] for endpoint in json_body['endpoints']] self.assertEqual(1, len(urls)) self.assertTrue( urls[0].startswith('http://mybase/'), '{0} does not start with "http://mybase"'.format(urls[0]))
def test_token_and_catalog_for_api_credentials_wrong_tenant(self): """ Tenant ID is validated when provided in api-key auth. If authed once as one tenant ID, and a second time with a different tenant ID, then the second auth will return with a 401 Unauthorized. """ core = MimicCore(Clock(), [ExampleAPI()]) root = MimicRoot(core).app.resource() creds = { "auth": { "RAX-KSKEY:apiKeyCredentials": { "username": "******", "apiKey": "jhgjhghg-nhghghgh-12222" }, "tenantName": "12345" } } (response, json_body) = self.successResultOf( json_request(self, root, "POST", "/identity/v2.0/tokens", creds)) self.assertEqual(response.code, 200) username = json_body["access"]["user"]["id"] creds['auth']['tenantName'] = "23456" (response, fail_body) = self.successResultOf( json_request(self, root, "POST", "/identity/v2.0/tokens", creds)) self.assertEqual(response.code, 401) self.assertEqual( fail_body, { "unauthorized": { "code": 401, "message": ("Tenant with Name/Id: '23456' is not valid for " "User 'demoauthor' (id: '{0}')".format(username)) } })
def test_child_resource_gets_base_uri_from_request(self): """ Whatever the URI is used to access mimic is the one that is passed back to the plugin when :func:`mimic.imimic.IAPMock.resource_for_region` is called. """ example = ExampleAPI() core = MimicCore(Clock(), [example]) root = MimicRoot(core).app.resource() # get the region and service id registered for the example API (region, service_id) = one_api(self, core) request( self, root, "GET", "http://mybase/mimicking/{0}/{1}/more/stuff".format(service_id, region) ) self.assertEqual( "http://mybase/mimicking/{0}/{1}/".format(service_id, region), example.store['uri_prefix'])
def test_token_and_catalog_for_token_credentials_wrong_tenant(self): """ Tenant ID is validated when provided in token auth. If authed once as one tenant ID, and a second time with a different tenant ID, then the second auth will return with a 401 Unauthorized. """ core = MimicCore(Clock(), [ExampleAPI()]) root = MimicRoot(core).app.resource() creds = { "auth": { "tenantId": "12345", "token": { "id": "iuyiuyiuy-uyiuyiuy-1987878" } } } (response, json_body) = self.successResultOf( json_request(self, root, "POST", "/identity/v2.0/tokens", creds)) self.assertEqual(response.code, 200) creds['auth']['tenantId'] = "23456" (response, fail_body) = self.successResultOf( json_request(self, root, "POST", "/identity/v2.0/tokens", creds)) self.assertEqual(response.code, 401) self.assertEqual( fail_body, { "unauthorized": { "code": 401, "message": ("Token doesn't belong to Tenant with Id/Name: " "'23456'") } })