Skip to content

Commit

Permalink
fixing bugs in the filter/tastypie api. adding tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdreher committed Mar 10, 2014
1 parent 5dff98f commit 311c7cc
Show file tree
Hide file tree
Showing 11 changed files with 426 additions and 137 deletions.
3 changes: 2 additions & 1 deletion mediathread/api.py
Expand Up @@ -275,5 +275,6 @@ def render_list(self, request, tags):

def filter(self, request, filters):
self.filters = filters
objects = self.obj_get_list(request=request)
base_bundle = self.build_bundle(request=request)
objects = self.obj_get_list(bundle=base_bundle)
return self.render_list(request, objects)
2 changes: 1 addition & 1 deletion mediathread/assetmgr/features/asset_access.feature
Expand Up @@ -157,7 +157,7 @@ Feature: Direct Asset Access
When I click the "Sample Course" link
Then I am in the Sample Course class

# Try to access an asset from the Alternate Course
# Try to access an asset from the Sample Course
When I access the url "/asset/4/"
Then I see "Oops!"
Then there is a "Go to Alternate Course" link
Expand Down
84 changes: 84 additions & 0 deletions mediathread/main/tests/test_api.py
Expand Up @@ -2,6 +2,7 @@
#pylint: disable-msg=E1103
from courseaffils.models import Course
from django.contrib.auth.models import User
from django.http.request import HttpRequest
from mediathread.api import UserResource, TagResource
from mediathread.assetmgr.models import Asset
from mediathread.main import course_details
Expand Down Expand Up @@ -35,6 +36,89 @@ def test_render_list(self):
self.assertEquals(lst[4]['last'], True)
self.assertEquals(lst[4]['name'], 'youtube')

def test_filter_by_asset(self):
self.assertTrue(
self.api_client.client.login(username="test_student_one",
password="test"))

asset = Asset.objects.get(id=1)
filters = {
'assets': [asset.id],
}
request = HttpRequest()
request.course = asset.course

lst = TagResource(asset.course).filter(request, filters)

self.assertEquals(len(lst), 5)

for item in lst:
self.assertFalse('count' in item)

self.assertEquals(lst[0]['last'], False)
self.assertEquals(lst[0]['name'], 'test_instructor_item')

self.assertFalse('count' in lst[4])
self.assertEquals(lst[4]['last'], True)
self.assertEquals(lst[4]['name'], 'youtube')

def test_filter_by_record_owner(self):
self.assertTrue(
self.api_client.client.login(username="test_student_one",
password="test"))

asset = Asset.objects.get(id=1)
filters = {
'record_owner': User.objects.get(username='test_student_one'),
'counts': True
}
request = HttpRequest()
request.course = asset.course

lst = TagResource(asset.course).filter(request, filters)

self.assertEquals(len(lst), 2)
self.assertEquals(lst[0]['count'], 1)
self.assertEquals(lst[0]['last'], False)
self.assertEquals(lst[0]['name'], 'student_one_item')

self.assertEquals(lst[1]['count'], 1)
self.assertEquals(lst[1]['last'], True)
self.assertEquals(lst[1]['name'], 'student_one_selection')

def test_filter_by_both(self):
self.assertTrue(
self.api_client.client.login(username="test_student_one",
password="test"))

asset = Asset.objects.get(id=1)
filters = {
'assets': [asset.id],
'record_owner': User.objects.get(username='test_instructor'),
'counts': True
}
request = HttpRequest()
request.course = asset.course

lst = TagResource(asset.course).filter(request, filters)

self.assertEquals(len(lst), 4)
self.assertEquals(lst[0]['count'], 1)
self.assertEquals(lst[0]['last'], False)
self.assertEquals(lst[0]['name'], 'test_instructor_item')

self.assertEquals(lst[1]['count'], 1)
self.assertEquals(lst[1]['last'], False)
self.assertEquals(lst[1]['name'], 'test_instructor_selection')

self.assertEquals(lst[2]['count'], 2)
self.assertEquals(lst[2]['last'], False)
self.assertEquals(lst[2]['name'], 'video')

self.assertEquals(lst[3]['count'], 1)
self.assertEquals(lst[3]['last'], True)
self.assertEquals(lst[3]['name'], 'youtube')


