def test_recipient_user_can_accept_transfer(self): owner = UserFactory() recipient = UserFactory() subject = VisibleDatasetFactory(owner=owner) transfer = TransferFactory(owner=owner, recipient=recipient, subject=subject) owner.reload() # Needs updated metrics assert owner.metrics['datasets'] == 1 recipient.reload() # Needs updated metrics assert recipient.metrics['datasets'] == 0 login_user(recipient) transfer = accept_transfer(transfer) assert transfer.status == 'accepted' subject.reload() assert subject.owner == recipient recipient.reload() assert recipient.metrics['datasets'] == 1 owner.reload() assert owner.metrics['datasets'] == 0
def test_org_admin_can_accept_transfer(self): owner = UserFactory() admin = UserFactory() org = OrganizationFactory(members=[Member(user=admin, role='admin')]) subject = VisibleDatasetFactory(owner=owner) transfer = TransferFactory(owner=owner, recipient=org, subject=subject) owner.reload() # Needs updated metrics assert owner.metrics['datasets'] == 1 org.reload() # Needs updated metrics assert org.metrics['datasets'] == 0 admin.reload() # Needs updated metrics assert admin.metrics['datasets'] == 0 login_user(admin) transfer = accept_transfer(transfer) assert transfer.status == 'accepted' subject.reload() assert subject.organization == org assert subject.owner is None org.reload() assert org.metrics['datasets'] == 1 admin.reload() assert admin.metrics['datasets'] == 0 owner.reload() assert owner.metrics['datasets'] == 0
def test_user_api_update_with_a_non_admin_connected_user(self): '''It should raise a 403''' user = UserFactory() self.login(user) data = user.to_dict() response = self.put(url_for('api.user', user=user), data) self.assert403(response)
def test_user_api_update_with_a_non_existing_role(self): '''It should raise a 400''' self.login(AdminFactory()) user = UserFactory() data = user.to_dict() data['roles'] = ['non_existing_role'] response = self.put(url_for('api.user', user=user), data) self.assert400(response)
def test_user_api_update_with_an_existing_role(self): '''It should update a user''' self.login(AdminFactory()) user = UserFactory() data = user.to_dict() data['roles'] = ['admin'] response = self.put(url_for('api.user', user=user), data) self.assert200(response) self.assertEqual(response.json['roles'], ['admin'])
def test_user_api_update(self): '''It should update a user''' self.login(AdminFactory()) user = UserFactory() data = user.to_dict() data['active'] = False response = self.put(url_for('api.user', user=user), data) self.assert200(response) self.assertFalse(response.json['active'])
def test_suggest_users_api_no_match(self): '''It should not provide user suggestion if no match''' with self.autoindex(): UserFactory.create_batch(3) response = self.get(url_for('api.suggest_users'), qs={'q': 'xxxxxx', 'size': '5'}) self.assert200(response) self.assertEqual(len(response.json), 0)
def test_delete_user(self): user = AdminFactory() self.login(user) other_user = UserFactory() response = self.delete(url_for('api.user', user=other_user)) self.assert204(response) other_user.reload() response = self.delete(url_for('api.user', user=other_user)) self.assert410(response) response = self.delete(url_for('api.user', user=user)) self.assert403(response)
def test_user_api_update_with_website(self): '''It should raise a 400''' self.login(AdminFactory()) user = UserFactory() data = user.to_dict() data['website'] = 'foo' response = self.put(url_for('api.user', user=user), data) self.assert400(response) data['website'] = faker.url() response = self.put(url_for('api.user', user=user), data) self.assert200(response)
def test_suggest_users_api_no_dedup(self): '''It should suggest users without deduplicating homonyms''' with self.autoindex(): UserFactory.create_batch(2, first_name='test', last_name='homonym') response = self.get(url_for('api.suggest_users'), qs={'q': 'homonym', 'size': '5'}) self.assert200(response) self.assertEqual(len(response.json), 2) for suggestion in response.json: self.assertEqual(suggestion['first_name'], 'test') self.assertEqual(suggestion['last_name'], 'homonym')
def test_update_user_on_authorize_callback(self): '''Should only update specific field on authorize callback''' user = UserFactory() old_slug = user.slug with self.mock_authorize(email=user.email, is_superuser=True) as (profile, client): response = self.get(url_for('youckan.authorized', code='code'), client=client) self.assertRedirects(response, url_for('site.home')) self.assertIn('youckan.token', session) self.assertTrue(current_user.is_authenticated) user.reload() self.assertEqual(user.slug, old_slug) self.assertEqual(user.first_name, profile['first_name']) self.assertEqual(user.last_name, profile['last_name']) self.assertEqual(user.email, profile['email']) self.assertEqual(user.avatar_url, profile['profile']['avatar']) self.assertEqual(len(User.objects), 1)
def test_minimal(self): user = UserFactory.build() # Does not have an URL u = user_to_rdf(user) g = u.graph self.assertIsInstance(u, RdfResource) self.assertEqual(len(list(g.subjects(RDF.type, FOAF.Person))), 1) self.assertEqual(u.value(RDF.type).identifier, FOAF.Person) self.assertIsInstance(u.identifier, BNode) self.assertEqual(u.value(FOAF.name), Literal(user.fullname)) self.assertEqual(u.value(RDFS.label), Literal(user.fullname))
def test_render_profile(self): '''It should render the user profile''' user = UserFactory(about='* Title 1\n* Title 2', website='http://www.datagouv.fr/user', avatar_url='http://www.datagouv.fr/avatar') response = self.get(url_for('users.show', user=user)) self.assert200(response) json_ld = self.get_json_ld(response) self.assertEquals(json_ld['@context'], 'http://schema.org') self.assertEquals(json_ld['@type'], 'Person') self.assertEquals(json_ld['name'], user.fullname) self.assertEquals(json_ld['description'], 'Title 1 Title 2') self.assertEquals(json_ld['url'], 'http://www.datagouv.fr/user') self.assertEquals(json_ld['image'], 'http://www.datagouv.fr/avatar')
def test_closed_discussion_mail(self): owner = UserFactory() poster = UserFactory() commenter = UserFactory() message = Message(content=faker.sentence(), posted_by=poster) second_message = Message(content=faker.sentence(), posted_by=commenter) closing_message = Message(content=faker.sentence(), posted_by=owner) discussion = Discussion.objects.create( subject=DatasetFactory(owner=owner), user=poster, title=faker.sentence(), discussion=[message, second_message, closing_message]) with self.capture_mails() as mails: notify_discussion_closed(discussion, message=closing_message) # Should have sent one mail to each participant # and no mail to the closer expected_recipients = (poster.email, commenter.email) self.assertEqual(len(mails), len(expected_recipients)) for mail in mails: self.assertIn(mail.recipients[0], expected_recipients) self.assertNotIn(owner.email, mail.recipients)
def test_pending_harvester_validations(self): source = HarvestSourceFactory() admin = AdminFactory() user = UserFactory() assert len(validate_harvester_notifications(user)) == 0 notifications = validate_harvester_notifications(admin) assert len(notifications) == 1 dt, details = notifications[0] assert_equal_dates(dt, source.created_at) assert details['id'] == source.id assert details['name'] == source.name
def test_new_discussion_comment_mail(self): owner = UserFactory() poster = UserFactory() commenter = UserFactory() message = Message(content=faker.sentence(), posted_by=poster) new_message = Message(content=faker.sentence(), posted_by=commenter) discussion = Discussion.objects.create( subject=DatasetFactory(owner=owner), user=poster, title=faker.sentence(), discussion=[message, new_message] ) with capture_mails() as mails: notify_new_discussion_comment(discussion.id, message=len(discussion.discussion) - 1) # Should have sent one mail to the owner and the other participants # and no mail to the commenter expected_recipients = (owner.email, poster.email) self.assertEqual(len(mails), len(expected_recipients)) for mail in mails: self.assertIn(mail.recipients[0], expected_recipients) self.assertNotIn(commenter.email, mail.recipients)
def test_add_resource_without_checksum(self): user = UserFactory() dataset = DatasetFactory(owner=user) resource = ResourceFactory(checksum=None) expected_signals = post_save, Dataset.after_save, Dataset.on_update with self.assert_emit(*expected_signals): dataset.add_resource(ResourceFactory(checksum=None)) self.assertEqual(len(dataset.resources), 1) with self.assert_emit(*expected_signals): dataset.add_resource(resource) self.assertEqual(len(dataset.resources), 2) self.assertEqual(dataset.resources[0].id, resource.id)
def test_add_resource_without_checksum(self): user = UserFactory() dataset = DatasetFactory(owner=user) resource = ResourceFactory(checksum=None) expected_signals = post_save, Dataset.after_save, Dataset.on_update with assert_emit(*expected_signals): dataset.add_resource(ResourceFactory(checksum=None)) assert len(dataset.resources) == 1 with assert_emit(*expected_signals): dataset.add_resource(resource) assert len(dataset.resources) == 2 assert dataset.resources[0].id == resource.id
def test_community_resource(self): user = UserFactory() dataset = DatasetFactory(owner=user) community_resource1 = CommunityResourceFactory() community_resource1.dataset = dataset community_resource1.save() assert len(dataset.community_resources) == 1 community_resource2 = CommunityResourceFactory() community_resource2.dataset = dataset community_resource2.save() assert len(dataset.community_resources) == 2 assert dataset.community_resources[1].id == community_resource1.id assert dataset.community_resources[0].id == community_resource2.id
def test_revoke_token(self, client, oauth, token_type): user = UserFactory() token = OAuth2Token.objects.create( client=oauth, user=user, access_token='access-token', refresh_token='refresh-token', ) response = client.post(url_for('oauth.revoke_token'), { 'token': getattr(token, token_type), }, headers=basic_header(oauth)) assert200(response) assert OAuth2Token.objects(pk=token.pk).first() is None
def test_only_admin_can_create_member(self, api): user = api.login() added_user = UserFactory() organization = OrganizationFactory(members=[ Member(user=user, role='editor'), ]) api_url = url_for('api.member', org=organization, user=added_user) response = api.post(api_url, {'role': 'editor'}) assert403(response) organization.reload() assert not organization.is_member(added_user)
def test_only_admin_can_refuse_membership(self, api): user = api.login() applicant = UserFactory() membership_request = MembershipRequest(user=applicant, comment='test') member = Member(user=user, role='editor') organization = OrganizationFactory(members=[member], requests=[membership_request]) data = {'comment': 'no'} api_url = url_for('api.refuse_membership', org=organization, id=membership_request.id) response = api.post(api_url, data) assert403(response)
def test_owner_changed_from_org_to_user_with_owned_postsave_signal(self): # Test with an additionnal post save signal that will retriger save signals user = UserFactory() org = OrganizationFactory() owned = OwnedPostSave.objects.create(organization=org) with post_save.connected_to(OwnedPostSave.post_save, sender=OwnedPostSave): owned.owner = user owned.save() assert owned.owner == user assert owned.organization is None
def test_delete_discussion_comment_permissions(self): dataset = Dataset.objects.create(title='Test dataset') user = UserFactory() message = Message(content='bla bla', posted_by=user) discussion = Discussion.objects.create( subject=dataset, user=user, title='test discussion', discussion=[message] ) self.login() response = self.delete(url_for('api.discussion_comment', id=discussion.id, cidx=0)) self.assert403(response)
def test_only_admin_can_delete_member(self, api): user = api.login() deleted_user = UserFactory() organization = OrganizationFactory(members=[ Member(user=user, role='editor'), Member(user=deleted_user, role='editor') ]) api_url = url_for('api.member', org=organization, user=deleted_user) response = api.delete(api_url) assert403(response) organization.reload() assert organization.is_member(deleted_user)
def test_delete_member(self): user = self.login() deleted_user = UserFactory() organization = OrganizationFactory(members=[ Member(user=user, role='admin'), Member(user=deleted_user, role='editor') ]) api_url = url_for('api.member', org=organization, user=deleted_user) response = self.delete(api_url) self.assert204(response) organization.reload() self.assertFalse(organization.is_member(deleted_user))
def test_add_badge_with_logged_user(self): '''It should track the user that add a badge''' user = UserFactory() fake = Fake.objects.create() login_user(user) result = fake.add_badge(TEST) self.assertEqual(len(fake.badges), 1) badge = fake.badges[0] self.assertEqual(result, badge) self.assertEqual(badge.kind, TEST) self.assertIsNotNone(badge.created) self.assertEqual(badge.created_by, user)
def test_issues_for_user(self): owner = UserFactory() dataset = DatasetFactory(owner=owner) reuse = ReuseFactory(owner=owner) open_issues = [] for i in range(3): user = UserFactory() message = Message(content=faker.sentence(), posted_by=user) open_issues.append( Issue.objects.create(subject=dataset, user=user, title=faker.sentence(), discussion=[message])) open_issues.append( Issue.objects.create(subject=reuse, user=user, title=faker.sentence(), discussion=[message])) # Creating a closed issue that shouldn't show up in response. user = UserFactory() message = Message(content=faker.sentence(), posted_by=user) Issue.objects.create(subject=dataset, user=user, title=faker.sentence(), discussion=[message], closed=datetime.now(), closed_by=user) issues = issues_for(owner) self.assertIsInstance(issues, db.BaseQuerySet) self.assertEqual(len(issues), len(open_issues)) for issue in issues: self.assertIn(issue, open_issues)
def test_notify_user_issues(self): owner = UserFactory() dataset = DatasetFactory(owner=owner) open_issues = {} for i in range(3): user = UserFactory() message = Message(content=faker.sentence(), posted_by=user) issue = Issue.objects.create( subject=dataset, user=user, title=faker.sentence(), discussion=[message] ) open_issues[issue.id] = issue # Creating a closed issue that shouldn't show up in response. user = UserFactory() message = Message(content=faker.sentence(), posted_by=user) Issue.objects.create( subject=dataset, user=user, title=faker.sentence(), discussion=[message], closed=datetime.now(), closed_by=user ) notifications = issues_notifications(owner) self.assertEqual(len(notifications), len(open_issues)) for dt, details in notifications: issue = open_issues[details['id']] self.assertEqual(details['title'], issue.title) self.assertEqual(details['subject']['id'], issue.subject.id) self.assertEqual(details['subject']['type'], 'dataset')
def test_registered_provider_provide_values(self): dt = datetime.now() def fake_provider(user): return [(dt, {'some': 'value'})] actions.register_provider('fake', fake_provider) user = UserFactory() notifs = actions.get_notifications(user) self.assertEqual(len(notifs), 1) self.assertEqual(notifs[0]['type'], 'fake') self.assertEqual(notifs[0]['details'], {'some': 'value'}) self.assertEqualDates(notifs[0]['created_on'], dt)
def test_suggest_users_api_by_id(self): '''It should suggest an user based on its ID''' with self.autoindex(): users = UserFactory.create_batch(4) first_user = users[0] response = self.get(url_for('api.suggest_users'), qs={'q': str(first_user.id), 'size': '5'}) self.assert200(response) # The batch factory generates ids that might be too close # which then are found with the fuzzy search. suggested_ids = [u['id'] for u in response.json] self.assertGreaterEqual(len(suggested_ids), 1) self.assertIn(str(first_user.id), suggested_ids)
def test_list_issues_closed_filter(self): dataset = Dataset.objects.create(title='Test dataset') open_issues = [] closed_issues = [] for i in range(2): user = UserFactory() message = Message(content=faker.sentence(), posted_by=user) issue = Issue.objects.create(subject=dataset, user=user, title='test issue {}'.format(i), discussion=[message]) open_issues.append(issue) for i in range(3): user = UserFactory() message = Message(content=faker.sentence(), posted_by=user) issue = Issue.objects.create(subject=dataset, user=user, title='test issue {}'.format(i), discussion=[message], closed=datetime.now(), closed_by=user) closed_issues.append(issue) response = self.get(url_for('api.issues', closed=True)) self.assert200(response) self.assertEqual(len(response.json['data']), len(closed_issues)) for issue in response.json['data']: self.assertIsNotNone(issue['closed']) response = self.get(url_for('api.issues', id=dataset.id, closed=False)) self.assert200(response) self.assertEqual(len(response.json['data']), len(open_issues)) for issue in response.json['data']: self.assertIsNone(issue['closed'])
def test_list_discussions_for(self): dataset = DatasetFactory() discussions = [] for i in range(3): user = UserFactory() message = Message(content=faker.sentence(), posted_by=user) discussion = Discussion.objects.create( subject=dataset, user=user, title='test discussion {}'.format(i), discussion=[message]) discussions.append(discussion) user = UserFactory() message = Message(content=faker.sentence(), posted_by=user) Discussion.objects.create(subject=DatasetFactory(), user=user, title='test discussion {}'.format(i), discussion=[message]) kwargs = {'for': str(dataset.id)} response = self.get(url_for('api.discussions', **kwargs)) self.assert200(response) self.assertEqual(len(response.json['data']), len(discussions))
def test_with_valid_user_self_json(self): Ownable, OwnableForm = self.factory() user = UserFactory() login_user(user) form = OwnableForm.from_json({'owner': str(user.id)}) self.assertEqual(form.owner.data, user) form.validate() self.assertEqual(form.errors, {}) ownable = Ownable() form.populate_obj(ownable) self.assertEqual(ownable.owner, user)
def test_revoke_token_with_bad_hint(self, client, oauth): user = UserFactory() token = OAuth2Token.objects.create( client=oauth, user=user, access_token='access-token', refresh_token='refresh-token', ) response = client.post(url_for('oauth.revoke_token'), { 'token': token.access_token, 'token_type_hint': 'refresh_token', }, headers=basic_header(oauth)) assert400(response) assert OAuth2Token.objects(pk=token.pk).first() == token
def test_auth_with_existing_remote_user(client, ldap): UserFactory(email=EMAIL) page_url = url_for('site.home') response = client.get(page_url, headers={'REMOTE_USER': UID}) assert200(response) assert User.objects.count() == 1 user = User.objects.first() assert user.email == EMAIL assert user.first_name == FIRST_NAME assert user.last_name == LAST_NAME assert user.active
def test_mark_as_deleted(self): user = UserFactory() other_user = UserFactory() org = OrganizationFactory(editors=[user]) discussion = DiscussionFactory( user=user, subject=org, discussion=[MessageDiscussionFactory(posted_by=user)]) user_follow_org = Follow.objects.create(follower=user, following=org) user_followed = Follow.objects.create(follower=other_user, following=user) user.mark_as_deleted() org.reload() assert len(org.members) == 0 discussion.reload() assert discussion.discussion[0].content == 'DELETED' assert Follow.objects(id=user_follow_org.id).first() is None assert Follow.objects(id=user_followed.id).first() is None assert user.slug == 'deleted'
def test_with_owner(self): user = UserFactory() dataset = DatasetFactory(owner=user) d = dataset_to_rdf(dataset) g = d.graph self.assertIsInstance(d, RdfResource) datasets = g.subjects(RDF.type, DCAT.Dataset) users = g.subjects(RDF.type, FOAF.Person) self.assertEqual(len(list(datasets)), 1) self.assertEqual(len(list(users)), 1) publisher = d.value(DCT.publisher) self.assertEqual(publisher.value(RDF.type).identifier, FOAF.Person)
def test_add_resource(self): user = UserFactory() dataset = DatasetFactory(owner=user) resource = ResourceFactory() expected_signals = (Dataset.on_resource_added, ) with assert_emit(*expected_signals): dataset.add_resource(ResourceFactory()) assert len(dataset.resources) == 1 with assert_emit(*expected_signals): dataset.add_resource(resource) assert len(dataset.resources) == 2 assert dataset.resources[0].id == resource.id assert dataset.resources[0].dataset == dataset
def test_empty_values_logged(self): Ownable, OwnableForm = self.factory() user = UserFactory() login_user(user) form = OwnableForm() self.assertEqual(form.owner.data, user) self.assertIsNone(form.organization.data) ownable = Ownable() form.populate_obj(ownable) self.assertEqual(ownable.owner, user) self.assertIsNone(ownable.organization)
def test_quality_all(self): user = UserFactory() visitor = UserFactory() dataset = DatasetFactory(owner=user, frequency='weekly', tags=['foo', 'bar'], description='a' * 42) dataset.add_resource(ResourceFactory(format='pdf')) DiscussionFactory( subject=dataset, user=visitor, discussion=[MessageDiscussionFactory(posted_by=visitor)]) self.assertEqual(dataset.quality['score'], 0) self.assertEqual( sorted(dataset.quality.keys()), [ 'description_length', 'discussions', 'frequency', 'has_only_closed_formats', 'has_resources', 'has_unavailable_resources', 'has_untreated_discussions', 'score', 'tags_count', 'update_in' ])
def generate_fixtures(datasets, reuses): '''Build sample fixture data (users, datasets and reuses).''' user = UserFactory() log.info('Generated user "{user.email}".'.format(user=user)) organization = OrganizationFactory(members=[Member(user=user)]) log.info('Generated organization "{org.name}".'.format(org=organization)) for _ in range(datasets): dataset = VisibleDatasetFactory(organization=organization) DiscussionFactory(subject=dataset, user=user) ReuseFactory.create_batch(reuses, datasets=[dataset], owner=user) msg = 'Generated {datasets} dataset(s) with {reuses} reuse(s) each.' log.info(msg.format(**locals()))