示例#1
0
    def test_project_create(self):
        """Test Auditlog API project create works."""
        CategoryFactory.create()
        user = UserFactory.create()

        data = {'name': 'New Name',
                'short_name': 'new_short_name',
                'description': 'new_description',
                'long_description': 'new_long_description',
                'allow_anonymous_contributors': 'False',
                }
        url = '/api/project?api_key=%s' % (user.api_key)
        self.app.post(url, data=json.dumps(data))
        logs = auditlog_repo.filter_by(project_short_name='new_short_name')

        assert len(logs) == 1, logs
        for log in logs:
            assert log.user_id == user.id, log.user_id
            assert log.user_name == user.name, log.user_name
            assert log.project_short_name == 'new_short_name', log.project_short_name
            assert log.caller == 'api', log.caller
            assert log.action == 'create', log.action
            assert log.attribute == 'project', log.attribute
            assert log.old_value == 'Nothing', log.old_value
            assert log.new_value == 'New project', log.new_value
    def test_get_category_by_returns_none_if_no_category(self):
        """Test get_category returns None if no category matches the query"""

        CategoryFactory.create(name='My Project', short_name='mycategory')

        category = self.project_repo.get_by(name='no_name')

        assert category is None, category
    def test_get_used_returns_only_categories_with_projects(self):
        used_category = CategoryFactory.create()
        ProjectFactory.create(category=used_category)
        unused_category = CategoryFactory.create()

        used_categories = cached_categories.get_used()

        assert used_categories[0]["id"] == used_category.id, used_categories
示例#4
0
文件: test_views.py 项目: ccnmtl/rolf
 def test_application_crud(self):
     c = CategoryFactory()
     r = self.c.get(c.get_absolute_url())
     self.assertFalse("test application" in r.content)
     r = self.c.post(c.get_absolute_url() + "add_application/",
                     {'name': 'test application'})
     self.assertEqual(r.status_code, 302)
     r = self.c.get(c.get_absolute_url())
     self.assertTrue("test application" in r.content)
    def test_filter_categories_by_no_matches(self):
        """Test filter_categories_by returns an empty list if no categories
        match the query"""

        CategoryFactory.create(name='My Project', short_name='mycategory')

        retrieved_categories = self.project_repo.filter_categories_by(name='no_name')

        assert isinstance(retrieved_categories, list)
        assert len(retrieved_categories) == 0, retrieved_categories
示例#6
0
    def test_filter_categories_by_ownerid(self):
        """Test filter_categories_by removes ownerid from query."""

        CategoryFactory.create(name='My Project', short_name='mycategory')

        retrieved_categories = self.project_repo.filter_categories_by(short_name='mycategory',
                                                                      owner_id=1)

        assert isinstance(retrieved_categories, list)
        assert len(retrieved_categories) == 1, retrieved_categories
    def test_filter_categories_by_one_condition(self):
        """Test filter_categories_by returns a list of categories that meet
        the filtering condition"""

        CategoryFactory.create_batch(3, description='generic category')
        should_be_missing = CategoryFactory.create(description='other category')

        retrieved_categories = (self.project_repo
            .filter_categories_by(description='generic category'))

        assert len(retrieved_categories) == 3, retrieved_categories
        assert should_be_missing not in retrieved_categories, retrieved_categories
