def test_auth_accepts_tenant_id(self): """ If "tenantId" is passed, the tenant specified is used instead of a generated tenant ID. """ core = MimicCore(Clock(), []) root = MimicRoot(core).app.resource() (response, json_body) = self.successResultOf( json_request( self, root, "POST", "/identity/v2.0/tokens", { "auth": { "passwordCredentials": { "username": "******", "password": "******" }, "tenantId": "turtlepower" } })) self.assertEqual(200, response.code) self.assertEqual("turtlepower", json_body['access']['token']['tenant']['id']) token = json_body['access']['token']['id'] session = core.sessions.session_for_token(token) self.assertEqual(token, session.token) self.assertEqual("turtlepower", session.tenant_id)
def setUp(self): """ Initialize core and root """ self.core = MimicCore(Clock(), []) self.root = MimicRoot(self.core).app.resource() self.node_details_attributes = [ "instance_uuid", "target_power_state", "chassis_uuid", "properties", "uuid", "driver_info", "target_provision_state", "last_error", "console_enabled", "extra", "driver", "links", "maintenance_reason", "updated_at", "provision_updated_at", "maintenance", "provision_state", "reservation", "created_at", "power_state", "instance_info", "ports", "name", "driver_internal_info", "inspection_finished_at", "inspection_started_at", "clean_step" ] self.url = "/ironic/v1/nodes" self.create_request = { "chassis_uuid": str(uuid4()), "driver": "agent_ipmitool", "driver_info": { "ipmi_username": "******", "ipmi_address": "127.0.0.0", "ipmi_password": "******" }, "name": "test_node", "properties": { "cpus": "1", "local_gb": "10", "memory_mb": "1024" } }
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 setUp(self): self.core = MimicCore(Clock(), []) self.root = MimicRoot(self.core).app.resource() self.uri = "/identity/v2.0/services" self.eeapi_name = u"externalServiceName" self.headers = {b'X-Auth-Token': [b'ABCDEF987654321']} self.verb = b"GET"
def createSwiftService(self, rackspace_flavor=True): """ Set up to create the requests """ self.swift_mock = SwiftMock(rackspace_flavor) self.core = MimicCore(Clock(), [self.swift_mock]) self.root = MimicRoot(self.core).app.resource() self.response = request( self, self.root, b"POST", b"/identity/v2.0/tokens", dumps({ "auth": { "passwordCredentials": { "username": "******", "password": "******", }, # TODO: should this really be 'tenantId'? "tenantName": "fun_tenant", } }).encode("utf-8")) self.auth_response = self.successResultOf(self.response) text_body = self.successResultOf(treq.content( self.auth_response)).decode("utf-8") self.json_body = loads(text_body)
def test_response_has_auth_token(self): """ The JSON response has a access.token.id key corresponding to its MimicCore session, and therefore access.token.tenant.id should match that session's tenant_id. """ core = MimicCore(Clock(), []) 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(200, response.code) token = json_body['access']['token']['id'] tenant_id = json_body['access']['token']['tenant']['id'] session = core.sessions.session_for_token(token) self.assertEqual(token, session.token) self.assertEqual(tenant_id, session.tenant_id)
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_tick(self): """ ``/mimic/v1.1/tick`` (handled by :func:`MimicRoot.advance_time`) advances the clock associated with the service. """ clock = Clock() def do(): do.done = True do.done = False clock.callLater(3.5, do) core = MimicCore(clock, []) root = MimicRoot(core, clock).app.resource() self.assertEqual(do.done, False) jreq = json_request( self, root, "POST", "/mimic/v1.1/tick", body={"amount": 3.6} ) [response, json_content] = self.successResultOf(jreq) self.assertEqual(response.code, 200) expected = { 'advanced': 3.6, 'now': '1970-01-01T00:00:03.600000Z', } self.assertEqual(json_content, expected) self.assertEqual(do.done, True)
def __init__(self, test_case, apis): """ Initialize a mimic core and the specified :obj:`mimic.imimic.IAPIMock`s :param apis: A list of :obj:`mimic.imimic.IAPIMock` objects to be initialized """ self.test_case = test_case self.clock = Clock() self.core = MimicCore(self.clock, apis) self.root = MimicRoot(self.core).app.resource() # Pass in arbitrary username and password self.auth = TenantAuthentication(test_case, self.root, "test1", "test1password") # map some attributes and methods self.service_catalog_json = self.auth.service_catalog_json self.get_service_endpoint = self.auth.get_service_endpoint # Tenant ID of test tenant authenticated against mimic identity self.tenant_id = self.auth.service_catalog_json["access"]["token"][ "tenant"]["id"] service_name = apis[0].catalog_entries(self.tenant_id)[0].name self.uri = self.get_service_endpoint(service_name)
def test_response_has_same_roles_despite_number_of_auths(self): """ The JSON response for authenticate has only one `identity:user-admin` role, no matter how many times the user authenticates. """ core = MimicCore(Clock(), []) root = MimicRoot(core).app.resource() creds = { "auth": { "passwordCredentials": { "username": "******", "password": "******" } } } self.successResultOf( json_request(self, root, "POST", "/identity/v2.0/tokens", creds)) self.successResultOf( json_request(self, root, "POST", "/identity/v2.0/tokens", creds)) (response, json_body) = self.successResultOf( json_request(self, root, "POST", "/identity/v2.0/tokens", creds)) self.assertEqual(200, response.code) self.assertEqual(json_body['access']['user']['roles'], HARD_CODED_ROLES)
def test_entries_for_tenant_external(self): """ Validate that the external API shows up in the service catalog for a given tenant. """ eeapi = make_example_external_api(self, name=self.eeapi_name, set_enabled=True) core = MimicCore(Clock(), [eeapi]) prefix_map = {} base_uri = "http://some/random/prefix" catalog_entries = [ entry for entry in core.entries_for_tenant('some-tenant', prefix_map, base_uri) ] self.assertEqual(len(core._uuid_to_api_internal), 0) self.assertEqual(len(core._uuid_to_api_external), 1) self.assertEqual(len(catalog_entries), 1) self.assertEqual(catalog_entries[0].type, eeapi.type_key) self.assertEqual(catalog_entries[0].name, eeapi.name_key) self.assertEqual(catalog_entries[0].tenant_id, "some-tenant") self.assertEqual(len(catalog_entries[0].endpoints), 1)
def setUp(self): """ Create a check """ core = MimicCore(Clock(), []) self.root = MimicRoot(core).app.resource() self.create_check = { "check": { "attributes": { "name": "name", "module": "module", "target": "target", "period": "period", "timeout": "timeout", "filterset": "filterset" } } } self.create_check_xml_payload = xmltodict.unparse( self.create_check).encode("utf-8") self.check_id = uuid.uuid4() url = "noit/checks/set/{0}".format(self.check_id) (self.response, response_body) = self.successResultOf( request_with_content(self, self.root, "PUT", url, body=self.create_check_xml_payload)) self.create_json_response = xmltodict.parse(response_body)
def setUp(self): """ Create a :obj:`MimicCore` with :obj:`QueueApi` as the only plugin, and create a queue """ self.clock = Clock() self.core = MimicCore(self.clock, [QueueApi()]) self.root = MimicRoot(self.core).app.resource() self.response = request( self, self.root, b"POST", "/identity/v2.0/tokens", json.dumps({ "auth": { "passwordCredentials": { "username": "******", "password": "******", }, } }).encode("utf-8")) self.auth_response = self.successResultOf(self.response) self.json_body = self.successResultOf( treq.json_content(self.auth_response)) self.uri = self.json_body['access']['serviceCatalog'][0]['endpoints'][ 0]['publicURL'] self.queue_name = "test_queue" self.create_queue = request(self, self.root, b"PUT", self.uri + '/queues/' + self.queue_name) self.create_queue_response = self.successResultOf(self.create_queue)
def setUp(self): """ Initialize core and root """ self.core = MimicCore(Clock(), []) self.root = MimicRoot(self.core).app.resource() self.url = "/valkyrie/v2.0"
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_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_different_username_different_token(self): """ Sessions are distinct if they are requested with distinct usernames. """ core = MimicCore(Clock(), []) a = core.session_for_username_password("a", "ignored") b = core.session_for_username_password("b", "ignored") self.assertNotEqual(a.token, b.token)
def test_get_external_api_invalid(self): """ Validate retrieving a non-existent external API which should raise an `IndexError` exception. """ core = MimicCore(Clock(), []) with self.assertRaises(ServiceDoesNotExist): core.get_external_api(self.eeapi_name)
def setUp(self): """ Initialize core and root """ self.core = MimicCore(Clock(), []) self.root = MimicRoot(self.core).app.resource() self.uri = "/glance/v2/images" self.create_request = {"name": "OnMetal - MIMIC", "distro": "linux"}
def test_remove_api_invalid(self): """ Validate API removal fails when it does not find the api name in the listing """ core = MimicCore(Clock(), []) with self.assertRaises(ServiceDoesNotExist): core.remove_external_api('some-id')
def test_no_uuids_if_no_plugins(self): """ If there are no plugins provided to :class:`MimicCore`, there are no uri prefixes or entries for the tenant. """ core = MimicCore(Clock(), []) self.assertEqual(0, len(core._uuid_to_api)) self.assertEqual([], list(core.entries_for_tenant('any_tenant', {}, 'http://mimic')))
def setUp(self): self.core = MimicCore(Clock(), []) self.root = MimicRoot(self.core).app.resource() self.uri = "/identity/v2.0/OS-KSCATALOG/endpointTemplates" self.eeapi_name = u"externalServiceName" self.eeapi = make_example_external_api(self, name=self.eeapi_name, set_enabled=True) self.headers = {b'X-Auth-Token': [b'ABCDEF987654321']} self.verb = b"GET"
def test_session_for_tenant_id(self): """ MimicCore.session_for_tenant_id will return a session that can be retrieved by tenant_id. """ clock = Clock() core = MimicCore(clock, []) session = core.session_for_username_password("someuser", "testpass") session2 = core.session_for_tenant_id(session.tenant_id) self.assertIdentical(session, session2)
def test_send_grid(self): """ ``/sendgrid/mail.send.json`` returns response code 200. """ core = MimicCore(Clock(), []) root = MimicRoot(core).app.resource() response = self.successResultOf( request(self, root, b"POST", "/sendgrid/mail.send.json")) self.assertEqual(200, response.code)
def test_fastly(self): """ The /fastly pointing to the fastly endpoint """ core = MimicCore(Clock(), []) root = MimicRoot(core).app.resource() (response, json_content) = self.successResultOf( json_request(self, root, b'GET', '/fastly')) self.assertEqual(200, response.code) self.assertEqual(json_content, {'status': 'ok'})
def test_get_external_api(self): """ Validate retrieving an external API. """ eeapi = make_example_external_api(self, name=self.eeapi_name, set_enabled=True) core = MimicCore(Clock(), [eeapi]) api_from_core = core.get_external_api(eeapi.uuid_key) self.assertEqual(eeapi, api_from_core)
def test_authentication_request_with_no_body_causes_http_bad_request(self): """ The response for empty body request is bad_request. """ core = MimicCore(Clock(), []) root = MimicRoot(core).app.resource() (response, json_body) = self.successResultOf( json_request(self, root, "POST", "/identity/v2.0/tokens", "")) self.assertEqual(400, response.code)
def test_generate_username_from_tenant_id(self): """ MimicCore.session_for_tenant_id will create a new session with a synthetic username if no such tenant ID yet exists. """ clock = Clock() core = MimicCore(clock, []) session = core.session_for_tenant_id("user_specified_tenant") session2 = core.session_for_username_password(session.username, "testpass") self.assertIdentical(session, session2)
def test_by_username_after_token(self): """ MimicCore.session_for_username_password should retrieve the same session that was created by MimicCore.session_for_token. """ core = MimicCore(Clock(), []) a = core.session_for_token("testtoken") b = core.session_for_username_password(a.username, "testpswd") c = core.session_for_api_key(a.username, "testapikey") self.assertIdentical(a, b) self.assertIdentical(a, c)
def test_remove_api_with_endpoints(self): """ Validate an API cannot be removed if it still has endpoints assigned to it """ eeapi = make_example_external_api(self, name=self.eeapi_name, set_enabled=True) self.assertIsNotNone(eeapi) core = MimicCore(Clock(), [eeapi]) with self.assertRaises(ServiceHasTemplates): core.remove_external_api(eeapi.uuid_key)