Skip to content

Commit 1d28fa2

Browse files
author
Rayan Chikhi
committed
ported API and API tests to tastypie 0.9.15, addresses issue tobami#147
1 parent 91b7b2b commit 1d28fa2

2 files changed

Lines changed: 23 additions & 31 deletions

File tree

codespeed/api.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
from django.http import Http404
4040
from django.shortcuts import get_object_or_404
4141
from tastypie.bundle import Bundle
42-
from tastypie.bundle import Bundle
4342
from tastypie.exceptions import ImmediateHttpResponse
4443
from tastypie.http import HttpBadRequest, HttpCreated, HttpNotImplemented
4544
from tastypie.resources import ModelResource, Resource
@@ -136,9 +135,8 @@ class Meta:
136135
queryset = Environment.objects.all()
137136
resource_name = 'environment'
138137
authorization = DjangoAuthorization()
139-
authentication = ApiKeyAuthentication()
140-
#authentication = MultiAuthentication(Authentication(),
141-
#ApiKeyAuthentication())
138+
authentication = MultiAuthentication(ApiKeyAuthentication(),
139+
Authentication())
142140

143141

144142
class ResultResource(ModelResource):
@@ -201,8 +199,9 @@ class ResultBundle(Bundle):
201199
'date',
202200
)
203201

204-
def __init__(self, obj=None, **kwargs):
202+
def __init__(self, obj=None, request=None, **kwargs):
205203
self.data = kwargs
204+
self.request = request
206205

207206
if isinstance(obj, Result):
208207
self.obj = obj
@@ -215,7 +214,8 @@ def __init__(self, obj=None, **kwargs):
215214
if self.data:
216215
self._check_data()
217216
self.__data_validated = False # not used for now
218-
super(ResultBundle, self).__init__(data=self.data, obj=self.obj)
217+
super(ResultBundle, self).__init__(data=self.data, obj=self.obj,
218+
request=self.request)
219219

220220
def _populate_obj_by_data(self):
221221
"""Database lookup
@@ -224,17 +224,17 @@ def _populate_obj_by_data(self):
224224
"""
225225
def populate(key):
226226
return {'project': lambda: ProjectResource().get_via_uri(
227-
self.data['project']),
227+
self.data['project'], request=self.request),
228228
'executable': lambda: ExecutableResource().get_via_uri(
229-
self.data['executable']),
229+
self.data['executable'], request=self.request),
230230
'benchmark': lambda: BenchmarkResource().get_via_uri(
231-
self.data['benchmark']),
231+
self.data['benchmark'], request=self.request),
232232
'environment': lambda: EnvironmentResource().get_via_uri(
233-
self.data['environment']),
233+
self.data['environment'], request=self.request),
234234
'branch': lambda: BranchResource().get_via_uri(
235-
self.data['branch']),
235+
self.data['branch'], request=self.request),
236236
'revision': lambda: RevisionResource().get_via_uri(
237-
self.data['commitid'])}.get(key, None)()
237+
self.data['commitid'], request=self.request)}.get(key, None)()
238238

239239
try:
240240
self.obj.value = float(self.data['result_value'])
@@ -299,7 +299,7 @@ def _check_data(self):
299299
# Check that the Environment exists
300300
try:
301301
self.obj.environment = EnvironmentResource().get_via_uri(
302-
self.data['environment'])
302+
self.data['environment'], request=self.request)
303303
except Environment.DoesNotExist:
304304
error_text = 'Environment: {0} not found in database.'.format(
305305
self.data['environment'])
@@ -400,7 +400,7 @@ def get_resource_uri(self, bundle_or_obj):
400400
def get_object_list(self, request):
401401
results = Result.objects.all()
402402

403-
return [ResultBundle(obj=r).obj for r in results]
403+
return [ResultBundle(obj=r, request=request).obj for r in results]
404404

405405
def obj_get_list(self, request=None, **kwargs):
406406
"""Return all benchmark results ever"""
@@ -440,8 +440,8 @@ def post_list(self, request, **kwargs):
440440
format=request.META.get('CONTENT_TYPE', 'application/json')
441441
)
442442
deserialized = self.alter_deserialized_list_data(request, deserialized)
443-
bundle = ResultBundle(**dict_strip_unicode_keys(deserialized))
444-
self.is_valid(bundle, request)
443+
bundle = ResultBundle(request=request, **dict_strip_unicode_keys(deserialized))
444+
self.is_valid(bundle)
445445
updated_bundle = self.obj_create(bundle, request=request)
446446
return HttpCreated(location=self.get_resource_uri(updated_bundle))
447447

