def test_reuse_metrics(self): dataset = VisibleDatasetFactory() reuse = VisibleReuseFactory() issue = IssueFactory(subject=reuse) DiscussionFactory(subject=reuse) reuse.count_datasets() reuse.count_issues() reuse.count_discussions() assert reuse.get_metrics()['datasets'] == 1 assert reuse.get_metrics()['issues'] == 1 assert reuse.get_metrics()['discussions'] == 1 with assert_emit(Reuse.on_update): reuse.datasets.append(dataset) reuse.save() reuse.count_datasets() assert reuse.get_metrics()['datasets'] == 2 dataset.count_reuses() assert dataset.get_metrics()['reuses'] == 1 with assert_emit(Reuse.on_update): reuse.datasets.remove(dataset) reuse.save() dataset_tasks.update_datasets_reuses_metrics() dataset.reload() assert dataset.get_metrics()['reuses'] == 0
def test_issues_csv_content_filled(self): organization = OrganizationFactory() dataset = DatasetFactory(organization=organization) user = UserFactory(first_name='John', last_name='Snow') issue = IssueFactory(subject=dataset, user=user) response = self.get( url_for('organizations.issues_csv', org=organization)) self.assert200(response) headers, data = response.data.decode('utf-8').strip().split('\r\n') expected = '"{issue.id}";"{issue.user}"'.format(issue=issue) assert_starts_with(data, expected)
def test_reuse_metrics(self): reuse = VisibleReuseFactory() issue = IssueFactory(subject=reuse) DiscussionFactory(subject=reuse) reuse.count_datasets() reuse.count_issues() reuse.count_discussions() assert reuse.get_metrics()['datasets'] == 1 assert reuse.get_metrics()['issues'] == 1 assert reuse.get_metrics()['discussions'] == 1
def test_delete(self): '''It should delete the connected user''' user = self.login() self.assertIsNone(user.deleted) other_user = UserFactory() members = [Member(user=user), Member(user=other_user)] organization = OrganizationFactory(members=members) disc_msg_content = faker.sentence() disc_msg = DiscMsg(content=disc_msg_content, posted_by=user) other_disc_msg_content = faker.sentence() other_disc_msg = DiscMsg(content=other_disc_msg_content, posted_by=other_user) discussion = DiscussionFactory(user=user, discussion=[disc_msg, other_disc_msg]) issue_msg_content = faker.sentence() issue_msg = IssueMsg(content=issue_msg_content, posted_by=user) other_issue_msg_content = faker.sentence() other_issue_msg = IssueMsg(content=other_issue_msg_content, posted_by=other_user) issue = IssueFactory(user=user, discussion=[issue_msg, other_issue_msg]) dataset = DatasetFactory(owner=user) reuse = ReuseFactory(owner=user) resource = CommunityResourceFactory(owner=user) activity = UserCreatedDataset.objects().create(actor=user, related_to=dataset) following = Follow.objects().create(follower=user, following=other_user) followed = Follow.objects().create(follower=other_user, following=user) with capture_mails() as mails: response = self.delete(url_for('api.me')) self.assertEqual(len(mails), 1) self.assertEqual(mails[0].send_to, set([user.email])) self.assertEqual(mails[0].subject, _('Account deletion')) self.assert204(response) user.reload() organization.reload() discussion.reload() issue.reload() dataset.reload() reuse.reload() resource.reload() activity.reload() # The following are deleted with self.assertRaises(Follow.DoesNotExist): following.reload() # The followers are deleted with self.assertRaises(Follow.DoesNotExist): followed.reload() # The personal data of the user are anonymized self.assertEqual(user.email, '{}@deleted'.format(user.id)) self.assertEqual(user.password, None) self.assertEqual(user.active, False) self.assertEqual(user.first_name, 'DELETED') self.assertEqual(user.last_name, 'DELETED') self.assertFalse(bool(user.avatar)) self.assertEqual(user.avatar_url, None) self.assertEqual(user.website, None) self.assertEqual(user.about, None) # The user is marked as deleted self.assertIsNotNone(user.deleted) # The user is removed from his organizations self.assertEqual(len(organization.members), 1) self.assertEqual(organization.members[0].user.id, other_user.id) # The discussions are kept but the messages are anonymized self.assertEqual(len(discussion.discussion), 2) self.assertEqual(discussion.discussion[0].content, 'DELETED') self.assertEqual(discussion.discussion[1].content, other_disc_msg_content) # The issues are kept and the messages are not anonymized self.assertEqual(len(issue.discussion), 2) self.assertEqual(issue.discussion[0].content, issue_msg_content) self.assertEqual(issue.discussion[1].content, other_issue_msg_content) # The datasets are unchanged self.assertEqual(dataset.owner, user) # The reuses are unchanged self.assertEqual(reuse.owner, user) # The community resources are unchanged self.assertEqual(resource.owner, user) # The activities are unchanged self.assertEqual(activity.actor, user)