def test_mapping_request_serialization(self): e = MappingRequest(method='GET', url='test1', url_path='test2', url_path_pattern='test3', url_pattern='test4', basic_auth_credentials=BasicAuthCredentials( username='******', password='******'), cookies={'chocolate': 'chip'}, headers={'Accept': 'stuff'}, query_parameters={'param': '1'}, body_patterns={'test': 'test2'}) serialized = e.get_json_data() self.assertDictContainsKeyWithValue(serialized, 'method', 'GET') self.assertDictContainsKeyWithValue(serialized, 'url', 'test1') self.assertDictContainsKeyWithValue(serialized, 'urlPath', 'test2') self.assertDictContainsKeyWithValue(serialized, 'urlPathPattern', 'test3') self.assertDictContainsKeyWithValue(serialized, 'urlPattern', 'test4') self.assertDictContainsKeyWithValue(serialized, 'basicAuthCredentials', { 'username': '******', 'password': '******' }) self.assertDictContainsKeyWithValue(serialized, 'cookies', {'chocolate': 'chip'}) self.assertDictContainsKeyWithValue(serialized, 'headers', {'Accept': 'stuff'}) self.assertDictContainsKeyWithValue(serialized, 'queryParameters', {'param': '1'}) self.assertDictContainsKeyWithValue(serialized, 'bodyPatterns', {'test': 'test2'})
def test_mapping_request_serialization(self): e = MappingRequest(method="GET", url="test1", url_path="test2", url_path_pattern="test3", url_pattern="test4", basic_auth_credentials=BasicAuthCredentials( username="******", password="******"), cookies={"chocolate": "chip"}, headers={"Accept": "stuff"}, query_parameters={"param": "1"}, body_patterns={"test": "test2"}, metadata={'key': 'value'}) serialized = e.get_json_data() self.assertDictContainsKeyWithValue(serialized, "method", "GET") self.assertDictContainsKeyWithValue(serialized, "url", "test1") self.assertDictContainsKeyWithValue(serialized, "urlPath", "test2") self.assertDictContainsKeyWithValue(serialized, "urlPathPattern", "test3") self.assertDictContainsKeyWithValue(serialized, "urlPattern", "test4") self.assertDictContainsKeyWithValue(serialized, "basicAuthCredentials", { "username": "******", "password": "******" }) self.assertDictContainsKeyWithValue(serialized, "cookies", {"chocolate": "chip"}) self.assertDictContainsKeyWithValue(serialized, "headers", {"Accept": "stuff"}) self.assertDictContainsKeyWithValue(serialized, "queryParameters", {"param": "1"}) self.assertDictContainsKeyWithValue(serialized, "bodyPatterns", {"test": "test2"}) self.assertDictContainsKeyWithValue(serialized, "metadata", {"key": "value"})
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_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 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 set_bootstrap_response(data): Config.base_url = 'http://wiremock:8080/__admin' if data is None: mapping = Mapping( priority=100, request=MappingRequest(method=HttpMethods.GET, url='/api/bootstrap/'), response=MappingResponse(status=403, ), persistent=False, ) else: mapping = Mapping( priority=100, request=MappingRequest(method=HttpMethods.GET, url='/api/bootstrap/'), response=MappingResponse( status=200, body=json.dumps(data), headers={'Content-Type': 'application/json'}), persistent=False, ) Mappings.create_mapping(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_mapping_request_deserialization(self): serialized = { "method": "GET", "url": "test1", "urlPath": "test2", "urlPathPattern": "test3", "urlPattern": "test4", "basicAuthCredentials": { "username": "******", "password": "******" }, "cookies": { "chocolate": "chip" }, "headers": { "Accept": "stuff" }, "queryParameters": { "param": "1" }, "bodyPatterns": { "test": "test2" }, 'metadata': { 'key': [1, 2, 3] }, } e = MappingRequest.from_dict(serialized) self.assertIsInstance(e, MappingRequest) self.assertEqual("GET", e.method) self.assertEqual("test1", e.url) self.assertEqual("test2", e.url_path) self.assertEqual("test3", e.url_path_pattern) self.assertEqual("test4", e.url_pattern) self.assertIsInstance(e.basic_auth_credentials, BasicAuthCredentials) self.assertEqual("username", e.basic_auth_credentials.username) self.assertEqual("password", e.basic_auth_credentials.password) self.assertEqual({"chocolate": "chip"}, e.cookies) self.assertEqual({"Accept": "stuff"}, e.headers) self.assertEqual({"param": "1"}, e.query_parameters) self.assertEqual({"test": "test2"}, e.body_patterns) self.assertEqual({"key": [1, 2, 3]}, e.metadata)
def test_mapping_request_deserialization(self): serialized = { 'method': 'GET', 'url': 'test1', 'urlPath': 'test2', 'urlPathPattern': 'test3', 'urlPattern': 'test4', 'basicAuthCredentials': { 'username': '******', 'password': '******' }, 'cookies': { 'chocolate': 'chip' }, 'headers': { 'Accept': 'stuff' }, 'queryParameters': { 'param': '1' }, 'bodyPatterns': { 'test': 'test2' } } e = MappingRequest.from_dict(serialized) self.assertIsInstance(e, MappingRequest) self.assertEquals('GET', e.method) self.assertEquals('test1', e.url) self.assertEquals('test2', e.url_path) self.assertEquals('test3', e.url_path_pattern) self.assertEquals('test4', e.url_pattern) self.assertIsInstance(e.basic_auth_credentials, BasicAuthCredentials) self.assertEquals('username', e.basic_auth_credentials.username) self.assertEquals('password', e.basic_auth_credentials.password) self.assertEquals({'chocolate': 'chip'}, e.cookies) self.assertEquals({'Accept': 'stuff'}, e.headers) self.assertEquals({'param': '1'}, e.query_parameters) self.assertEquals({'test': 'test2'}, e.body_patterns)
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'