Exemplo n.º 1
0
    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
Exemplo n.º 2
0
 def test_community_resource_api_update_with_file(self):
     '''It should update a community resource file from the API'''
     dataset = VisibleDatasetFactory()
     user = self.login()
     community_resource = CommunityResourceFactory(dataset=dataset,
                                                   owner=user)
     response = self.post(url_for('api.upload_community_resource',
                                  community=community_resource),
                          {'file': (StringIO(b'aaa'), 'test.txt')},
                          json=False)
     self.assert200(response)
     data = json.loads(response.data)
     self.assertEqual(data['id'], str(community_resource.id))
     self.assertEqual(data['title'], 'test.txt')
     data['description'] = 'new description'
     response = self.put(
         url_for('api.community_resource', community=community_resource),
         data)
     self.assert200(response)
     self.assertEqual(CommunityResource.objects.count(), 1)
     self.assertEqual(CommunityResource.objects.first().description,
                      'new description')
     self.assertTrue(
         CommunityResource.objects.first().url.endswith('test.txt'))
Exemplo n.º 3
0
    def test_json_ld(self):
        '''It should render a json-ld markup into the dataset page'''
        resource = ResourceFactory(format='png',
                                   description='* Title 1\n* Title 2',
                                   metrics={'views': 10})
        license = LicenseFactory(url='http://www.datagouv.fr/licence')
        dataset = DatasetFactory(license=license,
                                 tags=['foo', 'bar'],
                                 resources=[resource],
                                 description='a&éèëù$£',
                                 owner=UserFactory(),
                                 extras={'foo': 'bar'})
        community_resource = CommunityResourceFactory(
            dataset=dataset,
            format='csv',
            description='* Title 1\n* Title 2',
            metrics={'views': 42})

        url = url_for('datasets.show', dataset=dataset)
        response = self.get(url)
        self.assert200(response)
        json_ld = self.get_json_ld(response)
        self.assertEquals(json_ld['@context'], 'http://schema.org')
        self.assertEquals(json_ld['@type'], 'Dataset')
        self.assertEquals(json_ld['@id'], str(dataset.id))
        self.assertEquals(json_ld['description'], 'a&éèëù$£')
        self.assertEquals(json_ld['alternateName'], dataset.slug)
        self.assertEquals(json_ld['dateCreated'][:16],
                          dataset.created_at.isoformat()[:16])
        self.assertEquals(json_ld['dateModified'][:16],
                          dataset.last_modified.isoformat()[:16])
        self.assertEquals(json_ld['url'], 'http://localhost{}'.format(url))
        self.assertEquals(json_ld['name'], dataset.title)
        self.assertEquals(json_ld['keywords'], 'bar,foo')
        self.assertEquals(len(json_ld['distribution']), 1)

        json_ld_resource = json_ld['distribution'][0]
        self.assertEquals(json_ld_resource['@type'], 'DataDownload')
        self.assertEquals(json_ld_resource['@id'], str(resource.id))
        self.assertEquals(json_ld_resource['url'], resource.latest)
        self.assertEquals(json_ld_resource['name'], resource.title)
        self.assertEquals(json_ld_resource['contentUrl'], resource.url)
        self.assertEquals(json_ld_resource['dateCreated'][:16],
                          resource.created_at.isoformat()[:16])
        self.assertEquals(json_ld_resource['dateModified'][:16],
                          resource.modified.isoformat()[:16])
        self.assertEquals(json_ld_resource['datePublished'][:16],
                          resource.published.isoformat()[:16])
        self.assertEquals(json_ld_resource['encodingFormat'], 'png')
        self.assertEquals(json_ld_resource['contentSize'],
                          resource.filesize)
        self.assertEquals(json_ld_resource['fileFormat'], resource.mime)
        self.assertEquals(json_ld_resource['description'],
                          'Title 1 Title 2')
        self.assertEquals(json_ld_resource['interactionStatistic'],
                          {
                              '@type': 'InteractionCounter',
                              'interactionType': {
                                  '@type': 'DownloadAction',
                              },
                              'userInteractionCount': 10,
                          })

        self.assertEquals(len(json_ld['contributedDistribution']), 1)
        json_ld_resource = json_ld['contributedDistribution'][0]
        self.assertEquals(json_ld_resource['@type'], 'DataDownload')
        self.assertEquals(json_ld_resource['@id'], str(community_resource.id))
        self.assertEquals(json_ld_resource['url'], community_resource.latest)
        self.assertEquals(json_ld_resource['name'], community_resource.title)
        self.assertEquals(json_ld_resource['contentUrl'],
                          community_resource.url)
        self.assertEquals(json_ld_resource['dateCreated'][:16],
                          community_resource.created_at.isoformat()[:16])
        self.assertEquals(json_ld_resource['dateModified'][:16],
                          community_resource.modified.isoformat()[:16])
        self.assertEquals(json_ld_resource['datePublished'][:16],
                          community_resource.published.isoformat()[:16])
        self.assertEquals(json_ld_resource['encodingFormat'],
                          community_resource.format)
        self.assertEquals(json_ld_resource['contentSize'],
                          community_resource.filesize)
        self.assertEquals(json_ld_resource['fileFormat'],
                          community_resource.mime)
        self.assertEquals(json_ld_resource['description'], 'Title 1 Title 2')
        self.assertEquals(json_ld_resource['interactionStatistic'], {
            '@type': 'InteractionCounter',
            'interactionType': {
                '@type': 'DownloadAction',
            },
            'userInteractionCount': 42,
        })

        self.assertEquals(json_ld['extras'],
                          [{
                              '@type': 'http://schema.org/PropertyValue',
                              'name': 'foo',
                              'value': 'bar',
                          }])
        self.assertEquals(json_ld['license'], 'http://www.datagouv.fr/licence')
        self.assertEquals(json_ld['author']['@type'], 'Person')
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
def community_resource(app):
    community_resource = CommunityResourceFactory()
    download(community_resource)
    download(community_resource, latest=True)
    return community_resource
Exemplo n.º 6
0
 def test_community_resource_deleted_dataset(self):
     dataset = DatasetFactory()
     community_resource = CommunityResourceFactory(dataset=dataset)
     community_resource.dataset.delete()
     community_resource.reload()
     assert community_resource.dataset is None
Exemplo n.º 7
0
 def test_community_resource_latest_url(self):
     '''It should redirect to the real community resource URL'''
     resource = CommunityResourceFactory()
     response = self.get(url_for('datasets.resource', id=resource.id))
     self.assertStatus(response, 302)
     self.assertEqual(response.location, resource.url)
Exemplo n.º 8
0
 def test_community_resource_deleted_dataset(self):
     dataset = DatasetFactory()
     community_resource = CommunityResourceFactory(dataset=dataset)
     community_resource.dataset.delete()
     community_resource.reload()
     assert community_resource.dataset is None