Exemplo n.º 1
0
    def test_reuse_api_unfeature(self):
        '''It should mark the reuse featured on POST'''
        reuse = ReuseFactory(featured=True)

        with self.api_user(AdminFactory()):
            response = self.delete(url_for('api.reuse_featured', reuse=reuse))
        self.assert200(response)

        reuse.reload()
        self.assertFalse(reuse.featured)
Exemplo n.º 2
0
    def test_reuse_api_unfeature_already(self):
        '''It shouldn't do anything to unfeature a not featured reuse'''
        reuse = ReuseFactory(featured=False)

        with self.api_user(AdminFactory()):
            response = self.delete(url_for('api.reuse_featured', reuse=reuse))
        self.assert200(response)

        reuse.reload()
        self.assertFalse(reuse.featured)
Exemplo n.º 3
0
    def test_reuse_api_unfeature_already(self):
        '''It shouldn't do anything to unfeature a not featured reuse'''
        reuse = ReuseFactory(featured=False)

        with self.api_user(AdminFactory()):
            response = self.delete(url_for('api.reuse_featured', reuse=reuse))
        self.assert200(response)

        reuse.reload()
        self.assertFalse(reuse.featured)
Exemplo n.º 4
0
    def test_reuse_api_feature_already(self):
        '''It shouldn't do anything to feature an already featured reuse'''
        reuse = ReuseFactory(featured=True)

        with self.api_user(AdminFactory()):
            response = self.post(url_for('api.reuse_featured', reuse=reuse))
        self.assert200(response)

        reuse.reload()
        self.assertTrue(reuse.featured)
Exemplo n.º 5
0
    def test_reuse_api_feature_already(self):
        '''It shouldn't do anything to feature an already featured reuse'''
        reuse = ReuseFactory(featured=True)

        with self.api_user(AdminFactory()):
            response = self.post(url_for('api.reuse_featured', reuse=reuse))
        self.assert200(response)

        reuse.reload()
        self.assertTrue(reuse.featured)
Exemplo n.º 6
0
    def test_reuse_api_unfeature(self):
        '''It should mark the reuse featured on POST'''
        reuse = ReuseFactory(featured=True)

        with self.api_user(AdminFactory()):
            response = self.delete(url_for('api.reuse_featured', reuse=reuse))
        self.assert200(response)

        reuse.reload()
        self.assertFalse(reuse.featured)
Exemplo n.º 7
0
    def test_reuse_api_unfeature(self, api):
        '''It should mark the reuse featured on POST'''
        reuse = ReuseFactory(featured=True)

        with api.user(AdminFactory()):
            response = api.delete(url_for('api.reuse_featured', reuse=reuse))
        assert200(response)

        reuse.reload()
        assert not reuse.featured
Exemplo n.º 8
0
    def test_reuse_api_add_dataset_not_found(self):
        '''It should return 404 when adding an unknown dataset to a reuse'''
        self.login()
        reuse = ReuseFactory(owner=self.user)

        data = {'id': 'not-found', 'class': 'Dataset'}
        url = url_for('api.reuse_add_dataset', reuse=reuse)
        response = self.post(url, data)

        self.assert404(response)
        reuse.reload()
        self.assertEqual(len(reuse.datasets), 0)
Exemplo n.º 9
0
    def test_reuse_api_add_dataset_not_found(self):
        '''It should return 404 when adding an unknown dataset to a reuse'''
        self.login()
        reuse = ReuseFactory(owner=self.user)

        data = {'id': 'not-found', 'class': 'Dataset'}
        url = url_for('api.reuse_add_dataset', reuse=reuse)
        response = self.post(url, data)

        self.assert404(response)
        reuse.reload()
        self.assertEqual(len(reuse.datasets), 0)
Exemplo n.º 10
0
    def test_reuse_api_add_dataset_twice(self):
        '''It should not add twice a dataset to a reuse from the API'''
        self.login()
        dataset = DatasetFactory()
        reuse = ReuseFactory(owner=self.user, datasets=[dataset])

        data = {'id': dataset.id, 'class': 'Dataset'}
        url = url_for('api.reuse_add_dataset', reuse=reuse)
        response = self.post(url, data)
        self.assert200(response)
        reuse.reload()
        self.assertEqual(len(reuse.datasets), 1)
        self.assertEqual(reuse.datasets[-1], dataset)
