From 3e12a8f575e83fe1a57a90fd11677941f8d9b390 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Lombra=C3=B1a=20Gonz=C3=A1lez?= Date: Wed, 11 Jul 2018 14:12:41 +0200 Subject: [PATCH] Allow to specify a category_id when creating a project. If the category_id does not exist, it raises a BadRequest error. --- pybossa/api/project.py | 6 +++++ test/test_api/test_project_api.py | 43 ++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/pybossa/api/project.py b/pybossa/api/project.py index b2f7d1d570..9200d02848 100644 --- a/pybossa/api/project.py +++ b/pybossa/api/project.py @@ -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): diff --git a/test/test_api/test_project_api.py b/test/test_api/test_project_api.py index 875f308455..7d24da0392 100644 --- a/test/test_api/test_project_api.py +++ b/test/test_api/test_project_api.py @@ -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, @@ -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)