'Test that a user masquerading as a member of a group sees appropriate content in preview mode.'

@patch('courseware.access_utils.in_preview_mode', Mock(return_value=True))
def test_has_access_in_preview_mode_with_group(self):

    partition_id = MINIMUM_STATIC_PARTITION_ID
    group_0_id = (MINIMUM_STATIC_PARTITION_ID + 1)
    group_1_id = (MINIMUM_STATIC_PARTITION_ID + 2)
    user_partition = UserPartition(partition_id, 'Test    User    Partition', '', [Group(group_0_id, 'Group    1'), Group(group_1_id, 'Group    2')], scheme_id='cohort')
    self.course.user_partitions.append(user_partition)
    self.course.cohort_config = {'cohorted': True}
    chapter = ItemFactory.create(category='chapter', parent_location=self.course.location)
    chapter.group_access = {partition_id: [group_0_id]}
    modulestore().update_item(self.course, ModuleStoreEnum.UserID.test)
    with patch('courseware.access.get_user_role') as mock_user_role:
        mock_user_role.return_value = 'student'
        self.assertFalse(bool(access.has_access(self.global_staff, 'load', chapter, course_key=self.course.id)))
    for mocked_role in ['staff', 'instructor']:
        with patch('courseware.access.get_user_role') as mock_user_role:
            mock_user_role.return_value = mocked_role
            self.assertTrue(bool(access.has_access(self.global_staff, 'load', chapter, course_key=self.course.id)))
    self.assertEqual(200, masquerade_as_group_member(self.global_staff, self.course, partition_id, group_0_id))
    self.assertTrue(bool(access.has_access(self.global_staff, 'load', chapter, course_key=self.course.id)))
    self.assertEqual(200, masquerade_as_group_member(self.global_staff, self.course, partition_id, group_1_id))
    self.assertFalse(bool(access.has_access(self.global_staff, 'load', chapter, course_key=self.course.id)))
