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
Example #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
    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
Example #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
    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
Example #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
    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
    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
Example #9
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
Example #10
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
Example #11
0
    def test_filter_by_limit_offset(self):
        """Test that filter_by supports limit and offset options"""

        HelpingMaterialFactory.create_batch(4)
        all_helpingmaterials = self.helping_repo.filter_by()

        first_two = self.helping_repo.filter_by(limit=2)
        last_two = self.helping_repo.filter_by(limit=2, offset=2)

        assert len(first_two) == 2, first_two
        assert len(last_two) == 2, last_two
        assert first_two == all_helpingmaterials[:2]
        assert last_two == all_helpingmaterials[2:]
    def test_filter_by_limit_offset(self):
        """Test that filter_by supports limit and offset options"""

        HelpingMaterialFactory.create_batch(4)
        all_helpingmaterials = self.helping_repo.filter_by()

        first_two = self.helping_repo.filter_by(limit=2)
        last_two = self.helping_repo.filter_by(limit=2, offset=2)

        assert len(first_two) == 2, first_two
        assert len(last_two) == 2, last_two
        assert first_two == all_helpingmaterials[:2]
        assert last_two == all_helpingmaterials[2:]
Example #13
0
    def test_anonymous_user_create_given_helpingmaterial(self):
        """Test anonymous users cannot create helping materials for a project"""

        project = ProjectFactory.create()
        helping = HelpingMaterialFactory.build(project_id=project.id)

        assert_raises(Unauthorized, ensure_authorized_to, 'create', helping)
    def test_save_fails_if_integrity_error(self):
        """Test save raises a DBIntegrityError if the instance to be saved lacks
        a required value"""

        helpingmaterial = HelpingMaterialFactory.build(project_id=None)

        assert_raises(DBIntegrityError, self.helping_repo.save, helpingmaterial)
    def test_anonymous_user_create_given_helpingmaterial(self):
        """Test anonymous users cannot create helping materials for a project"""

        project = ProjectFactory.create()
        helping = HelpingMaterialFactory.build(project_id=project.id)

        assert_raises(Unauthorized, ensure_authorized_to, 'create', helping)
Example #16
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)
    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)
Example #18
0
    def test_save_fails_if_integrity_error(self):
        """Test save raises a DBIntegrityError if the instance to be saved lacks
        a required value"""

        helpingmaterial = HelpingMaterialFactory.build(project_id=None)

        assert_raises(DBIntegrityError, self.helping_repo.save,
                      helpingmaterial)
    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
    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)
Example #21
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)
    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
    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)
Example #24
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
Example #25
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
Example #26
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)
Example #27
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)
Example #28
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
    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)
Example #30
0
    def test_owner_create_given_helpingmaterial(self):
        """Test authenticated user can create a given helpingmaterial if it's project owner"""

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

        assert self.mock_authenticated.id == project.owner_id
        assert_not_raises(Exception, ensure_authorized_to, 'create', helpingmaterial)
    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
Example #32
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)
Example #33
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)
    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)
    def test_non_owner_authenticated_user_create_given_helpingmaterial(self):
        """Test authenticated user cannot create a given helpingmaterial if is not the
        project owner"""

        project = ProjectFactory.create()
        helpingmaterial = HelpingMaterialFactory.build(project_id=project.id)

        assert self.mock_authenticated.id != project.owner_id
        assert_raises(Forbidden, ensure_authorized_to, 'create',
                      helpingmaterial)
    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)
Example #37
0
    def test_non_owner_authenticated_user_create_given_helpingmaterial(self):
        """Test authenticated user cannot create a given helpingmaterial if is not the
        project owner"""

        project = ProjectFactory.create()
        helpingmaterial = HelpingMaterialFactory.build(project_id=project.id)

        assert self.mock_authenticated.id != project.owner_id
        assert_raises(Forbidden, ensure_authorized_to,
                      'create', helpingmaterial)
Example #38
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)
    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)
    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)
Example #41
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)
    def test_owner_create_given_helpingmaterial(self):
        """Test authenticated user can create a given helpingmaterial if it's project owner"""

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

        assert self.mock_authenticated.id == project.owner_id
        assert_not_raises(Exception, ensure_authorized_to, 'create',
                          helpingmaterial)
    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
Example #44
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)
Example #45
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)
    def test_save(self):
        """Test save persist the helpingmaterial"""

        helpingmaterial = HelpingMaterialFactory.build()
        project = ProjectFactory.create()
        helpingmaterial.project_id = project.id
        assert self.helping_repo.get(helpingmaterial.id) is None

        self.helping_repo.save(helpingmaterial)

        assert self.helping_repo.get(helpingmaterial.id) == helpingmaterial, "Helping material not saved"
Example #47
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)
    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)
    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)
    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)
Example #51
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)
    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)
Example #53
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
    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
Example #55
0
    def test_save(self):
        """Test save persist the helpingmaterial"""

        helpingmaterial = HelpingMaterialFactory.build()
        project = ProjectFactory.create()
        helpingmaterial.project_id = project.id
        assert self.helping_repo.get(helpingmaterial.id) is None

        self.helping_repo.save(helpingmaterial)

        assert self.helping_repo.get(
            helpingmaterial.id
        ) == helpingmaterial, "Helping material not saved"
    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
    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
    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
Example #59
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']
    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'])