Example #1
0
    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)
Example #2
0
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()))
Example #3
0
    def test_add_resource(self):
        user = UserFactory()
        dataset = DatasetFactory(owner=user)
        resource = ResourceFactory()
        expected_signals = post_save, Dataset.after_save, Dataset.on_update

        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
Example #4
0
    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'
Example #5
0
 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'
         ])
Example #6
0
    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)
Example #7
0
    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
Example #8
0
    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)
Example #9
0
    def test_notify_user_discussions(self):
        owner = UserFactory()
        dataset = DatasetFactory(owner=owner)

        open_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=faker.sentence(),
                discussion=[message]
            )
            open_discussions[discussion.id] = discussion
        # Creating a closed discussion that shouldn't show up in response.
        user = UserFactory()
        message = Message(content=faker.sentence(), posted_by=user)
        discussion = Discussion.objects.create(
            subject=dataset,
            user=user,
            title=faker.sentence(),
            discussion=[message],
            closed=datetime.now(),
            closed_by=user
        )

        notifications = discussions_notifications(owner)

        self.assertEqual(len(notifications), len(open_discussions))

        for dt, details in notifications:
            discussion = open_discussions[details['id']]
            self.assertEqual(details['title'], discussion.title)
            self.assertEqual(details['subject']['id'], discussion.subject.id)
            self.assertEqual(details['subject']['type'], 'dataset')
Example #10
0
    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)
Example #11
0
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
Example #12
0
    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)
Example #13
0
    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))
Example #14
0
    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)
Example #15
0
    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
Example #16
0
    def test_closed_issue_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)
        issue = Issue.objects.create(
            subject=DatasetFactory(owner=owner),
            user=poster,
            title=faker.sentence(),
            discussion=[message, second_message, closing_message])

        # issue = IssueFactory()
        with capture_mails() as mails:
            notify_issue_closed(issue, 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)
Example #17
0
    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_only_admin_can_update_member(self, api):
        user = api.login()
        updated_user = UserFactory()
        organization = OrganizationFactory(members=[
            Member(user=user, role='editor'),
            Member(user=updated_user, role='editor')
        ])

        api_url = url_for('api.member', org=organization, user=updated_user)
        response = api.put(api_url, {'role': 'admin'})

        assert403(response)

        organization.reload()
        assert organization.is_member(updated_user)
        assert not organization.is_admin(updated_user)
Example #19
0
    def test_with_initial_owner_and_no_data_provided(self):
        Ownable, OwnableForm = self.factory()
        user = UserFactory()
        ownable = Ownable(owner=user)

        form = OwnableForm(MultiDict({}), obj=ownable)

        self.assertEqual(form.owner.data, user)

        login_user(user)
        form.validate()
        self.assertEqual(form.errors, {})

        form.populate_obj(ownable)
        self.assertEqual(ownable.owner, user)
        self.assertIsNone(ownable.organization)
Example #20
0
    def test_bad_oauth_auth(self, api, oauth):
        '''Should handle wrong OAuth header authentication'''
        user = UserFactory()
        OAuth2Token.objects.create(
            client=oauth,
            user=user,
            access_token='access-token',
            refresh_token='refresh-token',
        )

        response = api.post(url_for('api.fake'), headers={
            'Authorization': ' '.join(['Bearer', 'not-my-token'])
        })

        assert401(response)
        assert response.content_type == 'application/json'
Example #21
0
    def test_with_initial_and_not_member(self):
        Ownable, OwnableForm = self.factory()
        user = UserFactory()
        org = OrganizationFactory(members=[Member(user=user, role='editor')])
        neworg = OrganizationFactory()
        ownable = Ownable(organization=org)

        form = OwnableForm(MultiDict({'organization': str(neworg.id)}),
                           obj=ownable)

        self.assertEqual(form.organization.data, neworg)

        login_user(user)
        form.validate()
        self.assertIn('organization', form.errors)
        self.assertEqual(len(form.errors['organization']), 1)
Example #22
0
    def test_community_resource(self):
        user = UserFactory()
        dataset = DatasetFactory(owner=user)
        community_resource1 = CommunityResourceFactory()
        community_resource1.dataset = dataset
        community_resource1.save()
        self.assertEqual(len(dataset.community_resources), 1)

        community_resource2 = CommunityResourceFactory()
        community_resource2.dataset = dataset
        community_resource2.save()
        self.assertEqual(len(dataset.community_resources), 2)
        self.assertEqual(dataset.community_resources[1].id,
                         community_resource1.id)
        self.assertEqual(dataset.community_resources[0].id,
                         community_resource2.id)