class UserResourceTest(ResourceTestCase):
# Use ``fixtures`` & ``urls`` as normal. See Django's ``TestCase``
Expand Down
254 changes: 127 additions & 127 deletions mediathread/projects/tests/test_api.py
Expand Up @@ -20,133 +20,133 @@ def assertProjectEquals(self, project, title, author, selection_ids):
for idx, selection in enumerate(project['annotations']):
self.assertEquals(int(selection['id']), selection_ids[idx])

# def test_student_one_getlist(self):
# self.assertTrue(
# self.api_client.client.login(username="test_student_one",
# password="test"))
#
# response = self.api_client.get('/_main/api/v1/project/',
# format='json')
# self.assertValidJSONResponse(response)
#
# json = self.deserialize(response)
# objects = json['objects']
# self.assertEquals(len(objects), 4)
#
# self.assertProjectEquals(objects[0], 'Private Composition',
# 'Student One', [8, 10])
#
# self.assertProjectEquals(objects[1], 'Instructor Shared',
# 'Student One', [])
#
# self.assertProjectEquals(objects[2], 'Public To Class Composition',
# 'Student One', [2, 5, 7])
#
# self.assertProjectEquals(objects[3], 'Sample Course Assignment',
# 'test_instructor_two', [1, 10, 18, 19, 20])
#
# def test_student_two_getlist(self):
# self.assertTrue(
# self.api_client.client.login(username="test_student_two",
# password="test"))
#
# response = self.api_client.get('/_main/api/v1/project/',
# format='json')
# self.assertValidJSONResponse(response)
#
# json = self.deserialize(response)
# objects = json['objects']
# self.assertEquals(len(objects), 2)
#
# self.assertProjectEquals(objects[0], 'Public To Class Composition',
# 'Student One', [2, 5, 7])
#
# self.assertProjectEquals(objects[1], 'Sample Course Assignment',
# 'test_instructor_two', [1, 10, 18, 19, 20])
#
# def test_student_two_getlist_filtered(self):
# self.assertTrue(
# self.api_client.client.login(username="test_student_two",
# password="test"))
#
# response = self.api_client.get('/_main/api/v1/project/?author__id=4',
# format='json')
# self.assertValidJSONResponse(response)
#
# json = self.deserialize(response)
# objects = json['objects']
# self.assertEquals(len(objects), 0)
#
# def test_instructor_getlist(self):
# self.assertTrue(
# self.api_client.client.login(username="test_instructor",
# password="test"))
#
# response = self.api_client.get('/_main/api/v1/project/',
# format='json')
# self.assertValidJSONResponse(response)
#
# json = self.deserialize(response)
# objects = json['objects']
# self.assertEquals(len(objects), 3)
#
# self.assertProjectEquals(objects[0], 'Instructor Shared',
# 'Student One', [])
#
# self.assertProjectEquals(objects[1], 'Public To Class Composition',
# 'Student One', [2, 5, 7])
#
# self.assertProjectEquals(objects[2], 'Sample Course Assignment',
# 'test_instructor_two', [1, 10, 18, 19, 20])
#
# def test_student_one_getobject(self):
# self.assertTrue(
# self.api_client.client.login(username="test_student_one",
# password="test"))
#
# # My own private composition
# response = self.api_client.get('/_main/api/v1/project/1/',
# format='json')
# self.assertValidJSONResponse(response)
#
# json = self.deserialize(response)
#
# self.assertProjectEquals(json, 'Private Composition',
# 'Student One', [8, 10])
#
# # Student three composition in alt course
# response = self.api_client.get('/_main/api/v1/project/4/',
# format='json')
# self.assertEqual(response.status_code, 401)
#
# def test_student_two_getobject(self):
# self.assertTrue(
# self.api_client.client.login(username="test_student_two",
# password="test"))
#
# # Student one private composition
# response = self.api_client.get('/_main/api/v1/project/1/',
# format='json')
# self.assertEqual(response.status_code, 401)
#
# # Student one instructor shared composition
# response = self.api_client.get('/_main/api/v1/project/2/',
# format='json')
# self.assertEqual(response.status_code, 401)
#
# # Student one public to class composition
# response = self.api_client.get('/_main/api/v1/project/3/',
# format='json')
# self.assertValidJSONResponse(response)
# json = self.deserialize(response)
# self.assertProjectEquals(json, 'Public To Class Composition',
# 'Student One', [2, 5, 7])
#
# # Student three composition in alt course
# response = self.api_client.get('/_main/api/v1/project/4/',
# format='json')
# self.assertEqual(response.status_code, 401)
#
def test_student_one_getlist(self):
self.assertTrue(
self.api_client.client.login(username="test_student_one",
password="test"))

