Skip to content

Commit c2ea6b2

Browse files
committed
Improved docstrings and minor code cleanups
Change-Id: I4268567cf26ff62e5659950485ff883b1dcc1f84
1 parent e7ba52f commit c2ea6b2

2 files changed

Lines changed: 25 additions & 10 deletions

File tree

codespeed/api.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
from tastypie.resources import ModelResource, Resource
4444
from tastypie import fields
4545
from tastypie.authorization import Authorization, DjangoAuthorization
46-
#from tastypie.authentication import ApiKeyAuthentication, Authentication, MultiAuthentication
4746
from tastypie.authentication import Authentication, ApiKeyAuthentication, MultiAuthentication
4847
from tastypie.models import create_api_key
4948
from tastypie.utils.dict import dict_strip_unicode_keys

codespeed/tests/tests_api.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,17 @@ class UserTest(FixtureTestCase):
9292
"""Test api user related stuff"""
9393

9494
def test_has_apikey(self):
95+
"""User() should have an api key attr that was generated automatically."""
9596
self.assertTrue(hasattr(self.api_user, 'api_key'))
9697

9798
def test_len_apikey(self):
98-
"""Test the key has a length"""
99+
"""Should have api user key with a non-zero length."""
99100
self.assertTrue(len(self.api_user.api_key.key) >= 1)
100101

101102
def test_is_authenticated_header(self):
102103
"""Taken from tastypie test suite to ensure api key is generated for
103-
new users correctly.
104+
new users correctly and tastypie is installed correctly.
104105
"""
105-
106106
auth = ApiKeyAuthentication()
107107
request = HttpRequest()
108108

@@ -135,7 +135,7 @@ def test_is_authenticated_header(self):
135135
self.assertEqual(auth.is_authenticated(request), True)
136136

137137
def test_api_key(self):
138-
# Correct user/api_key.
138+
"""User should be authenticated by it's api key."""
139139
auth = ApiKeyAuthentication()
140140
request = HttpRequest()
141141
authorization='ApiKey %s:%s' % (self.api_user.username, self.api_user.api_key.key)
@@ -177,6 +177,7 @@ def setUp(self):
177177
self.client = Client()
178178

179179
def test_no_perms(self):
180+
"""User() should have only GET permission"""
180181
# sanity check: user has no permissions
181182
self.assertFalse(self.api_user.get_all_permissions())
182183

@@ -194,6 +195,7 @@ def test_no_perms(self):
194195
)
195196

196197
def test_add_perm(self):
198+
"""User() should have add permission granted."""
197199
request = HttpRequest()
198200
request.user = self.api_user
199201

@@ -204,6 +206,7 @@ def test_add_perm(self):
204206
EnvironmentResource()._meta.authorization.is_authorized(request))
205207

206208
def test_change_perm(self):
209+
"""User() should have change permission granted."""
207210
request = HttpRequest()
208211
request.user = self.api_user
209212

@@ -214,6 +217,7 @@ def test_change_perm(self):
214217
EnvironmentResource()._meta.authorization.is_authorized(request))
215218

216219
def test_delete_perm(self):
220+
"""User() should have delete permission granted."""
217221
request = HttpRequest()
218222
request.user = self.api_user
219223

@@ -224,6 +228,7 @@ def test_delete_perm(self):
224228
EnvironmentResource()._meta.authorization.is_authorized(request))
225229

226230
def test_all(self):
231+
"""User() should have add, change, delete permissions granted."""
227232
request = HttpRequest()
228233
request.user = self.api_user
229234

@@ -239,6 +244,7 @@ def test_all(self):
239244
)
240245

241246
def test_patch_perms(self):
247+
"""User() should have patch (add, change, delete) permissions granted."""
242248
request = HttpRequest()
243249
request.user = self.api_user
244250
request.method = 'PATCH'
@@ -261,6 +267,7 @@ def test_patch_perms(self):
261267
EnvironmentResource()._meta.authorization.is_authorized(request))
262268

263269
def test_unrecognized_method(self):
270+
"""User() should not have the permission to call non-existent method."""
264271
request = HttpRequest()
265272
self.api_user.user_permissions.clear()
266273
request.user = self.api_user
@@ -271,13 +278,20 @@ def test_unrecognized_method(self):
271278
EnvironmentResource()._meta.authorization.is_authorized(request))
272279

273280
def test_get_environment(self):
274-
"""Should get an existing environment"""
281+
"""Should get an environment when given an existing ID"""
275282
response = self.client.get(
276283
'/api/v1/environment/1/?username={0}&api_key={1}'.format(
277284
self.api_user.username, self.api_user.api_key.key))
278285
self.assertEquals(response.status_code, 200)
279286
self.assertEqual(json.loads(response.content)['name'], "Dual Core")
280287

288+
def test_get_non_existing_environment(self):
289+
"""Should return 404 when given a non existing environment ID"""
290+
response = self.client.get(
291+
'/api/v1/environment/999/?username={0}&api_key={1}'.format(
292+
self.api_user.username, self.api_user.api_key.key))
293+
self.assertEquals(response.status_code, 404)
294+
281295
def test_get_environment_all_fields(self):
282296
"""Should get all fields for an environment"""
283297
response = self.client.get(
@@ -425,6 +439,7 @@ def setUp(self):
425439
self.client = Client()
426440

427441
def test_all(self):
442+
"""User should have all permissions granted."""
428443
request = HttpRequest()
429444
request.user = self.api_user
430445

@@ -1204,6 +1219,7 @@ def setUp(self):
12041219
self.env1.save()
12051220

12061221
def test_populate_and_save(self):
1222+
"""Should populate ResultBundle() with data"""
12071223
bundle = ResultBundle(**self.data1)
12081224
bundle._populate_obj_by_data()
12091225
# should raise exception if not OK
@@ -1233,7 +1249,7 @@ def test_insufficient_data(self):
12331249
self.assertRaises(ImmediateHttpResponse, ResultBundle, **modified_data)
12341250

12351251
def test_date_attr_set(self):
1236-
"""Check if date attr of Result() is set if not given"""
1252+
"""Should add date attr to Result() obj if date is not given"""
12371253
# date is set automatically
12381254
modified_data = copy.deepcopy(self.data1)
12391255
bundle = ResultBundle(**modified_data)
@@ -1247,7 +1263,7 @@ def test_date_attr_set(self):
12471263
self.assertRaises(ImmediateHttpResponse, ResultBundle, **modified_data)
12481264

12491265
def test_optional_data(self):
1250-
"""Check handling of optional data"""
1266+
"""Should save optional data."""
12511267
data = dict(self.data1.items() + self.data_optional.items())
12521268
bundle = ResultBundle(**data)
12531269
bundle.save()
@@ -1259,8 +1275,8 @@ def test_optional_data(self):
12591275
self.assertEqual(bundle.obj.val_min,
12601276
float(self.data_optional['val_min']))
12611277

1262-
def test_non_exiting_items(self):
1263-
"""Check handling of optional data"""
1278+
def test_overwrite_exiting_items(self):
1279+
"""Should overwrite existing attributes"""
12641280
modified_data = copy.deepcopy(self.data1)
12651281
modified_data['commitid'] = '0b31bf33a469ac2cb1949666eea54d69a36c3724'
12661282
modified_data['project'] = 'Cython'

0 commit comments

Comments
 (0)