Example #1
0
 def test_post_api_delete(self, api):
     '''It should delete a post from the API'''
     post = PostFactory()
     with api.user(AdminFactory()):
         response = api.delete(url_for('api.post', post=post))
     assert204(response)
     assert Post.objects.count() == 0
    def test_create_crontab_job(self):
        @job('a-job')
        def test_job():
            pass

        data = {
            'name': 'A crontab job',
            'description': 'A simple crontab job doing nothing',
            'task': 'a-job',
            'crontab': {
                'minute': '0',
                'hour': '0'
            }
        }

        self.login(AdminFactory())
        response = self.post(url_for('api.jobs'), data)
        self.assert201(response)

        self.assertEqual(response.json['name'], data['name'])
        self.assertEqual(response.json['description'], data['description'])
        self.assertEqual(response.json['task'], data['task'])
        self.assertEqual(
            response.json['crontab'], {
                'minute': '0',
                'hour': '0',
                'day_of_week': '*',
                'day_of_month': '*',
                'month_of_year': '*',
            })
Example #3
0
    def test_purge_organizations(self):
        with self.autoindex():
            org = Organization.objects.create(name='delete me',
                                              description='XXX')
            resources = [ResourceFactory() for _ in range(2)]
            dataset = DatasetFactory(resources=resources, organization=org)

        # Upload organization's logo
        file = create_test_image()
        user = AdminFactory()
        self.login(user)
        response = self.post(url_for('api.organization_logo', org=org),
                             {'file': (file, 'test.png')},
                             json=False)
        self.assert200(response)

        # Delete organization
        response = self.delete(url_for('api.organization', org=org))
        self.assert204(response)

        tasks.purge_organizations()

        # Check organization's logo is deleted
        self.assertEqual(list(storages.avatars.list_files()), [])

        dataset = Dataset.objects(id=dataset.id).first()
        self.assertIsNone(dataset.organization)

        organization = Organization.objects(name='delete me').first()
        self.assertIsNone(organization)

        indexed_dataset = DatasetSearch.get(id=dataset.id,
                                            using=es.client,
                                            index=es.index_name)
        self.assertIsNone(indexed_dataset.organization)
Example #4
0
    def test_delete_discussion_comment(self):
        owner = self.login(AdminFactory())
        user = UserFactory()
        dataset = Dataset.objects.create(title='Test dataset', owner=owner)
        message = Message(content='bla bla', posted_by=user)
        message2 = Message(content='bla bla bla', posted_by=user)
        discussion = Discussion.objects.create(subject=dataset,
                                               user=user,
                                               title='test discussion',
                                               discussion=[message, message2])
        self.assertEqual(len(discussion.discussion), 2)

        # test first comment deletion
        response = self.delete(
            url_for('api.discussion_comment', id=discussion.id, cidx=0))
        self.assertStatus(response, 400)

        # test effective deletion
        response = self.delete(
            url_for('api.discussion_comment', id=discussion.id, cidx=1))
        self.assertStatus(response, 204)
        discussion.reload()
        self.assertEqual(len(discussion.discussion), 1)
        self.assertEqual(discussion.discussion[0].content, 'bla bla')

        # delete again to test list overflow
        response = self.delete(
            url_for('api.discussion_comment', id=discussion.id, cidx=3))
        self.assertStatus(response, 404)

        # delete again to test last comment deletion
        response = self.delete(
            url_for('api.discussion_comment', id=discussion.id, cidx=0))
        self.assertStatus(response, 400)
    def test_update_job_change_type(self):
        @job('a-job')
        def test_job():
            pass

        task = PeriodicTask.objects.create(
            name=faker.name(),
            description=faker.sentence(),
            task='a-job',
            crontab=PeriodicTask.Crontab(minute='5'))

        self.login(AdminFactory())
        response = self.put(
            url_for('api.job', id=task.id), {
                'name': task.name,
                'description': task.description,
                'task': task.task,
                'interval': {
                    'every': 5,
                    'period': 'minutes',
                }
            })
        self.assert200(response)

        self.assertEqual(response.json['id'], str(task.id))
        self.assertEqual(response.json['name'], task.name)
        self.assertEqual(response.json['task'], task.task)
        self.assertEqual(response.json['description'], task.description)
        self.assertEqual(response.json['interval']['every'], 5)
        self.assertEqual(response.json['interval']['period'], 'minutes')
        self.assertIsNone(response.json['crontab'])