示例#8
0
    def test_query_category(self):
        """Test API query for category endpoint works"""
        CategoryFactory.create(name='thinking', short_name='thinking')
        # Test for real field
        url = "/api/category"
        res = self.app.get(url + "?short_name=thinking")
        data = json.loads(res.data)
        # Should return one result
        assert len(data) == 1, data
        # Correct result
        assert data[0]['short_name'] == 'thinking', data

        # Valid field but wrong value
        res = self.app.get(url + "?short_name=wrongvalue")
        data = json.loads(res.data)
        assert len(data) == 0, data

        # Multiple fields
        res = self.app.get(url + '?short_name=thinking&name=thinking')
        data = json.loads(res.data)
        # One result
        assert len(data) == 1, data
        # Correct result
        assert data[0]['short_name'] == 'thinking', data
        assert data[0]['name'] == 'thinking', data

        # Limits
        res = self.app.get(url + "?limit=1")
        data = json.loads(res.data)
        for item in data:
            assert item['short_name'] == 'thinking', item
        assert len(data) == 1, data

        # Keyset pagination
        CategoryFactory.create(name='computing', short_name='computing')
        res = self.app.get(url)
        data = json.loads(res.data)
        tmp = '?limit=1&last_id=%s' % data[0]['id']
        res = self.app.get(url + tmp)
        data_new = json.loads(res.data)
        assert len(data_new) == 1, data_new
        assert data_new[0]['id'] == data[1]['id']

        # Errors
        res = self.app.get(url + "?something")
        err = json.loads(res.data)
        err_msg = "AttributeError exception should be raised"
        res.status_code == 415, err_msg
        assert res.status_code == 415, err_msg
        assert err['action'] == 'GET', err_msg
        assert err['status'] == 'failed', err_msg
        assert err['exception_cls'] == 'AttributeError', err_msg
    def test_query_category(self):
        """Test API query for category endpoint works"""
        CategoryFactory.create(name="thinking", short_name="thinking")
        # Test for real field
        url = "/api/category"
        res = self.app.get(url + "?short_name=thinking")
        data = json.loads(res.data)
        # Should return one result
        assert len(data) == 1, data
        # Correct result
        assert data[0]["short_name"] == "thinking", data

        # Valid field but wrong value
        res = self.app.get(url + "?short_name=wrongvalue")
        data = json.loads(res.data)
        assert len(data) == 0, data

        # Multiple fields
        res = self.app.get(url + "?short_name=thinking&name=thinking")
        data = json.loads(res.data)
        # One result
        assert len(data) == 1, data
        # Correct result
        assert data[0]["short_name"] == "thinking", data
        assert data[0]["name"] == "thinking", data

        # Limits
        res = self.app.get(url + "?limit=1")
        data = json.loads(res.data)
        for item in data:
            assert item["short_name"] == "thinking", item
        assert len(data) == 1, data

        # Keyset pagination
        CategoryFactory.create(name="computing", short_name="computing")
        res = self.app.get(url)
        data = json.loads(res.data)
        tmp = "?limit=1&last_id=%s" % data[0]["id"]
        res = self.app.get(url + tmp)
        data_new = json.loads(res.data)
        assert len(data_new) == 1, data_new
        assert data_new[0]["id"] == data[1]["id"]

        # Errors
        res = self.app.get(url + "?something")
        err = json.loads(res.data)
        err_msg = "AttributeError exception should be raised"
        res.status_code == 415, err_msg
        assert res.status_code == 415, err_msg
        assert err["action"] == "GET", err_msg
        assert err["status"] == "failed", err_msg
        assert err["exception_cls"] == "AttributeError", err_msg
    def test_filter_categories_by_limit_offset(self):
        """Test that filter_categories_by supports limit and offset options"""

        CategoryFactory.create_batch(4)
        all_categories = self.project_repo.filter_categories_by()

        first_two = self.project_repo.filter_categories_by(limit=2)
        last_two = self.project_repo.filter_categories_by(limit=2, offset=2)

        assert len(first_two) == 2, first_two
        assert len(last_two) == 2, last_two
        assert first_two == all_categories[:2]
        assert last_two == all_categories[2:]
    def test_save_category_fails_if_integrity_error(self):
        """Test save_category raises a DBIntegrityError if the instance to be
       saved lacks a required value"""

        category = CategoryFactory.build(name=None)

        assert_raises(DBIntegrityError, self.project_repo.save_category, category)
