def test_base_rendering(self): response = internal(faker.word(), 32) assert200(response) assert response.mimetype == 'image/png' assert response.is_streamed etag, weak = response.get_etag() assert etag is not None
def test_catalog_rdf_json_ld(self, fmt, client): url = url_for('site.rdf_catalog_format', format=fmt) response = client.get(url) assert200(response) assert response.content_type == 'application/ld+json' context_url = url_for('site.jsonld_context', _external=True) assert response.json['@context'] == context_url
def test_post_api_list(self, api): '''It should fetch a post list from the API''' posts = PostFactory.create_batch(3) response = api.get(url_for('api.posts')) assert200(response) assert len(response.json['data']) == len(posts)
def test_swagger_resource_type(self, api): response = api.get(url_for('api.specs')) assert200(response) swagger = json.loads(response.data) expected = swagger['paths']['/datasets/{dataset}/resources/'] expected = expected['put']['responses']['200']['schema']['type'] assert expected == 'array'
def test_render_dataconnexions_6_with_data(self, client): # Use tags until we are sure all reuse are correctly labeled for tag, label, description in DATACONNEXIONS_6_CATEGORIES: badge = Badge(kind=DATACONNEXIONS_6_CANDIDATE) VisibleReuseFactory(tags=['dataconnexions-6', tag], badges=[badge]) response = client.get(url_for('gouvfr.dataconnexions6')) assert200(response)
def test_render_home_with_discourse(self, rmock, client): '''It should render the home page with the latest forum topic''' data = { 'categories': [{ 'id': 1, 'name': 'Category #1', }] } data_latest = { 'users': [], 'topic_list': { 'topics': [ { 'last_posted_at': '2017-01-01', 'id': 1, 'title': 'Title', 'fancy_title': 'Fancy Title', 'slug': 'title', 'category_id': 1, 'posts_count': 1, 'reply_count': 1, 'like_count': 1, 'views': 1, 'created_at': '2017-01-01', 'posters': [], } ], }, } rmock.get('%s/site.json' % DISCOURSE_URL, json=data) rmock.get('%s/l/latest.json' % DISCOURSE_URL, json=data_latest) response = client.get(url_for('site.home')) assert200(response) assert 'Title' in response.data.decode('utf8')
def test_authorization_multiple_grant_token(self, client, oauth): for i in range(3): client.login() response = client.post(url_for( 'oauth.authorize', response_type='code', client_id=oauth.client_id, ), { 'scope': 'default', 'accept': '', }) uri, params = response.location.split('?') code = parse_qs(params)['code'][0] client.logout() response = client.post(url_for('oauth.token'), { 'grant_type': 'authorization_code', 'code': code, }, headers=basic_header(oauth)) assert200(response) assert response.content_type == 'application/json' assert 'access_token' in response.json
def test_oembed_region_territory_api_get(self, api): '''It should fetch a region territory in the oembed format.''' paca, bdr, arles = create_geozones_fixtures() licence_ouverte = LicenseFactory(id='fr-lo', title='Licence Ouverte') LicenseFactory(id='notspecified', title='Not Specified') for territory_dataset_class in TERRITORY_DATASETS['region'].values(): organization = OrganizationFactory( id=territory_dataset_class.organization_id) territory = territory_dataset_class(paca) reference = 'territory-{id}'.format(id=territory.slug) response = api.get(url_for('api.oembeds', references=reference)) assert200(response) data = json.loads(response.data)[0] assert 'html' in data assert 'width' in data assert 'maxwidth' in data assert 'height' in data assert 'maxheight' in data assert data['type'] == 'rich' assert data['version'] == '1.0' html = data['html'] assert territory.title in html assert cgi.escape(territory.url) in html assert 'alt="{name}"'.format(name=organization.name) in html assert md(territory.description, source_tooltip=True) in html assert 'Download from local.test' in html assert 'Add to your own website' in html if territory_dataset_class not in ( TERRITORY_DATASETS['region']['zonages_reg'], ): assert 'License: {0}'.format(licence_ouverte.title) in html assert '© {0}'.format(licence_ouverte.id) in html assert ( '<a data-tooltip="Source" href="http://local.test/datasets' in html)
def test_no_auth(self, api): '''Should not return a content type if there is no content on delete''' response = api.get(url_for('api.fake')) assert200(response) assert response.content_type == 'application/json' assert response.json == {'success': True}
def test_authorization_grant_token_body_credentials(self, client, oauth): client.login() response = client.post(url_for( 'oauth.authorize', response_type='code', client_id=oauth.client_id, ), { 'scope': 'default', 'accept': '', }) uri, params = response.location.split('?') code = parse_qs(params)['code'][0] client.logout() response = client.post(url_for('oauth.token'), { 'grant_type': 'authorization_code', 'code': code, 'client_id': oauth.client_id, 'client_secret': oauth.secret, }) assert200(response) assert response.content_type == 'application/json' assert 'access_token' in response.json
def test_render_reuse_page(self, client): '''It should render the reuse page''' org = OrganizationFactory() dataset = DatasetFactory(organization=org) reuse = ReuseFactory(organization=org, datasets=[dataset]) response = client.get(url_for('reuses.show', reuse=reuse)) assert200(response)
def test_render_metrics(self, client): '''It should render the dashboard page''' for i in range(3): org = OrganizationFactory() DatasetFactory(organization=org) ReuseFactory(organization=org) response = client.get(url_for('site.dashboard')) assert200(response)
def test_render_dataset_page(self, client): '''It should render the dataset page''' org = OrganizationFactory() dataset = DatasetFactory(organization=org) ReuseFactory(organization=org, datasets=[dataset]) response = client.get(url_for('datasets.show', dataset=dataset)) assert200(response)
def test_expose_jsonld_context(self, client): url = url_for('site.jsonld_context') assert url == '/context.jsonld' response = client.get(url) assert200(response) assert response.content_type == 'application/ld+json' assert response.json == CONTEXT
def test_client_credentials_grant_token(self, client, oauth): response = client.post(url_for('oauth.token'), { 'grant_type': 'client_credentials', }, headers=basic_header(oauth)) assert200(response) assert response.content_type == 'application/json' assert 'access_token' in response.json
def test_oembed_with_port_in_https_url(self, api): '''It should works on HTTPS URLs with explicit port.''' dataset = DatasetFactory() url = dataset.external_url.replace('http://local.test/', 'https://local.test:443/') api_url = url_for('api.oembed', url=url) assert200(api.get(api_url, base_url='https://local.test:443/'))
def test_dataset_rdf_json_ld(self, client): dataset = DatasetFactory() for fmt in 'json', 'jsonld': url = url_for('datasets.rdf_format', dataset=dataset, format=fmt) response = client.get(url) assert200(response) assert response.content_type == 'application/ld+json' context_url = url_for('site.jsonld_context', _external=True) assert response.json['@context'] == context_url
def test_header_auth(self, api): '''Should handle header API Key authentication''' with api.user() as user: # API Key auth response = api.post(url_for('api.fake'), headers={'X-API-KEY': user.apikey}) assert200(response) assert response.content_type == 'application/json' assert response.json == {'success': True}
def test_session_auth(self, api): '''Should handle session authentication''' api.client.login() # Session auth response = api.post(url_for('api.fake')) assert200(response) assert response.content_type == 'application/json' assert response.json == {'success': True}
def test_render_organization_page(self, client): '''It should render the organization page''' org = OrganizationFactory() datasets = [DatasetFactory(organization=org) for _ in range(3)] for dataset in datasets: ReuseFactory(organization=org, datasets=[dataset]) response = client.get(url_for('organizations.show', org=org)) assert200(response)
def test_render_list(self, client, templates): '''It should render the post list page''' posts = PostFactory.create_batch(3) response = client.get(url_for('posts.list')) assert200(response) rendered_posts = templates.get_context_variable('posts') assert len(rendered_posts) == len(posts)
def test_no_validation_error(self, api): '''Should pass if no validation error''' response = api.put(url_for('api.fake'), { 'required': 'value', 'email': '*****@*****.**', 'choices': 'first', }) assert200(response) assert response.json == {'success': True}
def test_render_search(self, client, autoindex): '''It should render the search page''' with autoindex(): for i in range(3): org = OrganizationFactory() DatasetFactory(organization=org) ReuseFactory(organization=org) response = client.get(url_for('search.index')) assert200(response)
def test_renaming(self, client): tester = self.model.objects.create(slug='old') old_url = url_for('model_tester', model=tester) assert200(client.get(old_url)) tester.slug = 'new' tester.save() new_url = url_for('model_tester', model=tester) assert200(client.get(new_url)) assert404(client.get(old_url))
def test_post_api_update(self, api): '''It should update a post from the API''' post = PostFactory() data = post.to_dict() data['content'] = 'new content' api.login(AdminFactory()) response = api.put(url_for('api.post', post=post), data) assert200(response) assert Post.objects.count() == 1 assert Post.objects.first().content == 'new content'
def test_render_home(self, autoindex, client): '''It should render the home page''' with autoindex: for i in range(3): org = OrganizationFactory() DatasetFactory(organization=org) ReuseFactory(organization=org) response = client.get(url_for('site.home')) assert200(response)
def test_post_api_unpublish(self, api): '''It should update a post from the API''' post = PostFactory() api.login(AdminFactory()) response = api.delete(url_for('api.publish_post', post=post)) assert200(response) assert Post.objects.count() == 1 post.reload() assert post.published is None
def test_oembed_for_dataset_with_organization(self, api): '''It should fetch a dataset in the oembed format with org.''' organization = OrganizationFactory() dataset = DatasetFactory(organization=organization) url = url_for('api.oembed', url=dataset.external_url) response = api.get(url) assert200(response) card = theme.render('dataset/card.html', dataset=dataset) assert card in response.json['html']
def test_oembeds_dataset_api_get_with_organization(self, api): '''It should fetch a dataset in the oembed format with org.''' organization = OrganizationFactory() dataset = DatasetFactory(organization=organization) url = url_for('api.oembeds', references='dataset-{id}'.format(id=dataset.id)) response = api.get(url) assert200(response) data = response.json[0] assert organization.name in data['html'] assert organization.external_url in data['html']
def test_datasets_without_dynamic_region(self, autoindex, client): paca, bdr, arles = create_geozones_fixtures() with autoindex: organization = OrganizationFactory() for _ in range(3): VisibleDatasetFactory( organization=organization, spatial=SpatialCoverageFactory(zones=[paca.id])) response = client.get(url_for('api.zone_datasets', id=paca.id)) assert200(response) assert len(response.json) == 3
def test_match_normalized_utf8_slug(self, client): slug = 'slüg' Tester.objects.create(slug=slug) assert200(client.get('/model/sl%C3%BCg'))
def test_render_with_data(self, client): for i in range(3): badge = Badge(kind=OPENFIELD16) VisibleDatasetFactory(badges=[badge]) response = client.get(url_for('gouvfr.openfield16')) assert200(response)
def test_terms(self, client): response = client.get(url_for('site.terms')) assert200(response)
def test_render_dataconnexions_5_with_data(self, client): for tag, label, description in DATACONNEXIONS_5_CATEGORIES: badge = Badge(kind=DATACONNEXIONS_5_CANDIDATE) VisibleReuseFactory(tags=[tag], badges=[badge]) response = client.get(url_for('gouvfr.dataconnexions5')) assert200(response)
def test_render_home_no_data(self, client): '''It should render the home page without data''' response = client.get(url_for('site.home')) assert200(response)
def test_organization_api_get(self, api): '''It should fetch an organization from the API''' organization = OrganizationFactory() response = api.get(url_for('api.organization', org=organization)) assert200(response)
def test_resolve_zone_with_validity(self, client): zone = GeoZoneFactory() url = '/territory/{level}/{code}@{validity.start}/{slug}' response = client.get(url.format(**zone._data)) assert200(response)
def test_catalog_rdf_trig(self, client): url = url_for('site.rdf_catalog_format', format='trig') response = client.get(url) assert200(response) assert response.content_type == 'application/trig'
def test_organization_role_api_get(self, api): '''It should fetch an organization's roles list from the API''' response = api.get(url_for('api.org_roles')) assert200(response)
def test_catalog_rdf_rdfxml(self, fmt, client): url = url_for('site.rdf_catalog_format', format=fmt) response = client.get(url) assert200(response) assert response.content_type == 'application/rdf+xml'
def test_catalog_rdf_n3(self, client): url = url_for('site.rdf_catalog_format', format='n3') response = client.get(url) assert200(response) assert response.content_type == 'text/n3'
def test_list_sources(self, api): sources = HarvestSourceFactory.create_batch(3) response = api.get(url_for('api.harvest_sources')) assert200(response) assert len(response.json['data']) == len(sources)
def test_resolve_default_prefix_zone_without_validity(self, client): zone = GeoZoneFactory(level='fr:level', validity=None) url = '/territory/level/{code}@latest/{slug}' response = client.get(url.format(**zone._data)) assert200(response)
def test_render_c3_without_data(self, client): response = client.get(url_for('gouvfr.climate_change_challenge')) assert200(response)
def test_resolve_uuid(self, client): uuid = uuid4() url = '/uuid/{0}'.format(str(uuid)) assert200(client.get(url))
def test_dataset_rdf_formats(self, client, fmt, mime): dataset = DatasetFactory() url = url_for('datasets.rdf_format', dataset=dataset, format=fmt) response = client.get(url) assert200(response) assert response.content_type == mime
def test_render_dataconnexions_6_without_data(self, client): response = client.get(url_for('gouvfr.dataconnexions6')) assert200(response)
def test_render_metrics_no_data(self, client): '''It should render the dashboard page without data''' response = client.get(url_for('site.dashboard')) assert200(response)
def test_licences(self, client): response = client.get(url_for('gouvfr.licences')) assert200(response)
def test_render_search_no_data(self, autoindex, client): '''It should render the search page without data''' response = client.get(url_for('search.index')) assert200(response)
def test_credits(self, client): response = client.get(url_for('gouvfr.credits')) assert200(response)
def test_render_with_data(self, client): for i in range(3): badge = Badge(kind=SPD) VisibleDatasetFactory(badges=[badge]) response = client.get(url_for('gouvfr.spd')) assert200(response)
def test_redevances(self, client): response = client.get(url_for('gouvfr.redevances')) assert200(response)
def test_resolve_model_from_slug(self, client): self.model.objects.create(slug='slug') assert200(client.get('/model/slug'))
def test_render_home_if_discourse_error(self, rmock, client): '''It should render the home page when forum is not available''' url = '%s/site.json' % DISCOURSE_URL rmock.get(url, exc=requests.ConnectionError('Error')) response = client.get(url_for('site.home')) assert200(response)
def test_get_source(self, api): source = HarvestSourceFactory() url = url_for('api.harvest_source', ident=str(source.id)) response = api.get(url) assert200(response)
def test_post_api_get(self, api): '''It should fetch a post from the API''' post = PostFactory() response = api.get(url_for('api.post', post=post)) assert200(response)
def test_resolve_model_from_id(self, client): tester = self.model.objects.create(slug='slug') url = '/model/{0}'.format(str(tester.id)) assert200(client.get(url))
def test_source_from_config(self, api): api.login() data = {'name': faker.word(), 'url': faker.url(), 'backend': 'factory'} response = api.post(url_for('api.preview_harvest_source_config'), data) assert200(response)
def test_render_without_data(self, client): response = client.get(url_for('gouvfr.spd')) assert200(response)