diff --git a/infrabox/test/api/collaborators_test.py b/infrabox/test/api/collaborators_test.py index 7619862d..86ae2bc4 100644 --- a/infrabox/test/api/collaborators_test.py +++ b/infrabox/test/api/collaborators_test.py @@ -1,3 +1,5 @@ +import json + from temp_tools import TestClient from test_template import ApiTestTemplate @@ -64,3 +66,29 @@ def test_collaborators_delete(self): SELECT count(*) FROM collaborator WHERE user_id = %s """, [self.collaborator_id]) self.assertEqual(r[0], 0) + + def test_administrator_cannot_promote_self_to_owner(self): + TestClient.execute(""" + INSERT INTO "user" (id, github_id, username, avatar_url) + VALUES (%s, 2, %s, 'url1'); + """, [self.collaborator_id, + self.test_collaborator_data['username']]) + TestClient.execute(""" + INSERT INTO collaborator (user_id, project_id, role) + VALUES (%s, %s, 'Administrator') + """, [self.collaborator_id, self.project_id]) + TestClient.opa_push() + + r = TestClient.app.put( + 'api/v1/projects/%s/collaborators/%s' % (self.project_id, self.collaborator_id), + data=json.dumps({'role': 'Owner'}), + headers=TestClient.get_user_authorization(self.collaborator_id), + content_type='application/json') + + self.assertEqual(r.status_code, 403) + self.assertEqual(json.loads(r.data)['message'], 'A project is limited to one owner.') + role = TestClient.execute_one(""" + SELECT role FROM collaborator + WHERE user_id = %s AND project_id = %s + """, [self.collaborator_id, self.project_id]) + self.assertEqual(role[0], 'Administrator') diff --git a/src/api/handlers/projects/collaborators.py b/src/api/handlers/projects/collaborators.py index af10e312..06597698 100644 --- a/src/api/handlers/projects/collaborators.py +++ b/src/api/handlers/projects/collaborators.py @@ -146,6 +146,9 @@ def put(self, project_id, user_id): if [userrole] not in roles: abort(400, "Role unknown.") + if userrole == 'Owner': + abort(403, "A project is limited to one owner.") + # Do update g.db.execute( """