@@ -172,7 +172,6 @@ def test_build_default_containers(self):
172172 def test_build_str (self ):
173173 """Test the text representation of a build."""
174174 url = '/v1/apps'
175- response = self .client .post (url )
176175 response = self .client .post (url , HTTP_AUTHORIZATION = 'token {}' .format (self .token ))
177176 self .assertEqual (response .status_code , 201 )
178177 app_id = response .data ['id' ]
@@ -207,3 +206,18 @@ def test_admin_can_create_builds_on_other_apps(self):
207206 build = Build .objects .get (uuid = response .data ['uuid' ])
208207 self .assertEqual (str (build ), "{}-{}" .format (
209208 response .data ['app' ], response .data ['uuid' ][:7 ]))
209+
210+ @mock .patch ('requests.post' , mock_import_repository_task )
211+ def test_unauthorized_user_cannot_create_build (self ):
212+ """An unauthorized user should not be able to create builds for other apps."""
213+ url = '/v1/apps'
214+ response = self .client .post (url , HTTP_AUTHORIZATION = 'token {}' .format (self .token ))
215+ app_id = response .data ['id' ]
216+ # attempt to create a build as a malicious user
217+ evil_user = User .objects .get (username = 'autotest2' )
218+ evil_token = Token .objects .get (user = evil_user ).key
219+ url = "/v1/apps/{app_id}/builds" .format (** locals ())
220+ body = {'image' : 'eeeeeevillllll' }
221+ response = self .client .post (url , json .dumps (body ), content_type = 'application/json' ,
222+ HTTP_AUTHORIZATION = 'token {}' .format (evil_token ))
223+ self .assertEqual (response .status_code , 403 )
0 commit comments