def test_mapping_serialization(self): e = Mapping( priority=1, request=MappingRequest(method="GET", url="test"), response=MappingResponse(status=200, status_message="test2"), persistent=False, post_serve_actions={"test": "1"}, new_scenario_state="test3", required_scenario_state="test4", scenario_name="test5", ) serialized = e.get_json_data() self.assertDictContainsKeyWithValue(serialized, "priority", 1) self.assertDictContainsKeyWithValue(serialized, "request", { "method": "GET", "url": "test" }) self.assertDictContainsKeyWithValue(serialized, "response", { "status": 200, "statusMessage": "test2" }) self.assertDictContainsKeyWithValue(serialized, "persistent", False) self.assertDictContainsKeyWithValue(serialized, "postServeActions", {"test": "1"}) self.assertDictContainsKeyWithValue(serialized, "newScenarioState", "test3") self.assertDictContainsKeyWithValue(serialized, "requiredScenarioState", "test4") self.assertDictContainsKeyWithValue(serialized, "scenarioName", "test5")
def test_mapping_deserialization(self): serialized = { "priority": 1, "request": { "method": "GET", "url": "test" }, "response": { "status": 200, "statusMessage": "test2" }, "persistent": False, "postServeActions": { "test": "1" }, "newScenarioState": "test3", "requiredScenarioState": "test4", "scenarioName": "test5", } e = Mapping.from_dict(serialized) self.assertIsInstance(e, Mapping) self.assertEqual(1, e.priority) self.assertIsInstance(e.request, MappingRequest) self.assertEqual("GET", e.request.method) self.assertEqual("test", e.request.url) self.assertIsInstance(e.response, MappingResponse) self.assertEqual(200, e.response.status) self.assertEqual("test2", e.response.status_message) self.assertEqual(False, e.persistent) self.assertEqual({"test": "1"}, e.post_serve_actions) self.assertEqual("test3", e.new_scenario_state) self.assertEqual("test4", e.required_scenario_state) self.assertEqual("test5", e.scenario_name)
def test_mapping_deserialization(self): serialized = { 'priority': 1, 'request': { 'method': 'GET', 'url': 'test' }, 'response': { 'status': 200, 'statusMessage': 'test2' }, 'persistent': False, 'postServeActions': { 'test': '1' }, 'newScenarioState': 'test3', 'requiredScenarioState': 'test4', 'scenarioName': 'test5' } e = Mapping.from_dict(serialized) self.assertIsInstance(e, Mapping) self.assertEquals(1, e.priority) self.assertIsInstance(e.request, MappingRequest) self.assertEquals('GET', e.request.method) self.assertEquals('test', e.request.url) self.assertIsInstance(e.response, MappingResponse) self.assertEquals(200, e.response.status) self.assertEquals('test2', e.response.status_message) self.assertEquals(False, e.persistent) self.assertEquals({'test': '1'}, e.post_serve_actions) self.assertEquals('test3', e.new_scenario_state) self.assertEquals('test4', e.required_scenario_state) self.assertEquals('test5', e.scenario_name)
def test_mapping_serialization(self): e = Mapping(priority=1, request=MappingRequest(method='GET', url='test'), response=MappingResponse(status=200, status_message='test2'), persistent=False, post_serve_actions={'test': '1'}, new_scenario_state='test3', required_scenario_state='test4', scenario_name='test5') serialized = e.get_json_data() self.assertDictContainsKeyWithValue(serialized, 'priority', 1) self.assertDictContainsKeyWithValue(serialized, 'request', { 'method': 'GET', 'url': 'test' }) self.assertDictContainsKeyWithValue(serialized, 'response', { 'status': 200, 'statusMessage': 'test2' }) self.assertDictContainsKeyWithValue(serialized, 'persistent', False) self.assertDictContainsKeyWithValue(serialized, 'postServeActions', {'test': '1'}) self.assertDictContainsKeyWithValue(serialized, 'newScenarioState', 'test3') self.assertDictContainsKeyWithValue(serialized, 'requiredScenarioState', 'test4') self.assertDictContainsKeyWithValue(serialized, 'scenarioName', 'test5')
def retrieve_mapping(cls, mapping_id, parameters={}): mapping_id = cls.get_entity_id(mapping_id, Mapping) ids = {'id': mapping_id} response = cls.REST_CLIENT.get(cls.get_base_uri( cls.endpoint_single(), **ids), headers=make_headers(), params=parameters) response = cls.REST_CLIENT.handle_response(response) return Mapping.from_dict(response.json())
def update_mapping(cls, mapping, parameters={}): cls.validate_is_entity(mapping, Mapping) mapping_id = cls.get_entity_id(mapping, Mapping) ids = {'id': mapping_id} response = cls.REST_CLIENT.put(cls.get_base_uri( cls.endpoint_single(), **ids), json=mapping.get_json_data(), headers=make_headers(), params=parameters) response = cls.REST_CLIENT.handle_response(response) return Mapping.from_dict(response.json())
def test_all_mappings_serialization(self): e = AllMappings(mappings=[ Mapping(priority=1), ], meta=MappingMeta(total=1)) serialized = e.get_json_data() self.assertDictContainsKeyWithValue(serialized, "mappings", [ { "priority": 1 }, ]) self.assertDictContainsKeyWithValue(serialized, "meta", {"total": 1})
def test_all_mappings_serialization(self): e = AllMappings(mappings=[ Mapping(priority=1), ], meta=MappingMeta(total=1)) serialized = e.get_json_data() self.assertDictContainsKeyWithValue(serialized, 'mappings', [ { 'priority': 1 }, ]) self.assertDictContainsKeyWithValue(serialized, 'meta', {'total': 1})
def stub_url_options_for_cors(mock_url): mapping = Mapping(priority=100, request=MappingRequest(method=HttpMethods.OPTIONS, url=mock_url), response=MappingResponse( status=200, headers={ 'Access-Control-Allow-Headers': 'Content-Type', 'Access-Control-Allow-Methods': "GET, POST" }, body=''), persistent=False) Mappings.create_mapping(mapping)
def stub_payment_status(mock_url, mock_json): mapping = Mapping(priority=100, request=MappingRequest(method=HttpMethods.GET, url=mock_url), response=MappingResponse( status=200, headers={ 'Access-Control-Allow-Headers': 'Content-Type', 'Access-Control-Allow-Methods': "GET, POST" }, json_body=get_mock_response_from_json(mock_json)), persistent=False) Mappings.create_mapping(mapping)
def response(status: int, status_message: str): Mappings.create_mapping(mapping=Mapping( priority=100, request=MappingRequest( method=HttpMethods.POST, url='/api/v2/logs/ingest', headers={'Authorization': {'equalTo': "Api-Token {}".format(ACCESS_KEY)}}, ), response=MappingResponse( status=status, status_message=status_message ), persistent=False ))
def stub_config(config_json): mapping = Mapping(priority=100, request=MappingRequest( method=HttpMethods.GET, url=MockUrl.CONFIG_MOCK_URI.value), response=MappingResponse( status=200, headers={ 'Access-Control-Allow-Headers': 'Content-Type', 'Access-Control-Allow-Methods': "GET, POST" }, json_body=get_mock_response_from_json(config_json)), persistent=False) Mappings.create_mapping(mapping)
def stub_st_request_type_server_error(mock_json, request_type): stub_url_options_for_cors(MockUrl.GATEWAY_MOCK_URI.value) configure_for_local_host() mapping = Mapping(priority=100, request=MappingRequest( method=HttpMethods.POST, url=MockUrl.GATEWAY_MOCK_URI.value, body_patterns=[{ "contains": request_type }]), response=MappingResponse( status=500, headers={ 'Access-Control-Allow-Headers': 'Content-Type', 'Access-Control-Allow-Methods': "GET, POST" }, json_body=get_mock_response_from_json(mock_json)), persistent=False) Mappings.create_mapping(mapping)
def test_labs(app_fixture): login_response = requests.post(app_fixture.base_url + '/api/auth/login', json={ 'username': '******', 'password': '******', }) assert login_response.ok requests.post(app_fixture.base_url + '/api/workers', json={ 'worker_id': 'TEST_ONE', 'host': 'localhost', 'port': app_fixture.wiremock_server.port, 'state': 1 }) requests.post(app_fixture.base_url + '/api/workers', json={ 'worker_id': 'TEST_TWO', 'host': 'localhost', 'port': 7601, 'state': 2 }) post_response = requests.post( app_fixture.base_url + '/api/labs', json={ 'lab_name': 'First lab', 'lab_type': 'Kathara', 'start_date': '2020-04-06T14:00:00.000Z', 'expiration_date': '2020-04-08T14:00:00.000Z', 'description': 'description', 'machines': ['m1', 'm2'] }, headers={ 'Authorization': 'Bearer {}'.format(login_response.json()['token']) }) assert post_response.ok lab_id = post_response.json() Config.base_url = 'http://localhost:{}/__admin'.format( app_fixture.wiremock_server.port) mapping = Mapping( priority=100, request=MappingRequest(method=HttpMethods.GET, url_path='/labs/status', query_parameters={'lab_ids': { 'matches': '.*' }}), response=MappingResponse(status=200, body=json.dumps([{ 'lab_id': lab_id, 'status': 'running' }])), persistent=True, ) Mappings.create_mapping(mapping=mapping) response = requests.get(app_fixture.base_url + '/api/labs', headers={ 'Authorization': 'Bearer {}'.format( login_response.json()['token']) }) assert response.ok body = response.json() assert len(body) == 1 assert body[0]['id'] == lab_id assert body[0]['status'] == 'running'