Example #6
0
 def test_topic_api_delete(self):
     '''It should delete a topic from the API'''
     topic = TopicFactory()
     with self.api_user(AdminFactory()):
         response = self.delete(url_for('api.topic', topic=topic))
     self.assertStatus(response, 204)
     self.assertEqual(Topic.objects.count(), 0)
Example #7
0
    def test_purge_reuses(self):
        reuse = ReuseFactory(title='test-reuse')

        # Upload reuse's image
        file = create_test_image()
        user = AdminFactory()
        self.login(user)
        response = self.post(url_for('api.reuse_image', reuse=reuse),
                             {'file': (file, 'test.png')},
                             json=False)
        self.assert200(response)

        # Delete reuse
        response = self.delete(url_for('api.reuse', reuse=reuse))
        self.assert204(response)

        user = UserFactory()
        transfer = Transfer.objects.create(
            owner=user,
            recipient=user,
            subject=reuse,
            comment='comment',
        )

        tasks.purge_reuses()

        assert Transfer.objects.filter(id=transfer.id).count() == 0

        # Check reuse's image is deleted
        self.assertEqual(list(storages.images.list_files()), [])

        deleted_reuse = Reuse.objects(title='test-reuse').first()
        self.assertIsNone(deleted_reuse)
    def test_update_job(self):
        @job('a-job')
        def test_job():
            pass

        task = PeriodicTask.objects.create(
            name=faker.name(),
            description=faker.sentence(),
            task='a-job',
            crontab=PeriodicTask.Crontab(minute='5'))

        self.login(AdminFactory())
        response = self.put(
            url_for('api.job', id=task.id), {
                'name': task.name,
                'description': 'New description',
                'task': task.task,
                'crontab': task.crontab.to_json()
            })
        self.assert200(response)

        self.assertEqual(response.json['id'], str(task.id))
        self.assertEqual(response.json['name'], task.name)
        self.assertEqual(response.json['task'], task.task)
        self.assertEqual(response.json['description'], 'New description')
        self.assertIsNotNone(response.json['crontab'])
        self.assertIsNone(response.json['interval'])
Example #9
0
 def test_post_api_delete(self):
     '''It should delete a post from the API'''
     post = PostFactory()
     with self.api_user(AdminFactory()):
         response = self.delete(url_for('api.post', post=post))
     self.assertStatus(response, 204)
     self.assertEqual(Post.objects.count(), 0)
Example #10
0
    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()
Example #11
0
 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 setUp(self, api, clean_db):
        # Register at least two badges
        Organization.__badges__['test-1'] = 'Test 1'
        Organization.__badges__['test-2'] = 'Test 2'

        self.factory = badge_factory(Organization)
        self.user = api.login(AdminFactory())
        self.organization = OrganizationFactory()
Example #13
0
 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'])
Example #14
0
 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'])
Example #15
0
 def test_topic_api_update(self):
     '''It should update a topic from the API'''
     topic = TopicFactory()
     data = topic.to_dict()
     data['description'] = 'new description'
     self.login(AdminFactory())
     response = self.put(url_for('api.topic', topic=topic), data)
     self.assert200(response)
     self.assertEqual(Topic.objects.count(), 1)
     self.assertEqual(Topic.objects.first().description, 'new description')
Example #16
0
    def test_dataset_api_feature(self):
        '''It should mark the dataset featured on POST'''
        self.login(AdminFactory())
        dataset = DatasetFactory(featured=False)

        response = self.post(url_for('api.dataset_featured', dataset=dataset))
        self.assert200(response)

        dataset.reload()
        self.assertTrue(dataset.featured)
Example #17
0
    def test_dataset_api_feature_already(self):
        '''It shouldn't do anything to feature an already featured dataset'''
        self.login(AdminFactory())
        dataset = DatasetFactory(featured=True)

        response = self.post(url_for('api.dataset_featured', dataset=dataset))
        self.assert200(response)

        dataset.reload()
        self.assertTrue(dataset.featured)
