def test_user_is_owner_of_data_set(self): user = UserFactory() collector = CollectorFactory() collector.data_source.owners.add(user) assert_that(collector.validate(user=user), contains_string('owner')) assert_that(collector.validate(user=user), contains_string('data set')) collector.data_set.owners.add(user) assert_that(collector.validate(user=user), none())
def test_run_collectors_by_type(self, mock_group): collector_type = CollectorType.objects.get(slug='ga') CollectorFactory(type=collector_type) CollectorFactory(type=collector_type) another_collector_type = CollectorType.objects.get(slug='gcloud') CollectorFactory(type=another_collector_type) run_collectors_by_type(collector_type.slug, another_collector_type.slug) assert_that(mock_group.call_count, equal_to(2))
def test_user_is_owner_of_data_set(self): user = UserFactory() collector = CollectorFactory() collector.data_source.owners.add(user) assert_that(collector.validate(user=user), contains_string('owner')) assert_that(collector.validate(user=user), contains_string('data set')) collector.data_set.owners.add(user) assert_that(collector.validate(user=user), none())
def test_requires_common_provider(self): provider_1 = ProviderFactory() provider_2 = ProviderFactory() collector_type = CollectorTypeFactory(provider=provider_1) data_source = DataSourceFactory(provider=provider_2) collector = CollectorFactory( type=collector_type, data_source=data_source) assert_that(collector.validate(), contains_string('provider')) data_source.provider = provider_1 assert_that(collector.validate(), none())
def test_requires_common_provider(self): provider_1 = ProviderFactory() provider_2 = ProviderFactory() collector_type = CollectorTypeFactory(provider=provider_1) data_source = DataSourceFactory(provider=provider_2) collector = CollectorFactory(type=collector_type, data_source=data_source) assert_that(collector.validate(), contains_string('provider')) data_source.provider = provider_1 assert_that(collector.validate(), none())
def test_get(self): collector = CollectorFactory() response = self.client.get( '/collector/{}'.format(collector.slug), 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['id'], equal_to(str(collector.id))) assert_that(resp_json['slug'], equal_to(collector.slug)) assert_that(resp_json['name'], equal_to(collector.name)) assert_that(resp_json['type']['name'], equal_to(collector.type.name)) assert_that(resp_json['type']['slug'], equal_to(collector.type.slug)) assert_that(resp_json['entry_point'], equal_to(collector.type.entry_point)) assert_that(resp_json['data_source']['name'], equal_to(collector.data_source.name)) assert_that(resp_json['data_set']['data_type'], equal_to(collector.data_set.data_type.name)) assert_that(resp_json['data_set']['data_group'], equal_to(collector.data_set.data_group.name)) assert_that(resp_json['data_set'], has_key('bearer_token')) assert_that(resp_json['query'], equal_to(collector.query)) assert_that(resp_json['options'], equal_to(collector.options)) assert_that(resp_json['provider']['slug'], equal_to(collector.type.provider.slug)) assert_that(resp_json['provider']['name'], equal_to(collector.type.provider.name)) assert_that(resp_json, not (has_key('owners')))
def test_put_404_when_user_not_in_data_source_ownership_array(self): collector = CollectorFactory() collector_user, _ = User.objects.get_or_create( email='*****@*****.**') collector.owners.add(collector_user) data_source = DataSourceFactory() data_source_user, _ = User.objects.get_or_create( email='*****@*****.**') data_source.owners.add(data_source_user) collector_update = { "slug": collector.slug, "type_id": collector.type.id, "data_source_id": data_source.id, "data_set": { "data_type": collector.data_set.data_type.name, "data_group": collector.data_set.data_group.name } } response = self.client.put('/collector/{}'.format(collector.slug), data=to_json(collector_update), HTTP_AUTHORIZATION='Bearer correct-token', content_type='application/json') assert_that(response.status_code, equal_to(404))
def test_can_list_collectors_with_collector_permission(self): collector = CollectorFactory() response = self.client.get('/collector', HTTP_AUTHORIZATION='Bearer correct-token') assert_that(response.status_code, equal_to(200))
def test_put(self): collector = CollectorFactory() data_source = DataSourceFactory(provider=collector.type.provider) collector_update = { "slug": collector.slug, "type_id": collector.type.id, "data_source_id": data_source.id, "data_set": { "data_type": collector.data_set.data_type.name, "data_group": collector.data_set.data_group.name } } response = self.client.put( '/collector/{}'.format(collector.slug), data=to_json(collector_update), 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['data_source']['name'], equal_to(data_source.name))
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_each_choices_tuple_contains_three_values(self): collector1 = CollectorFactory() queryset = DataSource.objects.all() choice_instance = admin.CollectorModelChoiceField(queryset) choices = choice_instance._get_choices() iteration = list(choices.__iter__()) assert_that(iteration[0], not_none) assert_that(iteration[1], not_none)
def test_run_collector(self, mock_ga_collector): # Collector Types are created through a migration and should exist in # the test database. collector_type = CollectorType.objects.get(slug="ga") collector = CollectorFactory(type=collector_type) run_collector(collector.slug, "2015-08-01", "2015-08-08") assert_that(mock_ga_collector.called, equal_to(True))
def test_400_if_only_end_date(self, run_collector_mock): collector = CollectorFactory() end_date = datetime(2015, 8, 9).strftime('%Y-%m-%d') response = self.client.post('/collector-run/{}?end-at={}'.format( collector.slug, end_date), HTTP_AUTHORIZATION='Bearer correct-token', content_type='application/json') assert_that(response.status_code, equal_to(400))
def test_returns_404_if_user_not_owner(self, run_collector_mock): collector = CollectorFactory() user, _ = User.objects.get_or_create( email='*****@*****.**') collector.owners.add(user) response = self.client.post('/collector-run/{}'.format(collector.slug), HTTP_AUTHORIZATION='Bearer correct-token', content_type='application/json') assert_that(response.status_code, equal_to(404))
def test_run_collector(self, run_collector_mock): collector = CollectorFactory() response = self.client.post('/collector-run/{}'.format(collector.slug), HTTP_AUTHORIZATION='Bearer correct-token', content_type='application/json') assert_that(response.status_code, equal_to(200)) run_collector_mock.delay.assert_called_with(collector.slug, start_at=None, end_at=None, dry_run=False)
def test_get_404_when_user_not_in_ownership_array(self): collector = CollectorFactory() user, _ = User.objects.get_or_create( email='*****@*****.**') collector.owners.add(user) resp = self.client.get('/collector/{}'.format(collector.slug), HTTP_AUTHORIZATION='Bearer correct-token') assert_that(resp.status_code, equal_to(404))
def test_query_is_validated_against_collector_type(self): collector_type = CollectorTypeFactory( query_schema={ "$schema": "http://json-schema.org/schema#", "type": "object", "properties": { "filter": {"type": "string"}, }, "required": ["filter"], "additionalProperties": False, }) collector = CollectorFactory(type=collector_type, query={"field": "somefield"}) assert_that(collector.validate(), contains_string("query")) collector.query = {"filter": "somefilter"} assert_that(collector.validate(), none())
def test_options_are_validated_against_collector_type(self): collector_type = CollectorTypeFactory( options_schema={ "$schema": "http://json-schema.org/schema#", "type": "object", "properties": { "extras": {"type": "string"}, }, "required": ["extras"], "additionalProperties": False, }) collector = CollectorFactory(type=collector_type, options={"field": "somefield"}) assert_that(collector.validate(), contains_string("options")) collector.options = {"extras": "an extra"} assert_that(collector.validate(), none())
def test_create_produces_a_name(self): data_type = DataTypeFactory(name="a_type") data_group = DataGroupFactory(name="a_group") data_set = DataSetFactory(data_type=data_type, data_group=data_group) collector_type = CollectorTypeFactory(name="a_collector_type") collector = CollectorFactory(type=collector_type, data_set=data_set) assert_that(collector.name, contains_string("a_type")) assert_that(collector.name, contains_string("a_group")) assert_that(collector.name, contains_string("a_collector_type"))
def test_options_are_validated_against_collector_type(self): collector_type = CollectorTypeFactory( options_schema={ "$schema": "http://json-schema.org/schema#", "type": "object", "properties": { "extras": { "type": "string" }, }, "required": ["extras"], "additionalProperties": False, }) collector = CollectorFactory(type=collector_type, options={"field": "somefield"}) assert_that(collector.validate(), contains_string("options")) collector.options = {"extras": "an extra"} assert_that(collector.validate(), none())
def test_query_is_validated_against_collector_type(self): collector_type = CollectorTypeFactory( query_schema={ "$schema": "http://json-schema.org/schema#", "type": "object", "properties": { "filter": { "type": "string" }, }, "required": ["filter"], "additionalProperties": False, }) collector = CollectorFactory(type=collector_type, query={"field": "somefield"}) assert_that(collector.validate(), contains_string("query")) collector.query = {"filter": "somefilter"} assert_that(collector.validate(), none())
def test_dry_run_with_start_and_end_dates(self, run_collector_mock): collector = CollectorFactory() start_date = datetime(2015, 8, 1).strftime('%Y-%m-%d') end_date = datetime(2015, 8, 9).strftime('%Y-%m-%d') response = self.client.post( '/collector-run/{}?start-at={}&end-at={}&dry-run=True'.format( collector.slug, start_date, end_date), HTTP_AUTHORIZATION='Bearer correct-token', content_type='application/json') assert_that(response.status_code, equal_to(200)) run_collector_mock.delay.assert_called_with(collector.slug, start_at="2015-08-01", end_at="2015-08-09", dry_run=True)
def test_get_404_when_user_not_in_data_set_ownership_array(self): data_set = DataSetFactory() data_set_user, _ = User.objects.get_or_create( email='*****@*****.**') data_set.owners.add(data_set_user) collector = CollectorFactory(data_set=data_set) collector_user, _ = User.objects.get_or_create( email='*****@*****.**') collector.owners.add(collector_user) resp = self.client.get('/collector/{}'.format(collector.slug), HTTP_AUTHORIZATION='Bearer correct-token') assert_that(resp.status_code, equal_to(404))
def test_run_collector_with_oauth2_credentials( self, mock_credential_storage_class, mock_ga_collector): def credentials(): return json.dumps({ "CLIENT_SECRETS": { "installed": { "auth_uri": "accounts.foo.com/o/oauth2/auth", "client_secret": "a client secret", "token_uri": "accounts.foo.com/o/oauth2/token", "client_email": "", "redirect_uris": ["urn:ietf:wg:oauth:2.0:oob", "oob"], "client_x509_cert_url": "", "client_id": "a client id", "auth_provider_x509_cert_url": "foo.com/oauth2/certs" } }, "OAUTH2_CREDENTIALS": { "_module": "oauth2client.client", "_class": "OAuth2Credentials", "access_token": "an access token", "token_uri": "accounts.foo.com/o/oauth2/token", "invalid": False, "client_id": "a client id", "id_token": None, "client_secret": "a client secret", "token_expiry": "2015-09-30T14:54:59Z", "refresh_token": "a refresh token", "user_agent": None } }) # Collector Types are created through a migration and should exist in # the test database. collector_type = CollectorType.objects.get(slug="ga") data_source = DataSourceFactory(credentials=credentials()) collector = CollectorFactory(type=collector_type, data_source=data_source) mock_storage_object = CredentialStorage(data_source) mock_credential_storage_class.return_value = mock_storage_object run_collector(collector.slug) expected_secret = json.loads(credentials()).get("CLIENT_SECRETS") expected_credentials = { "CLIENT_SECRETS": expected_secret, "OAUTH2_CREDENTIALS": mock_storage_object } mock_ga_collector.assert_called_with(expected_credentials, ANY, ANY, ANY, ANY, ANY)
def setUpClass(cls): cls.collector_type1 = CollectorTypeFactory(name="a_collector_type1") cls.collector1 = CollectorFactory(type=cls.collector_type1) cls.collector_type2 = CollectorTypeFactory(name="a_collector_type2") cls.collector2 = CollectorFactory(type=cls.collector_type2)
def test_requires_collectors_permission(self, run_collector_mock): collector = CollectorFactory() response = self.client.post('/collector-run/{}'.format(collector.slug), HTTP_AUTHORIZATION='Bearer correct-token', content_type='application/json') assert_that(response.status_code, equal_to(403))
def test_get_from_unauthorised_client_fails(self): collector = CollectorFactory() resp = self.client.get('/collector/{}'.format(collector.slug)) assert_that(resp.status_code, equal_to(403))