示例#12
0
 def test_index_based_sorting(self):
     """Tests index based category sorting"""
     parent_1 = CategoryFactory.create(name="parent 1", index=10)
     sub_1 = CategoryFactory.create(name="sub 1", index=13,
         parent=parent_1)
     sub_2 = CategoryFactory.create(name="sub 2", index=12,
         parent=parent_1)
     sub_3 = CategoryFactory.create(name="sub 3", index=11,
         parent=parent_1)
     categories = Category.objects.all()
     indexes = []
     for category in categories:
         indexes.append(category.index)
     self.assertEquals(indexes, sorted(indexes))
     self.assertEquals(categories[0], parent_1)
     self.assertEquals(categories[3], sub_1)
    def test_get_category_returns_category(self):
        """Test get_category method returns a category if exists"""

        category = CategoryFactory.create()

        retrieved_category = self.project_repo.get_category(category.id)

        assert category == retrieved_category, retrieved_category
    def test_get_category_by(self):
        """Test get_category returns a category with the specified attribute"""

        category = CategoryFactory.create(name='My Cat', short_name='mycat')

        retrieved_category = self.project_repo.get_category_by(name=category.name)

        assert category == retrieved_category, retrieved_category
    def test_update_category_fails_if_integrity_error(self):
        """Test update raises a DBIntegrityError if the instance to be updated
        lacks a required value"""

        category = CategoryFactory.create()
        category.name = None

        assert_raises(DBIntegrityError, self.project_repo.update_category, category)
    def test_save_category(self):
        """Test save_category persist the category"""

        category = CategoryFactory.build()
        assert self.project_repo.get(category.id) is None

        self.project_repo.save_category(category)

        assert self.project_repo.get_category(category.id) == category, "Category not saved"
    def test_delete_category(self):
        """Test delete_category removes the category instance"""

        category = CategoryFactory.create()

        self.project_repo.delete_category(category)
        deleted = self.project_repo.get_category(category.id)

        assert deleted is None, deleted
示例#18
0
    def test_anonymous_user_cannot_crud(self):
        """Test anonymous users cannot crud categories"""
        category = CategoryFactory.build()

        assert_raises(Unauthorized, ensure_authorized_to, "create", category)
        assert_not_raises(Exception, ensure_authorized_to, "read", category)
        assert_not_raises(Exception, ensure_authorized_to, "read", Category)
        assert_raises(Unauthorized, ensure_authorized_to, "update", category)
        assert_raises(Unauthorized, ensure_authorized_to, "delete", category)
示例#19
0
    def test_authenticated_user_can_crud(self):
        """Test authenticated users cannot crud categories"""
        category = CategoryFactory.build()

        assert_raises(Forbidden, ensure_authorized_to, "create", category)
        assert_not_raises(Exception, ensure_authorized_to, "read", category)
        assert_not_raises(Exception, ensure_authorized_to, "read", Category)
        assert_raises(Forbidden, ensure_authorized_to, "update", category)
        assert_raises(Forbidden, ensure_authorized_to, "delete", category)
示例#20
0
    def test_admin_can_crud(self):
        """Test admin user can crud categories"""
        category = CategoryFactory.build()

        assert_not_raises(Forbidden, ensure_authorized_to, "create", category)
        assert_not_raises(Forbidden, ensure_authorized_to, "read", category)
        assert_not_raises(Forbidden, ensure_authorized_to, "read", Category)
        assert_not_raises(Forbidden, ensure_authorized_to, "update", category)
        assert_not_raises(Forbidden, ensure_authorized_to, "delete", category)
示例#21
0
 def test_blogpost_public_json(self):
     """Test CATEGORY to_public_json method works with extra fields."""
     cat = CategoryFactory.create()
     cat.info = {'secret': 'mysecret', 'public': 'hello'}
     err_msg = "There should be info keys"
     with patch.dict(self.flask_app.config, {'CATEGORY_INFO_PUBLIC_FIELDS': ['public']}):
         json = cat.to_public_json()
         assert json['info'].keys().sort() == Category().public_info_keys().sort(), err_msg
         assert 'public' in json['info'].keys()
         assert 'secret' not in json['info'].keys()
示例#22
0
 def test_project_put_invalid_short_name(self):
     """Test API project PUT returns error if short_name is invalid (i.e. is
         a name used by the Flask app as a URL endpoint"""
     user = UserFactory.create()
     CategoryFactory.create()
     project = ProjectFactory.create(owner=user)
     name = u'XXXX Project'
     data = {'short_name': 'new'}
     datajson = json.dumps(data)
     res = self.app.put('/api/project/%s?api_key=%s' % (project.id, user.api_key),
                         data=datajson)
     error = json.loads(res.data)
     assert res.status_code == 415, res.status_code
     assert error['status'] == 'failed', error
     assert error['action'] == 'PUT', error
     assert error['target'] == 'project', error
     assert error['exception_cls'] == 'ValueError', error
     message = "Project short_name is not valid, as it's used by the system."
     assert error['exception_msg'] == message, error
