def test_upload_evl_service_failures(self, mock_client_class): self._sign_in("*****@*****.**") mock_post = get_mock_post(mock_client_class) fixture_path = os.path.join('features', 'fixtures', 'contrib', 'EVL Services Volumetrics Sample.xls') response = self.client.post( 'evl_services_failures/upload', data={ 'file': open(fixture_path) } ) mock_client_class.from_group_and_type.assert_called_once_with( 'http://localhost:3039/data', 'group', 'type', token='some_token' ) stored_filtered_data = [ {"_timestamp": "2013-08-01T00:00:00+00:00", "_id": "2013-08-01.tax-disc.0", "type": "tax-disc", "reason": 0, "count": 1, "description": "Abandoned"}, {"_timestamp": "2013-08-01T00:00:00+00:00", "_id": "2013-08-01.tax-disc.66", "type": "tax-disc", "reason": 66, "count": 67, "description": "LPB Response Code was PSP Session Timeout"}, {"_timestamp": "2013-08-01T00:00:00+00:00", "_id": "2013-08-01.sorn.5", "type": "sorn", "reason": 5, "count": 8, "description": "User Cancelled Transaction"}, ] pos_args = mock_post.call_args[0] post_arg = pos_args[0] assert_that(post_arg, has_items(*stored_filtered_data)) assert_that(len(post_arg), equal_to(136)) assert_that(response, has_status(200))
def test_upload_evl_service_volumetrics(self, mock_client_class): self._sign_in("*****@*****.**") mock_post = get_mock_post(mock_client_class) fixture_path = os.path.join('features', 'fixtures', 'contrib', 'EVL Services Volumetrics Sample.xls') response = self.client.post( 'evl_services_volumetrics/upload', data={ 'file': open(fixture_path) } ) mock_client_class.from_group_and_type.assert_called_once_with( 'http://localhost:3039/data', 'group', 'type', token='some_token' ) stored_filtered_data = { "_timestamp": "2013-08-01T00:00:00+00:00", "_id": "2013-08-01", "timeSpan": "day", "successful_tax_disc": 100.0, "successful_sorn": 200.0 } pos_args = mock_post.call_args[0] post_arg = pos_args[0] assert_that(post_arg[0], has_entries(stored_filtered_data)) assert_that(len(post_arg), equal_to(1)) assert_that(response, has_status(200))
def test_upload_evl_customer_satisfaction(self, mock_client_class): self._sign_in("*****@*****.**") mock_post = get_mock_post(mock_client_class) fixture_path = os.path.join('features', 'fixtures', 'contrib', 'EVL Customer Satisfaction.xlsx') response = self.client.post( 'evl_customer_satisfaction/upload', data={ 'file': open(fixture_path) } ) mock_client_class.from_group_and_type.assert_called_once_with( 'http://localhost:3039/data', 'group', 'type', token='some_token' ) stored_filtered_data = [ {"_timestamp": "2013-08-01T00:00:00+00:00", "_id": "2013-08-01", "satisfaction_tax_disc": 1.2487024060928635, "satisfaction_sorn": 1.4370298628996634}, {"_timestamp": "2007-07-01T00:00:00+00:00", "_id": "2007-07-01", "satisfaction_tax_disc": 1.1662755514934828, "satisfaction_sorn": 1.3581011781786714}, ] pos_args = mock_post.call_args[0] post_arg = pos_args[0] assert_that(post_arg, has_items(*stored_filtered_data)) assert_that(len(post_arg), equal_to(113)) assert_that(response, has_status(200))
def test_upload_evl_channel_volumetrics(self, mock_client_class): self._sign_in("*****@*****.**") mock_post = get_mock_post(mock_client_class) fixture_path = os.path.join('features', 'fixtures', 'contrib', 'EVL Channel Volumetrics Sample.xls') response = self.client.post( 'evl_channel_volumetrics/upload', data={ 'file': open(fixture_path) } ) mock_client_class.from_group_and_type.assert_called_once_with( 'http://localhost:3039/data', 'group', 'type', token='some_token' ) stored_filtered_data = [ {"_timestamp": "2013-07-29T00:00:00+00:00", "_id": "2013-07-29", "successful_agent": 100.0, "successful_ivr": 101.0, "successful_web": 102.0, "total_agent": 200.0, "total_ivr": 201.0, "total_web": 202.0}, {"_timestamp": "2013-07-30T00:00:00+00:00", "_id": "2013-07-30", "successful_agent": 101.0, "successful_ivr": 102.0, "successful_web": 103.0, "total_agent": 201.0, "total_ivr": 202.0, "total_web": 203.0}, ] pos_args = mock_post.call_args[0] post_arg = pos_args[0] assert_that(post_arg, has_items(*stored_filtered_data)) assert_that(len(post_arg), equal_to(2)) assert_that(response, has_status(200))
def test_upload_evl_ceg_data(self, mock_client_class): self._sign_in("*****@*****.**") mock_post = get_mock_post(mock_client_class) fixture_path = os.path.join('features', 'fixtures', 'contrib', 'CEG Transaction Tracker.xlsx') response = self.client.post( 'evl_ceg_data/upload', data={ 'file': open(fixture_path) } ) mock_client_class.from_group_and_type.assert_called_once_with( 'http://localhost:3039/data', 'group', 'type', token='some_token' ) stored_filtered_data = { "calls_answered_by_advisor": 56383, "sorn_web": 108024, "_timestamp": '2007-07-01T00:00:00+00:00' } # This is definitely not incredibly obtuse # it is basically saying that one of the bits of data that we posted # has this entries in it which have something to do with the operation of a filter # this could not work if filters were not working... apparently. pos_args = mock_post.call_args[0] post_arg = pos_args[0] assert_that(post_arg[0], has_entries(stored_filtered_data)) assert_that(len(post_arg), equal_to(71)) assert_that(response, has_status(200))
def test_upload_evl_volumetrics(self, mock_client_class): self._sign_in("*****@*****.**") mock_post = get_mock_post(mock_client_class) fixture_path = os.path.join('features', 'fixtures', 'contrib', 'evl-volumetrics.xls') response = self.client.post( 'evl_volumetrics/upload', data={ 'file': open(fixture_path) } ) mock_client_class.from_group_and_type.assert_called_once_with( 'http://localhost:3039/data', 'group', 'type', token='some_token' ) stored_filtered_data = [ {"_timestamp": "2012-04-01T00:00:00+00:00", "transaction": "V-V890 SORN Declaration Refunds Input", "service": "sorn", "volume": 1025.0}, {"_timestamp": "2012-05-01T00:00:00+00:00", "transaction": "V-V890 SORN Declaration Vehicles Input", "service": "sorn", "volume": 0.0}, {"_timestamp": "2012-06-01T00:00:00+00:00", "transaction": "V-V890 SORN Declaration Vehicles Triage", "service": "sorn", "volume": None}, ] pos_args = mock_post.call_args[0] post_arg = pos_args[0] assert_that(post_arg, has_items(*[has_entries(i) for i in stored_filtered_data])) assert_that(len(post_arg), equal_to(336)) assert_that(response, has_status(200))
def test_authorize_returns_a_redirect_to_signon_service(self): oauth_service = Signonotron2(None, None, None, "") oauth_service.signon = Mock() oauth_service.signon.get_authorize_url.return_value = "" response = oauth_service.authorize() assert_that(response, has_status(302))
def test_exception_handling(self, db, statsd): db.alive.side_effect = ValueError("BOOM") response = self.app.get("/_status") assert_that(response, has_status(500)) assert_that(response, is_error_response()) statsd.incr.assert_called_with("write.error", bucket="/_status")
def test_exception_handling(self, storage, statsd): storage.alive.side_effect = ValueError("BOOM") response = self.app.get("/_status") assert_that(response, has_status(500)) assert_that(response, is_error_response()) statsd.incr.assert_called_with("write.error", data_set="/_status")
def test_accepts_content_type_for_csv(self): self._sign_in("*****@*****.**") response = self.client.post( 'test/upload', data={ 'file': (StringIO('foo, bar'), 'a_big_csv.csv') } ) assert_that(response, has_status(200))
def test_authorized_handler_redirects_you_to_index_page( self, oauth_service): self.stub_oauth(oauth_service, authorized=True) response = self.client.get('/authorized?code=any_code') path = response.headers['Location'].split('?')[0] assert_that(response, has_status(302)) assert_that(path, is_('http://localhost/'))
def test_user_is_redirected_to_not_authorized_page_for_bad_permissions( self, oauth_service): self.stub_oauth(oauth_service, user_name="test", user_email="*****@*****.**", authorized=False) with self.app.test_request_context('/authorized?code=12345'): response = self.app.dispatch_request() path = response.headers['Location'].split('?')[0] assert_that(session.get('user'), is_(None)) assert_that(path, is_('/not-authorized')) assert_that(response, has_status(302))
def test_signing_in_redirects_me_to_signon(self): response = self.client.get('/sign-in') params = url_decode(response.headers['Location'].split('?')[1]) assert_that(response, has_status(302)) assert_that(params, has_entry('response_type', 'code')) assert_that(params, has_entry('redirect_uri', 'http://backdrop-admin.dev.gov.uk/authorized')) assert_that(params, has_entry('client_id', app.app.config['OAUTH_CLIENT_ID']))
def test_rejects_content_type_for_exe(self, mock_from_group_and_type): self._sign_in("*****@*****.**") response = self.client.post( 'test/upload', data = { 'file': (StringIO('virus virus virus'), 'kittens.exe') } ) assert_that(response, has_status(400))
def test_upload_auto_generate_ids(self, mock_post): self._drop_collection("data_set_with_timestamp_auto_id") self._sign_in("*****@*****.**") fixture_path = os.path.join('features', 'fixtures', 'LPA_MI_EXAMPLE.xls') response = self.client.post( 'data_set_with_timestamp_auto_id/upload', data={ 'file': open(fixture_path) } ) assert_that(response, has_status(200)) assert_that(response, has_status(200)) pos_args = mock_post.call_args[0] post_arg = pos_args[0] assert_that(len(post_arg), is_(18))
def test_exception_handling(self, parse_and_store, statsd): parse_and_store.side_effect = RuntimeError("BOOM") response = self.app.post( "/foo", data="{}", content_type="application/json", headers=[("Authorization", "Bearer foo-bearer-token")] ) assert_that(response, has_status(500)) assert_that(response, is_error_response()) statsd.incr.assert_called_with("write.error", bucket="foo")
def test_rejects_content_type_for_exe(self): self._sign_in("*****@*****.**") response = self.client.post( 'test/upload', data = { 'file': (StringIO('virus virus virus'), 'kittens.exe') } ) assert_that(response, has_status(400))
def test_exception_handling(self, store, statsd): store.side_effect = RuntimeError("BOOM") response = self.app.post("/foo", data="{}", content_type='application/json', headers=[('Authorization', 'Bearer foo-bearer-token')]) assert_that(response, has_status(500)) assert_that(response, is_error_response()) statsd.incr.assert_called_with("write.error", data_set="foo")
def test_exception_handling(self, bucket_is_valid, statsd): bucket_is_valid.side_effect = ValueError("BOOM") response = self.app.post("/foo", data={'foo': 'bar'}, content_type='application/json', headers=[('Authorization', 'Bearer foo-bearer-token')]) assert_that(response, has_status(500)) assert_that(response, is_error_response()) statsd.incr.assert_called_with("write.error", bucket="foo")
def test_signing_in_redirects_me_to_signon(self): response = self.client.get('/sign-in') params = url_decode(response.headers['Location'].split('?')[1]) assert_that(response, has_status(302)) assert_that(params, has_entry('response_type', 'code')) assert_that( params, has_entry('redirect_uri', 'http://backdrop-admin.dev.gov.uk/authorized')) assert_that(params, has_entry('client_id', app.app.config['OAUTH_CLIENT_ID']))
def test_exception_handling(self, bucket_is_valid, statsd): bucket_is_valid.side_effect = ValueError("BOOM") response = self.app.post( "/foo", data={'foo': 'bar'}, content_type='application/json', headers=[('Authorization', 'Bearer foo-bearer-token')] ) assert_that(response, has_status(500)) assert_that(response, is_error_response()) statsd.incr.assert_called_with("write.error", bucket="foo")
def test_data_hits_the_database_when_uploading_csv(self): self._sign_in("*****@*****.**") self._drop_collection('test_upload_integration') response = self.client.post( 'test_upload_integration/upload', data = { 'file': (StringIO('_id,value\nhello,some_value'), 'data.csv') } ) assert_that(response, has_status(200)) db = MongoClient('localhost', 27017).backdrop_test record = list(db.test_upload_integration.find({'_id': 'hello'}))[0] assert_that(record, has_entry('_id', 'hello')) assert_that(record, has_entry('value', 'some_value'))
def test_data_hits_the_database_when_uploading_xlsx(self): self._drop_collection('integration_test_excel_bucket') self._sign_in("*****@*****.**") fixture_path = os.path.join('features', 'fixtures', 'data.xlsx') response = self.client.post( 'integration_test_excel_bucket/upload', data = { 'file': open(fixture_path) } ) assert_that(response, has_status(200)) db = MongoClient('localhost', 27017).backdrop_test record = list(db.integration_test_excel_bucket.find( {'name': 'Pawel'}))[0] assert_that(record, has_entry('age', 27)) assert_that(record, has_entry('nationality', 'Polish'))
def test_data_hits_the_backdrop_client_when_uploading_csv( self, mock_client_class): self._sign_in("*****@*****.**") self._drop_collection('test_upload_integration') mock_post = get_mock_post(mock_client_class) response = self.client.post( 'test_upload_integration/upload', data = { 'file': (StringIO('_id,value\nhello,some_value'), 'data.csv') } ) mock_client_class.from_group_and_type.assert_called_once_with( 'http://localhost:3039/data', 'test', 'upload_integration', token='some_nonsense' ) mock_post.assert_called_once_with([{u'_id': u'hello', u'value': u'some_value'}]) assert_that(response, has_status(200))
def test_upload_auto_generate_ids(self): self._drop_collection("bucket_with_timestamp_auto_id") self._sign_in("*****@*****.**") fixture_path = os.path.join('features', 'fixtures', 'LPA_MI_EXAMPLE.xls') response = self.client.post( 'bucket_with_timestamp_auto_id/upload', data={ 'file': open(fixture_path) } ) assert_that(response, has_status(200)) db = MongoClient('localhost', 27017).backdrop_test results = list(db.bucket_with_timestamp_auto_id.find()) assert_that(len(results), is_(18)) assert_that(results[0], has_entries({ "_id": "MjAxMy0wNy0wMVQwMDowMDowMCswMDowMC5wcm9wZXJ0eV9hbmRfZmluYW5jaWFsX2Zvcm1zX3Bvc3RlZA==", }))
def test_uploaded_csv_file_contains_utf8( self, mock_client_class): self._sign_in("*****@*****.**") mock_post = get_mock_post(mock_client_class) response = self.client.post( 'test_upload_integration/upload', data = { 'file': (StringIO(u'english,italian\ncity,città \ncoffee,caffè'.encode('utf-8')), 'data.csv') } ) mock_client_class.from_group_and_type.assert_called_once_with( 'http://localhost:3039/data', 'test', 'upload_integration', token='some_nonsense' ) mock_post.assert_called_once_with([ {u'english': u'city', u'italian': u'città '}, {u'english': u'coffee', u'italian': u'caffè'}]) assert_that(response, has_status(200))
def test_data_hits_the_backdrop_client_when_uploading_xlsx(self, mock_client_class): self._drop_collection('integration_test_excel_data_set') self._sign_in("*****@*****.**") mock_post = get_mock_post(mock_client_class) fixture_path = os.path.join('features', 'fixtures', 'data.xlsx') response = self.client.post( 'integration_test_excel_data_set/upload', data = { 'file': open(fixture_path) } ) mock_client_class.from_group_and_type.assert_called_once_with( 'http://localhost:3039/data', 'test', 'upload_integration', token='some_nonsense' ) mock_post.assert_called_once_with([{u'nationality': u'Polish', u'age': 27.0, u'name': u'Pawel'}, {u'nationality': u'Italian', u'age': 35.0, u'name': u'Max'}]) assert_that(response, has_status(200))
def test_upload_applies_filters(self): self._drop_collection("evl_ceg_data") self._sign_in("*****@*****.**") fixture_path = os.path.join('features', 'fixtures', 'contrib', 'CEG Transaction Tracker.xlsx') response = self.client.post( 'evl_ceg_data/upload', data={ 'file': open(fixture_path) } ) assert_that(response, has_status(200)) db = MongoClient('localhost', 27017).backdrop_test results = list(db.evl_ceg_data.find()) assert_that(len(results), is_(71)) assert_that(results[0], has_entries({ "calls_answered_by_advisor": 56383, "sorn_web": 108024, "_timestamp": datetime.datetime(2007, 7, 1, 0, 0), }))
def test_upload_page_redirects_non_authenticated_user_to_sign_in(self): self.given_user_is_not_signed_in() response = self.client.get('/test/upload') assert_that(response, has_status(302))
def test_returns_404_when_data_set_does_not_exist(self): response = self.app.get('/data/no-group/no-type') assert_that(response, has_status(404))
def test_returns_404_when_bucket_is_not_queryable(self): response = self.app.get('/data/some-group/some-type') assert_that(response, has_status(404))
def test_allow_origin_set_on_500(self, mock_query): mock_query.side_effect = StandardError('fail!') response = self.app.get('/foo') assert_that(response, has_status(500)) assert_that(response, has_header('Access-Control-Allow-Origin', '*'))
def test_allow_origin_set_on_404(self): response = self.app.get('/data_set') assert_that(response, has_status(404)) assert_that(response, has_header('Access-Control-Allow-Origin', '*'))
def test_error_cases_when_upload_validation_fails(self, mock_parser): self._sign_in("*****@*****.**") mock_parser.side_effect = ValidationError() response = self.do_simple_file_post() assert_that(response, has_status(500))
def test_upload_page_is_available_to_user_with_permission(self): self.given_user_is_signed_in_as(email="*****@*****.**") response = self.client.get('/test/upload') assert_that(response, has_status(200))
def test_upload_page_is_not_found_if_user_has_no_permissions(self): self.given_user_is_signed_in_as(email="*****@*****.**") response = self.client.get('/test/upload') assert_that(response, has_status(404))
def test_returning_a_400_when_auth_code_is_not_present(self): response = self.client.get('/authorized') assert_that(response, has_status(400))
def test_accepts_content_type_for_csv(self, mock_post): self._sign_in("*****@*****.**") response = self.do_simple_file_post() assert_that(response, has_status(200))
def test_returns_404_when_data_set_is_not_queryable(self): response = self.app.get('/data_set') assert_that(response, has_status(404))
def test_error_cases_when_post_to_backdrop_fails(self, mock_post): self._sign_in("*****@*****.**") mock_post.side_effect = RequestException() response = self.do_simple_file_post() assert_that(response, has_status(500))