Skip to content

Commit

Permalink
Merge pull request #1875 from Scifabric/fix-category_id-post
Browse files Browse the repository at this point in the history
Allow to specify a category_id when creating a project.
  • Loading branch information
teleyinex committed Jul 11, 2018
2 parents 56b0932 + 3e12a8f commit 779414c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
6 changes: 6 additions & 0 deletions pybossa/api/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ class ProjectAPI(APIBase):

def _create_instance_from_request(self, data):
inst = super(ProjectAPI, self)._create_instance_from_request(data)
category_ids = [c.id for c in get_categories()]
default_category = get_categories()[0]
inst.category_id = default_category.id
if 'category_id' in data.keys():
if int(data.get('category_id')) in category_ids:
inst.category_id = data.get('category_id')
else:
raise BadRequest("category_id does not exist")
return inst

def _update_object(self, obj):
Expand Down
43 changes: 40 additions & 3 deletions test/test_api/test_project_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ def test_query_project_with_context(self):
def test_project_post(self):
"""Test API project creation and auth"""
users = UserFactory.create_batch(2)
CategoryFactory.create()
cat1 = CategoryFactory.create()
cat2 = CategoryFactory.create()
name = u'XXXX Project'
data = dict(
name=name,
Expand Down Expand Up @@ -382,10 +383,46 @@ def test_project_post(self):
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"
# Test that a default category is assigned to the project
assert cat1.id == out.category_id, "No category assigned to project"
id_ = out.id

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

# now a real user with headers auth and non-existing category_id
headers = [('Authorization', users[1].api_key)]
new_project3 = dict(
name=name + '4',
short_name='xxxx-project4',
description='description4',
owner_id=1,
category_id=5014,
long_description=u'Long Description\n================')
new_project3 = json.dumps(new_project3)
res = self.app.post('/api/project', headers=headers,
data=new_project3)
err = json.loads(res.data)
assert err['status'] == 'failed'
assert err['exception_msg'] == 'category_id does not exist'
assert err['status_code'] == 400

# test re-create should fail
res = self.app.post('/api/project?api_key=' + users[1].api_key,
data=data)
Expand Down

0 comments on commit 779414c

Please sign in to comment.