response = self.api_client.get('/_main/api/v1/project/',
format='json')
self.assertValidJSONResponse(response)

json = self.deserialize(response)
objects = json['objects']
self.assertEquals(len(objects), 4)

self.assertProjectEquals(objects[0], 'Private Composition',
'Student One', [8, 10])

self.assertProjectEquals(objects[1], 'Instructor Shared',
'Student One', [])

self.assertProjectEquals(objects[2], 'Public To Class Composition',
'Student One', [2, 5, 7])

self.assertProjectEquals(objects[3], 'Sample Course Assignment',
'test_instructor_two', [1, 10, 18, 19, 20])

def test_student_two_getlist(self):
self.assertTrue(
self.api_client.client.login(username="test_student_two",
password="test"))

response = self.api_client.get('/_main/api/v1/project/',
format='json')
self.assertValidJSONResponse(response)

json = self.deserialize(response)
objects = json['objects']
self.assertEquals(len(objects), 2)

self.assertProjectEquals(objects[0], 'Public To Class Composition',
'Student One', [2, 5, 7])

self.assertProjectEquals(objects[1], 'Sample Course Assignment',
'test_instructor_two', [1, 10, 18, 19, 20])

def test_student_two_getlist_filtered(self):
self.assertTrue(
self.api_client.client.login(username="test_student_two",
password="test"))

response = self.api_client.get('/_main/api/v1/project/?author__id=4',
format='json')
self.assertValidJSONResponse(response)

json = self.deserialize(response)
objects = json['objects']
self.assertEquals(len(objects), 0)

def test_instructor_getlist(self):
self.assertTrue(
self.api_client.client.login(username="test_instructor",
password="test"))

response = self.api_client.get('/_main/api/v1/project/',
format='json')
self.assertValidJSONResponse(response)

json = self.deserialize(response)
objects = json['objects']
self.assertEquals(len(objects), 3)

self.assertProjectEquals(objects[0], 'Instructor Shared',
'Student One', [])

self.assertProjectEquals(objects[1], 'Public To Class Composition',
'Student One', [2, 5, 7])

self.assertProjectEquals(objects[2], 'Sample Course Assignment',
'test_instructor_two', [1, 10, 18, 19, 20])

def test_student_one_getobject(self):
self.assertTrue(
self.api_client.client.login(username="test_student_one",
password="test"))

# My own private composition
response = self.api_client.get('/_main/api/v1/project/1/',
format='json')
self.assertValidJSONResponse(response)

json = self.deserialize(response)

self.assertProjectEquals(json, 'Private Composition',
'Student One', [8, 10])

# Student three composition in alt course
response = self.api_client.get('/_main/api/v1/project/4/',
format='json')
self.assertEqual(response.status_code, 401)

def test_student_two_getobject(self):
self.assertTrue(
self.api_client.client.login(username="test_student_two",
password="test"))

# Student one private composition
response = self.api_client.get('/_main/api/v1/project/1/',
format='json')
self.assertEqual(response.status_code, 401)

# Student one instructor shared composition
response = self.api_client.get('/_main/api/v1/project/2/',
format='json')
self.assertEqual(response.status_code, 401)

# Student one public to class composition
response = self.api_client.get('/_main/api/v1/project/3/',
format='json')
self.assertValidJSONResponse(response)
json = self.deserialize(response)
self.assertProjectEquals(json, 'Public To Class Composition',
'Student One', [2, 5, 7])

# Student three composition in alt course
response = self.api_client.get('/_main/api/v1/project/4/',
format='json')
self.assertEqual(response.status_code, 401)

def test_instructor_getobject(self):
self.assertTrue(
self.api_client.client.login(username="test_instructor",
Expand Down

0 comments on commit 311c7cc

Please sign in to comment.