Example #18
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)
     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)
Example #19
0
 def test_user_api_create_as_admin(self):
     '''It should create a user'''
     self.login(AdminFactory())
     data = {
         "first_name": faker.first_name(),
         "last_name": faker.last_name(),
         "email": faker.email()
     }
     response = self.post(url_for('api.users'), data=data)
     self.assert201(response)
Example #20
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
Example #21
0
 def test_post_api_update(self, api):
     '''It should update a post from the API'''
     post = PostFactory()
     data = post.to_dict()
     data['content'] = 'new content'
     api.login(AdminFactory())
     response = api.put(url_for('api.post', post=post), data)
     assert200(response)
     assert Post.objects.count() == 1
     assert Post.objects.first().content == 'new content'
Example #22
0
 def test_post_api_update(self):
     '''It should update a post from the API'''
     post = PostFactory()
     data = post.to_dict()
     data['content'] = 'new content'
     self.login(AdminFactory())
     response = self.put(url_for('api.post', post=post), data)
     self.assert200(response)
     self.assertEqual(Post.objects.count(), 1)
     self.assertEqual(Post.objects.first().content, 'new content')
Example #23
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)
Example #24
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)
Example #25
0
    def test_post_api_unpublish(self, api):
        '''It should update a post from the API'''
        post = PostFactory()
        api.login(AdminFactory())
        response = api.delete(url_for('api.publish_post', post=post))
        assert200(response)
        assert Post.objects.count() == 1

        post.reload()
        assert post.published is None
Example #26
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)
Example #27
0
    def test_purge_organizations(self):
        org = Organization.objects.create(name='delete me', description='XXX')
        resources = [ResourceFactory() for _ in range(2)]
        dataset = DatasetFactory(resources=resources, organization=org)

        # Upload organization's logo
        file = create_test_image()
        user = AdminFactory()
        self.login(user)
        response = self.post(
            url_for('api.organization_logo', org=org),
            {'file': (file, 'test.png')},
            json=False)
        self.assert200(response)

        transfer_to_org = Transfer.objects.create(
            owner=user,
            recipient=org,
            subject=dataset,
            comment='comment',
        )
        transfer_from_org = Transfer.objects.create(
            owner=org,
            recipient=user,
            subject=dataset,
            comment='comment',
        )

        oauth_client = OAuth2Client.objects.create(
            name='test-client',
            owner=user,
            organization=org,
            redirect_uris=['https://test.org/callback'],
        )

        # Delete organization
        response = self.delete(url_for('api.organization', org=org))
        self.assert204(response)

        tasks.purge_organizations()

        oauth_client.reload()
        assert oauth_client.organization is None

        assert Transfer.objects.filter(id=transfer_from_org.id).count() == 0
        assert Transfer.objects.filter(id=transfer_to_org.id).count() == 0

        # Check organization's logo is deleted
        self.assertEqual(list(storages.avatars.list_files()), [])

        dataset = Dataset.objects(id=dataset.id).first()
        self.assertIsNone(dataset.organization)

        organization = Organization.objects(name='delete me').first()
        self.assertIsNone(organization)
Example #28
0
    def test_get_home_reuses(self):
        site = SiteFactory.create(
            id=self.app.config['SITE_ID'],
            settings__home_reuses=VisibleReuseFactory.create_batch(3))
        current_site.reload()

        self.login(AdminFactory())
        response = self.get(url_for('api.home_reuses'))
        self.assert200(response)

        self.assertEqual(len(response.json), len(site.settings.home_reuses))
Example #29
0
    def test_dataset_api_unfeature_already(self):
        '''It shouldn't do anything to unfeature a not featured dataset'''
        self.login(AdminFactory())
        dataset = DatasetFactory(featured=False)

        response = self.delete(url_for('api.dataset_featured',
                                       dataset=dataset))
        self.assert200(response)

        dataset.reload()
        self.assertFalse(dataset.featured)
Example #30
0
 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)