示例#23
0
    def test_get_used_returns_requiered_fields(self):
        used_category = CategoryFactory.create()
        ProjectFactory.create(category=used_category)
        fields = ("id", "name", "short_name", "description")

        used_categories = cached_categories.get_used()

        for field in fields:
            assert field in used_categories[0].keys()
        assert len(fields) == len(used_categories[0].keys())
    def test_update_category(self):
        """Test update_category persists the changes made to the category"""

        category = CategoryFactory.create(description='this is a category')
        category.description = 'the description has changed'

        self.project_repo.update_category(category)
        updated_category = self.project_repo.get_category(category.id)

        assert updated_category.description == 'the description has changed', updated_category
    def get_all_returns_list_of_all_categories(self):
        """Test get_all_categories returns a list of all the existing categories"""

        categories = CategoryFactory.create_batch(3)

        retrieved_categories = self.project_repo.get_all_categories()

        assert isinstance(retrieved_categories, list)
        assert len(retrieved_categories) == len(categories), retrieved_categories
        for category in retrieved_categories:
            assert category in categories, category
示例#26
0
 def test_project_cache_post_is_refreshed(self, caching_mock):
     """Test API project cache is updated after POST."""
     clean_project_mock = MagicMock()
     caching_mock.get.return_value = dict(refresh=clean_project_mock)
     owner = UserFactory.create()
     category = CategoryFactory.create()
     url = '/api/project?api_key=%s' % owner.api_key
     payload = dict(name='foo', short_name='foo', description='foo')
     res = self.app.post(url, data=json.dumps(payload))
     print res.data
     project_id = json.loads(res.data)['id']
     clean_project_mock.assert_called_with(project_id), res.data
示例#27
0
    def test_project_post_with_reserved_fields_returns_error(self):
        user = UserFactory.create()
        CategoryFactory.create()
        data = dict(
            name='name',
            short_name='name',
            description='description',
            owner_id=user.id,
            long_description=u'Long Description\n================',
            info={},
            id=222,
            created='today',
            updated='now',
            contacted=False,
            completed=False)
        data = json.dumps(data)
        res = self.app.post('/api/project?api_key=' + user.api_key, data=data)

        assert res.status_code == 400, res.status_code
        error = json.loads(res.data)
        assert error['exception_msg'] == "Reserved keys in payload", error
示例#28
0
 def test_project_post_invalid_short_name(self):
     """Test API project POST returns error if short_name is invalid (i.e. is
         a name used by the Flask app as a URL endpoint"""
     users = UserFactory.create_batch(2)
     CategoryFactory.create()
     name = u'XXXX Project'
     data = dict(
         name=name,
         short_name='new',
         description='description',
         owner_id=1,
         long_description=u'Long Description\n================')
     data = json.dumps(data)
     res = self.app.post('/api/project?api_key=' + users[1].api_key,
                         data=data)
     error = json.loads(res.data)
     assert res.status_code == 415, res.status_code
     assert error['status'] == 'failed', error
     assert error['action'] == 'POST', error
     assert error['target'] == 'project', error
     assert error['exception_cls'] == 'ValueError', error
     message = "Project short_name is not valid, as it's used by the system."
     assert error['exception_msg'] == message, error
示例#29
0
 def test_project_filter_by_category_works(self):
     """Test API project filter by category works."""
     category = CategoryFactory.create()
     projects_published = ProjectFactory.create_batch(2,
                                                      published=True,
                                                      category=category)
     projects_not_published = ProjectFactory.create_batch(2,
                                                          published=False,
                                                          category=category)
     res = self.app.get('/api/project?category_id=%s' % category.id)
     data = json.loads(res.data)
     assert len(data) == 2, data
     assert data[0]['id'] == projects_published[0].id
     assert data[1]['id'] == projects_published[1].id
示例#30
0
 def test_index_based_sorting_with_two_parents(self):
     """Tests index based category sorting with two parents"""
     parent_2 = CategoryFactory.create(name="parent 2", index=1)
     sub_1a = CategoryFactory.create(name="sub 1a", index=2,
         parent=parent_2)
     sub_2a = CategoryFactory.create(name="sub 2a", index=4,
         parent=parent_2)
     sub_3a = CategoryFactory.create(name="sub 3a", index=3,
         parent=parent_2)
     parent_1 = CategoryFactory.create(name="parent 1", index=10)
     sub_1 = CategoryFactory.create(name="sub 1", index=13,
         parent=parent_1)
     sub_2 = CategoryFactory.create(name="sub 2", index=12,
         parent=parent_1)
     sub_3 = CategoryFactory.create(name="sub 3", index=11,
         parent=parent_1)
     categories = Category.objects.all()
     indexes = []
     for category in categories:
         indexes.append(category.index)
     # first two categories should be the parents in the right order
     self.assertEquals(categories[0], parent_2)
     self.assertEquals(categories[1], parent_1)
示例#31
0
 def test_project_creation_unauthorised_as_anon(self):
     """Test that a project is unauthorised for anonymous users."""
     category = CategoryFactory()
     endpoint = '/lc/projects/{}/new'.format(category.short_name)
     res = self.app_post_json(endpoint)
     assert_equal(res.status_code, 401)
示例#32
0
 def setUp(self):
     super(TestOnesignal, self).setUp()
     CategoryFactory.create()
示例#33
0
 def setUp(self):
     super(TestJsonProject, self).setUp()
     CategoryFactory.create()
示例#34
0
    def test_project_post(self):
        """Test API project creation and auth"""
        users = UserFactory.create_batch(2)
        CategoryFactory.create()
        name = u'XXXX Project'
        data = dict(name=name,
                    short_name='xxxx-project',
                    description='description',
                    owner_id=1,
                    long_description=u'Long Description\n================')
        data = json.dumps(data)
        # no api-key
        res = self.app.post('/api/project', data=data)
        assert_equal(res.status, '401 UNAUTHORIZED',
                     'Should not be allowed to create')
        # now a real user
        res = self.app.post('/api/project?api_key=' + users[1].api_key,
                            data=data)
        out = project_repo.get_by(name=name)
        assert out, out
        assert_equal(out.short_name, 'xxxx-project'), out
        assert_equal(out.owner.name, 'user2')
        id_ = out.id

        # now a real user with headers auth
        headers = [('Authorization', users[1].api_key)]
        new_project = dict(
            name=name + '2',
            short_name='xxxx-project2',
            description='description2',
            owner_id=1,
            long_description=u'Long Description\n================')
        new_project = json.dumps(new_project)
        res = self.app.post('/api/project', headers=headers, data=new_project)
        out = project_repo.get_by(name=name + '2')
        assert out, out
        assert_equal(out.short_name, 'xxxx-project2'), out
        assert_equal(out.owner.name, 'user2')
        ## Test that a default category is assigned to the project
        assert out.category_id, "No category assigned to project"
        id_ = out.id

        # test re-create should fail
        res = self.app.post('/api/project?api_key=' + users[1].api_key,
                            data=data)
        err = json.loads(res.data)
        assert res.status_code == 415, err
        assert err['status'] == 'failed', err
        assert err['action'] == 'POST', err
        assert err['exception_cls'] == "DBIntegrityError", err

        # test create with non-allowed fields should fail
        data = dict(name='fail', short_name='fail', link='hateoas', wrong=15)
        res = self.app.post('/api/project?api_key=' + users[1].api_key,
                            data=data)
        err = json.loads(res.data)
        err_msg = "ValueError exception should be raised"
        assert res.status_code == 415, err
        assert err['action'] == 'POST', err
        assert err['status'] == 'failed', err
        assert err['exception_cls'] == "ValueError", err_msg
        # Now with a JSON object but not valid
        data = json.dumps(data)
        res = self.app.post('/api/project?api_key=' + users[1].api_key,
                            data=data)
        err = json.loads(res.data)
        err_msg = "TypeError exception should be raised"
        assert err['action'] == 'POST', err_msg
        assert err['status'] == 'failed', err_msg
        assert err['exception_cls'] == "TypeError", err_msg
        assert res.status_code == 415, err_msg

        # test update
        data = {'name': 'My New Title', 'links': 'hateoas'}
        data = dict(name='My New Title', links='hateoas', info={})
        datajson = json.dumps(data)
        ## anonymous
        res = self.app.put('/api/project/%s' % id_, data=datajson)
        error_msg = 'Anonymous should not be allowed to update'
        assert_equal(res.status, '401 UNAUTHORIZED', error_msg)
        error = json.loads(res.data)
        assert error['status'] == 'failed', error
        assert error['action'] == 'PUT', error
        assert error['exception_cls'] == 'Unauthorized', error

        ### real user but not allowed as not owner!
        non_owner = UserFactory.create()
        url = '/api/project/%s?api_key=%s' % (id_, non_owner.api_key)
        res = self.app.put(url, data=datajson)
        error_msg = 'Should not be able to update projects of others'
        assert_equal(res.status, '403 FORBIDDEN', error_msg)
        error = json.loads(res.data)
        assert error['status'] == 'failed', error
        assert error['action'] == 'PUT', error
        assert error['exception_cls'] == 'Forbidden', error

        res = self.app.put('/api/project/%s?api_key=%s' %
                           (id_, users[1].api_key),
                           data=datajson)

        # with hateoas links
        assert_equal(res.status, '200 OK', res.data)
        out2 = project_repo.get(id_)
        assert_equal(out2.name, data['name'])
        out = json.loads(res.data)
        assert out.get('status') is None, error
        assert out.get('id') == id_, error

        # without hateoas links
        del data['links']
        newdata = json.dumps(data)
        res = self.app.put('/api/project/%s?api_key=%s' %
                           (id_, users[1].api_key),
                           data=newdata)

        assert_equal(res.status, '200 OK', res.data)
        out2 = project_repo.get(id_)
        assert_equal(out2.name, data['name'])
        out = json.loads(res.data)
        assert out.get('status') is None, error
        assert out.get('id') == id_, error
        assert 'task_presenter' not in out.get('info').keys(), error

        data['info']['task_presenter'] = 'htmlpresenter'
        newdata = json.dumps(data)
        res = self.app.put('/api/project/%s?api_key=%s' %
                           (id_, users[1].api_key),
                           data=newdata)

        assert_equal(res.status, '200 OK', res.data)
        out2 = project_repo.get(id_)
        assert_equal(out2.name, data['name'])
        out = json.loads(res.data)
        assert out.get('status') is None, error
        assert out.get('id') == id_, error

        # With wrong id
        res = self.app.put('/api/project/5000?api_key=%s' % users[1].api_key,
                           data=datajson)
        assert_equal(res.status, '404 NOT FOUND', res.data)
        error = json.loads(res.data)
        assert error['status'] == 'failed', error
        assert error['action'] == 'PUT', error
        assert error['exception_cls'] == 'NotFound', error

        # With fake data
        data['algo'] = 13
        datajson = json.dumps(data)
        res = self.app.put('/api/project/%s?api_key=%s' %
                           (id_, users[1].api_key),
                           data=datajson)
        err = json.loads(res.data)
        assert res.status_code == 415, err
        assert err['status'] == 'failed', err
        assert err['action'] == 'PUT', err
        assert err['exception_cls'] == 'TypeError', err

        # With empty fields
        data.pop('algo')
        data['name'] = None
        datajson = json.dumps(data)
        res = self.app.put('/api/project/%s?api_key=%s' %
                           (id_, users[1].api_key),
                           data=datajson)
        err = json.loads(res.data)
        assert res.status_code == 415, err
        assert err['status'] == 'failed', err
        assert err['action'] == 'PUT', err
        assert err['exception_cls'] == 'DBIntegrityError', err

        data['name'] = ''
        datajson = json.dumps(data)
        res = self.app.put('/api/project/%s?api_key=%s' %
                           (id_, users[1].api_key),
                           data=datajson)
        err = json.loads(res.data)
        assert res.status_code == 415, err
        assert err['status'] == 'failed', err
        assert err['action'] == 'PUT', err
        assert err['exception_cls'] == 'DBIntegrityError', err

        data['name'] = 'something'
        data['short_name'] = ''
        datajson = json.dumps(data)
        res = self.app.put('/api/project/%s?api_key=%s' %
                           (id_, users[1].api_key),
                           data=datajson)
        err = json.loads(res.data)
        assert res.status_code == 415, err
        assert err['status'] == 'failed', err
        assert err['action'] == 'PUT', err
        assert err['exception_cls'] == 'DBIntegrityError', err

        # With not JSON data
        datajson = {'foo': 'bar'}
        res = self.app.put('/api/project/%s?api_key=%s' %
                           (id_, users[1].api_key),
                           data=datajson)
        err = json.loads(res.data)
        assert res.status_code == 415, err
        assert err['status'] == 'failed', err
        assert err['action'] == 'PUT', err
        assert err['exception_cls'] == 'ValueError', err

        # With wrong args in the URL
        data = dict(name=name,
                    short_name='xxxx-project',
                    long_description=u'Long Description\n================')

        datajson = json.dumps(data)
        res = self.app.put('/api/project/%s?api_key=%s&search=select1' %
                           (id_, users[1].api_key),
                           data=datajson)
        err = json.loads(res.data)
        assert res.status_code == 415, err
        assert err['status'] == 'failed', err
        assert err['action'] == 'PUT', err
        assert err['exception_cls'] == 'AttributeError', err

        # test delete
        ## anonymous
        res = self.app.delete('/api/project/%s' % id_, data=data)
        error_msg = 'Anonymous should not be allowed to delete'
        assert_equal(res.status, '401 UNAUTHORIZED', error_msg)
        error = json.loads(res.data)
        assert error['status'] == 'failed', error
        assert error['action'] == 'DELETE', error
        assert error['target'] == 'project', error
        ### real user but not allowed as not owner!
        url = '/api/project/%s?api_key=%s' % (id_, non_owner.api_key)
        res = self.app.delete(url, data=datajson)
        error_msg = 'Should not be able to delete projects of others'
        assert_equal(res.status, '403 FORBIDDEN', error_msg)
        error = json.loads(res.data)
        assert error['status'] == 'failed', error
        assert error['action'] == 'DELETE', error
        assert error['target'] == 'project', error

        url = '/api/project/%s?api_key=%s' % (id_, users[1].api_key)
        res = self.app.delete(url, data=datajson)

        assert_equal(res.status, '204 NO CONTENT', res.data)

        # delete a project that does not exist
        url = '/api/project/5000?api_key=%s' % users[1].api_key
        res = self.app.delete(url, data=datajson)
        error = json.loads(res.data)
        assert res.status_code == 404, error
        assert error['status'] == 'failed', error
        assert error['action'] == 'DELETE', error
        assert error['target'] == 'project', error
        assert error['exception_cls'] == 'NotFound', error

        # delete a project that does not exist
        url = '/api/project/?api_key=%s' % users[1].api_key
        res = self.app.delete(url, data=datajson)
        assert res.status_code == 404, error
