def test_get_user_course_permissions(self, mock_client): """ Verify course permissions are retrieved and cached, even when paged. """ page_size = 5 course_ids = [str(x) for x in range(page_size * 2)] expected_calls = self._setup_mock_course_ids_responses_and_expects( mock_client, course_ids, page_size=page_size) # Check permissions self.assertItemsEqual( permissions.get_user_course_permissions(self.user), course_ids) self.assertEqual(expected_calls, mock_client.mock_calls) # Check newly permitted course is not returned because the earlier permissions are cached mock_client.reset_mock() self._setup_mock_course_ids_responses_and_expects( mock_client, [self.new_course_id]) self.assertItemsEqual( permissions.get_user_course_permissions(self.user), course_ids) self.assertFalse(mock_client.mock_calls) # Check original permissions again mock_client.reset_mock() self.assertItemsEqual( permissions.get_user_course_permissions(self.user), course_ids) self.assertFalse(mock_client.mock_calls)
def get_context_data(self, **kwargs): context = super(CourseIndex, self).get_context_data(**kwargs) courses = permissions.get_user_course_permissions(self.request.user) if not courses: # The user is probably not a course administrator and should not be using this application. raise PermissionDenied summaries_presenter = CourseSummariesPresenter() summaries, last_updated = summaries_presenter.get_course_summaries( courses) context.update( {'update_message': self.get_last_updated_message(last_updated)}) enable_course_filters = switch_is_active('enable_course_filters') data = { 'course_list_json': summaries, 'enable_course_filters': enable_course_filters, 'enable_passing_users': switch_is_active('enable_course_passing'), 'course_list_download_url': reverse('courses:index_csv'), } if enable_course_filters: programs_presenter = ProgramsPresenter() programs = programs_presenter.get_programs(course_ids=courses) data['programs_json'] = programs context['js_data']['course'] = data context['page_data'] = self.get_page_data(context) context['summary'] = summaries_presenter.get_course_summary_metrics( summaries) return context
def get_data(self): courses = permissions.get_user_course_permissions(self.request.user) if not courses: # The user is probably not a course administrator and should not be using this application. raise PermissionDenied enable_course_filters = switch_is_active('enable_course_filters') presenter = CourseSummariesPresenter() summaries, _ = presenter.get_course_summaries(courses) if not summaries: # Instead of returning a useless blank CSV, return a 404 error raise Http404 # Exclude specified fields from each summary entry summaries = [remove_keys(summary, self.exclude_fields) for summary in summaries] if enable_course_filters: # Add list of associated program IDs to each summary entry programs_presenter = ProgramsPresenter() programs = programs_presenter.get_programs(course_ids=courses) for summary in summaries: summary_programs = [program for program in programs if summary['course_id'] in program['course_ids']] summary['program_ids'] = ' | '.join([program['program_id'] for program in summary_programs]) summary['program_titles'] = ' | '.join([program['program_title'] for program in summary_programs]) summaries_csv = self.renderer.render(summaries) return summaries_csv
def test_get_user_course_permissions(self): courses = [self.course_id] with mock.patch('courses.permissions.refresh_user_course_permissions', return_value=courses): self.assertListEqual(permissions.get_user_course_permissions(self.user), courses) # Raise a PermissionsError if the backend is unavailable. G(UserSocialAuth, user=self.user, provider='edx-oidc', extra_data={'access_token': '1234'}) with mock.patch('auth_backends.backends.EdXOpenIdConnect.get_json', side_effect=Exception): self.assertRaises(PermissionsRetrievalFailedError, permissions.get_user_course_permissions, self.user)
def get_context_data(self, **kwargs): context = super(CourseIndex, self).get_context_data(**kwargs) courses = permissions.get_user_course_permissions(self.request.user) if not courses: # The user is probably not a course administrator and should not be using this application. raise PermissionDenied context['courses'] = sorted(courses) return context
def get_data(self): courses = permissions.get_user_course_permissions(self.request.user) if not courses: # The user is probably not a course administrator and should not be using this application. raise PermissionDenied presenter = CourseSummariesPresenter() summaries, _ = presenter.get_course_summaries(courses) if not summaries: # Instead of returning a useless blank CSV, return a 404 error raise Http404 summaries_csv = self.renderer.render(summaries) return summaries_csv
def get_context_data(self, **kwargs): context = super(CourseIndex, self).get_context_data(**kwargs) courses = permissions.get_user_course_permissions(self.request.user) if not courses: # The user is probably not a course administrator and should not be using this application. raise PermissionDenied context['courses'] = sorted(courses) context['page_data'] = self.get_page_data(context) return context
def test_get_user_course_permissions_after_permission_timeout( self, mock_client): """ Verify course permissions are retrieved multiple times when the permission cache times out. """ self._setup_mock_course_ids_responses_and_expects( mock_client, [self.course_id]) with override_settings(COURSE_PERMISSIONS_TIMEOUT=0): # Check permissions expected_courses = [self.course_id] self.assertEqual( permissions.get_user_course_permissions(self.user), expected_courses) # Check permission succeeds for a newly permitted course because the earlier permission timed out self._setup_mock_course_ids_responses_and_expects( mock_client, [self.new_course_id]) expected_courses = [self.new_course_id] self.assertEqual( permissions.get_user_course_permissions(self.user), expected_courses)
def test_setting_enabled(self): """ When ENABLE_AUTO_AUTH is set to True, the view should create and authenticate a new User. """ original_user_count = User.objects.count() response = self.client.get(self.auto_auth_path) self.assertEqual(response.status_code, 302) self.assertEqual(User.objects.count(), original_user_count + 1) user = self.get_latest_user() self.assertUserLoggedIn(user) self.assertTrue(user.username.startswith(settings.AUTO_AUTH_USERNAME_PREFIX)) self.assertListEqual(get_user_course_permissions(user), ['edX/DemoX/Demo_Course'])
def get_data(self): courses = permissions.get_user_course_permissions(self.request.user) if not courses: # The user is probably not a course administrator and should not be using this application. raise PermissionDenied presenter = CourseSummariesPresenter() summaries, _ = presenter.get_course_summaries(courses) if not summaries: # Instead of returning a useless blank CSV, return a 404 error raise Http404 # Exclude specified fields from each summary entry and render them to a CSV summaries = [ remove_keys(summary, self.exclude_fields) for summary in summaries ] summaries_csv = self.renderer.render(summaries) return summaries_csv
def get_context_data(self, **kwargs): context = super(CourseIndex, self).get_context_data(**kwargs) courses = permissions.get_user_course_permissions(self.request.user) if not courses: # The user is probably not a course administrator and should not be using this application. raise PermissionDenied presenter = CourseSummariesPresenter() summaries, last_updated = presenter.get_course_summaries(courses) context.update( {'update_message': self.get_last_updated_message(last_updated)}) data = { 'course_list_json': summaries, } context['js_data']['course'] = data context['page_data'] = self.get_page_data(context) return context
def test_course_permissions(self): user = self.do_login() authorized_courses = get_user_course_permissions(user) self.assertEqual(len(authorized_courses), 1) self.assertIn(DUMMY_AUTHORIZED_COURSE, authorized_courses)