Finished tests for access.py (as far as able)

This commit is contained in:
Deena Wang
2013-01-15 15:39:05 -05:00
parent 8a1f30bfa0
commit 3ff3189260
2 changed files with 193 additions and 0 deletions

View File

@@ -0,0 +1,126 @@
import unittest
import logging
import time
from mock import MagicMock, patch
from django.conf import settings
from django.test import TestCase
from xmodule.course_module import CourseDescriptor
from xmodule.error_module import ErrorDescriptor
from xmodule.modulestore import Location
from xmodule.timeparse import parse_time
from xmodule.x_module import XModule, XModuleDescriptor
import courseware.access as access
class Stub:
def __init__(self):
pass
class AccessTestCase(TestCase):
def setUp(self):
pass
def test__has_global_staff_access(self):
# Only 2 branches?
mock_user = MagicMock()
mock_user.is_staff = True
self.assertTrue(access._has_global_staff_access(mock_user))
mock_user_2 = MagicMock()
mock_user.is_staff = False
self.assertFalse(access._has_global_staff_access(mock_user))
def test__has_access_to_location(self):
mock_user = MagicMock()
mock_user.is_authenticated.return_value = False
self.assertFalse(access._has_access_to_location(mock_user, "dummy",
"dummy"))
mock_user_2 = MagicMock()
mock_user_2.groups.all.return_value = ['instructor_toy']
location = MagicMock(spec=Location)
location.course = 'toy'
self.assertTrue(access._has_access_to_location(mock_user_2, location,
"instructor"))
mock_user_3 = MagicMock()
mock_user_3.is_staff = False
self.assertFalse(access._has_access_to_location(mock_user_3, 'dummy',
'dummy'))
def test__dispatch(self):
self.assertRaises(ValueError, access._dispatch,{}, 'action', 'dummy',
'dummy')
def test__has_access_string(self):
mock_user = MagicMock()
mock_user.is_staff = True
self.assertTrue(access._has_access_string(mock_user, 'global', 'staff'))
self.assertFalse(access._has_access_string(mock_user, 'dummy', 'staff'))
def test__has_access_descriptor(self):
mock_descriptor = MagicMock()
mock_descriptor.start = 0
# test has dependency on time.gmtime() > 0
self.assertTrue(access._has_access_descriptor("dummy", mock_descriptor,
'load'))
mock_descriptor_2 = MagicMock()
mock_descriptor_2.start = None
self.assertTrue(access._has_access_descriptor("dummy", mock_descriptor_2,
'load'))
def test__has_access_error_desc(self):
mock_user = None
mock_descriptor = MagicMock()
mock_descriptor.location = None
# Just want to make sure function goes through path.
self.assertFalse(access._has_access_error_desc(mock_user, mock_descriptor,
'load'))
def test__get_access_group_name_course_desc(self):
self.assertEquals(access._get_access_group_name_course_desc('dummy',
'notstaff'),
[])
# Problem: Can't use a Mock for location because needs to be a valid
# input to Location
# Getting "IndentationError: expected an indented block"
## tag, org, course, category, name = [MagicMock()]*5
## #mock_course.location = ['tag', 'org', 'course', 'category', 'name']
## L = Location([tag, org, course, category, name])
## print L.course_id()
## assert False
#mock_course.location.course = 'toy'
#access._get_access_group_name_course_desc(mock_course, 'staff')
def test__has_access_course_desc(self):
# This is more of a test for see_exists
mock_course = MagicMock()
mock_course.metadata.get = 'is_public'
self.assertTrue(access._has_access_course_desc('dummy', mock_course,
'see_exists'))
mock_course_2 = MagicMock()
mock_course_2.metadata.get = 'private'
# Is there a way to see all the functions that have been called on a mock?
# Basically, I want to see if _has_staff_access_to_descriptor is called on
# the mock user and course
# This actually doesn't seem possible, according to the API
# None user can see course even if not 'is_public'?
self.assertTrue(access._has_access_course_desc(None, mock_course_2,
'see_exists'))
def test_get_access_group_name(self):
# Need to create an instance of CourseDescriptor
# Is it necessary to test? basically "testing" python
self.assertRaises(TypeError, access.get_access_group_name,
'notCourseDescriptor', 'dummy_action')
def test_has_access(self):
magic = MagicMock()
error = ErrorDescriptor(magic)
mock_user = MagicMock()
self.assertFalse(access.has_access(None, error, 'load'))
self.assertFalse(access.has_access(mock_user, 'dummy', 'staff'))
self.assertRaises(TypeError, access.has_access,'dummyuser', {}, 'dummy')
# How do decorators work? I think that is the correct
## def test_patches(self):
## user = Stub()
## @patch.object(Stub, "is_staff", True)
## self.assertTrue(access._has_global_staff_access(mock_user))

View File

@@ -0,0 +1,67 @@
from django.test import TestCase
from courseware import progress
from mock import MagicMock
class ProgessTests(TestCase):
def setUp(self):
self.d = dict({'duration_total': 0,
'duration_watched': 0,
'done': True,
'questions_correct': 4,
'questions_incorrect': 0,
'questions_total': 0})
self.c = progress.completion()
self.c2= progress.completion()
self.c2.dict = dict({'duration_total': 0,
'duration_watched': 0,
'done': True,
'questions_correct': 2,
'questions_incorrect': 1,
'questions_total': 0})
self.cplusc2 = dict({'duration_total': 0,
'duration_watched': 0,
'done': True,
'questions_correct': 2,
'questions_incorrect': 1,
'questions_total': 0})
self.oth = dict({'duration_total': 0,
'duration_watched': 0,
'done': True,
'questions_correct': 4,
'questions_incorrect': 0,
'questions_total': 7})
self.x = MagicMock()
self.x.dict = self.oth
self.d_oth = {'duration_total': 0,
'duration_watched': 0,
'done': True,
'questions_correct': 4,
'questions_incorrect': 0,
'questions_total': 7}
def test_getitem(self):
self.assertEqual(self.c.__getitem__('duration_watched'), 0)
def test_setitem(self):
self.c.__setitem__('questions_correct', 4)
self.assertEqual(str(self.c),str(self.d))
# def test_add(self):
# self.assertEqual(self.c.__add__(self.c2), self.cplusc2)
def test_contains(self):
return self.c.__contains__('meow')
#self.assertEqual(self.c.__contains__('done'), True)
def test_repr(self):
self.assertEqual(self.c.__repr__(), str(progress.completion()))