Commit fe2c92e6 by jagonzalr

add unit tests

parent 9cf08261
......@@ -100,6 +100,18 @@ class CourseTestCase(ProceduralCourseTestMixin, ModuleStoreTestCase):
nonstaff.is_authenticated = lambda: authenticate
return client, nonstaff
def create_staff_authed_user_client(self, authenticate=True):
"""
Create a staff user, log them in (if authenticate=True), and return the client, user to use for testing.
"""
staff, password = self.create_staff_user()
client = AjaxEnabledTestClient()
if authenticate:
client.login(username=staff.username, password=password)
staff.is_authenticated = lambda: authenticate
return client, staff
def reload_course(self):
"""
Reloads the course object from the database
......
......@@ -16,7 +16,7 @@ from contentstore.courseware_index import CoursewareSearchIndexer, SearchIndexin
from contentstore.tests.utils import CourseTestCase
from contentstore.utils import reverse_course_url, reverse_library_url, add_instructor, reverse_usage_url
from contentstore.views.course import (
course_outline_initial_state, reindex_course_and_check_access, _deprecated_blocks_info
course_outline_initial_state, reindex_course_and_check_access, _deprecated_blocks_info, _get_library_creator_status
)
from contentstore.views.item import create_xblock_info, VisibilityState
from course_action_state.managers import CourseRerunUIStateManager
......@@ -30,6 +30,8 @@ from xmodule.modulestore import ModuleStoreEnum
from xmodule.modulestore.exceptions import ItemNotFoundError
from xmodule.modulestore.django import modulestore
from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory, LibraryFactory
from student import auth
from student.roles import CourseCreatorRole
class TestCourseIndex(CourseTestCase):
......@@ -48,6 +50,36 @@ class TestCourseIndex(CourseTestCase):
display_name='dotted.course.name-2',
)
@mock.patch("contentstore.views.library.LIBRARIES_ENABLED", False)
def test_library_creator_status_libraries_not_enabled(self):
_, nostaff_user = self.create_non_staff_authed_user_client()
self.assertEqual(_get_library_creator_status(nostaff_user), "disallowed_for_this_site")
@mock.patch("contentstore.views.library.LIBRARIES_ENABLED", True)
def test_library_creator_status_with_is_staff_user(self):
_, staff_user = self.create_staff_authed_user_client()
self.assertEqual(_get_library_creator_status(staff_user), "granted")
@mock.patch("contentstore.views.library.LIBRARIES_ENABLED", True)
def test_library_creator_status_with_disable_library_creation(self):
_, nostaff_user = self.create_non_staff_authed_user_client()
with mock.patch.dict('django.conf.settings.FEATURES', {"DISABLE_LIBRARY_CREATION": True}):
self.assertEqual(_get_library_creator_status(nostaff_user), "disallowed_for_this_site")
@mock.patch("contentstore.views.library.LIBRARIES_ENABLED", True)
def test_library_creator_status_with_course_creator_role(self):
_, staff_user = self.create_staff_authed_user_client()
_, nostaff_user = self.create_non_staff_authed_user_client()
auth.add_users(staff_user, CourseCreatorRole(), nostaff_user)
self.assertEqual(_get_library_creator_status(nostaff_user), "granted")
@mock.patch("contentstore.views.library.LIBRARIES_ENABLED", True)
def test_library_creator_status_with_no_course_creator_role_no_is_staff_no_disable_library_creation(self):
_, nostaff_user = self.create_non_staff_authed_user_client()
with mock.patch.dict('django.conf.settings.FEATURES', {"DISABLE_LIBRARY_CREATION": False}):
self.assertEqual(_get_library_creator_status(nostaff_user), "disallowed_for_this_site")
def check_index_and_outline(self, authed_client):
"""
Test getting the list of courses and then pulling up their outlines
......
......@@ -436,6 +436,22 @@ class ModuleStoreTestCase(ModuleStoreIsolationMixin, TestCase):
nonstaff_user.save()
return nonstaff_user, password
def create_staff_user(self):
"""
Creates a staff test user.
Returns the staff test user and its password.
"""
uname = 'teststaff'
password = 'bar'
staff_user = User.objects.create_user(uname, 'test+staff@edx.org', password)
# Note that we do not actually need to do anything
# for registration if we directly mark them active.
staff_user.is_active = True
staff_user.is_staff = True
staff_user.save()
return staff_user, password
def update_course(self, course, user_id):
"""
Updates the version of course in the modulestore
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment