コード例 #1
0
    def test_delete_helpingmaterial(self, mock_delete):
        """Test API HelpingMaterialpost delete post (DEL)."""
        mock_delete.return_value = True
        admin, owner, user = UserFactory.create_batch(3)
        project = ProjectFactory.create(owner=owner)
        file_info = {'file_name': 'name.jpg', 'container': 'user_3'}
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id,
                                                        info=file_info)
        helpingmaterial2 = HelpingMaterialFactory.create(project_id=project.id)

        # As anon
        url = '/api/helpingmaterial/%s' % helpingmaterial.id
        res = self.app.delete(url)
        assert res.status_code == 401, res.status_code

        # As user
        url = '/api/helpingmaterial/%s?api_key=%s' % (helpingmaterial.id, user.api_key)
        res = self.app.delete(url)
        assert res.status_code == 403, res.status_code

        # As owner
        url = '/api/helpingmaterial/%s?api_key=%s' % (helpingmaterial.id, owner.api_key)
        res = self.app.delete(url)
        assert res.status_code == 204, res.status_code
        mock_delete.assert_called_with(file_info['file_name'],
                                       file_info['container'])

        # As admin
        url = '/api/helpingmaterial/%s?api_key=%s' % (helpingmaterial2.id, admin.api_key)
        res = self.app.delete(url)
        assert res.status_code == 204, res.status_code
コード例 #2
0
    def test_delete_helpingmaterial(self, mock_delete):
        """Test API HelpingMaterialpost delete post (DEL)."""
        mock_delete.return_value = True
        admin, owner, user = UserFactory.create_batch(3)
        project = ProjectFactory.create(owner=owner)
        file_info = {'file_name': 'name.jpg', 'container': 'user_3'}
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id,
                                                        info=file_info)
        helpingmaterial2 = HelpingMaterialFactory.create(project_id=project.id)

        # As anon
        url = '/api/helpingmaterial/%s' % helpingmaterial.id
        res = self.app.delete(url)
        assert res.status_code == 401, res.status_code

        # As user
        url = '/api/helpingmaterial/%s?api_key=%s' % (helpingmaterial.id, user.api_key)
        res = self.app.delete(url)
        assert res.status_code == 403, res.status_code

        # As owner
        url = '/api/helpingmaterial/%s?api_key=%s' % (helpingmaterial.id, owner.api_key)
        res = self.app.delete(url)
        assert res.status_code == 204, res.status_code
        mock_delete.assert_called_with(file_info['file_name'],
                                       file_info['container'])

        # As admin
        url = '/api/helpingmaterial/%s?api_key=%s' % (helpingmaterial2.id, admin.api_key)
        res = self.app.delete(url)
        assert res.status_code == 204, res.status_code
コード例 #3
0
    def test_get_by_returns_none_if_no_helpingmaterial(self):
        """Test get_by returns None if no helpingmaterial matches the query"""

        HelpingMaterialFactory.create()

        helpingmaterial = self.helping_repo.get_by(project_id=10000)

        assert helpingmaterial is None, helpingmaterial
コード例 #4
0
    def test_get_by_returns_none_if_no_helpingmaterial(self):
        """Test get_by returns None if no helpingmaterial matches the query"""

        HelpingMaterialFactory.create()

        helpingmaterial = self.helping_repo.get_by(project_id=10000)

        assert helpingmaterial is None, helpingmaterial
コード例 #5
0
    def test_filter_by_no_matches(self):
        """Test filter_by returns an empty list if no helpingmaterials match the query"""

        HelpingMaterialFactory.create()

        retrieved_helpingmaterials = self.helping_repo.filter_by(project_id=100)

        assert isinstance(retrieved_helpingmaterials, list)
        assert len(retrieved_helpingmaterials) == 0, retrieved_helpingmaterials
コード例 #6
0
    def test_filter_by_no_matches(self):
        """Test filter_by returns an empty list if no helpingmaterials match the query"""

        HelpingMaterialFactory.create()

        retrieved_helpingmaterials = self.helping_repo.filter_by(
            project_id=100)

        assert isinstance(retrieved_helpingmaterials, list)
        assert len(retrieved_helpingmaterials) == 0, retrieved_helpingmaterials
コード例 #7
0
    def test_filter_by_multiple_conditions(self):
        """Test filter_by supports multiple-condition queries"""

        h1 = HelpingMaterialFactory.create(media_url='url')
        h2 = HelpingMaterialFactory.create(media_url='url')

        retrieved_helpingmaterials = self.helping_repo.filter_by(project_id=h2.project_id,
                                                                 media_url='url')

        assert len(retrieved_helpingmaterials) == 1, retrieved_helpingmaterials
        assert h2 in retrieved_helpingmaterials, retrieved_helpingmaterials
コード例 #8
0
    def test_filter_by_multiple_conditions(self):
        """Test filter_by supports multiple-condition queries"""

        h1 = HelpingMaterialFactory.create(media_url='url')
        h2 = HelpingMaterialFactory.create(media_url='url')

        retrieved_helpingmaterials = self.helping_repo.filter_by(
            project_id=h2.project_id, media_url='url')

        assert len(retrieved_helpingmaterials) == 1, retrieved_helpingmaterials
        assert h2 in retrieved_helpingmaterials, retrieved_helpingmaterials
コード例 #9
0
    def test_anonymous_user_read_given_helpingmaterial(self):
        """Test anonymous users can read a given helpingmaterial"""

        project = ProjectFactory.create(published=True)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert_not_raises(Exception, ensure_authorized_to,
                          'read', helpingmaterial)
コード例 #10
0
    def test_get_returns_helpingmaterial(self):
        """Test get method returns a helpingmaterial if exists"""

        helpingmaterial = HelpingMaterialFactory.create()

        retrieved_helpingmaterial = self.helping_repo.get(helpingmaterial.id)

        assert helpingmaterial == retrieved_helpingmaterial, retrieved_helpingmaterial
コード例 #11
0
    def test_anonymous_user_delete_helpingmaterial(self):
        """Test anonymous users cannot delete helpingmaterials"""

        project = ProjectFactory.create(published=True)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert_raises(Unauthorized, ensure_authorized_to, 'delete',
                      helpingmaterial)
コード例 #12
0
    def test_anonymous_user_read_given_helpingmaterial(self):
        """Test anonymous users can read a given helpingmaterial"""

        project = ProjectFactory.create(published=True)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert_not_raises(Exception, ensure_authorized_to, 'read',
                          helpingmaterial)
コード例 #13
0
    def test_update_fails_if_integrity_error(self):
        """Test update raises a DBIntegrityError if the instance to be updated
        lacks a required value"""

        helpingmaterial = HelpingMaterialFactory.create()
        helpingmaterial.project_id = None

        assert_raises(DBIntegrityError, self.helping_repo.update, helpingmaterial)
コード例 #14
0
    def test_get_by(self):
        """Test get_by returns a helpingmaterial with the specified attribute"""

        helpingmaterial = HelpingMaterialFactory.create(media_url="algo")

        retrieved_helpingmaterial = self.helping_repo.get_by(media_url="algo")

        assert helpingmaterial == retrieved_helpingmaterial, retrieved_helpingmaterial
コード例 #15
0
    def test_anonymous_user_delete_helpingmaterial(self):
        """Test anonymous users cannot delete helpingmaterials"""

        project = ProjectFactory.create(published=True)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert_raises(Unauthorized, ensure_authorized_to, 'delete',
                      helpingmaterial)
コード例 #16
0
    def test_get_returns_helpingmaterial(self):
        """Test get method returns a helpingmaterial if exists"""

        helpingmaterial = HelpingMaterialFactory.create()

        retrieved_helpingmaterial = self.helping_repo.get(helpingmaterial.id)

        assert helpingmaterial == retrieved_helpingmaterial, retrieved_helpingmaterial
コード例 #17
0
    def test_get_by(self):
        """Test get_by returns a helpingmaterial with the specified attribute"""

        helpingmaterial = HelpingMaterialFactory.create(media_url="algo")

        retrieved_helpingmaterial = self.helping_repo.get_by(media_url="algo")

        assert helpingmaterial == retrieved_helpingmaterial, retrieved_helpingmaterial
コード例 #18
0
    def test_update_fails_if_integrity_error(self):
        """Test update raises a DBIntegrityError if the instance to be updated
        lacks a required value"""

        helpingmaterial = HelpingMaterialFactory.create()
        helpingmaterial.project_id = None

        assert_raises(DBIntegrityError, self.helping_repo.update,
                      helpingmaterial)
コード例 #19
0
    def test_delete(self):
        """Test delete removes the helpingmaterial instance"""

        helpingmaterial = HelpingMaterialFactory.create()

        self.helping_repo.delete(helpingmaterial)
        deleted = self.helping_repo.get(helpingmaterial.id)

        assert deleted is None, deleted
コード例 #20
0
    def test_anonymous_user_read_given_helpingmaterial_draft_project(self):
        """Test anonymous users cannot read a given helpingmaterial of
        a draft project"""

        project = ProjectFactory.create(published=False)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert_raises(Unauthorized, ensure_authorized_to,
                      'read', helpingmaterial)
コード例 #21
0
    def test_non_owner_authenticated_user_read_given_helpingmaterial_draft_project(self):
        """Test authenticated user cannot read a given helpingmaterial of a
        draft project if is not the project owner"""

        project = ProjectFactory.create(published=False)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert self.mock_authenticated.id != project.owner.id
        assert_raises(Forbidden, ensure_authorized_to, 'read', helpingmaterial)
コード例 #22
0
    def test_anonymous_user_read_given_helpingmaterial_draft_project(self):
        """Test anonymous users cannot read a given helpingmaterial of
        a draft project"""

        project = ProjectFactory.create(published=False)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert_raises(Unauthorized, ensure_authorized_to, 'read',
                      helpingmaterial)
コード例 #23
0
    def test_delete(self):
        """Test delete removes the helpingmaterial instance"""

        helpingmaterial = HelpingMaterialFactory.create()

        self.helping_repo.delete(helpingmaterial)
        deleted = self.helping_repo.get(helpingmaterial.id)

        assert deleted is None, deleted
コード例 #24
0
    def test_non_owner_authenticated_user_read_given_helpingmaterial(self):
        """Test authenticated user can read a given helpingmaterial
        if is not the project owner"""

        project = ProjectFactory.create(published=True)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert self.mock_authenticated.id != project.owner.id
        assert_not_raises(Exception, ensure_authorized_to, 'read',
                          helpingmaterial)
コード例 #25
0
    def test_admin_read_given_helpingmaterial_draft_project(self):
        """Test admin can read a given helpingmaterial of a draft project"""

        owner = UserFactory.create(id=5)
        project = ProjectFactory.create(owner=owner, published=False)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert self.mock_admin.id != project.owner.id
        assert_not_raises(Exception, ensure_authorized_to, 'read',
                          helpingmaterial)
コード例 #26
0
    def test_owner_update_helpingmaterial(self):
        """Test admins can update helpingmaterial
        even if it's not the post owner"""

        owner = UserFactory.create(id=5)
        project = ProjectFactory.create(owner=owner)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert_not_raises(Exception, ensure_authorized_to, 'update',
                          helpingmaterial)
コード例 #27
0
    def test_non_owner_authenticated_user_read_given_helpingmaterial(self):
        """Test authenticated user can read a given helpingmaterial
        if is not the project owner"""

        project = ProjectFactory.create(published=True)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert self.mock_authenticated.id != project.owner.id
        assert_not_raises(Exception, ensure_authorized_to,
                          'read', helpingmaterial)
コード例 #28
0
    def test_non_owner_authenticated_user_read_given_helpingmaterial_draft_project(
            self):
        """Test authenticated user cannot read a given helpingmaterial of a
        draft project if is not the project owner"""

        project = ProjectFactory.create(published=False)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert self.mock_authenticated.id != project.owner.id
        assert_raises(Forbidden, ensure_authorized_to, 'read', helpingmaterial)
コード例 #29
0
    def test_admin_read_given_helpingmaterial_draft_project(self):
        """Test admin can read a given helpingmaterial of a draft project"""

        owner = UserFactory.create(id=5)
        project = ProjectFactory.create(owner=owner, published=False)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert self.mock_admin.id != project.owner.id
        assert_not_raises(Exception, ensure_authorized_to, 'read',
                          helpingmaterial)
コード例 #30
0
    def test_owner_update_helpingmaterial(self):
        """Test admins can update helpingmaterial
        even if it's not the post owner"""

        owner = UserFactory.create(id=5)
        project = ProjectFactory.create(owner=owner)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert_not_raises(Exception, ensure_authorized_to, 'update',
                          helpingmaterial)
コード例 #31
0
    def test_update_helpingmaterial(self):
        """Test API HelpingMaterialpost update post (PUT)."""
        admin, user, owner = UserFactory.create_batch(3)
        project = ProjectFactory.create(owner=owner)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        # As anon
        helpingmaterial.media_url = 'new'
        url = '/api/helpingmaterial/%s' % helpingmaterial.id
        res = self.app.put(url, data=json.dumps(helpingmaterial.dictize()))
        data = json.loads(res.data)
        assert res.status_code == 401, res.status_code

        # As user
        url = '/api/helpingmaterial/%s?api_key=%s' % (helpingmaterial.id,
                                                      user.api_key)
        res = self.app.put(url, data=json.dumps(helpingmaterial.dictize()))
        data = json.loads(res.data)
        assert res.status_code == 403, res.status_code

        # As owner
        url = '/api/helpingmaterial/%s?api_key=%s' % (helpingmaterial.id,
                                                      owner.api_key)
        payload = helpingmaterial.dictize()
        del payload['created']
        del payload['id']
        res = self.app.put(url, data=json.dumps(payload))
        data = json.loads(res.data)
        assert res.status_code == 200, res.status_code
        assert data['media_url'] == 'new', data

        # as owner with reserved key
        helpingmaterial.media_url = 'new'
        helpingmaterial.created = 'today'
        url = '/api/helpingmaterial/%s?api_key=%s' % (helpingmaterial.id,
                                                      owner.api_key)
        payload = helpingmaterial.dictize()
        del payload['id']
        res = self.app.put(url, data=json.dumps(payload))
        data = json.loads(res.data)
        assert res.status_code == 400, res.status_code
        assert data['exception_msg'] == 'Reserved keys in payload', data

        # as owner with wrong key
        helpingmaterial.media_url = 'new admin'
        url = '/api/helpingmaterial/%s?api_key=%s' % (helpingmaterial.id,
                                                      owner.api_key)
        payload = helpingmaterial.dictize()
        del payload['created']
        del payload['id']
        payload['foo'] = 'bar'
        res = self.app.put(url, data=json.dumps(payload))
        data = json.loads(res.data)
        assert res.status_code == 415, res.status_code
        assert 'foo' in data['exception_msg'], data
コード例 #32
0
    def test_owner_delete_helpingmaterial(self):
        """Test authenticated user can delete a helpingmaterial if is the post
        owner"""

        owner = UserFactory.create(id=2)
        project = ProjectFactory.create(owner=owner)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert self.mock_authenticated.id == owner.id
        assert_not_raises(Exception, ensure_authorized_to, 'delete',
                          helpingmaterial)
コード例 #33
0
    def test_owner_read_given_helpingmaterial_draft_project(self):
        """Test authenticated user can read a given helpingmaterial
        of a draft project if it's the project owner"""

        owner = UserFactory.create(id=2)
        project = ProjectFactory.create(owner=owner, published=False)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert self.mock_authenticated.id == project.owner.id
        assert_not_raises(Exception, ensure_authorized_to, 'read',
                          helpingmaterial)
コード例 #34
0
    def test_admin_authenticated_user_delete_helpingmaterial(self):
        """Test authenticated user can delete any helpingmaterial if
        it's admin"""

        owner = UserFactory.create(id=5)
        project = ProjectFactory.create(owner=owner)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert self.mock_admin.id != owner.id
        assert_not_raises(Exception, ensure_authorized_to, 'delete',
                          helpingmaterial)
コード例 #35
0
    def test_admin_authenticated_user_delete_helpingmaterial(self):
        """Test authenticated user can delete any helpingmaterial if
        it's admin"""

        owner = UserFactory.create(id=5)
        project = ProjectFactory.create(owner=owner)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert self.mock_admin.id != owner.id
        assert_not_raises(Exception, ensure_authorized_to, 'delete',
                          helpingmaterial)
コード例 #36
0
    def test_owner_delete_helpingmaterial(self):
        """Test authenticated user can delete a helpingmaterial if is the post
        owner"""

        owner = UserFactory.create(id=2)
        project = ProjectFactory.create(owner=owner)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert self.mock_authenticated.id == owner.id
        assert_not_raises(Exception, ensure_authorized_to, 'delete',
                          helpingmaterial)
コード例 #37
0
    def test_owner_read_given_helpingmaterial_draft_project(self):
        """Test authenticated user can read a given helpingmaterial
        of a draft project if it's the project owner"""

        owner = UserFactory.create(id=2)
        project = ProjectFactory.create(owner=owner, published=False)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert self.mock_authenticated.id == project.owner.id
        assert_not_raises(Exception, ensure_authorized_to, 'read',
                          helpingmaterial)
コード例 #38
0
    def test_filter_by_one_condition(self):
        """Test filter_by returns a list of helpingmaterials that meet the filtering
        condition"""

        HelpingMaterialFactory.create_batch(3, media_url="algo")
        should_be_missing = HelpingMaterialFactory.create(media_url="new")

        retrieved_helpingmaterials = self.helping_repo.filter_by(media_url="algo")

        assert len(retrieved_helpingmaterials) == 3, retrieved_helpingmaterials
        assert should_be_missing not in retrieved_helpingmaterials, retrieved_helpingmaterials
コード例 #39
0
    def test_update(self):
        """Test update persists the changes made to the helpingmaterial"""

        info = {'key': 'val'}
        helpingmaterial = HelpingMaterialFactory.create(info=info)
        info_new = {'f': 'v'}
        helpingmaterial.info = info_new

        self.helping_repo.update(helpingmaterial)
        updated_helpingmaterial = self.helping_repo.get(helpingmaterial.id)

        assert updated_helpingmaterial.info == info_new, updated_helpingmaterial
コード例 #40
0
    def test_non_owner_authenticated_user_delete_helpingmaterial(self):
        """Test authenticated user cannot delete a helpingmaterial if is not the post
        owner and is not admin"""

        owner = UserFactory.create(id=5)
        project = ProjectFactory.create(owner=owner, published=True)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert self.mock_authenticated.id != owner.id
        assert not self.mock_authenticated.admin
        assert_raises(Forbidden, ensure_authorized_to, 'delete',
                      helpingmaterial)
コード例 #41
0
    def test_filter_by_one_condition(self):
        """Test filter_by returns a list of helpingmaterials that meet the filtering
        condition"""

        HelpingMaterialFactory.create_batch(3, media_url="algo")
        should_be_missing = HelpingMaterialFactory.create(media_url="new")

        retrieved_helpingmaterials = self.helping_repo.filter_by(
            media_url="algo")

        assert len(retrieved_helpingmaterials) == 3, retrieved_helpingmaterials
        assert should_be_missing not in retrieved_helpingmaterials, retrieved_helpingmaterials
コード例 #42
0
    def test_non_owner_authenticated_user_delete_helpingmaterial(self):
        """Test authenticated user cannot delete a helpingmaterial if is not the post
        owner and is not admin"""

        owner = UserFactory.create(id=5)
        project = ProjectFactory.create(owner=owner, published=True)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        assert self.mock_authenticated.id != owner.id
        assert not self.mock_authenticated.admin
        assert_raises(Forbidden, ensure_authorized_to, 'delete',
                      helpingmaterial)
コード例 #43
0
    def test_update(self):
        """Test update persists the changes made to the helpingmaterial"""

        info = {'key': 'val'}
        helpingmaterial = HelpingMaterialFactory.create(info=info)
        info_new = {'f': 'v'}
        helpingmaterial.info = info_new

        self.helping_repo.update(helpingmaterial)
        updated_helpingmaterial = self.helping_repo.get(helpingmaterial.id)

        assert updated_helpingmaterial.info == info_new, updated_helpingmaterial
コード例 #44
0
    def test_update_helpingmaterial(self):
        """Test API HelpingMaterialpost update post (PUT)."""
        admin, user, owner = UserFactory.create_batch(3)
        project = ProjectFactory.create(owner=owner)
        helpingmaterial = HelpingMaterialFactory.create(project_id=project.id)

        # As anon
        helpingmaterial.media_url = 'new'
        url = '/api/helpingmaterial/%s' % helpingmaterial.id
        res = self.app.put(url, data=json.dumps(helpingmaterial.dictize()))
        data = json.loads(res.data)
        assert res.status_code == 401, res.status_code

        # As user
        url = '/api/helpingmaterial/%s?api_key=%s' % (helpingmaterial.id, user.api_key)
        res = self.app.put(url, data=json.dumps(helpingmaterial.dictize()))
        data = json.loads(res.data)
        assert res.status_code == 403, res.status_code

        # As owner
        url = '/api/helpingmaterial/%s?api_key=%s' % (helpingmaterial.id, owner.api_key)
        payload = helpingmaterial.dictize()
        del payload['created']
        del payload['id']
        res = self.app.put(url, data=json.dumps(payload))
        data = json.loads(res.data)
        assert res.status_code == 200, res.status_code
        assert data['media_url'] == 'new', data

        # as owner with reserved key
        helpingmaterial.media_url = 'new'
        helpingmaterial.created = 'today'
        url = '/api/helpingmaterial/%s?api_key=%s' % (helpingmaterial.id, owner.api_key)
        payload = helpingmaterial.dictize()
        del payload['id']
        res = self.app.put(url, data=json.dumps(payload))
        data = json.loads(res.data)
        assert res.status_code == 400, res.status_code
        assert data['exception_msg'] == 'Reserved keys in payload',  data

        # as owner with wrong key
        helpingmaterial.media_url = 'new admin'
        url = '/api/helpingmaterial/%s?api_key=%s' % (helpingmaterial.id, owner.api_key)
        payload = helpingmaterial.dictize()
        del payload['created']
        del payload['id']
        payload['foo'] = 'bar'
        res = self.app.put(url, data=json.dumps(payload))
        data = json.loads(res.data)
        assert res.status_code == 415, res.status_code
        assert 'foo' in data['exception_msg'], data
