'Test that only topics that a user has access to are returned. The
ways in which a user may not have access are:
* Module is visible to staff only
* Module has a start date in the future
* Module is accessible only to a group the user is not in
Also, there is a case that ensures that a category with no accessible
subcategories does not appear in the result.'

def test_access_control(self):

    beta_tester = BetaTesterFactory.create(course_key=self.course.id)
    CourseEnrollmentFactory.create(user=beta_tester, course_id=self.course.id)
    staff = StaffFactory.create(course_key=self.course.id)
    for (user, group_idx) in [(self.user, 0), (beta_tester, 1)]:
        cohort = CohortFactory.create(course_id=self.course.id, name=self.partition.groups[group_idx].name, users=[user])
        CourseUserGroupPartitionGroup.objects.create(course_user_group=cohort, partition_id=self.partition.id, group_id=self.partition.groups[group_idx].id)
    with self.store.bulk_operations(self.course.id, emit_signals=False):
        self.make_discussion_xblock('courseware-1', 'First', 'Everybody')
        self.make_discussion_xblock('courseware-2', 'First', 'Cohort    A', group_access={self.partition.id: [self.partition.groups[0].id]})
        self.make_discussion_xblock('courseware-3', 'First', 'Cohort    B', group_access={self.partition.id: [self.partition.groups[1].id]})
        self.make_discussion_xblock('courseware-4', 'Second', 'Staff    Only', visible_to_staff_only=True)
        self.make_discussion_xblock('courseware-5', 'Second', 'Future    Start    Date', start=(datetime.now(UTC) + timedelta(days=1)))
    student_actual = self.get_course_topics()
    student_expected = {'courseware_topics': [self.make_expected_tree(None, 'First', [self.make_expected_tree('courseware-2', 'Cohort    A'), self.make_expected_tree('courseware-1', 'Everybody')])], 'non_courseware_topics': [self.make_expected_tree('non-courseware-topic-id', 'Test    Topic')]}
    self.assertEqual(student_actual, student_expected)
    self.request.user = beta_tester
    beta_actual = self.get_course_topics()
    beta_expected = {'courseware_topics': [self.make_expected_tree(None, 'First', [self.make_expected_tree('courseware-3', 'Cohort    B'), self.make_expected_tree('courseware-1', 'Everybody')]), self.make_expected_tree(None, 'Second', [self.make_expected_tree('courseware-5', 'Future    Start    Date')])], 'non_courseware_topics': [self.make_expected_tree('non-courseware-topic-id', 'Test    Topic')]}
    self.assertEqual(beta_actual, beta_expected)
    self.request.user = staff
    staff_actual = self.get_course_topics()
    staff_expected = {'courseware_topics': [self.make_expected_tree(None, 'First', [self.make_expected_tree('courseware-2', 'Cohort    A'), self.make_expected_tree('courseware-3', 'Cohort    B'), self.make_expected_tree('courseware-1', 'Everybody')]), self.make_expected_tree(None, 'Second', [self.make_expected_tree('courseware-5', 'Future    Start    Date'), self.make_expected_tree('courseware-4', 'Staff    Only')])], 'non_courseware_topics': [self.make_expected_tree('non-courseware-topic-id', 'Test    Topic')]}
    self.assertEqual(staff_actual, staff_expected)