Exemplo n.º 11
0
    def test_reuse_api_add_dataset_twice(self):
        '''It should not add twice a dataset to a reuse from the API'''
        self.login()
        dataset = DatasetFactory()
        reuse = ReuseFactory(owner=self.user, datasets=[dataset])

        data = {'id': dataset.id, 'class': 'Dataset'}
        url = url_for('api.reuse_add_dataset', reuse=reuse)
        response = self.post(url, data)
        self.assert200(response)
        reuse.reload()
        self.assertEqual(len(reuse.datasets), 1)
        self.assertEqual(reuse.datasets[-1], dataset)
Exemplo n.º 12
0
    def test_reuse_api_add_dataset_twice(self, api):
        '''It should not add twice a dataset to a reuse from the API'''
        user = api.login()
        dataset = DatasetFactory()
        reuse = ReuseFactory(owner=user, datasets=[dataset])

        data = {'id': dataset.id, 'class': 'Dataset'}
        url = url_for('api.reuse_add_dataset', reuse=reuse)
        response = api.post(url, data)
        assert200(response)
        reuse.reload()
        assert len(reuse.datasets) == 1
        assert reuse.datasets[-1] == dataset
Exemplo n.º 13
0
class ReuseBadgeAPITest(APITestCase):
    @classmethod
    def setUpClass(cls):
        # Register at least two badges
        Reuse.__badges__['test-1'] = 'Test 1'
        Reuse.__badges__['test-2'] = 'Test 2'

        cls.factory = badge_factory(Reuse)

    def setUp(self):
        self.login(AdminFactory())
        self.reuse = ReuseFactory()

    def test_list(self):
        response = self.get(url_for('api.available_reuse_badges'))
        self.assertStatus(response, 200)
        self.assertEqual(len(response.json), len(Reuse.__badges__))
        for kind, label in Reuse.__badges__.items():
            self.assertIn(kind, response.json)
            self.assertEqual(response.json[kind], label)

    def test_create(self):
        data = self.factory.as_dict()
        with self.api_user():
            response = self.post(
                url_for('api.reuse_badges', reuse=self.reuse), data)
        self.assert201(response)
        self.reuse.reload()
        self.assertEqual(len(self.reuse.badges), 1)

    def test_create_same(self):
        data = self.factory.as_dict()
        with self.api_user():
            self.post(
                url_for('api.reuse_badges', reuse=self.reuse), data)
            response = self.post(
                url_for('api.reuse_badges', reuse=self.reuse), data)
        self.assertStatus(response, 200)
        self.reuse.reload()
        self.assertEqual(len(self.reuse.badges), 1)

    def test_create_2nd(self):
        # Explicitely setting the kind to avoid collisions given the
        # small number of choices for kinds.
        kinds_keys = Reuse.__badges__.keys()
        self.reuse.badges.append(
            self.factory(kind=kinds_keys[0]))
        self.reuse.save()
        data = self.factory.as_dict()
        data['kind'] = kinds_keys[1]
        with self.api_user():
            response = self.post(
                url_for('api.reuse_badges', reuse=self.reuse), data)
        self.assert201(response)
        self.reuse.reload()
        self.assertEqual(len(self.reuse.badges), 2)

    def test_delete(self):
        badge = self.factory()
        self.reuse.badges.append(badge)
        self.reuse.save()
        with self.api_user():
            response = self.delete(
                url_for('api.reuse_badge', reuse=self.reuse,
                        badge_kind=str(badge.kind)))
        self.assertStatus(response, 204)
        self.reuse.reload()
        self.assertEqual(len(self.reuse.badges), 0)

    def test_delete_404(self):
        with self.api_user():
            response = self.delete(
                url_for('api.reuse_badge', reuse=self.reuse,
                        badge_kind=str(self.factory().kind)))
        self.assert404(response)
