Check access for discussion modules in forums
TNL-650 Conflicts: lms/djangoapps/django_comment_client/base/views.py lms/djangoapps/django_comment_client/tests/test_utils.py lms/djangoapps/django_comment_client/tests/utils.py lms/djangoapps/django_comment_client/utils.py
This commit is contained in:
@@ -10,6 +10,7 @@ import mock
|
||||
|
||||
from django_comment_client.tests.factories import RoleFactory
|
||||
from django_comment_client.tests.unicode import UnicodeTestMixin
|
||||
from django_comment_client.tests.utils import ContentGroupTestCase
|
||||
import django_comment_client.utils as utils
|
||||
from student.tests.factories import UserFactory, CourseEnrollmentFactory
|
||||
from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory
|
||||
@@ -88,7 +89,7 @@ class CoursewareContextTestCase(ModuleStoreTestCase):
|
||||
comment client service integration
|
||||
"""
|
||||
def setUp(self):
|
||||
super(CoursewareContextTestCase, self).setUp()
|
||||
super(CoursewareContextTestCase, self).setUp(create_user=True)
|
||||
|
||||
self.course = CourseFactory.create(org="TestX", number="101", display_name="Test Course")
|
||||
self.discussion1 = ItemFactory.create(
|
||||
@@ -107,12 +108,12 @@ class CoursewareContextTestCase(ModuleStoreTestCase):
|
||||
)
|
||||
|
||||
def test_empty(self):
|
||||
utils.add_courseware_context([], self.course)
|
||||
utils.add_courseware_context([], self.course, self.user)
|
||||
|
||||
def test_missing_commentable_id(self):
|
||||
orig = {"commentable_id": "non-inline"}
|
||||
modified = dict(orig)
|
||||
utils.add_courseware_context([modified], self.course)
|
||||
utils.add_courseware_context([modified], self.course, self.user)
|
||||
self.assertEqual(modified, orig)
|
||||
|
||||
def test_basic(self):
|
||||
@@ -120,7 +121,7 @@ class CoursewareContextTestCase(ModuleStoreTestCase):
|
||||
{"commentable_id": self.discussion1.discussion_id},
|
||||
{"commentable_id": self.discussion2.discussion_id}
|
||||
]
|
||||
utils.add_courseware_context(threads, self.course)
|
||||
utils.add_courseware_context(threads, self.course, self.user)
|
||||
|
||||
def assertThreadCorrect(thread, discussion, expected_title): # pylint: disable=invalid-name
|
||||
"""Asserts that the given thread has the expected set of properties"""
|
||||
@@ -144,13 +145,29 @@ class CoursewareContextTestCase(ModuleStoreTestCase):
|
||||
assertThreadCorrect(threads[1], self.discussion2, "Subsection / Discussion 2")
|
||||
|
||||
|
||||
class CategoryMapTestCase(ModuleStoreTestCase):
|
||||
class CategoryMapTestMixin(object):
|
||||
"""
|
||||
Provides functionality for classes that test
|
||||
`get_discussion_category_map`.
|
||||
"""
|
||||
def assert_category_map_equals(self, expected, requesting_user=None):
|
||||
"""
|
||||
Call `get_discussion_category_map`, and verify that it returns
|
||||
what is expected.
|
||||
"""
|
||||
self.assertEqual(
|
||||
utils.get_discussion_category_map(self.course, requesting_user or self.user),
|
||||
expected
|
||||
)
|
||||
|
||||
|
||||
class CategoryMapTestCase(CategoryMapTestMixin, ModuleStoreTestCase):
|
||||
"""
|
||||
Base testcase class for discussion categories for the
|
||||
comment client service integration
|
||||
"""
|
||||
def setUp(self):
|
||||
super(CategoryMapTestCase, self).setUp()
|
||||
super(CategoryMapTestCase, self).setUp(create_user=True)
|
||||
|
||||
self.course = CourseFactory.create(
|
||||
org="TestX", number="101", display_name="Test Course",
|
||||
@@ -176,20 +193,8 @@ class CategoryMapTestCase(ModuleStoreTestCase):
|
||||
**kwargs
|
||||
)
|
||||
|
||||
def assertCategoryMapEquals(self, expected):
|
||||
"""
|
||||
Compare a manually-constructed category map to the actual result from `utils.get_discussion_category_map`
|
||||
"""
|
||||
self.assertEqual(
|
||||
utils.get_discussion_category_map(self.course),
|
||||
expected
|
||||
)
|
||||
|
||||
def test_empty(self):
|
||||
self.assertEqual(
|
||||
utils.get_discussion_category_map(self.course),
|
||||
{"entries": {}, "subcategories": {}, "children": []}
|
||||
)
|
||||
self.assert_category_map_equals({"entries": {}, "subcategories": {}, "children": []})
|
||||
|
||||
def test_configured_topics(self):
|
||||
self.course.discussion_topics = {
|
||||
@@ -199,7 +204,7 @@ class CategoryMapTestCase(ModuleStoreTestCase):
|
||||
}
|
||||
|
||||
def check_cohorted_topics(expected_ids): # pylint: disable=missing-docstring
|
||||
self.assertCategoryMapEquals(
|
||||
self.assert_category_map_equals(
|
||||
{
|
||||
"entries": {
|
||||
"Topic A": {"id": "Topic_A", "sort_key": "Topic A", "is_cohorted": "Topic_A" in expected_ids},
|
||||
@@ -231,7 +236,7 @@ class CategoryMapTestCase(ModuleStoreTestCase):
|
||||
|
||||
def test_single_inline(self):
|
||||
self.create_discussion("Chapter", "Discussion")
|
||||
self.assertCategoryMapEquals(
|
||||
self.assert_category_map_equals(
|
||||
{
|
||||
"entries": {},
|
||||
"subcategories": {
|
||||
@@ -261,7 +266,7 @@ class CategoryMapTestCase(ModuleStoreTestCase):
|
||||
|
||||
def check_cohorted(is_cohorted):
|
||||
|
||||
self.assertCategoryMapEquals(
|
||||
self.assert_category_map_equals(
|
||||
{
|
||||
"entries": {},
|
||||
"subcategories": {
|
||||
@@ -362,7 +367,7 @@ class CategoryMapTestCase(ModuleStoreTestCase):
|
||||
self.create_discussion("Chapter 2 / Section 1 / Subsection 1", "Discussion")
|
||||
self.create_discussion("Chapter 2 / Section 1 / Subsection 1", "Discussion") # duplicate
|
||||
|
||||
category_map = utils.get_discussion_category_map(self.course)
|
||||
category_map = utils.get_discussion_category_map(self.course, self.user)
|
||||
|
||||
chapter1 = category_map["subcategories"]["Chapter 1"]
|
||||
chapter1_discussions = set(["Discussion A", "Discussion B", "Discussion A (1)", "Discussion A (2)"])
|
||||
@@ -385,7 +390,7 @@ class CategoryMapTestCase(ModuleStoreTestCase):
|
||||
self.create_discussion("Chapter 2 / Section 1 / Subsection 2", "Discussion", start=later)
|
||||
self.create_discussion("Chapter 3 / Section 1", "Discussion", start=later)
|
||||
|
||||
self.assertCategoryMapEquals(
|
||||
self.assert_category_map_equals(
|
||||
{
|
||||
"entries": {},
|
||||
"subcategories": {
|
||||
@@ -423,7 +428,7 @@ class CategoryMapTestCase(ModuleStoreTestCase):
|
||||
self.create_discussion("Chapter", "Discussion 4", sort_key="C")
|
||||
self.create_discussion("Chapter", "Discussion 5", sort_key="B")
|
||||
|
||||
self.assertCategoryMapEquals(
|
||||
self.assert_category_map_equals(
|
||||
{
|
||||
"entries": {},
|
||||
"subcategories": {
|
||||
@@ -475,7 +480,7 @@ class CategoryMapTestCase(ModuleStoreTestCase):
|
||||
"Topic B": {"id": "Topic_B", "sort_key": "C"},
|
||||
"Topic C": {"id": "Topic_C", "sort_key": "A"}
|
||||
}
|
||||
self.assertCategoryMapEquals(
|
||||
self.assert_category_map_equals(
|
||||
{
|
||||
"entries": {
|
||||
"Topic A": {"id": "Topic_A", "sort_key": "B", "is_cohorted": False},
|
||||
@@ -496,7 +501,7 @@ class CategoryMapTestCase(ModuleStoreTestCase):
|
||||
self.create_discussion("Chapter", "Discussion C")
|
||||
self.create_discussion("Chapter", "Discussion B")
|
||||
|
||||
self.assertCategoryMapEquals(
|
||||
self.assert_category_map_equals(
|
||||
{
|
||||
"entries": {},
|
||||
"subcategories": {
|
||||
@@ -549,7 +554,7 @@ class CategoryMapTestCase(ModuleStoreTestCase):
|
||||
self.create_discussion("Chapter B", "Discussion 1")
|
||||
self.create_discussion("Chapter A", "Discussion 2")
|
||||
|
||||
self.assertCategoryMapEquals(
|
||||
self.assert_category_map_equals(
|
||||
{
|
||||
"entries": {},
|
||||
"subcategories": {
|
||||
@@ -602,7 +607,7 @@ class CategoryMapTestCase(ModuleStoreTestCase):
|
||||
)
|
||||
|
||||
def test_ids_empty(self):
|
||||
self.assertEqual(utils.get_discussion_categories_ids(self.course), [])
|
||||
self.assertEqual(utils.get_discussion_categories_ids(self.course, self.user), [])
|
||||
|
||||
def test_ids_configured_topics(self):
|
||||
self.course.discussion_topics = {
|
||||
@@ -611,7 +616,7 @@ class CategoryMapTestCase(ModuleStoreTestCase):
|
||||
"Topic C": {"id": "Topic_C"}
|
||||
}
|
||||
self.assertItemsEqual(
|
||||
utils.get_discussion_categories_ids(self.course),
|
||||
utils.get_discussion_categories_ids(self.course, self.user),
|
||||
["Topic_A", "Topic_B", "Topic_C"]
|
||||
)
|
||||
|
||||
@@ -623,7 +628,7 @@ class CategoryMapTestCase(ModuleStoreTestCase):
|
||||
self.create_discussion("Chapter 2 / Section 1 / Subsection 2", "Discussion")
|
||||
self.create_discussion("Chapter 3 / Section 1", "Discussion")
|
||||
self.assertItemsEqual(
|
||||
utils.get_discussion_categories_ids(self.course),
|
||||
utils.get_discussion_categories_ids(self.course, self.user),
|
||||
["discussion1", "discussion2", "discussion3", "discussion4", "discussion5", "discussion6"]
|
||||
)
|
||||
|
||||
@@ -637,11 +642,177 @@ class CategoryMapTestCase(ModuleStoreTestCase):
|
||||
self.create_discussion("Chapter 2", "Discussion")
|
||||
self.create_discussion("Chapter 2 / Section 1 / Subsection 1", "Discussion")
|
||||
self.assertItemsEqual(
|
||||
utils.get_discussion_categories_ids(self.course),
|
||||
utils.get_discussion_categories_ids(self.course, self.user),
|
||||
["Topic_A", "Topic_B", "Topic_C", "discussion1", "discussion2", "discussion3"]
|
||||
)
|
||||
|
||||
|
||||
class ContentGroupCategoryMapTestCase(CategoryMapTestMixin, ContentGroupTestCase):
|
||||
"""
|
||||
Tests `get_discussion_category_map` on discussion modules which are
|
||||
only visible to some content groups.
|
||||
"""
|
||||
def test_staff_user(self):
|
||||
"""
|
||||
Verify that the staff user can access the alpha, beta, and
|
||||
global discussion topics.
|
||||
"""
|
||||
self.assert_category_map_equals(
|
||||
{
|
||||
'subcategories': {
|
||||
'Week 1': {
|
||||
'subcategories': {},
|
||||
'children': [
|
||||
'Visible to Alpha',
|
||||
'Visible to Beta',
|
||||
'Visible to Everyone'
|
||||
],
|
||||
'entries': {
|
||||
'Visible to Alpha': {
|
||||
'sort_key': None,
|
||||
'is_cohorted': True,
|
||||
'id': 'alpha_group_discussion'
|
||||
},
|
||||
'Visible to Beta': {
|
||||
'sort_key': None,
|
||||
'is_cohorted': True,
|
||||
'id': 'beta_group_discussion'
|
||||
},
|
||||
'Visible to Everyone': {
|
||||
'sort_key': None,
|
||||
'is_cohorted': True,
|
||||
'id': 'global_group_discussion'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
'children': ['General', 'Week 1'],
|
||||
'entries': {
|
||||
'General': {
|
||||
'sort_key': 'General',
|
||||
'is_cohorted': False,
|
||||
'id': 'i4x-org-number-course-run'
|
||||
}
|
||||
}
|
||||
},
|
||||
requesting_user=self.staff_user
|
||||
)
|
||||
|
||||
def test_alpha_user(self):
|
||||
"""
|
||||
Verify that the alpha user can access the alpha and global
|
||||
discussion topics.
|
||||
"""
|
||||
self.assert_category_map_equals(
|
||||
{
|
||||
'subcategories': {
|
||||
'Week 1': {
|
||||
'subcategories': {},
|
||||
'children': [
|
||||
'Visible to Alpha',
|
||||
'Visible to Everyone'
|
||||
],
|
||||
'entries': {
|
||||
'Visible to Alpha': {
|
||||
'sort_key': None,
|
||||
'is_cohorted': True,
|
||||
'id': 'alpha_group_discussion'
|
||||
},
|
||||
'Visible to Everyone': {
|
||||
'sort_key': None,
|
||||
'is_cohorted': True,
|
||||
'id': 'global_group_discussion'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
'children': ['General', 'Week 1'],
|
||||
'entries': {
|
||||
'General': {
|
||||
'sort_key': 'General',
|
||||
'is_cohorted': False,
|
||||
'id': 'i4x-org-number-course-run'
|
||||
}
|
||||
}
|
||||
},
|
||||
requesting_user=self.alpha_user
|
||||
)
|
||||
|
||||
def test_beta_user(self):
|
||||
"""
|
||||
Verify that the beta user can access the beta and global
|
||||
discussion topics.
|
||||
"""
|
||||
self.assert_category_map_equals(
|
||||
{
|
||||
'subcategories': {
|
||||
'Week 1': {
|
||||
'subcategories': {},
|
||||
'children': [
|
||||
'Visible to Beta',
|
||||
'Visible to Everyone'
|
||||
],
|
||||
'entries': {
|
||||
'Visible to Beta': {
|
||||
'sort_key': None,
|
||||
'is_cohorted': True,
|
||||
'id': 'beta_group_discussion'
|
||||
},
|
||||
'Visible to Everyone': {
|
||||
'sort_key': None,
|
||||
'is_cohorted': True,
|
||||
'id': 'global_group_discussion'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
'children': ['General', 'Week 1'],
|
||||
'entries': {
|
||||
'General': {
|
||||
'sort_key': 'General',
|
||||
'is_cohorted': False,
|
||||
'id': 'i4x-org-number-course-run'
|
||||
}
|
||||
}
|
||||
},
|
||||
requesting_user=self.beta_user
|
||||
)
|
||||
|
||||
def test_non_cohorted_user(self):
|
||||
"""
|
||||
Verify that the non-cohorted user can access the global
|
||||
discussion topic.
|
||||
"""
|
||||
self.assert_category_map_equals(
|
||||
{
|
||||
'subcategories': {
|
||||
'Week 1': {
|
||||
'subcategories': {},
|
||||
'children': [
|
||||
'Visible to Everyone'
|
||||
],
|
||||
'entries': {
|
||||
'Visible to Everyone': {
|
||||
'sort_key': None,
|
||||
'is_cohorted': True,
|
||||
'id': 'global_group_discussion'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
'children': ['General', 'Week 1'],
|
||||
'entries': {
|
||||
'General': {
|
||||
'sort_key': 'General',
|
||||
'is_cohorted': False,
|
||||
'id': 'i4x-org-number-course-run'
|
||||
}
|
||||
}
|
||||
},
|
||||
requesting_user=self.non_cohorted_user
|
||||
)
|
||||
|
||||
|
||||
class JsonResponseTestCase(TestCase, UnicodeTestMixin):
|
||||
def _test_unicode_data(self, text):
|
||||
response = utils.JsonResponse(text)
|
||||
|
||||
Reference in New Issue
Block a user