Skip to content

Commit 2fc6c54

Browse files
committed
added tests for ReportResource authentication and authorization
Change-Id: Iefc4c8b344ff984f19fd4479776aaaa6f7ae0291
1 parent 2c1f84a commit 2fc6c54

1 file changed

Lines changed: 43 additions & 3 deletions

File tree

codespeed/tests/tests_api.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,14 @@ class ReportTest(FixtureTestCase):
10161016
"""Test Report() API"""
10171017

10181018
def setUp(self):
1019+
super(ReportTest, self).setUp()
1020+
self.add = Permission.objects.get_by_natural_key(
1021+
'add_report', 'codespeed', 'report')
1022+
self.change = Permission.objects.get_by_natural_key(
1023+
'change_report', 'codespeed', 'report')
1024+
self.delete = Permission.objects.get_by_natural_key(
1025+
'delete_report', 'codespeed', 'report')
1026+
10191027
self.report1 = Report.objects.get(pk=1)
10201028
self.revision1 = Revision.objects.get(pk=1)
10211029
self.executable1 = Executable.objects.get(pk=1)
@@ -1041,7 +1049,6 @@ def setUp(self):
10411049
executable='/api/v1/executable/{0}/'.format(self.executable2.id),
10421050
)
10431051
self.client = Client()
1044-
super(ReportTest, self).setUp()
10451052

10461053
def test_get_report(self):
10471054
"""Should get an existing report"""
@@ -1061,30 +1068,63 @@ def test_get_report_all_fields(self):
10611068
self.assertEqual(json.loads(response.content)[k], v)
10621069

10631070
def test_post(self):
1064-
"""Should save a new report"""
1071+
"""Should not save a new report"""
1072+
request = HttpRequest()
1073+
request.user = self.api_user
1074+
1075+
request.user.user_permissions.add(self.add)
1076+
10651077
modified_data = copy.deepcopy(self.report2_data)
10661078
response = self.client.post('/api/v1/report/',
10671079
data=json.dumps(modified_data),
10681080
content_type='application/json')
1081+
self.assertEquals(response.status_code, 405)
1082+
# next has to be 405 (method not allowed),
1083+
# otherwise would raise IntegrityError
1084+
response = self.client.post('/api/v1/report/',
1085+
data=json.dumps(modified_data),
1086+
content_type='application/json',
1087+
**self.post_auth)
10691088
# next has to be 405, otherwise would raise IntegrityError
10701089
self.assertEquals(response.status_code, 405)
10711090

10721091
def test_put(self):
1073-
"""Should modify an existing report"""
1092+
"""Should not modify an existing report"""
1093+
request = HttpRequest()
1094+
request.user = self.api_user
1095+
1096+
request.user.user_permissions.add(self.add)
1097+
request.user.user_permissions.add(self.change)
1098+
request.user.user_permissions.add(self.delete)
1099+
10741100
modified_data = copy.deepcopy(self.report2_data)
10751101
response = self.client.put('/api/v1/report/1/',
10761102
data=json.dumps(modified_data),
10771103
content_type='application/json')
10781104
self.assertEquals(response.status_code, 405)
1105+
response = self.client.put('/api/v1/report/1/',
1106+
data=json.dumps(modified_data),
1107+
content_type='application/json',
1108+
**self.post_auth)
1109+
self.assertEquals(response.status_code, 405)
10791110

10801111
def test_delete(self):
10811112
"""Should delete a report"""
1113+
request = HttpRequest()
1114+
request.user = self.api_user
1115+
1116+
request.user.user_permissions.add(self.delete)
1117+
10821118
response = self.client.get('/api/v1/report/1/')
10831119
self.assertEquals(response.status_code, 200)
10841120
# from fixture
10851121
response = self.client.delete('/api/v1/report/1/',
10861122
content_type='application/json')
10871123
self.assertEquals(response.status_code, 405)
1124+
response = self.client.delete('/api/v1/report/1/',
1125+
content_type='application/json',
1126+
**self.post_auth)
1127+
self.assertEquals(response.status_code, 405)
10881128

10891129

10901130
class ApiKeyAuthenticationTestCase(FixtureTestCase):

0 commit comments

Comments
 (0)