Exemplo n.º 14
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.º 15
0
class ReuseBadgeAPITest(APITestCase):
    @classmethod
    def setUpClass(cls):
        # Register at least two badges
        Reuse.__badges__['test-1'] = 'Test 1'
        Reuse.__badges__['test-2'] = 'Test 2'

        cls.factory = badge_factory(Reuse)

    def setUp(self):
        self.login(AdminFactory())
        self.reuse = ReuseFactory()

    def test_list(self):
        response = self.get(url_for('api.available_reuse_badges'))
        self.assertStatus(response, 200)
        self.assertEqual(len(response.json), len(Reuse.__badges__))
        for kind, label in Reuse.__badges__.items():
            self.assertIn(kind, response.json)
            self.assertEqual(response.json[kind], label)

    def test_create(self):
        data = self.factory.as_dict()
        with self.api_user():
            response = self.post(
                url_for('api.reuse_badges', reuse=self.reuse), data)
        self.assert201(response)
        self.reuse.reload()
        self.assertEqual(len(self.reuse.badges), 1)

    def test_create_same(self):
        data = self.factory.as_dict()
        with self.api_user():
            self.post(
                url_for('api.reuse_badges', reuse=self.reuse), data)
            response = self.post(
                url_for('api.reuse_badges', reuse=self.reuse), data)
        self.assertStatus(response, 200)
        self.reuse.reload()
        self.assertEqual(len(self.reuse.badges), 1)

    def test_create_2nd(self):
        # Explicitely setting the kind to avoid collisions given the
        # small number of choices for kinds.
        kinds_keys = Reuse.__badges__.keys()
        self.reuse.add_badge(kinds_keys[0])
        data = self.factory.as_dict()
        data['kind'] = kinds_keys[1]
        with self.api_user():
            response = self.post(
                url_for('api.reuse_badges', reuse=self.reuse), data)
        self.assert201(response)
        self.reuse.reload()
        self.assertEqual(len(self.reuse.badges), 2)

    def test_delete(self):
        badge = self.factory()
        self.reuse.add_badge(badge.kind)
        with self.api_user():
            response = self.delete(
                url_for('api.reuse_badge', reuse=self.reuse,
                        badge_kind=str(badge.kind)))
        self.assertStatus(response, 204)
        self.reuse.reload()
        self.assertEqual(len(self.reuse.badges), 0)

    def test_delete_404(self):
        with self.api_user():
            response = self.delete(
                url_for('api.reuse_badge', reuse=self.reuse,
                        badge_kind=str(self.factory().kind)))
        self.assert404(response)
Exemplo n.º 16
0
class ReuseBadgeAPITest:
    modules = []

    @pytest.fixture(autouse=True)
    def setup(self, api, clean_db):
        # Register at least two badges
        Reuse.__badges__['test-1'] = 'Test 1'
        Reuse.__badges__['test-2'] = 'Test 2'

        self.factory = badge_factory(Reuse)
        self.user = api.login(AdminFactory())
        self.reuse = ReuseFactory()

    def test_list(self, api):
        response = api.get(url_for('api.available_reuse_badges'))
        assert200(response)
        assert len(response.json) == len(Reuse.__badges__)
        for kind, label in Reuse.__badges__.items():
            assert kind in response.json
            assert response.json[kind] == label

    def test_create(self, api):
        data = self.factory.as_dict()
        response = api.post(url_for('api.reuse_badges', reuse=self.reuse),
                            data)
        assert201(response)
        self.reuse.reload()
        assert len(self.reuse.badges) == 1

    def test_create_same(self, api):
        data = self.factory.as_dict()
        api.post(url_for('api.reuse_badges', reuse=self.reuse), data)
        response = api.post(url_for('api.reuse_badges', reuse=self.reuse),
                            data)
        assert200(response)
        self.reuse.reload()
        assert len(self.reuse.badges) == 1

    def test_create_2nd(self, api):
        # Explicitely setting the kind to avoid collisions given the
        # small number of choices for kinds.
        kinds_keys = list(Reuse.__badges__)
        self.reuse.add_badge(kinds_keys[0])
        data = self.factory.as_dict()
        data['kind'] = kinds_keys[1]
        response = api.post(url_for('api.reuse_badges', reuse=self.reuse),
                            data)
        assert201(response)
        self.reuse.reload()
        assert len(self.reuse.badges) == 2

    def test_delete(self, api):
        badge = self.factory()
        self.reuse.add_badge(badge.kind)
        response = api.delete(
            url_for('api.reuse_badge',
                    reuse=self.reuse,
                    badge_kind=str(badge.kind)))
        assert204(response)
        self.reuse.reload()
        assert len(self.reuse.badges) == 0

    def test_delete_404(self, api):
        response = api.delete(
            url_for('api.reuse_badge',
                    reuse=self.reuse,
                    badge_kind=str(self.factory().kind)))
        assert404(response)