コード例 #45
0
    def test_helpingmaterial_put_file(self):
        """Test API HelpingMaterialpost file put upload works."""
        admin, owner, user = UserFactory.create_batch(3)
        project = ProjectFactory.create(owner=owner)
        project2 = ProjectFactory.create(owner=user)
        hp = HelpingMaterialFactory.create(project_id=project.id)

        img = (io.BytesIO(b'test'), 'test_file.jpg')

        payload = dict(project_id=project.id,
                       file=img)

        # As anon
        url = '/api/helpingmaterial/%s' % hp.id
        res = self.app.put(url, data=payload,
                           content_type="multipart/form-data")
        data = json.loads(res.data)
        assert res.status_code == 401, data
        assert data['status_code'] == 401, data

        # As a user
        img = (io.BytesIO(b'test'), 'test_file.jpg')

        payload = dict(project_id=project.id,
                       file=img)

        url = '/api/helpingmaterial/%s?api_key=%s' % (hp.id, user.api_key)
        res = self.app.put(url, data=payload,
                           content_type="multipart/form-data")
        data = json.loads(res.data)
        assert res.status_code == 403, data
        assert data['status_code'] == 403, data

        # As owner
        img = (io.BytesIO(b'test'), 'test_file.jpg')

        payload = dict(project_id=project.id,
                       file=img)

        url = '/api/helpingmaterial/%s?api_key=%s' % (hp.id,
                                                      project.owner.api_key)
        res = self.app.put(url, data=payload,
                           content_type="multipart/form-data")
        data = json.loads(res.data)
        assert res.status_code == 200, data
        container = "user_%s" % owner.id
        assert data['info']['container'] == container, data
        assert data['info']['file_name'] == 'test_file.jpg', data
        assert 'test_file.jpg' in data['media_url'], data

        # As owner wrong 404 project_id
        img = (io.BytesIO(b'test'), 'test_file.jpg')

        payload = dict(project_id=project.id,
                       file=img)

        url = '/api/helpingmaterial/%s?api_key=%s' % (hp.id, owner.api_key)
        payload['project_id'] = -1
        res = self.app.put(url, data=payload,
                           content_type="multipart/form-data")
        data = json.loads(res.data)
        assert res.status_code == 415, data

        # As owner using wrong project_id
        img = (io.BytesIO(b'test'), 'test_file.jpg')

        payload = dict(project_id=project.id,
                       file=img)

        url = '/api/helpingmaterial/%s?api_key=%s' % (hp.id, owner.api_key)
        payload['project_id'] = project2.id
        res = self.app.put(url, data=payload,
                           content_type="multipart/form-data")
        data = json.loads(res.data)
        assert res.status_code == 403, data

        # As owner using wrong attribute
        img = (io.BytesIO(b'test'), 'test_file.jpg')

        payload = dict(project_id=project.id,
                       wrong=img)

        url = '/api/helpingmaterial/%s?api_key=%s' % (hp.id, owner.api_key)
        res = self.app.put(url, data=payload,
                           content_type="multipart/form-data")
        data = json.loads(res.data)
        assert res.status_code == 415, data

        # As owner using reserved key 
        img = (io.BytesIO(b'test'), 'test_file.jpg')

        payload = dict(project_id=project.id,
                       file=img)

        url = '/api/helpingmaterial/%s?api_key=%s' % (hp.id, owner.api_key)
        payload['project_id'] = project.id
        payload['id'] = 3
        res = self.app.put(url, data=payload,
                           content_type="multipart/form-data")
        data = json.loads(res.data)
        assert res.status_code == 400, data
        assert data['exception_msg'] == 'Reserved keys in payload', data