Example #23
0
    def test_recent_feed_owner(self):
        owner = UserFactory()
        DatasetFactory(owner=owner, resources=[ResourceFactory()])

        response = self.get(url_for('datasets.recent_feed'))

        self.assert200(response)

        feed = feedparser.parse(response.data)

        self.assertEqual(len(feed.entries), 1)
        entry = feed.entries[0]
        self.assertEqual(len(entry.authors), 1)
        author = entry.authors[0]
        self.assertEqual(author.name, owner.fullname)
        self.assertEqual(author.href,
                         self.full_url('users.show', user=owner.id))
Example #24
0
    def test_unfollow(self):
        '''It should unfollow the user on DELETE'''
        user = self.login()
        to_follow = UserFactory()
        Follow.objects.create(follower=user, following=to_follow)

        response = self.delete(url_for('api.user_followers', id=to_follow.id))
        self.assert200(response)

        nb_followers = Follow.objects.followers(to_follow).count()

        self.assertEqual(response.json['followers'], nb_followers)

        self.assertEqual(Follow.objects.following(to_follow).count(), 0)
        self.assertEqual(nb_followers, 0)
        self.assertEqual(Follow.objects.following(user).count(), 0)
        self.assertEqual(Follow.objects.followers(user).count(), 0)
Example #25
0
    def test_follow_user(self):
        '''It should follow an user on POST'''
        user = self.login()
        to_follow = UserFactory()

        response = self.post(url_for('api.user_followers', id=to_follow.id))
        self.assert201(response)

        nb_followers = Follow.objects.followers(to_follow).count()

        self.assertEqual(response.json['followers'], nb_followers)
        self.assertEqual(Follow.objects.following(to_follow).count(), 0)
        self.assertEqual(nb_followers, 1)
        self.assertIsInstance(Follow.objects.followers(to_follow).first(),
                              Follow)
        self.assertEqual(Follow.objects.following(user).count(), 1)
        self.assertEqual(Follow.objects.followers(user).count(), 0)
Example #26
0
    def test_all_fields(self):
        user = UserFactory(website=faker.uri())
        user_url = url_for('users.show_redirect', user=user.id, _external=True)
        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, URIRef)
        self.assertEqual(u.identifier.toPython(), user_url)
        self.assertEqual(u.value(FOAF.name), Literal(user.fullname))
        self.assertEqual(u.value(RDFS.label), Literal(user.fullname))
        self.assertEqual(
            u.value(FOAF.homepage).identifier, URIRef(user.website))
Example #27
0
    def test_with_other_user_admin(self):
        Ownable, OwnableForm = self.factory()
        user = UserFactory()
        admin = AdminFactory()

        login_user(admin)

        form = OwnableForm(MultiDict({'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)
Example #28
0
    def test_oauth_auth(self, api, oauth):
        '''Should handle  OAuth header authentication'''
        user = UserFactory()
        token = OAuth2Token.objects.create(
            client=oauth,
            user=user,
            access_token='access-token',
            refresh_token='refresh-token',
        )

        response = api.post(url_for('api.fake'), headers={
            'Authorization': ' '.join(['Bearer', token.access_token])
        })

        assert200(response)
        assert response.content_type == 'application/json'
        assert response.json == {'success': True}
Example #29
0
    def test_refresh_token(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.token'), {
            'grant_type': 'refresh_token',
            'refresh_token': token.refresh_token,
        }, headers=basic_header(oauth))

        assert200(response)
        assert response.content_type == 'application/json'
        assert 'access_token' in response.json
Example #30
0
    def test_no_duplicate(self):
        site = SiteFactory()
        org = OrganizationFactory()
        user = UserFactory()
        datasets = VisibleDatasetFactory.create_batch(2, owner=user)
        datasets += VisibleDatasetFactory.create_batch(2, organization=org)
        catalog = build_catalog(site, datasets)
        graph = catalog.graph

        orgs = list(graph.subjects(RDF.type, FOAF.Organization))
        self.assertEqual(len(orgs), 1 + 1)  # There is the site publisher
        users = list(graph.subjects(RDF.type, FOAF.Person))
        self.assertEqual(len(users), 1)
        org_names = list(graph.objects(orgs[0], FOAF.name))
        self.assertEqual(len(org_names), 1)
        user_names = list(graph.objects(users[0], FOAF.name))
        self.assertEqual(len(user_names), 1)