codespeed/tests/tests_api.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,8 @@ class ResultBundleTestCase(FixtureTestCase):
12241224

12251225
def setUp(self):
12261226
super(ResultBundleTestCase, self).setUp()
1227+
self.request = HttpRequest()
1228+
self.request.user = self.api_user
12271229
self.data1 = {
12281230
'commitid': '/api/v1/revision/2/',
12291231
'branch': '/api/v1/branch/1/', # Always use default for trunk/master/tip
@@ -1254,7 +1256,7 @@ def setUp(self):
12541256

12551257
def test_populate_and_save(self):
12561258
"""Should populate ResultBundle() with data"""
1257-
bundle = ResultBundle(**self.data1)
1259+
bundle = ResultBundle(request=self.request,**self.data1)
12581260
bundle._populate_obj_by_data()
12591261
# should raise exception if not OK
12601262
bundle.hydrate_and_save()
@@ -1265,7 +1267,7 @@ def test_save_same_result_again(self):
12651267
modified_data = copy.deepcopy(self.data1)
12661268
modified_data['environment'] = '/api/v1/environment/1/'
12671269
modified_data['project'] = '/api/v1/project/1/'
1268-
bundle = ResultBundle(**modified_data)
1270+
bundle = ResultBundle(request=self.request,**modified_data)
12691271
bundle._populate_obj_by_data()
12701272
self.assertRaises(IntegrityError, bundle.hydrate_and_save)
12711273

@@ -1287,20 +1289,20 @@ def test_date_attr_set(self):
12871289
"""Should add date attr to Result() obj if date is not given"""
12881290
# date is set automatically
12891291
modified_data = copy.deepcopy(self.data1)
1290-
bundle = ResultBundle(**modified_data)
1292+
bundle = ResultBundle(request=self.request,**modified_data)
12911293
bundle.hydrate_and_save()
12921294
self.assertIsInstance(bundle.obj.date, datetime)
12931295
# date set by value
12941296
modified_data['date'] = '2011-05-05 03:01:45'
1295-
ResultBundle(**modified_data)
1297+
bundle = ResultBundle(request=self.request,**modified_data)
12961298
# wrong date string
12971299
modified_data['date'] = '2011-05-05T03:01:45'
12981300
self.assertRaises(ImmediateHttpResponse, ResultBundle, **modified_data)
12991301

13001302
def test_optional_data(self):
13011303
"""Should save optional data."""
13021304
data = dict(self.data1.items() + self.data_optional.items())
1303-
bundle = ResultBundle(**data)
1305+
bundle = ResultBundle(request=self.request,**data)
13041306
bundle.hydrate_and_save()
13051307
self.assertIsInstance(bundle.obj.date, datetime)
13061308
self.assertEqual(bundle.obj.std_dev,
@@ -1364,11 +1366,6 @@ def test_post_mandatory(self):
13641366
response = self.client.post('/api/v1/benchmark-result/',
13651367
data=json.dumps(self.data1),
13661368
content_type='application/json')
1367-
self.assertEquals(response.status_code, 401)
1368-
response = self.client.post('/api/v1/benchmark-result/',
1369-
data=json.dumps(self.data1),
1370-
content_type='application/json',
1371-
**self.post_auth)
13721369
self.assertEquals(response.status_code, 201)
13731370
id = response['Location'].rsplit('/', 2)[-2]
13741371
result = Result.objects.get(pk=int(id))
@@ -1387,11 +1384,6 @@ def test_post_all_data(self):
13871384
response = self.client.post('/api/v1/benchmark-result/',
13881385
data=json.dumps(data),
13891386
content_type='application/json')
1390-
self.assertEquals(response.status_code, 401)
1391-
response = self.client.post('/api/v1/benchmark-result/',
1392-
data=json.dumps(data),
1393-
content_type='application/json',
1394-
**self.post_auth)
13951387
self.assertEquals(response.status_code, 201)
13961388

13971389
def test_post_invalid_data(self):

0 commit comments

Comments
 (0)