def test_create_digital_take_up_module_in_stagecraft( self, transform_types_patch, transform_patch, add_module_patch, list_module_types_patch, create_data_set_patch, get_data_set_patch, create_data_group_patch, get_data_group_patch, get_dashboard_patch, client ): get_dashboard_patch.return_value = {'slug': 'apply-uk-visa'} get_data_group_patch.return_value = None create_data_group_patch.return_value = {'name': 'apply-uk-visa'} get_data_set_patch.return_value = None create_data_set_patch.return_value = self.CREATE_DATA_SET_RETURN_VALUE list_module_types_patch.return_value = \ self.LIST_MODULE_TYPES_RETURN_VALUE with self.client.session_transaction() as session: session['upload_choice'] = 'week' client.post( '/dashboard/dashboard-uuid/digital-take-up/channel-options', data=self.params()) create_data_group_patch.assert_called_with( create_data_group_patch.return_value) create_data_set_patch.assert_called_with(match_equality(has_entries( { 'data_type': 'digital-takeup', 'data_group': 'apply-uk-visa', 'upload_format': 'csv', 'max_age_expected': 1300000 } ))) add_module_patch.assert_called_with( 'apply-uk-visa', match_equality(has_entries( { "data_group": "apply-uk-visa", "data_type": "digital-takeup", "type_id": "36546562-b2bd-44a9-b94a-e3cfc472ddf4" })))
def testSyncCommand_withCustomQueueTimeout(self, mock_ensure, sync): datastore_entities.Command.update_time._auto_now = False now = datetime.datetime.utcnow() command_1, command_2 = command_manager.CreateCommands( request_id=self.request_2.key.id(), command_infos=[ datastore_entities.CommandInfo( command_line='long command line', shard_count=2, run_target='foo', run_count=1, cluster='foobar'), datastore_entities.CommandInfo( command_line='longer_command_line', shard_count=2, run_target='foo', run_count=1, cluster='foobar'), ], shard_indexes=list(range(2)), queue_timeout_seconds=command_monitor.MAX_COMMAND_INACTIVE_TIME_MIN * 2 * 60) # Change update times. command_1 should ensure leasable, command_2 should # ensure leasable and cancel afterwards command_1.state = common.CommandState.QUEUED command_1.update_time = ( now - datetime.timedelta( minutes=command_monitor.MAX_COMMAND_INACTIVE_TIME_MIN)) command_1.put() command_2.state = common.CommandState.QUEUED command_2.update_time = ( now - datetime.timedelta( minutes=command_monitor.MAX_COMMAND_INACTIVE_TIME_MIN) * 3) command_2.put() command_monitor.SyncCommand(command_1.request_id, command_1.key.id()) command_monitor.SyncCommand(command_2.request_id, command_2.key.id()) mock_ensure.assert_has_calls([ mock.call( hamcrest.match_equality( hamcrest.has_property('key', command_1.key))), mock.call( hamcrest.match_equality( hamcrest.has_property('key', command_2.key))) ]) self.assertEqual(common.CommandState.QUEUED, command_1.key.get().state) self.assertEqual(common.CommandState.CANCELED, command_2.key.get().state) self.assertEqual(common.RequestState.CANCELED, self.request_2.key.get().state) sync.assert_called_once_with(command_1)
def test_send_email_if_all_exports_done_keeps_special_characters_unescaped( mocker, rf): User = get_user_model() # noqa extraction_order = ExtractionOrder(orderer=User(), excerpt=Excerpt(name='foo & bar')) incoming_request = rf.get('') im = mocker.patch.object(Emissary, 'inform_mail') extraction_order.send_email_if_all_exports_done(incoming_request) im.assert_called_once_with( subject=match_equality(contains_string('foo & bar')), mail_body=match_equality(contains_string('foo & bar')), )
def test_sets_info_to_unknown_when_no_organisation(self, transform_mock, dataset_module_mock, get_dashboard_mock, client): get_dashboard_mock.return_value = { 'organisation': None, 'slug': 'apply-uk-visa' } dataset_module_mock.return_value = {}, {}, {} with self.client.session_transaction() as session: session['upload_choice'] = 'week' client.post( '/dashboard/dashboard-uuid/digital-take-up/channel-options', data=self.params()) # match_equality(not_none()) is used because we dont care what any # arguments are except for the 3rd argument dataset_module_mock.assert_called_with( match_equality(not_none()), match_equality(not_none()), match_equality(not_none()), match_equality(not_none()), match_equality(not_none()), match_equality( has_entries({'info': has_item(contains_string('Unknown'))})), match_equality(not_none()), match_equality(not_none()))
def test_new_data_set_added_to_existing_module( self, add_module_patch, list_module_types_patch, transform_types_patch, transform_patch, create_data_set_patch, get_data_set_patch, get_data_group_patch, get_dashboard_patch, client): get_data_group_patch.return_value = {'name': 'apply-uk-visa'} get_data_set_patch.return_value = None create_data_set_patch.return_value = self.CREATE_DATA_SET_RETURN_VALUE list_module_types_patch.return_value = \ self.LIST_MODULE_TYPES_RETURN_VALUE with self.client.session_transaction() as session: session['upload_choice'] = 'week' client.post( '/dashboard/dashboard-uuid/digital-take-up/channel-options', data=self.params()) create_data_set_patch.assert_any_call( match_equality( has_entries({ 'data_group': 'apply-uk-visa', 'data_type': 'transactions-by-channel', 'auto_ids': '_timestamp, channel, period', 'max_age_expected': 1300000, 'upload_format': 'csv' }))) create_data_set_patch.assert_any_call( match_equality( has_entries({ 'data_group': 'apply-uk-visa', 'data_type': 'transactions-by-channel', 'max_age_expected': 1300000, 'upload_format': 'csv' }))) add_module_patch.assert_called_with( 'apply-uk-visa', match_equality( has_entries({ "data_group": "apply-uk-visa", "data_type": "digital-takeup", "type_id": "36546562-b2bd-44a9-b94a-e3cfc472ddf4" })))
def testSyncCommandAttempt_reset(self, mock_update): now = datetime.datetime.utcnow() update_time = ( now - datetime.timedelta( minutes=command_manager.MAX_COMMAND_EVENT_DELAY_MIN) * 2) _, request_id, _, command_id = self.command.key.flat() # disable auto_now or update_time will be ignored datastore_entities.CommandAttempt.update_time._auto_now = False command_event_test_util.CreateCommandAttempt( self.command, 'attempt_id', state=common.CommandState.UNKNOWN) event = command_event_test_util.CreateTestCommandEvent( request_id, command_id, 'attempt_id', common.InvocationEventType.INVOCATION_STARTED) command_manager.UpdateCommandAttempt(event) attempt = command_manager.GetCommandAttempts(request_id, command_id)[0] attempt.update_time = update_time attempt.put() command_attempt_monitor.SyncCommandAttempt( request_id, command_id, 'attempt_id') mock_update.assert_called_once_with( hamcrest.match_equality( hamcrest.all_of( hamcrest.has_property( 'task_id', 'task_id'), hamcrest.has_property( 'error', 'A task reset by command attempt monitor.'), hamcrest.has_property( 'attempt_state', common.CommandState.ERROR))))
def testProcessCommandEvent_final(self, plugin, attempt_metric): # Test ProcessCommandEvent for a final state command = self._CreateCommand() command_manager.ScheduleTasks([command]) _, request_id, _, command_id = command.key.flat() tasks = command_manager.GetActiveTasks(command) self.assertEqual(len(tasks), 1) command_task_store.LeaseTask(tasks[0].task_id) attempt = command_event_test_util.CreateCommandAttempt( command, "attempt0", common.CommandState.UNKNOWN, task=tasks[0]) event = command_event_test_util.CreateTestCommandEvent( request_id, command_id, "attempt0", common.InvocationEventType.INVOCATION_COMPLETED, task=tasks[0], time=TIMESTAMP) commander.ProcessCommandEvent(event) tasks = command_manager.GetActiveTasks(command) self.assertEqual(len(tasks), 0) command = command.key.get(use_cache=False) self.assertEqual(common.CommandState.COMPLETED, command.state) request = command.key.parent().get(use_cache=False) self.assertEqual(common.RequestState.COMPLETED, request.state) attempt_metric.assert_called_once_with(cluster_id=command.cluster, run_target=command.run_target, hostname="hostname", state="COMPLETED") plugin.assert_has_calls([ mock.call.OnCreateCommands([ plugin_base.CommandInfo(command_id=COMMAND_ID, command_line="command_line1", run_count=1, shard_count=1, shard_index=0) ], { "ants_invocation_id": "i123", "command_ants_work_unit_id": "w123" }, {}), mock.call.OnProcessCommandEvent( command, hamcrest.match_equality( hamcrest.all_of( hamcrest.has_property("command_id", attempt.command_id), hamcrest.has_property("attempt_id", attempt.attempt_id), hamcrest.has_property("task_id", attempt.task_id), )), event_data={ "total_test_count": 1000, "device_lost_detected": 0, "failed_test_run_count": 10, "passed_test_count": 900, "failed_test_count": 100, "summary": "summary" }), ])
def test_reports_failure_on_file_not_found(): cover_file = resources.path("missing_file.jpg") cover_art_selection = flexmock() cover_art_selection.should_receive("failed").with_args(match_equality(instance_of(FileNotFoundError))).once() artwork.load(cover_art_selection)(cover_file)
def testSyncCommand(self, mock_ensure, sync): datastore_entities.Command.update_time._auto_now = False now = datetime.datetime.utcnow() command = command_manager.CreateCommands( request_id=self.request.key.id(), command_infos=[ datastore_entities.CommandInfo( command_line='long command line', cluster='foobar', run_target='foo', run_count=1, shard_count=1) ], shard_indexes=list(range(1)))[0] command.state = common.CommandState.QUEUED command.update_time = ( now - datetime.timedelta( minutes=command_monitor.MAX_COMMAND_INACTIVE_TIME_MIN) * 2) command.put() command_monitor.SyncCommand(command.request_id, command.key.id()) mock_ensure.assert_called_once_with( hamcrest.match_equality(hamcrest.has_property('key', command.key))) self.assertEqual(common.CommandState.CANCELED, command.key.get().state) self.assertEqual(common.RequestState.CANCELED, self.request.key.get().state) sync.assert_not_called()
def test_large_payloads_are_compressed(self, mock_request): mock_request.__name__ = 'request' client = BaseClient('', 'token') client._post('', 'x' * 3000) mock_request.assert_called_with( method=mock.ANY, url=mock.ANY, headers=match_equality(has_entries({ 'Content-Encoding': 'gzip' })), data=mock.ANY, params=None, ) gzipped_bytes = mock_request.call_args[1]['data'].getvalue() eq_(38, len(gzipped_bytes)) # Does it look like a gzipped stream of bytes? # http://tools.ietf.org/html/rfc1952#page-5 eq_(b'\x1f'[0], gzipped_bytes[0]) eq_(b'\x8b'[0], gzipped_bytes[1]) eq_(b'\x08'[0], gzipped_bytes[2])
def testBackfillCommands(self, mock_add): command_1, command_2, command_3 = command_manager.CreateCommands( request_id=self.request.key.id(), command_infos=[ datastore_entities.CommandInfo( command_line='long command line', shard_count=3, run_target='foo', run_count=1, cluster='foobar'), datastore_entities.CommandInfo( command_line='longer_command_line', shard_count=3, run_target='foo', run_count=1, cluster='foobar'), datastore_entities.CommandInfo( command_line='short_cmd', shard_count=3, run_target='foo', run_count=1, cluster='foobar'), ], shard_indexes=list(range(3)), request_plugin_data={ 'ants_invocation_id': 'i123', 'ants_work_unit_id': 'w123' }) command_1.state = common.CommandState.QUEUED command_1.put() command_2.state = common.CommandState.QUEUED command_2.put() command_3.state = common.CommandState.RUNNING command_3.put() response = self.testapp.post_json( '/_ah/api/CoordinatorApi.BackfillCommands', {}) self.assertEqual('200 OK', response.status) mock_add.assert_has_calls( [ mock.call( hamcrest.match_equality( hamcrest.has_property('key', command_1.key))), mock.call( hamcrest.match_equality( hamcrest.has_property('key', command_2.key))), ], any_order=True) self.assertEqual(2, mock_add.call_count)
def test_list(self): collector_1 = CollectorFactory() collector_2 = CollectorFactory() response = self.client.get( '/collector', HTTP_AUTHORIZATION='Bearer development-oauth-access-token', content_type='application/json') assert_that(response.status_code, equal_to(200)) resp_json = json.loads(response.content) assert_that(resp_json, match_equality( has_entries({"slug": collector_1.slug}))) assert_that(resp_json, match_equality( has_entries({"slug": collector_2.slug})))
def test_list(self): provider_1 = ProviderFactory() provider_2 = ProviderFactory() response = self.client.get( '/provider', HTTP_AUTHORIZATION='Bearer development-oauth-access-token', content_type='application/json') assert_that(response.status_code, equal_to(200)) resp_json = json.loads(response.content) assert_that(resp_json, match_equality(has_entries({"slug": provider_1.slug}))) assert_that(resp_json, match_equality(has_entries({"slug": provider_2.slug})))
def test_task_manager_instantiated_correctly(self, mock_taskmanager_cons): PreviewStatsCommand( argv=["2013-03-03", "2013-04-03", "somehash", "dsusername", "dsapikey"]) timespan_type_matcher = match_equality(instance_of(TimespanSplitter)) mock_taskmanager_cons.assert_called_with( ANY, timespan_type_matcher, "somehash")
def test_create_data_group_and_set_if_nothing_exists( self, add_module_to_dashboard_patch, list_module_types_patch, create_data_group_patch, get_data_group_patch, create_data_set_patch, get_data_set_patch, get_dashboard_patch, client): get_dashboard_patch.return_value = {'slug': 'visas'} get_data_set_patch.return_value = None create_data_set_patch.return_value = { 'name': 'apply_uk_visa_transactions_by_channel', 'data_type': 'transactions-by-channel', 'data_group': 'apply-uk-visa', 'bearer_token': 'abc123', 'upload_format': 'csv', 'auto_ids': '_timestamp, period, channel', 'max_age_expected': 1300000 } get_data_group_patch.return_value = None create_data_group_patch.return_value = {'name': 'visas'} list_module_types_patch.return_value = [{ 'name': 'single_timeseries', 'id': 'uuid' }] add_module_to_dashboard_patch.return_value = {} client.post(self.upload_url, data=self.file_data) get_data_set_patch.assert_called_once_with("visas", "cost-per-transaction") create_data_set_patch.assert_called_once_with({ 'upload_format': 'csv', 'bearer_token': 'abc123def', 'data_group': 'visas', 'data_type': 'cost-per-transaction', 'max_age_expected': 0, 'auto_ids': '_timestamp, end_at, period, channel' }) get_data_group_patch.assert_called_once_with("visas") create_data_group_patch.assert_called_once_with({'name': 'visas'}) assert_that(list_module_types_patch.call_count, equal_to(1)) add_module_to_dashboard_patch.assert_called_once_with( 'visas', match_equality( has_entries({ 'type_id': 'uuid', 'data_group': 'visas', 'data_type': 'cost-per-transaction', 'title': 'Cost per transaction' })))
def test_add_task_empty_note_content(self): list = todo.list.List(self.mock_notestore_client, self.mock_authtoken) expected_content = '<?xml version="1.0" encoding="UTF-8"?>' expected_content += '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">' expected_content += '<en-note></en-note>' list._add_task(None, None) self.mock_notestore_client.createNote.\ assert_called_with(self.mock_authtoken, match_equality(has_property("content", expected_content)))
def test_create_data_group_and_set_if_nothing_exists( self, add_module_to_dashboard_patch, list_module_types_patch, create_data_group_patch, get_data_group_patch, create_data_set_patch, get_data_set_patch, get_dashboard_patch, client): get_dashboard_patch.return_value = { 'slug': 'visas'} get_data_set_patch.return_value = None create_data_set_patch.return_value = { 'name': 'apply_uk_visa_transactions_by_channel', 'data_type': 'transactions-by-channel', 'data_group': 'apply-uk-visa', 'bearer_token': 'abc123', 'upload_format': 'csv', 'auto_ids': '_timestamp, period, channel', 'max_age_expected': 1300000 } get_data_group_patch.return_value = None create_data_group_patch.return_value = { 'name': 'visas' } list_module_types_patch.return_value = [{'name': 'single_timeseries', 'id': 'uuid'}] add_module_to_dashboard_patch.return_value = {} client.post(self.upload_url, data=self.file_data) get_data_set_patch.assert_called_once_with( "visas", "cost-per-transaction") create_data_set_patch.assert_called_once_with({ 'upload_format': 'csv', 'bearer_token': 'abc123def', 'data_group': 'visas', 'data_type': 'cost-per-transaction', 'max_age_expected': 0, 'auto_ids': '_timestamp, end_at, period, channel'}) get_data_group_patch.assert_called_once_with("visas") create_data_group_patch.assert_called_once_with({'name': 'visas'}) assert_that(list_module_types_patch.call_count, equal_to(1)) add_module_to_dashboard_patch.assert_called_once_with( 'visas', match_equality(has_entries({ 'type_id': 'uuid', 'data_group': 'visas', 'data_type': 'cost-per-transaction', 'title': 'Cost per transaction' })))
def test_saving_a_user_config(self): user = UserConfig("*****@*****.**", buckets=["bucket_one", "bucket_two"]) self.repository.save(user) self.mongo_collection.save.assert_called_with( match_equality(has_entries({ "_id": "*****@*****.**", "buckets": ["bucket_one", "bucket_two"] })) )
def testBackfillCommandAttempts(self, mock_add): attempt_0 = self._CreateAttempt( 'attempt-0', 'task-0', common.CommandState.RUNNING) self._CreateAttempt('attempt-1', 'task-1', common.CommandState.COMPLETED) attempt_2 = self._CreateAttempt( 'attempt-2', 'task-2', common.CommandState.RUNNING) response = self.testapp.post_json( '/_ah/api/CoordinatorApi.BackfillCommandAttempts', {}) self.assertEqual('200 OK', response.status) mock_add.assert_has_calls( [ mock.call( hamcrest.match_equality( hamcrest.has_property('key', attempt_0.key))), mock.call( hamcrest.match_equality( hamcrest.has_property('key', attempt_2.key))), ], any_order=True) self.assertEqual(2, mock_add.call_count)
def test_sat_and_pulse(self): pusher = mock.Mock() data = StringIO("18-Dec-17 22:01:33 99 66 49") reader = N560Reader(data, pusher) reader.run() pusher.push.assert_called_with( "data", match_equality( all_of(has_entry('time', matches_regexp(TIMESTAMP_REGEX)), has_entry('spO2', 99), has_entry('pulse', 66))))
def test_no_values(self): pusher = mock.Mock() data = StringIO( "29-Dec-17 07:19:29 --- --- --- SD AS") N560Reader(data, pusher).run() pusher.push.assert_called_with( "data", match_equality( all_of(has_entry('time', matches_regexp(TIMESTAMP_REGEX)), not_(has_key('spO2')), not_(has_key('pulse')))))
def testEnqueueCommandEvents_transactionError(self, mock_process): event = command_event_test_util.CreateTestCommandEventJson( "rid", "cid", "aid", "InvocationStarted") event2 = command_event_test_util.CreateTestCommandEventJson( "rid", "cid", "aid", "InvocationCompleted") # for the first time, the second event failed due to TransactionFailedError # the second event will be reschedule to the queue. mock_process.side_effect = [None, ndb.exceptions.ContextError(), None] command_event_handler.EnqueueCommandEvents([event, event2]) tasks = self.mock_task_scheduler.GetTasks() self.assertEqual(len(tasks), 2) self.testapp.post( command_event_handler.COMMAND_EVENT_HANDLER_PATH, tasks[0].payload) with self.assertRaises(webtest.app.AppError): self.testapp.post( command_event_handler.COMMAND_EVENT_HANDLER_PATH, tasks[1].payload) # Simulate a task retry. self.testapp.post( command_event_handler.COMMAND_EVENT_HANDLER_PATH, tasks[1].payload) mock_process.assert_has_calls([ mock.call( hamcrest.match_equality( hamcrest.all_of( hamcrest.has_property("task_id", event["task_id"]), hamcrest.has_property("type", event["type"])))), mock.call( hamcrest.match_equality( hamcrest.all_of( hamcrest.has_property("task_id", event2["task_id"]), hamcrest.has_property("type", event2["type"])))), # this is the retry. mock.call( hamcrest.match_equality( hamcrest.all_of( hamcrest.has_property("task_id", event2["task_id"]), hamcrest.has_property("type", event2["type"])))) ])
def test_saving_a_bucket(self): bucket = BucketConfig("bucket_name", data_group="data_group", data_type="type") self.bucket_repo.save(bucket) self.mongo_collection.save.assert_called_with(match_equality(has_entries({ "_id": "bucket_name", "name": "bucket_name", "data_group": "data_group", "data_type": "type", "raw_queries_allowed": False, "bearer_token": None, "upload_format": "csv", })))
def test_get_data_set_by_name(self, mock_request): mock_request.__name__ = 'request' api = AdminAPI('http://admin.api', None) api.get_data_set_by_name('foo_bar') mock_request.assert_called_with( 'GET', 'http://admin.api/data-sets/foo_bar', headers=match_equality(has_entries({ 'Accept': 'application/json', })), data=None, )
def test_get_module(self, mock_request): mock_request.__name__ = 'request' api = AdminAPI('http://admin.api', 'token') api.get_module('uuid') mock_request.assert_called_with( 'GET', 'http://admin.api/module/uuid', headers=match_equality(has_entries({ 'Accept': 'application/json', 'Authorization': 'Bearer token' })), data=None, )
def test_create_data_set(self, mock_request): mock_request.__name__ = 'request' data_set_config = {'flibble': 'wibble'} base_url = 'base.url.com' api = AdminAPI(base_url, 'token') api.create_data_set(data_set_config) mock_request.assert_called_with( 'POST', base_url + '/data-sets', headers=match_equality(has_entries({ 'Authorization': 'Bearer token', 'Content-Type': 'application/json', })), data=json.dumps(data_set_config))
def test_list_organisations_with_filter(self, mock_request): mock_request.__name__ = 'request' api = AdminAPI('http://admin.api', 'token') api.list_organisations({'type': ['department', 'agency']}) mock_request.assert_called_with( 'GET', 'http://admin.api/organisation/node?type=department&type=agency', headers=match_equality(has_entries({ 'Accept': 'application/json', 'Authorization': 'Bearer token' })), data=None, )
def test_list_organisations(self, mock_request): mock_request.__name__ = 'request' api = AdminAPI('http://admin.api', 'token') api.list_organisations() mock_request.assert_called_with( 'GET', 'http://admin.api/organisation/node', headers=match_equality(has_entries({ 'Accept': 'application/json', 'Authorization': 'Bearer token' })), data=None, )
def test_get_data_set_transforms(self, mock_request): mock_request.__name__ = 'request' api = AdminAPI('http://admin.api', None) api.get_data_set_transforms('foo_bar') mock_request.assert_called_with( method='GET', url='http://admin.api/data-sets/foo_bar/transform', headers=match_equality(has_entries({ 'Accept': 'application/json', })), data=None, params=None, )
def test_get_data_set(self, mock_request): mock_request.__name__ = 'request' api = AdminAPI('http://admin.api', 'token') api.get_data_set('group', 'type') mock_request.assert_called_with( 'GET', 'http://admin.api/data-sets?data-group=group&data-type=type', headers=match_equality(has_entries({ 'Accept': 'application/json', 'Authorization': 'Bearer token' })), data=None, )
def test_get_collector_type(self, mock_request): mock_request.__name__ = 'request' api = CollectorAPI('http://collector.api', 'token') api.get_collector_type('foo-type') mock_request.assert_called_with( 'GET', 'http://collector.api/collector-type/foo-type', headers=match_equality(has_entries({ 'Accept': 'application/json', 'Authorization': 'Bearer token' })), data=None, )
def test_delete_dashboard(self, mock_request): mock_request.__name__ = 'request' api = AdminAPI('http://admin.api', 'token') api.delete_dashboard('uuid') mock_request.assert_called_with( 'DELETE', 'http://admin.api/dashboard/uuid', headers=match_equality(has_entries({ 'Accept': 'application/json', 'Authorization': 'Bearer token' })), data=None, )
def test_request_has_sets_request_id(self, mock_request): mock_request.__name__ = 'request' client = BaseClient( 'http://admin.api', 'token', request_id_fn=lambda: 'foo') client._get('/foo') mock_request.assert_called_with( 'GET', 'http://admin.api/foo', headers=match_equality(has_entries({ 'Govuk-Request-Id': 'foo', })), data=None, )
def test_get_collector_type(self, mock_request): mock_request.__name__ = 'request' api = CollectorAPI('http://collector.api', 'token') api.get_collector_type('foo-type') mock_request.assert_called_with( method='GET', url='http://collector.api/collector-type/foo-type', headers=match_equality(has_entries({ 'Accept': 'application/json', 'Authorization': 'Bearer token' })), data=None, params=None, )
def test_get_data_set_dashboard(self, mock_request): mock_request.__name__ = 'request' api = AdminAPI('http://admin.api', None) api.get_data_set_dashboard('foo_bar') mock_request.assert_called_with( method='GET', url='http://admin.api/data-sets/foo_bar/dashboard', headers=match_equality( has_entries({ 'Accept': 'application/json', })), data=None, params=None, )
def test_get_dashboard_by_tx_id(self, mock_request): mock_request.__name__ = 'request' api = AdminAPI('http://admin.api', 'token') api.get_dashboard_by_tx_id('dft-some-service') mock_request.assert_called_with( 'GET', "http://admin.api/transactions-explorer-service/" "dft-some-service/dashboard", headers=match_equality(has_entries({ 'Accept': 'application/json', 'Authorization': 'Bearer token' })), data=None, )
def test_list_collectors(self, mock_request): mock_request.__name__ = 'request' api = CollectorAPI('http://collector.api', 'token') api.list_collectors() mock_request.assert_called_with( method='GET', url='http://collector.api/collector', headers=match_equality(has_entries({ 'Accept': 'application/json', 'Authorization': 'Bearer token' })), data=None, params=None, )
def test_get_all_dashboards(self, mock_request): mock_request.__name__ = 'request' api = AdminAPI('http://admin.api', 'token') api.get_dashboards() mock_request.assert_called_with( method='GET', url='http://admin.api/dashboard', headers=match_equality(has_entries({ 'Accept': 'application/json', 'Authorization': 'Bearer token' })), data=None, params=None, )
def test_get_module(self, mock_request): mock_request.__name__ = 'request' api = AdminAPI('http://admin.api', 'token') api.get_module('uuid') mock_request.assert_called_with( method='GET', url='http://admin.api/module/uuid', headers=match_equality( has_entries({ 'Accept': 'application/json', 'Authorization': 'Bearer token' })), data=None, params=None, )
def test_request_has_correct_headers(self, mock_request): mock_request.__name__ = 'request' client = BaseClient('http://admin.api', 'token') client._get('/foo') mock_request.assert_called_with( 'GET', 'http://admin.api/foo', headers=match_equality(has_entries({ 'Accept': 'application/json', 'Authorization': 'Bearer token', 'User-Agent': starts_with('Performance Platform Client'), })), data=None, )
def test_list_organisations_with_filter(self, mock_request): mock_request.__name__ = 'request' api = AdminAPI('http://admin.api', 'token') api.list_organisations({'type': ['department', 'agency']}) mock_request.assert_called_with( method='GET', url='http://admin.api/organisation/node', headers=match_equality( has_entries({ 'Accept': 'application/json', 'Authorization': 'Bearer token' })), data=None, params={'type': ['department', 'agency']}, )
def test_delete_dashboard(self, mock_request): mock_request.__name__ = 'request' api = AdminAPI('http://admin.api', 'token') api.delete_dashboard('uuid') mock_request.assert_called_with( method='DELETE', url='http://admin.api/dashboard/uuid', headers=match_equality( has_entries({ 'Accept': 'application/json', 'Authorization': 'Bearer token' })), data=None, params=None, )
def test_get_data_set_by_name(self, mock_request): mock_request.__name__ = 'request' data_set = DataSet.from_name('http://dropthebase.com', 'my-buff-data-set') data_set.get() mock_request.assert_called_with( method='GET', url='http://dropthebase.com/my-buff-data-set', headers=match_equality( has_entries({ 'Accept': 'application/json', })), data=None, params=None, )
def test_post_has_content_type_header(self, mock_request): mock_request.__name__ = 'request' client = BaseClient('http://admin.api', 'token') client._post('/foo', 'bar') mock_request.assert_called_with( 'POST', 'http://admin.api/foo', headers = match_equality(has_entries({ 'Accept': 'application/json', 'Authorization': 'Bearer token', 'User-Agent': starts_with('Performance Platform Client'), 'Content-Type': 'application/json', })), data='bar', )
def test_create_transform(self, mock_request): mock_request.__name__ = 'request' transform_config = { 'name': 'carers-allowance' } base_url = 'example.com' api = AdminAPI(base_url, 'token') api.create_transform(transform_config) mock_request.assert_called_with( 'POST', base_url + '/transform', headers=match_equality(has_entries({ 'Authorization': 'Bearer token', 'Content-Type': 'application/json', })), data=json.dumps(transform_config) )
def test_settings(self): pusher = mock.Mock() data = StringIO( "N-560 VERSION 1.61.00 CRC:XXXX SpO2 Limit: 94-100% PR Limit: 50-170BPM" ) reader = N560Reader(data, pusher) reader.run() pusher.push.assert_called_with( "settings", match_equality( all_of(has_entry('time', matches_regexp(TIMESTAMP_REGEX)), has_entry('spO2LowerLimit', 94), has_entry('spO2UpperLimit', 100), has_entry('pulseLowerLimit', 50), has_entry('pulseUpperLimit', 170))))
def test_get_dashboard_by_tx_id(self, mock_request): mock_request.__name__ = 'request' api = AdminAPI('http://admin.api', 'token') api.get_dashboard_by_tx_id('dft-some-service') mock_request.assert_called_with( method='GET', url="http://admin.api/transactions-explorer-service/" "dft-some-service/dashboard", headers=match_equality( has_entries({ 'Accept': 'application/json', 'Authorization': 'Bearer token' })), data=None, params=None, )
def test_get_data_set_by_name(self, mock_request): mock_request.__name__ = 'request' data_set = DataSet.from_name( 'http://dropthebase.com', 'my-buff-data-set' ) data_set.get() mock_request.assert_called_with( 'GET', 'http://dropthebase.com/my-buff-data-set', headers=match_equality(has_entries({ 'Accept': 'application/json', })), data=mock.ANY )
def test_create_data_group(self, mock_request): mock_request.__name__ = 'request' data_group_config = { 'name': 'carers-allowance' } base_url = 'get_data_group.com' api = AdminAPI(base_url, 'token') api.create_data_group(data_group_config) mock_request.assert_called_with( 'POST', base_url + '/data-groups', headers=match_equality(has_entries({ 'Authorization': 'Bearer token', 'Content-Type': 'application/json', })), data=json.dumps(data_group_config) )
def test_request_has_sets_request_id(self, mock_request): mock_request.__name__ = 'request' client = BaseClient('http://admin.api', 'token', request_id_fn=lambda: 'foo') client._get('/foo') mock_request.assert_called_with( method='GET', url='http://admin.api/foo', headers=match_equality(has_entries({ 'Govuk-Request-Id': 'foo', })), data=None, params=None, )
def test_create_transform(self, mock_request): mock_request.__name__ = 'request' transform_config = {'name': 'carers-allowance'} base_url = 'example.com' api = AdminAPI(base_url, 'token') api.create_transform(transform_config) mock_request.assert_called_with( method='POST', url=base_url + '/transform', headers=match_equality( has_entries({ 'Authorization': 'Bearer token', 'Content-Type': 'application/json', })), data=json.dumps(transform_config), params=None, )
def test_create_data_set(self, mock_request): mock_request.__name__ = 'request' data_set_config = {'flibble': 'wibble'} base_url = 'base.url.com' api = AdminAPI(base_url, 'token') api.create_data_set(data_set_config) mock_request.assert_called_with( method='POST', url=base_url + '/data-sets', headers=match_equality( has_entries({ 'Authorization': 'Bearer token', 'Content-Type': 'application/json', })), data=json.dumps(data_set_config), params=None, )
def test_doesnt_render_templates_with_modules_without_data_type( self, mock_get_dashboard, mock_render_template): mock_render_template.return_value = '' mock_get_dashboard.return_value = dashboard_data( {'modules': [ {'slug': 'slug'}, {'data_type': 'user-satisfaction-score'}]}) self.client.get('/dashboards/dashboard-uuid') mock_render_template.assert_called_once_with( match_equality('builder/dashboard-hub.html'), dashboard_title=match_equality(not_none()), uuid=match_equality(not_none()), form=match_equality(not_none()), modules=match_equality(['user-satisfaction-score']), preview_url=match_equality(not_none()), environment=match_equality(not_none()), user=match_equality(not_none()), )
def test_doesnt_render_templates_with_modules_without_data_type( self, mock_get_dashboard, mock_render_template): mock_render_template.return_value = '' mock_get_dashboard.return_value = dashboard_data({ 'modules': [{ 'slug': 'slug' }, { 'data_type': 'user-satisfaction-score' }] }) self.client.get('/dashboards/dashboard-uuid') mock_render_template.assert_called_once_with( match_equality('builder/dashboard-hub.html'), dashboard_title=match_equality(not_none()), uuid=match_equality(not_none()), form=match_equality(not_none()), modules=match_equality(['user-satisfaction-score']), preview_url=match_equality(not_none()), environment=match_equality(not_none()), user=match_equality(not_none()), )
def test_reauth( self, mock_request): response = Response() response.status_code = 204 response._content = None mock_request.return_value = response mock_request.__name__ = 'request' client = AdminAPI('http://meta.api.com', 'token') client.reauth('foo') mock_request.assert_called_with( 'POST', 'http://meta.api.com/auth/gds/api/users/foo/reauth', headers=match_equality(has_entries({ 'Authorization': 'Bearer token', })), data=None )
def test_get_data_set(self, mock_request): mock_request.__name__ = 'request' api = AdminAPI('http://admin.api', 'token') api.get_data_set('group', 'type') mock_request.assert_called_with( method='GET', url='http://admin.api/data-sets', headers=match_equality( has_entries({ 'Accept': 'application/json', 'Authorization': 'Bearer token' })), data=None, params={ 'data-group': 'group', 'data-type': 'type' }, )
def test_large_payloads_to_admin_app_are_not_compressed( self, mock_request): mock_request.__name__ = 'request' client = AdminAPI('', 'token') client._post('', 'x' * 3000) mock_request.assert_called_with( method=mock.ANY, url=mock.ANY, headers=match_equality( is_not(has_entries({'Content-Encoding': 'gzip'}))), data=mock.ANY, params=None, ) unzipped_bytes = mock_request.call_args[1]['data'] eq_(3000, len(unzipped_bytes))
def test_large_payloads_to_admin_app_are_not_compressed( self, mock_request): mock_request.__name__ = 'request' client = AdminAPI('', 'token') client._post('', 'x' * 3000) mock_request.assert_called_with( mock.ANY, mock.ANY, headers=match_equality(is_not(has_entries({ 'Content-Encoding': 'gzip' }))), data=mock.ANY ) unzipped_bytes = mock_request.call_args[1]['data'] eq_(3000, len(unzipped_bytes))
def test_reauth(self, mock_request): response = Response() response.status_code = 204 response._content = None mock_request.return_value = response mock_request.__name__ = 'request' client = AdminAPI('http://meta.api.com', 'token') client.reauth('foo') mock_request.assert_called_with( method='POST', url='http://meta.api.com/auth/gds/api/users/foo/reauth', headers=match_equality( has_entries({ 'Authorization': 'Bearer token', })), data=None, params=None, )
def test_post_to_data_set_by_group_and_type(self, mock_request): mock_request.__name__ = 'request' data_set = DataSet.from_group_and_type( # bit of a gotcha in the /data here 'http://dropthebase.com/data', 'famous-knights', 'dragons-killed', token='token' ) data_set.post({'key': datetime(2012, 12, 12)}) mock_request.assert_called_with( 'POST', 'http://dropthebase.com/data/famous-knights/dragons-killed', headers=match_equality(has_entries({ 'Authorization': 'Bearer token', 'Content-Type': 'application/json', })), data='{"key": "2012-12-12T00:00:00+00:00"}' )