示例#35
0
 def test_category_public_attributes(self):
     """Test CATEGORY public attributes works."""
     cat = CategoryFactory.create()
     assert cat.public_attributes().sort() == list(
         cat.dictize().keys()).sort()
示例#36
0
    def test_query_category(self):
        """Test API query for category endpoint works"""
        categories = []
        cat = CategoryFactory.create(name='thinking', short_name='thinking')
        categories.append(cat.dictize())
        # Test for real field
        url = "/api/category"
        res = self.app.get(url + "?short_name=thinking")
        data = json.loads(res.data)
        # Should return one result
        assert len(data) == 1, data
        # Correct result
        assert data[0]['short_name'] == 'thinking', data

        # Valid field but wrong value
        res = self.app.get(url + "?short_name=wrongvalue")
        data = json.loads(res.data)
        assert len(data) == 0, data

        # Multiple fields
        res = self.app.get(url + '?short_name=thinking&name=thinking')
        data = json.loads(res.data)
        # One result
        assert len(data) == 1, data
        # Correct result
        assert data[0]['short_name'] == 'thinking', data
        assert data[0]['name'] == 'thinking', data

        # Limits
        res = self.app.get(url + "?limit=1")
        data = json.loads(res.data)
        for item in data:
            assert item['short_name'] == 'thinking', item
        assert len(data) == 1, data

        # Keyset pagination
        cat = CategoryFactory.create(name='computing', short_name='computing')
        categories.append(cat.dictize())
        res = self.app.get(url)
        data = json.loads(res.data)
        tmp = '?limit=1&last_id=%s' % data[0]['id']
        res = self.app.get(url + tmp)
        data_new = json.loads(res.data)
        assert len(data_new) == 1, data_new
        assert data_new[0]['id'] == data[1]['id']

        # Errors
        res = self.app.get(url + "?something")
        err = json.loads(res.data)
        err_msg = "AttributeError exception should be raised"
        res.status_code == 415, err_msg
        assert res.status_code == 415, err_msg
        assert err['action'] == 'GET', err_msg
        assert err['status'] == 'failed', err_msg
        assert err['exception_cls'] == 'AttributeError', err_msg

        # Desc filter
        url = "/api/category?orderby=wrongattribute"
        res = self.app.get(url)
        data = json.loads(res.data)
        err_msg = "It should be 415."
        assert data['status'] == 'failed', data
        assert data['status_code'] == 415, data
        assert 'has no attribute' in data['exception_msg'], data

        # Desc filter
        url = "/api/category?orderby=id"
        res = self.app.get(url)
        data = json.loads(res.data)
        err_msg = "It should get the last item first."
        categories_by_id = sorted(categories,
                                  key=lambda x: x['id'],
                                  reverse=False)
        for i in range(len(categories)):
            assert categories_by_id[i]['id'] == data[i]['id']

        # Desc filter
        url = "/api/category?orderby=id&desc=true"
        res = self.app.get(url)
        data = json.loads(res.data)
        err_msg = "It should get the last item first."
        categories_by_id = sorted(categories,
                                  key=lambda x: x['id'],
                                  reverse=True)
        for i in range(len(categories)):
            assert categories_by_id[i]['id'] == data[i]['id']
