<s> from __future__ import unicode_literals <EOL> import json <EOL> from django . contrib . auth . models import User <EOL> from django . test import TestCase <EOL> from rest_framework . authtoken . models import Token <EOL> class TestAdminPerms ( TestCase ) : <EOL> def test_first_signup ( self ) : <EOL> username , password = '<STR_LIT>' , '<STR_LIT:password>' <EOL> email = '<STR_LIT>' <EOL> submit = { <EOL> '<STR_LIT:username>' : username , <EOL> '<STR_LIT:password>' : password , <EOL> '<STR_LIT:email>' : email , <EOL> } <EOL> url = '<STR_LIT>' <EOL> response = self . client . post ( url , json . dumps ( submit ) , content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertTrue ( response . data [ '<STR_LIT>' ] ) <EOL> username , password = '<STR_LIT>' , '<STR_LIT:password>' <EOL> email = '<STR_LIT>' <EOL> submit = { <EOL> '<STR_LIT:username>' : username , <EOL> '<STR_LIT:password>' : password , <EOL> '<STR_LIT:email>' : email , <EOL> } <EOL> url = '<STR_LIT>' <EOL> response = self . client . post ( url , json . dumps ( submit ) , content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertFalse ( response . data [ '<STR_LIT>' ] ) <EOL> def test_list ( self ) : <EOL> submit = { <EOL> '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT:password>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> } <EOL> url = '<STR_LIT>' <EOL> response = self . client . post ( url , json . dumps ( submit ) , content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertTrue ( response . data [ '<STR_LIT>' ] ) <EOL> user = User . objects . get ( username = '<STR_LIT>' ) <EOL> token = Token . objects . get ( user = user ) . key <EOL> response = self . client . get ( '<STR_LIT>' , content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( len ( response . data [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( response . data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:username>' ] , '<STR_LIT>' ) <EOL> self . assertTrue ( response . data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> submit = { <EOL> '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT:password>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> } <EOL> url = '<STR_LIT>' <EOL> response = self . client . post ( url , json . dumps ( submit ) , content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertFalse ( response . data [ '<STR_LIT>' ] ) <EOL> user = User . objects . get ( username = '<STR_LIT>' ) <EOL> token = Token . objects . get ( user = user ) . key <EOL> response = self . client . get ( '<STR_LIT>' , content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertIn ( '<STR_LIT>' , response . data [ '<STR_LIT>' ] ) <EOL> def test_create ( self ) : <EOL> submit = { <EOL> '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT:password>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> } <EOL> url = '<STR_LIT>' <EOL> response = self . client . post ( url , json . dumps ( submit ) , content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertTrue ( response . data [ '<STR_LIT>' ] ) <EOL> submit = { <EOL> '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT:password>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> } <EOL> url = '<STR_LIT>' <EOL> response = self . client . post ( url , json . dumps ( submit ) , content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertFalse ( response . data [ '<STR_LIT>' ] ) <EOL> user = User . objects . get ( username = '<STR_LIT>' ) <EOL> token = Token . objects . get ( user = user ) . key <EOL> url = '<STR_LIT>' <EOL> body = { '<STR_LIT:username>' : '<STR_LIT>' } <EOL> response = self . client . post ( url , json . dumps ( body ) , content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> response = self . client . get ( url , HTTP_AUTHORIZATION = '<STR_LIT>' . format ( token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( len ( response . data [ '<STR_LIT>' ] ) , <NUM_LIT:2> ) <EOL> self . assertIn ( '<STR_LIT>' , str ( response . data [ '<STR_LIT>' ] ) ) <EOL> def test_delete ( self ) : <EOL> submit = { <EOL> '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT:password>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> } <EOL> url = '<STR_LIT>' <EOL> response = self . client . post ( url , json . dumps ( submit ) , content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertTrue ( response . data [ '<STR_LIT>' ] ) <EOL> submit = { <EOL> '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT:password>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> } <EOL> url = '<STR_LIT>' <EOL> response = self . client . post ( url , json . dumps ( submit ) , content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertFalse ( response . data [ '<STR_LIT>' ] ) <EOL> user = User . objects . get ( username = '<STR_LIT>' ) <EOL> token = Token . objects . get ( user = user ) . key <EOL> url = '<STR_LIT>' <EOL> body = { '<STR_LIT:username>' : '<STR_LIT>' } <EOL> response = self . client . post ( url , json . dumps ( body ) , content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> response = self . client . delete ( url + '<STR_LIT>' , HTTP_AUTHORIZATION = '<STR_LIT>' . format ( token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> response = self . client . get ( url , HTTP_AUTHORIZATION = '<STR_LIT>' . format ( token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( len ( response . data [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> self . assertNotIn ( '<STR_LIT>' , str ( response . data [ '<STR_LIT>' ] ) ) <EOL> class TestAppPerms ( TestCase ) : <EOL> fixtures = [ '<STR_LIT>' ] <EOL> def setUp ( self ) : <EOL> self . user = User . objects . get ( username = '<STR_LIT>' ) <EOL> self . token = Token . objects . get ( user = self . user ) . key <EOL> self . user2 = User . objects . get ( username = '<STR_LIT>' ) <EOL> self . token2 = Token . objects . get ( user = self . user2 ) . key <EOL> self . user3 = User . objects . get ( username = '<STR_LIT>' ) <EOL> self . token3 = Token . objects . get ( user = self . user3 ) . key <EOL> def test_create ( self ) : <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( len ( response . data [ '<STR_LIT>' ] ) , <NUM_LIT:2> ) <EOL> app_id = response . data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token2 ) ) <EOL> self . assertEqual ( len ( response . data [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> for model in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> response = self . client . get ( "<STR_LIT>" . format ( app_id , model ) , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token2 ) ) <EOL> msg = "<STR_LIT>" % ( response . status_code , response . data ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> , msg = msg ) <EOL> self . assertEqual ( response . data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' , msg = msg ) <EOL> url = "<STR_LIT>" . format ( app_id ) <EOL> body = { '<STR_LIT:username>' : '<STR_LIT>' } <EOL> response = self . client . post ( url , json . dumps ( body ) , content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token2 ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( len ( response . data [ '<STR_LIT>' ] ) , <NUM_LIT:2> ) <EOL> for model in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> response = self . client . get ( "<STR_LIT>" . format ( app_id , model ) , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token2 ) ) <EOL> self . assertEqual ( len ( response . data [ '<STR_LIT>' ] ) , <NUM_LIT:0> ) <EOL> def test_create_errors ( self ) : <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token ) ) <EOL> app_id = response . data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] <EOL> url = "<STR_LIT>" . format ( app_id ) <EOL> body = { '<STR_LIT:username>' : '<STR_LIT>' } <EOL> response = self . client . post ( url , json . dumps ( body ) , content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token2 ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_delete ( self ) : <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token ) ) <EOL> app_id = response . data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] <EOL> url = "<STR_LIT>" . format ( app_id ) <EOL> body = { '<STR_LIT:username>' : '<STR_LIT>' } <EOL> response = self . client . post ( url , json . dumps ( body ) , content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token2 ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( len ( response . data [ '<STR_LIT>' ] ) , <NUM_LIT:2> ) <EOL> url = "<STR_LIT>" . format ( app_id , '<STR_LIT>' ) <EOL> response = self . client . delete ( url , content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertIsNone ( response . data ) <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token2 ) ) <EOL> self . assertEqual ( len ( response . data [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> response = self . client . delete ( url , content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_list ( self ) : <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( len ( response . data [ '<STR_LIT>' ] ) , <NUM_LIT:2> ) <EOL> app_id = response . data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] <EOL> url = "<STR_LIT>" . format ( app_id ) <EOL> body = { '<STR_LIT:username>' : '<STR_LIT>' } <EOL> response = self . client . post ( url , json . dumps ( body ) , content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> response = self . client . get ( <EOL> "<STR_LIT>" . format ( app_id ) , content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token ) ) <EOL> self . assertEqual ( response . data , { '<STR_LIT>' : [ '<STR_LIT>' ] } ) <EOL> def test_admin_can_list ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( len ( response . data [ '<STR_LIT>' ] ) , <NUM_LIT:2> ) <EOL> def test_list_errors ( self ) : <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token ) ) <EOL> app_id = response . data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] <EOL> response = self . client . get ( <EOL> "<STR_LIT>" . format ( app_id ) , content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token2 ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_unauthorized_user_cannot_modify_perms ( self ) : <EOL> """<STR_LIT>""" <EOL> app_id = '<STR_LIT>' <EOL> url = '<STR_LIT>' <EOL> body = { '<STR_LIT:id>' : app_id } <EOL> response = self . client . post ( url , json . dumps ( body ) , content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( self . token ) ) <EOL> unauthorized_user = self . user2 <EOL> unauthorized_token = self . token2 <EOL> url = '<STR_LIT>' . format ( url , app_id ) <EOL> body = { '<STR_LIT:username>' : unauthorized_user . username } <EOL> response = self . client . post ( url , json . dumps ( body ) , content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( unauthorized_token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_collaborator_cannot_share ( self ) : <EOL> """<STR_LIT>""" <EOL> app_id = "<STR_LIT>" <EOL> owner_token = self . token <EOL> collab = self . user2 <EOL> collab_token = self . token2 <EOL> url = '<STR_LIT>' . format ( app_id ) <EOL> body = { '<STR_LIT:username>' : collab . username } <EOL> response = self . client . post ( url , json . dumps ( body ) , content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( owner_token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> body = { '<STR_LIT:username>' : self . user3 . username } <EOL> response = self . client . post ( url , json . dumps ( body ) , content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( collab_token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> response = self . client . get ( url , content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( collab_token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> response = self . client . post ( url , json . dumps ( body ) , content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( owner_token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> response = self . client . get ( url , content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = '<STR_LIT>' . format ( collab_token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> url += "<STR_LIT>" . format ( self . user3 . username ) <EOL> response = self . client . delete ( url , HTTP_AUTHORIZATION = '<STR_LIT>' . format ( collab_token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> url = '<STR_LIT>' . format ( app_id , collab . username ) <EOL> response = self . client . delete ( url , HTTP_AUTHORIZATION = '<STR_LIT>' . format ( collab_token ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) </s>
