def test_should_send_proper_data_and_tag_hyperopt_experiment( self, post_patched, get_patched, put_patched): post_patched.return_value = MockResponse(self.EXPECTED_RESPONSE) get_patched.return_value = MockResponse({}, ) put_patched.return_value = MockResponse( self.UPDATE_TAGS_RESPONSE_JSON_200) runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND_WITH_TAGS) post_patched.assert_called_once_with(self.URL_V2, headers=EXPECTED_HEADERS, json=self.EXPECTED_REQUEST_JSON, params=None, files=None, data=None) put_patched.assert_called_once_with( self.TAGS_URL, headers=EXPECTED_HEADERS, json=self.TAGS_JSON, params=None, data=None, ) assert result.output == self.EXPECTED_STDOUT, result.exc_info assert result.exit_code == 0
def test_should_send_proper_data_and_success(self, get_patched, put_patched, entity_command, entity, result_entity): entity_command = entity_command.split(" ") command = entity_command + self.COMMAND tags_json = self.TAGS_JSON.copy() tags_json["entity"] = entity expected_result = self.EXPECTED_STDOUT % result_entity get_patched.return_value = MockResponse({}, 200, "fake content") put_patched.return_value = MockResponse( self.UPDATE_TAGS_RESPONSE_JSON_200, 200, "fake content") runner = CliRunner() result = runner.invoke(cli.cli, command) assert result.output == expected_result, result.exc_info put_patched.assert_called_once_with( self.URL, headers=EXPECTED_HEADERS, json=tags_json, params=None, data=None, ) assert result.exit_code == 0
def test_should_read_options_from_yaml_file(self, get_patched, put_patched, entity_tags_add_config_path, entity_command, entity, result_entity): get_patched.return_value = MockResponse({}, 200, "fake content") put_patched.return_value = MockResponse( self.UPDATE_TAGS_RESPONSE_JSON_200, 200, "fake content") entity_command = entity_command.split(" ") command = entity_command + self.COMMAND_WITH_OPTIONS_FILE + [ entity_tags_add_config_path ] tags_json = self.TAGS_JSON.copy() tags_json["entity"] = entity expected_result = self.EXPECTED_STDOUT % result_entity runner = CliRunner() result = runner.invoke(cli.cli, command) put_patched.assert_called_once_with( self.URL, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=tags_json, params=None, data=None, ) assert result.output == expected_result, result.exc_info assert result.exit_code == 0
def test_should_send_proper_data_and_tag_project(self, post_patched, get_patched, put_patched): post_patched.return_value = MockResponse(self.EXPECTED_RESPONSE_JSON_WHEN_ALL_PARAMETERS_WERE_USED, 201) get_patched.return_value = MockResponse({}, 200, "fake content") put_patched.return_value = MockResponse(self.UPDATE_TAGS_RESPONSE_JSON_200, 200, "fake content") runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND_WITH_TAGS) post_patched.assert_called_once_with(self.URL, headers=EXPECTED_HEADERS, json=self.EXPECTED_REQUEST_JSON, params=None, files=None, data=None) put_patched.assert_called_once_with( self.TAGS_URL, headers=EXPECTED_HEADERS, json=self.TAGS_JSON, params=None, data=None, ) assert result.output == self.EXPECTED_STDOUT, result.exc_info assert result.exit_code == 0
def test_should_send_proper_data_and_tag_machine(self, post_patched, put_patched, get_patched): post_patched.return_value = MockResponse(self.CREATE_MODEL_V2_REPONSE) put_patched.return_value = MockResponse() get_patched.side_effect = [ MockResponse(self.GET_PRESIGNED_URL_RESPONSE), MockResponse({}) ] runner = CliRunner() with runner.isolated_filesystem(): with open(self.MODEL_FILE, "w") as h: h.write("I'm a model!") result = runner.invoke(cli.cli, self.BASE_COMMAND_WITH_TAGS) assert result.output == self.EXPECTED_STDOUT, result.exc_info post_patched.assert_has_calls([ mock.call(self.URL, headers=EXPECTED_HEADERS, json=None, files=None, data=None, params=self.BASE_PARAMS), ]) get_patched.assert_has_calls([ mock.call( self.GET_PRESIGNED_URL, headers=EXPECTED_HEADERS, params=self.GET_PRESIGNED_URL_PARAMS_BASIC, json=None, ), ]) put_patched.assert_has_calls([ mock.call( self.GET_PRESIGNED_URL_RESPONSE, headers={"Content-Type": mock.ANY}, json=None, params=None, data=mock.ANY, ), mock.call( self.TAGS_URL, headers=EXPECTED_HEADERS, json=self.TAGS_JSON, params=None, data=None, ), ]) assert EXPECTED_HEADERS["X-API-Key"] != "some_key"
def test_get_absolute_endpoint_from_response(self): """Any exposed endpoints (in HTTP header, HTML <head> or <body>) are found and returned as an absolute url.""" mock_response = MockResponse( url=self._get_absolute_target_url(), headers={"Link": snippets.http_link_endpoint()}, ) absolute_endpoint_from_http_headers = ( outgoing_webmentions._get_absolute_endpoint_from_response( mock_response)) self.assertEqual(self.absolute_endpoint, absolute_endpoint_from_http_headers) mock_response.headers = {} mock_response.text = snippets.html_head_endpoint() absolute_endpoint_from_html_head = ( outgoing_webmentions._get_absolute_endpoint_from_response( mock_response)) self.assertEqual(self.absolute_endpoint, absolute_endpoint_from_html_head) mock_response.headers = {} mock_response.text = snippets.html_body_endpoint() absolute_endpoint_from_html_body = ( outgoing_webmentions._get_absolute_endpoint_from_response( mock_response)) self.assertEqual(self.absolute_endpoint, absolute_endpoint_from_html_body)
def test_run_simple_file_with_args(self, get_files_patched, get_moniror_patched, workspace_zip_patched, post_patched): get_files_patched.return_value = mock.MagicMock() workspace_zip_patched.return_value = '/foo/bar' post_patched.return_value = MockResponse(status_code=200) mock_monitor = mock.MagicMock() mock_monitor.content_type = "mock/multipart" get_moniror_patched.return_value = mock_monitor runner = CliRunner() result = runner.invoke(cli.cli, [self.command_name] + self.common_commands + ["/myscript.py", "a", "b"]) expected_headers = self.headers.copy() expected_headers.update({ 'Content-Type': "mock/multipart" }) post_patched.assert_called_with(self.url, params={'name': u'test', 'projectId': u'projectId', 'workspaceFileName': 'bar', 'command': 'python{} myscript.py a b'.format(str(sys.version_info[0])), 'container': u'paperspace/tensorflow-python'}, data=mock.ANY, files=None, headers=expected_headers, json=None)
def test_run_python_command_with_args_and_no_workspace(self, post_patched): post_patched.return_value = MockResponse(status_code=200) runner = CliRunner() result = runner.invoke(cli.cli, [self.command_name] + self.common_commands + ["-c", "print(foo)", "--workspace", "none"]) expected_headers = self.headers.copy() post_patched.assert_called_with(self.url, json=None, data=None, files=None, headers=expected_headers, params={ 'name': u'test', 'projectId': u'projectId', 'workspaceFileName': 'none', 'command': 'python{} -c print(foo)'.format( str(sys.version_info[0])), 'container': u'paperspace/tensorflow-python', 'machineType': 'G1', })
def test_run_shell_command_with_args_with_s3_workspace( self, workspace_zip_patched, post_patched): workspace_zip_patched.return_value = '/foo/bar' post_patched.return_value = MockResponse() runner = CliRunner() result = runner.invoke( cli.cli, [self.command_name] + self.common_commands + ["-s", "echo foo", "--workspace", "s3://bucket/object"]) expected_headers = self.headers.copy() post_patched.assert_called_with(self.url, json=None, data=None, files=None, headers=expected_headers, params={ 'name': u'test', 'projectId': u'projectId', 'workspaceFileName': 's3://bucket/object', 'command': 'echo foo', 'container': u'paperspace/tensorflow-python', 'machineType': 'G1', })
def testAuth_sendAuthRequest_fail(self): expectedHeaders = { 'content-length': '201', 'keep-alive': 'timeout=5, max=100', 'server': 'Apache/2.4.6 (CentOS) mod_auth_kerb/5.4 mod_nss/2.4.6 NSS/3.16.2.3 Basic ECC mod_wsgi/3.4 Python/2.7.5', 'x-ipa-rejection-reason': 'invalid-password', 'connection': 'Keep-Alive', 'date': 'Sun, 06 Sep 2015 07:49:28 GMT', 'content-type': 'text/html; charset=utf-8' } expectedIPAResponse = IPAResponse( status_code=401, headers=expectedHeaders, failure='invalid-password' ) requestResponseObject = MockResponse( headers=expectedHeaders, status_code=401 ) requests.post = MagicMock(return_value=requestResponseObject) ipaClient = IPAAuth(requests=requests, baseUrl=self.baseUrl) result = ipaClient.authenticate( username='******', password='******' ) self.assertEquals(expectedIPAResponse.session, result.session) self.assertEquals(expectedIPAResponse.headers, result.headers) self.assertEquals(expectedIPAResponse.status_code, result.status_code) self.assertEquals(expectedIPAResponse.expiration, result.expiration)
def _mock_get_ok(url, headers=None, **kwargs): return { f"https://{TARGET_DOMAIN}/": MockResponse( url, text=OUTGOING_WEBMENTION_HTML, headers=headers, status_code=200, ), f"https://{TARGET_DOMAIN}/some-article/": MockResponse( url, text=OUTGOING_WEBMENTION_HTML_MULTIPLE_LINKS, headers=headers, status_code=200, ), }.get(url)
def setUp(self): self.sample_ok = { "ticket": cases.generate_ticket(1, cases.STATUSES[0], cases.SOURCES[1]) } self.sample_ok["ticket"]["via"] = cases.VIA_TEMPLATES["email"] self.sample_ok_list = {"tickets": list()} for i in range(1, 101): self.sample_ok_list["tickets"].append( cases.generate_ticket(i, cases.STATUSES[i % 6], cases.SOURCES[i % 3])) self.resp_ok = MockResponse(self.sample_ok, 200) self.resp_ok_list = MockResponse(self.sample_ok_list, 200) self.resp_not_found = MockResponse( { "error": "RecordNotFound", "description": "Not found" }, 404) self.resp_auth_error = MockResponse(cases.ERROR_TEMPLATE["auth"], 401) self.resp_rate_exceed = MockResponse(dict(), 429, headers={"Retry-After": "120"}) self.resp_service_unavailable = MockResponse( dict(), 503, headers={"Retry-After": "1080"}) self.resp_unexpected_error = MockResponse( { "error": { "title": "Title of this error", "description": "Description of this error" } }, 500)
def test_should_send_get_request_and_error_message_when_wrong_api_key_was_used( self, get_patched): get_patched.return_value = MockResponse( content="Authentication failed", status_code=401) runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND_WITH_FOLLOW) assert "Awaiting logs...\nFailed to fetch data: Authentication failed\n" in result.output
def _mock_post_ok(url, headers=None, **kwargs): return { f"https://{TARGET_DOMAIN}/webmention/": MockResponse( url, headers=headers, status_code=200, ), }.get(url)
def test_no_matching_events(self): mock_response = MockResponse(ok=True, status_code='200', content="[]", json=[]) Query.get = mock_get(mock_response) event = EventExpression('test') response = self.c.get_event(event, limit=10) self.assertEqual(len(response), 0)
def _response(url, status_code, text, headers=None): if headers is None: headers = {"content-type": "text/html"} return MockResponse( url, text=text, status_code=status_code, headers=headers, )
def test_get_endpoint_in_html_body(self): """Endpoints exposed in HTML <body> are found correctly.""" mock_response = MockResponse( url=self._get_absolute_target_url(), text=snippets.html_body_endpoint(), ) endpoint_from_html_body = outgoing_webmentions._get_endpoint_in_html_response( mock_response) self.assertEqual(self.relative_endpoint, endpoint_from_html_body)
def test_should_read_options_from_yaml_file(self, post_patched, put_patched, get_patched, models_upload_config_path): post_patched.return_value = MockResponse(self.CREATE_MODEL_V2_REPONSE) put_patched.return_value = MockResponse() get_patched.return_value = MockResponse( self.GET_PRESIGNED_URL_RESPONSE) command = self.COMMAND_WITH_OPTIONS_FILE[:] + [ models_upload_config_path ] runner = CliRunner() with runner.isolated_filesystem(): with open(self.MODEL_FILE, "w") as h: h.write("I'm a model!") result = runner.invoke(cli.cli, command) assert result.output == self.EXPECTED_STDOUT, result.exc_info post_patched.assert_has_calls([ mock.call(self.URL, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=None, files=None, data=None, params=self.ALL_OPTIONS_PARAMS), ]) put_patched.assert_called_once_with( self.GET_PRESIGNED_URL_RESPONSE, headers={"Content-Type": ""}, json=None, params=None, data=mock.ANY) get_patched.assert_called_once_with( self.GET_PRESIGNED_URL, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, params=self.GET_PRESIGNED_URL_PARAMS, json=None, ) assert put_patched.call_args.kwargs["data"].encoder.fields["file"][ 0] == self.MODEL_FILE assert EXPECTED_HEADERS["X-API-Key"] != "some_key"
def _mock_post_error(url, headers=None, **kwargs): return { f"https://{TARGET_DOMAIN}/webmention/": MockResponse( url, text=OUTGOING_WEBMENTION_HTML, headers=headers, status_code=400, ), }.get(url)
def test_get_endpoint_in_http_headers(self): """Endpoints exposed in HTTP header are found correctly.""" mock_response = MockResponse( url=self._get_absolute_target_url(), headers={"Link": snippets.http_link_endpoint()}, ) endpoint_from_http_headers = outgoing_webmentions._get_endpoint_in_http_headers( mock_response) self.assertEqual(self.relative_endpoint, endpoint_from_http_headers)
def test_hook_accept_revision_request_request_notify_owner_non_existing_sender( accept_revision_event, mocker): review_requests_collection = mongomock.MongoClient().db.collection mocker.patch('repository._get_review_requests_collection', return_value=review_requests_collection) user_collection = mongomock.MongoClient().db.collection user_collection.insert_one({ "_id": 5763345, "slack": { "user_id": "user1ID", "user_name": "user2", "response_url": "https://hooks.slack.com/commands/asdfasdf/asfasdf/qweqf", "team_domain": "test" }, "github": { "id": 5763345, "user_name": "user2", "name": "User 2 Name", "access_token": "ahtawtewerg", "refresh_token": "anshttsetges" } }) mocker.patch('repository._get_users_collection', return_value=user_collection) slack_response = {'ts': '111111', 'channel': 'faf3as'} mock_post = mocker.patch('requests.post', return_value=MockResponse( 200, json.dumps(slack_response))) ret = github_webhook.lambda_handler(accept_revision_event, "") assert ret["statusCode"] == 200 assert ret["body"] == 'ok' mock_post.assert_called_with( 'https://slack.com/api/chat.postMessage', json.dumps({ 'channel': 'user1ID', # pylint: disable=C0301 'text': 'user1 approved [testrepo#2] <https://github.com/user1/testrepo/pull/2|WIP>', "unfurl_links": False, "unfurl_media": False, "attachments": [] }), headers={ 'Authorization': f'Bearer asdfasdfae3fasfas', "Content-Type": "application/json" })
def test_show_dataset_details(self, method): method.return_value = MockResponse(self.JSON) result = CliRunner().invoke(cli.cli, self.COMMAND + ["--id=dsttn2y7j1ux882"]) assert self.STDOUT in result.output, result.exc_info method.assert_called_once_with(URL + "/datasets/dsttn2y7j1ux882", headers=EXPECTED_HEADERS, json=None, params=None)
def test_hook_create_comment_request(create_comment_event, mocker): review_requests_collection = mongomock.MongoClient().db.collection mocker.patch('repository._get_review_requests_collection', return_value=review_requests_collection) user_collection = mongomock.MongoClient().db.collection user_collection.insert_one({ "_id": 3452345, "slack": { "user_id": "faf3as", "user_name": "user1", "response_url": "https://hooks.slack.com/commands/asdfasdf/asfasdf/qweqf", "team_domain": "test" }, "github": { "id": 3452345, "user_name": "user1", "name": "User 1 Name", "access_token": "ahtawtewerg", "refresh_token": "anshttsetges" } }) mocker.patch('repository._get_users_collection', return_value=user_collection) comment_collection = mongomock.MongoClient().db.collection mocker.patch('repository._get_comment_collection', return_value=comment_collection) slack_response = {'ts': '111111', 'channel': 'faf3as'} mock_post = mocker.patch('requests.post', return_value=MockResponse( 200, json.dumps(slack_response))) ret = github_webhook.lambda_handler(create_comment_event, "") assert ret["statusCode"] == 200 assert ret["body"] == 'ok' mock_post.assert_not_called() assert comment_collection.count_documents({}) == 1 cursor = comment_collection.find() assert cursor[0] == { '_id': '425445400:432885618', 'comment_id': 432885618, 'user_id': 5763345, 'pull_request': 425445400 }
def test_should_send_get_request_and_print_all_received_logs_when_logs_command_was_used_with_follow_flag( self, get_patched): get_patched.return_value = MockResponse( json_data=example_responses.LIST_OF_LOGS_FOR_EXPERIMENT, status_code=200) runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND_WITH_FOLLOW) assert "Downloading https://storage.googleapis.com/cvdf-datasets/mnist/t10k-labels" \ "-idx1-ubyte.gz to /tmp/tmpbrss4txl.gz" in result.output
def test_nr_notification(app, session, option, nr_number, subject, expiration_date, refund_value, expected_legal_name, names): """Assert that the nr notification can be processed.""" nr_json = { 'expirationDate': expiration_date, 'names': names, 'applicants': { 'emailAddress': '*****@*****.**' } } nr_response = MockResponse(nr_json, 200) token = 'token' # run worker with patch.object(AccountService, 'get_bearer_token', return_value=token): with patch.object(NameXService, 'query_nr_number', return_value=nr_response) \ as mock_query_nr_number: with patch.object(worker, 'send_email', return_value='success') as mock_send_email: worker.process_email( { 'id': '123456789', 'type': 'bc.registry.names.request', 'source': f'/requests/{nr_number}', 'identifier': nr_number, 'data': { 'request': { 'nrNum': nr_number, 'option': option, 'refundValue': refund_value } } }, app) call_args = mock_send_email.call_args assert call_args[0][0]['content'][ 'subject'] == f'{nr_number} - {subject}' assert call_args[0][0]['recipients'] == '*****@*****.**' assert call_args[0][0]['content']['body'] if option == nr_notification.Option.REFUND.value: assert f'${refund_value} CAD' in call_args[0][0][ 'content']['body'] assert call_args[0][0]['content']['attachments'] == [] assert mock_query_nr_number.call_args[0][0] == nr_number assert call_args[0][1] == token if option == nr_notification.Option.BEFORE_EXPIRY.value: assert nr_number in call_args[0][0]['content']['body'] assert expected_legal_name in call_args[0][0]['content'][ 'body'] exp_date = LegislationDatetime.format_as_report_string( datetime.fromisoformat(expiration_date)) assert exp_date in call_args[0][0]['content']['body']
def test_should_replace_api_key_in_headers_when_api_key_parameter_was_used( self, post_patched, put_patched, get_patched): post_patched.return_value = MockResponse(self.CREATE_MODEL_V2_REPONSE) put_patched.return_value = MockResponse() get_patched.return_value = MockResponse( self.GET_PRESIGNED_URL_RESPONSE) runner = CliRunner() with runner.isolated_filesystem(): with open(self.MODEL_FILE, "w") as h: h.write("I'm a model!") result = runner.invoke(cli.cli, self.COMMAND_WITH_API_KEY_PARAMETER_USED) assert result.output == self.EXPECTED_STDOUT, result.exc_info post_patched.assert_has_calls([ mock.call(self.URL, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=None, files=None, data=None, params=self.ALL_OPTIONS_PARAMS), ]) put_patched.assert_has_calls([ mock.call(self.GET_PRESIGNED_URL_RESPONSE, headers={"Content-Type": mock.ANY}, json=None, params=None, data=mock.ANY) ]) get_patched.assert_called_once_with( self.GET_PRESIGNED_URL, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, params=self.GET_PRESIGNED_URL_PARAMS, json=None, ) assert put_patched.call_args.kwargs["data"].encoder.fields["file"][ 0] == self.MODEL_FILE assert EXPECTED_HEADERS["X-API-Key"] != "some_key"
def test_should_send_get_request_and_paginate_list_of_clusters( self, get_patched): get_patched.return_value = MockResponse(self.LIMITED_LIST_CLUSTERS) runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND_WITH_LIMIT) assert self.PAGINATED_LIST_STDOUT in str(result.output) get_patched.assert_called_once_with(self.URL, headers=EXPECTED_HEADERS, json=None, params=self.LIMITED_PARAMS)
def test_should_print_proper_message_when_got_error_response_without_data(self, get_patched): get_patched.return_value = MockResponse(status_code=500) runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND) get_patched.assert_called_once_with(self.URL, headers=self.EXPECTED_HEADERS, json=None, params=None) assert result.output == "Failed to fetch data\n"
def test_should_send_get_request_and_print_list_of_clusters( self, get_patched): get_patched.return_value = MockResponse(self.LIST_CLUSTERS) runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND) assert self.LIST_STDOUT in result.output, result.exc_info get_patched.assert_called_once_with(self.URL, headers=EXPECTED_HEADERS, json=None, params=self.DEFAULT_PARAMS)
def test_show_storage_provider_details(self, method): method.return_value = MockResponse(self.JSON) result = CliRunner().invoke(cli.cli, self.COMMAND + ["--id=spltautet072md4"]) assert self.STDOUT in result.output, result.exc_info method.assert_called_once_with( URL + "/storageProviders/spltautet072md4", headers=EXPECTED_HEADERS, json=None, params=None )