コード例 #46
0
    def test_query_helpingmaterial(self):
        """Test API query for helpingmaterial endpoint works"""
        owner = UserFactory.create()
        user = UserFactory.create()
        project = ProjectFactory(owner=owner)
        helpingmaterials = HelpingMaterialFactory.create_batch(9, project_id=project.id)
        helpingmaterial = HelpingMaterialFactory.create()

        # As anon
        url = '/api/helpingmaterial'
        res = self.app.get(url)
        data = json.loads(res.data)
        assert len(data) == 10, data

        # As user
        res = self.app.get(url + '?api_key=' + user.api_key)
        data = json.loads(res.data)
        assert len(data) == 0, data

        # As owner
        res = self.app.get(url + '?api_key=' + owner.api_key)
        data = json.loads(res.data)
        assert len(data) == 9, data

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

        # Multiple fields
        res = self.app.get(url + '?info=foo::' + helpingmaterials[0].info['foo'] + '&project_id=' + str(project.id))
        data = json.loads(res.data)
        # One result
        assert len(data) == 9, data
        # Correct result
        assert data[0]['project_id'] == helpingmaterials[0].project_id, data
        assert data[0]['media_url'] == helpingmaterials[0].media_url, data

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

        # Keyset pagination
        res = self.app.get(url + '?limit=1&last_id=' + str(helpingmaterials[8].id))
        data = json.loads(res.data)
        assert len(data) == 1, len(data)
        assert data[0]['id'] == helpingmaterial.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/helpingmaterial?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/helpingmaterial?orderby=id"
        res = self.app.get(url)
        data = json.loads(res.data)
        err_msg = "It should get the last item first."
        helpingmaterials.append(helpingmaterial)
        helpingmaterials_by_id = sorted(helpingmaterials, key=lambda x: x.id, reverse=False)
        for i in range(len(helpingmaterials)):
            assert helpingmaterials_by_id[i].id == data[i]['id']

        # Desc filter
        url = "/api/helpingmaterial?orderby=id&desc=true"
        res = self.app.get(url)
        data = json.loads(res.data)
        err_msg = "It should get the last item first."
        helpingmaterials_by_id = sorted(helpingmaterials, key=lambda x: x.id, reverse=True)
        for i in range(len(helpingmaterials)):
            assert helpingmaterials_by_id[i].id == data[i]['id']
コード例 #47
0
 def test_helpingmaterial_public_attributes(self):
     """Test public attributes works."""
     hm = HelpingMaterialFactory.create()
     public_attributes = ['created', 'id', 'info', 'media_url', 'priority']
     assert sorted(hm.public_attributes()) == sorted(public_attributes)
コード例 #48
0
    def test_query_helpingmaterial(self):
        """Test API query for helpingmaterial endpoint works"""
        owner = UserFactory.create()
        user = UserFactory.create()
        project = ProjectFactory(owner=owner)
        helpingmaterials = HelpingMaterialFactory.create_batch(9, project_id=project.id)
        helpingmaterial = HelpingMaterialFactory.create()

        # As anon
        url = '/api/helpingmaterial'
        res = self.app.get(url)
        data = json.loads(res.data)
        assert len(data) == 10, data

        # As user
        res = self.app.get(url + '?api_key=' + user.api_key)
        data = json.loads(res.data)
        assert len(data) == 0, data

        # As owner
        res = self.app.get(url + '?api_key=' + owner.api_key)
        data = json.loads(res.data)
        assert len(data) == 9, data

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

        # Multiple fields
        res = self.app.get(url + '?info=container::' +
                           helpingmaterials[0].info['container'] + '&project_id=' + str(project.id))
        data = json.loads(res.data)
        # One result
        assert len(data) == 9, data
        # Correct result
        assert data[0]['project_id'] == helpingmaterials[0].project_id, data
        assert data[0]['media_url'] == helpingmaterials[0].media_url, data

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

        # Keyset pagination
        res = self.app.get(url + '?limit=1&last_id=' + str(helpingmaterials[8].id))
        data = json.loads(res.data)
        assert len(data) == 1, len(data)
        assert data[0]['id'] == helpingmaterial.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/helpingmaterial?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/helpingmaterial?orderby=id"
        res = self.app.get(url)
        data = json.loads(res.data)
        err_msg = "It should get the last item first."
        helpingmaterials.append(helpingmaterial)
        helpingmaterials_by_id = sorted(helpingmaterials, key=lambda x: x.id, reverse=False)
        for i in range(len(helpingmaterials)):
            assert helpingmaterials_by_id[i].id == data[i]['id']

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

        # Test priority filtering
        helpingmaterials.append(HelpingMaterialFactory.create(priority=1.0,
                                                              project_id=project.id))
        helpingmaterials.append(HelpingMaterialFactory.create(priority=0.5,
                                                              project_id=project.id))
        helpingmaterials.append(HelpingMaterialFactory.create(priority=0.1,
                                                              project_id=project.id))

        url = "/api/helpingmaterial?orderby=priority&desc=true"
        res = self.app.get(url)
        data = json.loads(res.data)
        err_msg = "It should get the last item first."
        helpingmaterials_by_priority = sorted(helpingmaterials, key=lambda x: x.priority, reverse=True)
        for i in range(3):
            assert helpingmaterials_by_priority[i].id == data[i]['id'], (helpingmaterials_by_priority[i].id, data[i]['id'])
