@@ -147,6 +147,8 @@ def setUp(self):
147147 self .token = Token .objects .get (user = self .user ).key
148148 self .user2 = User .objects .get (username = 'autotest-2' )
149149 self .token2 = Token .objects .get (user = self .user2 ).key
150+ self .user3 = User .objects .get (username = 'autotest-3' )
151+ self .token3 = Token .objects .get (user = self .user3 ).key
150152
151153 def test_create (self ):
152154 # check that user 1 sees her lone app and user 2's app
@@ -210,12 +212,8 @@ def test_delete(self):
210212 response = self .client .get ('/v1/apps' , HTTP_AUTHORIZATION = 'token {}' .format (self .token2 ))
211213 self .assertEqual (response .status_code , 200 )
212214 self .assertEqual (len (response .data ['results' ]), 2 )
213- # try to delete the permission as user 2
214- url = "/v1/apps/{}/perms/{}" .format (app_id , 'autotest-2' )
215- response = self .client .delete (url , content_type = 'application/json' ,
216- HTTP_AUTHORIZATION = 'token {}' .format (self .token2 ))
217- self .assertEqual (response .status_code , 403 )
218215 # delete permission to user 1's app
216+ url = "/v1/apps/{}/perms/{}" .format (app_id , 'autotest-2' )
219217 response = self .client .delete (url , content_type = 'application/json' ,
220218 HTTP_AUTHORIZATION = 'token {}' .format (self .token ))
221219 self .assertEqual (response .status_code , 204 )
@@ -280,3 +278,42 @@ def test_unauthorized_user_cannot_modify_perms(self):
280278 response = self .client .post (url , json .dumps (body ), content_type = 'application/json' ,
281279 HTTP_AUTHORIZATION = 'token {}' .format (unauthorized_token ))
282280 self .assertEqual (response .status_code , 403 )
281+
282+ def test_collaborator_cannot_share (self ):
283+ """
284+ An collaborator should not be able to modify the app's permissions.
285+ """
286+ app_id = "autotest-1-app"
287+ owner_token = self .token
288+ collab = self .user2
289+ collab_token = self .token2
290+ url = '/v1/apps/{}/perms' .format (app_id )
291+ # Share app with collaborator
292+ body = {'username' : collab .username }
293+ response = self .client .post (url , json .dumps (body ), content_type = 'application/json' ,
294+ HTTP_AUTHORIZATION = 'token {}' .format (owner_token ))
295+ self .assertEqual (response .status_code , 201 )
296+ # Collaborator should fail to share app
297+ body = {'username' : self .user3 .username }
298+ response = self .client .post (url , json .dumps (body ), content_type = 'application/json' ,
299+ HTTP_AUTHORIZATION = 'token {}' .format (collab_token ))
300+ self .assertEqual (response .status_code , 403 )
301+ # Collaborator can list
302+ response = self .client .get (url , content_type = 'application/json' ,
303+ HTTP_AUTHORIZATION = 'token {}' .format (collab_token ))
304+ self .assertEqual (response .status_code , 200 )
305+ # Share app with user 3 for rest of tests
306+ response = self .client .post (url , json .dumps (body ), content_type = 'application/json' ,
307+ HTTP_AUTHORIZATION = 'token {}' .format (owner_token ))
308+ self .assertEqual (response .status_code , 201 )
309+ response = self .client .get (url , content_type = 'application/json' ,
310+ HTTP_AUTHORIZATION = 'token {}' .format (collab_token ))
311+ self .assertEqual (response .status_code , 200 )
312+ # Collaborator cannot delete other collaborator
313+ url += "/{}" .format (self .user3 .username )
314+ response = self .client .delete (url , HTTP_AUTHORIZATION = 'token {}' .format (collab_token ))
315+ self .assertEqual (response .status_code , 403 )
316+ # Collaborator can delete themselves
317+ url = '/v1/apps/{}/perms/{}' .format (app_id , collab .username )
318+ response = self .client .delete (url , HTTP_AUTHORIZATION = 'token {}' .format (collab_token ))
319+ self .assertEqual (response .status_code , 204 )
0 commit comments