def testClient_sendRequest_noJsonValue(self): expectedIPAResponse = IPAResponse( session="b99a25695f578c0bb30cafb0932035bf", status_code=200, expiration="Sun, 06 Sep 2015 05:12:56 GMT", headers=None, ) mockResponse = MockResponse(status_code=500, headers=None) ipaAuth = IPAAuth(requests=None, baseUrl=self.baseUrl) ipaAuth.authenticate = MagicMock(return_value=expectedIPAResponse) requests.post = MagicMock(return_value=mockResponse) mockResponse.json = MagicMock(side_effect=ValueError("No JSON object could be decoded")) ipaClient = IPAClient(requests=requests, baseUrl=self.baseUrl, sourceUrl=self.sourceUrl, ipaAuth=ipaAuth) result = ipaClient.sendRequest("user_find", ["admin", "register-marcher", "smercado"]) self.assertEquals("No JSON object could be decoded", result.failure)
def test_should_send_valid_post_request_when_destroying_artifacts_without_files_specified( self, post_patched): post_patched.return_value = MockResponse(status_code=200) job_id = "some_job_id" result = self.runner.invoke( cli.cli, ["jobs", "artifacts", "destroy", job_id, "--apiKey", "some_key"]) post_patched.assert_called_with( "{}/jobs/{}/artifactsDestroy".format(self.URL, job_id), files=None, headers=self.EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=None, params=None, data=None) assert result.exit_code == 0
def test_should_print_proper_message_when_wrong_api_key_was_used( self, get_patched): get_patched.return_value = MockResponse( json_data=self.RESPONSE_JSON_WHEN_WRONG_API_KEY_WAS_USED, status_code=403) runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND) get_patched.assert_called_once_with(self.URL, headers=self.EXPECTED_HEADERS, json=None, params={"limit": -1}) assert result.output == self.EXPECTED_STDOUT_WHEN_WRONG_API_KEY_WAS_USED assert self.EXPECTED_HEADERS["X-API-Key"] != "some_key"
def test_should_send_post_request_and_print_proper_message_when_model_with_given_id_was_not_found( self, post_patched): post_patched.return_value = MockResponse( example_responses.DELETE_MODEL_404_RESPONSE_JSON, status_code=404) runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND) post_patched.assert_called_once_with(self.URL, headers=EXPECTED_HEADERS, json=self.EXPECTED_REQUEST_JSON, files=None, data=None, params=None) assert result.output == "Failed to delete resource: Unable to find model\n"
def test_should_send_proper_data_and_print_message_when_create_experiment_was_run_with_basic_options( self, post_patched): post_patched.return_value = MockResponse(self.RESPONSE_JSON_200, 200, self.RESPONSE_CONTENT_200) runner = CliRunner() result = runner.invoke(cli.cli, self.BASIC_OPTIONS_COMMAND) post_patched.assert_called_once_with(self.URL, headers=self.EXPECTED_HEADERS, json=self.BASIC_OPTIONS_REQUEST, params=None, files=None, data=None) assert self.EXPECTED_STDOUT in result.output assert result.exit_code == 0
def test_should_send_get_request_and_print_list_of_hyperparameters( self, get_patched): get_patched.return_value = MockResponse( example_responses.LIST_HYPERPARAMETERS_RESPONSE_JSON, 200) runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND) get_patched.assert_called_once_with( self.URL, headers=self.EXPECTED_HEADERS, json=None, params=self.EXPECTED_REQUEST_PARAMS) assert result.output == self.EXPECTED_STDOUT assert self.EXPECTED_HEADERS["X-API-Key"] != "some_key"
def test_should_send_get_request_and_print_single_node_experiment_details_in_a_table( self, get_patched): get_patched.return_value = MockResponse(self.SINGLE_NODE_RESPONSE_JSON, 200, "fake content") 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 == self.SINGLE_NODE_DETAILS_STDOUT assert result.exit_code == 0 assert self.EXPECTED_HEADERS["X-API-Key"] != "some_key"
def test_should_replace_api_key_in_headers_when_api_key_parameter_was_used( self, post_patched): post_patched.return_value = MockResponse(self.EXPECTED_RESPONSE, 201) runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND_WITH_API_KEY_PARAMETER_USED) post_patched.assert_called_once_with( self.URL, headers=self.EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=None, params=None) assert result.output == self.EXPECTED_STDOUT assert self.EXPECTED_HEADERS["X-API-Key"] != "some_key"
def test_should_print_proper_message_when_wrong_api_key_was_used( self, get_patched): get_patched.return_value = MockResponse( { "status": 400, "message": "Invalid API token" }, 400) runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND) get_patched.assert_called_once_with(self.URL, headers=EXPECTED_HEADERS, json=None, params=None) assert result.output == "Failed to fetch data: Invalid API token\n", result.exc_info
def test_should_send_proper_data_and_print_message_when_deployments_stop_used_with_wrong_id( self, post_patched): post_patched.return_value = MockResponse(self.RESPONSE_JSON_400, 400, "fake content") runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND) assert result.output == self.EXPECTED_STDOUT_WITH_WRONG_ID, result.exc_info post_patched.assert_called_once_with(self.URL, headers=EXPECTED_HEADERS, json=self.REQUEST_JSON, params=None, files=None, data=None) assert result.exit_code == 0
def test_should_send_post_request_when_models_delete_command_was_executed( self, post_patched): post_patched.return_value = MockResponse(status_code=204) runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND) assert result.output == self.EXPECTED_STDOUT, result.exc_info post_patched.assert_called_once_with(self.URL, headers=EXPECTED_HEADERS, json=self.EXPECTED_REQUEST_JSON, files=None, data=None, params=None) assert EXPECTED_HEADERS["X-API-Key"] != "some_key"
def test_should_read_options_from_yaml_file(self, post_patched, hyperparameters_start_config_path): post_patched.return_value = MockResponse(self.EXPECTED_RESPONSE, 201) command = self.COMMAND_WITH_OPTIONS_FILE[:] + [hyperparameters_start_config_path] runner = CliRunner() result = runner.invoke(cli.cli, command) assert result.output == self.EXPECTED_STDOUT, result.exc_info post_patched.assert_called_once_with(self.URL_V2, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=None, params=None, data=None, ) assert EXPECTED_HEADERS["X-API-Key"] != "some_key"
def test_should_send_get_request_and_print_list_of_models_filtered_experiment_id( self, get_patched): get_patched.return_value = MockResponse( example_responses.LIST_MODELS_RESPONSE_JSON) runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND_WITH_FILTERING_BY_EXPERIMENT_ID) get_patched.assert_called_once_with( self.URL, headers=EXPECTED_HEADERS, json=self.EXPECTED_REQUEST_JSON_WITH_FILTERING, params={"limit": -1}) assert result.output == self.EXPECTED_STDOUT
def test_should_send_get_request_and_print_proper_message_when_create_command_was_used_with_all_options(self, post_patched): post_patched.return_value = MockResponse(self.EXPECTED_RESPONSE, 201) runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND_WHEN_ALL_PARAMETERS_WERE_USED) post_patched.assert_called_once_with(self.URL_V2, headers=EXPECTED_HEADERS, json=self.EXPECTED_REQUEST_JSON_WHEN_ALL_PARAMETERS_WERE_USED, params=None, files=None, data=None) assert result.output == self.EXPECTED_STDOUT assert EXPECTED_HEADERS["X-API-Key"] != "some_key"
def test_should_send_request_and_print_proper_message_when_error_code_returned_without_json_data(self, post_patched): post_patched.return_value = MockResponse(status_code=500) runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND) post_patched.assert_called_once_with(self.URL_V2, headers=EXPECTED_HEADERS, json=self.EXPECTED_REQUEST_JSON, params=None, files=None, data=None) assert result.output == "Failed to create resource\n" assert EXPECTED_HEADERS["X-API-Key"] != "some_key"
def test_should_send_valid_get_request_and_print_available_logs( self, get_patched): get_patched.return_value = MockResponse( json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) get_patched.assert_called_with( self.URL, headers=self.EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=None, params=self.BASIC_COMMAND_PARAMS) assert result.output == self.EXPECTED_STDOUT assert result.exit_code == 0
def test_should_send_proper_data_with_custom_api_key_when_api_key_parameter_was_provided( self, post_patched): post_patched.return_value = MockResponse(status_code=204) runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND_WITH_API_KEY) post_patched.assert_called_once_with( self.URL, headers=self.EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=self.REQUEST_JSON, params=None, files=None, data=None) assert result.output == self.EXPECTED_STDOUT assert result.exit_code == 0
def test_should_print_proper_message_when_error_message_received( self, post_patched): post_patched.return_value = MockResponse( self.EXPECTED_RESPONSE_JSON_WITH_ERROR, 400) runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND) post_patched.assert_called_once_with(self.URL, headers=self.EXPECTED_HEADERS, json=self.EXPECTED_REQUEST_JSON, params=None, files=None, data=None) assert result.output == self.EXPECTED_STDOUT_WHEN_ERROR_RECEIVED
def test_should_print_proper_message_when_wrong_api_key_was_used( self, post_patched): post_patched.return_value = MockResponse( self.EXPECTED_RESPONSE_WHEN_WRONG_API_KEY_WAS_USED, 401) runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND) post_patched.assert_called_once_with(self.URL, headers=EXPECTED_HEADERS, json=self.EXPECTED_REQUEST_JSON, files=None, data=None, params=None) assert result.output == "Failed to delete resource: Invalid API token\n"
def test_hook_issue_comment_by_bot_request(issue_bot_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_many([{ "_id": 1234, "slack": { "user_id": "user1ID", "user_name": "user1", "response_url": "https://hooks.slack.com/commands/asdfasdf/asfasdf/qweqf", "team_domain": "test" }, "github": { "id": 1234, "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(issue_bot_comment_event, "") assert ret["statusCode"] == 200 assert ret["body"] == 'ok' assert comment_collection.count_documents({}) == 0 mock_post.assert_not_called()
def test_list_dataset_versions(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/versions", headers=EXPECTED_HEADERS, json=None, params={ "filter[limit]": 21, "filter[skip]": 0, "filter[where][isCommitted]": "true", "filter[order][]": "dtCreated DESC", })
def test_should_read_options_from_yaml_file(self, get_patched, models_list_config_path): get_patched.return_value = MockResponse( example_responses.LIST_MODELS_RESPONSE_JSON) command = self.COMMAND_WITH_OPTIONS_FILE[:] + [models_list_config_path] runner = CliRunner() result = runner.invoke(cli.cli, command) get_patched.assert_called_once_with( self.URL, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=self.EXPECTED_REQUEST_JSON_WITH_FILTERING, params={"limit": -1}) assert result.output == self.EXPECTED_STDOUT assert EXPECTED_HEADERS["X-API-Key"] != "some_key"
def test_should_send_get_request_and_print_request_content_when_response_data_was_malformed( self, get_patched): get_patched.return_value = MockResponse({}, 200, "fake content") g = """Error parsing response data fake content """ 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 == g assert result.exit_code == 0
def test_should_print_proper_message_when_jobs_list_was_used_with_mutually_exclusive_filters( self, get_patched): get_patched.return_value = MockResponse( json_data=self.RESPONSE_JSON_WITH_MUTUALLY_EXCLUSIVE_FILTERS, status_code=422) cli_runner = CliRunner() result = cli_runner.invoke( cli.cli, self.BASIC_COMMAND_WITH_MUTUALLY_EXCLUSIVE_FILTERS) get_patched.assert_called_with( self.URL, headers=self.EXPECTED_HEADERS, json=self.EXPECTED_REQUEST_JSON_WITH_MUTUALLY_EXCLUSIVE_FILTERS, params=None) assert result.output == self.EXPECTED_STDOUT_WHEN_MUTUALLY_EXCLUSIVE_FILTERS assert result.exit_code == 0
def test_should_send_proper_data_and_print_message_when_create_wrong_model_id_was_given( self, post_patched): post_patched.return_value = MockResponse( self.RESPONSE_JSON_404_MODEL_NOT_FOUND, 404, self.RESPONSE_CONTENT_404_MODEL_NOT_FOUND) runner = CliRunner() result = runner.invoke(cli.cli, self.BASIC_OPTIONS_COMMAND) post_patched.assert_called_once_with(self.URL, headers=EXPECTED_HEADERS, json=self.BASIC_OPTIONS_REQUEST, params=None, files=None, data=None) assert result.output == self.EXPECTED_STDOUT_MODEL_NOT_FOUND assert result.exit_code == 0
def testAuth_sendAuthRequest_invalid(self): expectedIPAResponse = IPAResponse( status_code=999, headers=None, failure='graceful failure for status codes that is not 200 or 401' ) requestResponseObject = MockResponse( headers=None, status_code=999 ) requests.post = MagicMock(return_value=requestResponseObject) ipaClient = IPAAuth(requests=requests, baseUrl=self.baseUrl) result = ipaClient.authenticate('fff', 'fab') # The function returns the entire response so that it will be easier to debug self.assertEquals(requestResponseObject, result.failure)
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) # test processor with patch.object(NameXService, 'query_nr_number', return_value=nr_response) \ as mock_query_nr_number: email = nr_notification.process( { '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 } } }, option) assert email['content']['subject'] == f'{nr_number} - {subject}' assert '*****@*****.**' in email['recipients'] assert email['content']['body'] if option == nr_notification.Option.REFUND.value: assert f'${refund_value} CAD' in email['content']['body'] assert email['content']['attachments'] == [] assert mock_query_nr_number.call_args[0][0] == nr_number if option == nr_notification.Option.BEFORE_EXPIRY.value: assert nr_number in email['content']['body'] assert expected_legal_name in email['content']['body'] exp_date = LegislationDatetime.format_as_report_string( datetime.fromisoformat(expiration_date)) assert exp_date in email['content']['body']
def test_should_print_proper_message_when_wrong_api_key_was_used( self, post_patched): post_patched.return_value = MockResponse( self.EXPECTED_RESPONSE_WHEN_WRONG_API_KEY_WAS_USED, 403) runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND) post_patched.assert_called_once_with(self.URL_V2, headers=EXPECTED_HEADERS, json=self.EXPECTED_REQUEST_JSON, params=None, files=None, data=None) assert result.output == self.EXPECTED_STDOUT_WHEN_WRONG_API_KEY_WAS_USED assert EXPECTED_HEADERS["X-API-Key"] != "some_key"
def test_should_replate_api_key_in_headers_when_api_key_parameter_was_used( self, get_patched): get_patched.return_value = MockResponse( example_responses.LIST_MODELS_RESPONSE_JSON, 200, "fake content") runner = CliRunner() result = runner.invoke(cli.cli, self.COMMAND_WITH_API_KEY_PARAMETER_USED) get_patched.assert_called_once_with( self.URL, headers=self.EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=None, params={"limit": -1}) assert result.output == self.EXPECTED_STDOUT assert self.EXPECTED_HEADERS["X-API-Key"] != "some_key"
def test_should_read_options_defined_in_a_config_file( self, get_patched, clusters_list_config_path): get_patched.return_value = MockResponse( json_data=self.LIMITED_LIST_CLUSTERS) command = self.COMMAND_WITH_OPTIONS_FILE[:] + [ clusters_list_config_path ] runner = CliRunner() result = runner.invoke(cli.cli, command) assert self.PAGINATED_LIST_STDOUT in str(result.output) get_patched.assert_called_once_with( self.URL, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=None, params=self.LIMITED_PARAMS, )
def test_should_read_options_from_yaml_file( self, get_patched, hyperparameters_details_config_path): get_patched.return_value = MockResponse( example_responses.HYPERPARAMETERS_DETAILS_RESPONSE_JSON) command = self.COMMAND_WITH_OPTIONS_FILE[:] + [ hyperparameters_details_config_path ] runner = CliRunner() result = runner.invoke(cli.cli, command) assert result.output == self.EXPECTED_STDOUT, result.exc_info get_patched.assert_called_once_with( self.URL_V2, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=None, params=None) assert EXPECTED_HEADERS["X-API-Key"] != "some_key"