示例#37
0
    def test_get_all_returns_all_categories(self):
        categories = [CategoryFactory.create()]

        assert cached_categories.get_all() == categories
    def test_child_tasks_generated(self, mock_wa_client, requests):
        """Test that child tasks are generated."""
        n_canvases = 3
        n_images = 1
        manifest = self.create_manifest(canvases=n_canvases, images=n_images)
        headers = {'Content-Type': 'application/json'}
        response = FakeResponse(text=json.dumps(manifest),
                                status_code=200,
                                headers=headers,
                                encoding='utf-8')
        requests.get.return_value = response
        anno_fixtures = AnnotationFixtures()
        anno_collection_iri = 'example.org/annotations'
        category = CategoryFactory(
            info={'annotations': {
                'results': anno_collection_iri
            }})
        parent = ProjectFactory(category=category)
        tasks = TaskFactory.create_batch(n_canvases,
                                         project=parent,
                                         n_answers=1)

        # Create some annotations for each parent task
        expected = []
        return_values = []
        for i, task in enumerate(tasks):
            canvas_id = self.canvas_id_base.format(i)
            for j in range(n_images):
                TaskRunFactory.create(task=task)
                img_id = self.img_id_base.format(i, j)

                annotations = [
                    anno_fixtures.create(motivation='tagging',
                                         source=canvas_id),
                    anno_fixtures.create(motivation='describing',
                                         source=canvas_id),
                    anno_fixtures.create(motivation='commenting',
                                         source=canvas_id)
                ]

                result = self.result_repo.get_by(task_id=task.id)
                result.info = dict(annotations=anno_collection_iri)
                self.result_repo.update(result)
                return_values.append(annotations)

                # Store expected task data to check later
                link_query = '?manifest={}#?cv={}'.format(self.manifest_uri, i)
                link = 'http://universalviewer.io/uv.html' + link_query
                for anno in annotations[:2]:
                    expected.append({
                        'manifest':
                        self.manifest_uri,
                        'target':
                        anno['target'],
                        'link':
                        link,
                        'tileSource':
                        '{}/info.json'.format(img_id),
                        'url':
                        '{}/full/max/0/default.jpg'.format(img_id),
                        'url_m':
                        '{}/full/240,/0/default.jpg'.format(img_id),
                        'url_b':
                        '{}/full/1024,/0/default.jpg'.format(img_id),
                        'parent_task_id':
                        task.id
                    })

        mock_wa_client.search_annotations.side_effect = return_values
        importer = BulkTaskIIIFEnhancedImporter(manifest_uri=self.manifest_uri,
                                                parent_id=parent.id)
        tasks = importer.tasks()
        task_info = [task['info'] for task in tasks]
        expected = sorted(expected, key=lambda x: x['target'])
        assert_equal(task_info, expected)