コード例 #49
0
    def test_helpingmaterial_put_file(self):
        """Test API HelpingMaterialpost file put upload works."""
        admin, owner, user = UserFactory.create_batch(3)
        project = ProjectFactory.create(owner=owner)
        project2 = ProjectFactory.create(owner=user)
        hp = HelpingMaterialFactory.create(project_id=project.id)

        img = (io.BytesIO(b'test'), 'test_file.jpg')

        payload = dict(project_id=project.id, file=img)

        # As anon
        url = '/api/helpingmaterial/%s' % hp.id
        res = self.app.put(url,
                           data=payload,
                           content_type="multipart/form-data")
        data = json.loads(res.data)
        assert res.status_code == 401, data
        assert data['status_code'] == 401, data

        # As a user
        img = (io.BytesIO(b'test'), 'test_file.jpg')

        payload = dict(project_id=project.id, file=img)

        url = '/api/helpingmaterial/%s?api_key=%s' % (hp.id, user.api_key)
        res = self.app.put(url,
                           data=payload,
                           content_type="multipart/form-data")
        data = json.loads(res.data)
        assert res.status_code == 403, data
        assert data['status_code'] == 403, data

        # As owner
        img = (io.BytesIO(b'test'), 'test_file.jpg')

        payload = dict(project_id=project.id, file=img)

        url = '/api/helpingmaterial/%s?api_key=%s' % (hp.id,
                                                      project.owner.api_key)
        res = self.app.put(url,
                           data=payload,
                           content_type="multipart/form-data")
        data = json.loads(res.data)
        assert res.status_code == 200, data
        container = "user_%s" % owner.id
        assert data['info']['container'] == container, data
        assert data['info']['file_name'] == 'test_file.jpg', data
        assert 'test_file.jpg' in data['media_url'], data

        # As owner wrong 404 project_id
        img = (io.BytesIO(b'test'), 'test_file.jpg')

        payload = dict(project_id=project.id, file=img)

        url = '/api/helpingmaterial/%s?api_key=%s' % (hp.id, owner.api_key)
        payload['project_id'] = -1
        res = self.app.put(url,
                           data=payload,
                           content_type="multipart/form-data")
        data = json.loads(res.data)
        assert res.status_code == 415, data

        # As owner using wrong project_id
        img = (io.BytesIO(b'test'), 'test_file.jpg')

        payload = dict(project_id=project.id, file=img)

        url = '/api/helpingmaterial/%s?api_key=%s' % (hp.id, owner.api_key)
        payload['project_id'] = project2.id
        res = self.app.put(url,
                           data=payload,
                           content_type="multipart/form-data")
        data = json.loads(res.data)
        assert res.status_code == 403, data

        # As owner using wrong attribute
        img = (io.BytesIO(b'test'), 'test_file.jpg')

        payload = dict(project_id=project.id, wrong=img)

        url = '/api/helpingmaterial/%s?api_key=%s' % (hp.id, owner.api_key)
        res = self.app.put(url,
                           data=payload,
                           content_type="multipart/form-data")
        data = json.loads(res.data)
        assert res.status_code == 415, data

        # As owner using reserved key
        img = (io.BytesIO(b'test'), 'test_file.jpg')

        payload = dict(project_id=project.id, file=img)

        url = '/api/helpingmaterial/%s?api_key=%s' % (hp.id, owner.api_key)
        payload['project_id'] = project.id
        payload['id'] = 3
        res = self.app.put(url,
                           data=payload,
                           content_type="multipart/form-data")
        data = json.loads(res.data)
        assert res.status_code == 400, data
        assert data['exception_msg'] == 'Reserved keys in payload', data
コード例 #50
0
 def test_helpingmaterial_public_attributes(self):
     """Test public attributes works."""
     hm = HelpingMaterialFactory.create()
     assert hm.public_attributes().sort() == hm.dictize().keys().sort()