def test_expired_access_token(self, mock_refresh_oauth_token): request = self.get_mock_request_with_token(expired=True) get_oauth_token(request) mock_refresh_oauth_token.assert_called_with(request) stub_canvas_oauth2_token = request.user.canvas_oauth2_token expires_buffer = settings.CANVAS_OAUTH_TOKEN_EXPIRATION_BUFFER self.assertTrue(stub_canvas_oauth2_token.stub_expires_within_called_with(expires_buffer))
def test_missing_token_error(self): mock_user = MagicMock() # Because of the way mock attributes are stored you can’t directly attach a PropertyMock to a mock object. # Instead you can attach it to the mock type object type(mock_user).canvas_oauth2_token = PropertyMock(side_effect=CanvasOAuth2Token.DoesNotExist()) request = RequestFactory().get('/index') request.user = mock_user with self.assertRaises(MissingTokenError): get_oauth_token(request)
def course_data(request, course_id): access_token = get_oauth_token(request) request_context = RequestContext(**settings.CANVAS_SDK_SETTINGS, auth_token=access_token) try: students = get_students_list(request_context, course_id) assignments = get_assignments_list(request_context, course_id) assignment_ids = [assignment['id'] for assignment in assignments] submissions = get_submissions_with_rubric_assessments( request_context, course_id, assignment_ids) except CanvasAPIError as e: logger.exception("Canvas API error") msg = "Canvas API error {status_code}".format( status_code=e.status_code) return JsonResponse({"message": msg}, status=500) payload = { 'assignments': assignments, 'submissions': submissions, 'students': students, } denormalized_data = denormalize(payload) payload['denormalized_data'] = denormalized_data return JsonResponse(payload)
def test_unexpired_access_token(self, mock_refresh_oauth_token): expected_access_token = "unexpired-access-token-123" request = self.get_mock_request_with_token( expired=False, access_token=expected_access_token) actual_access_token = get_oauth_token(request) self.assertFalse(mock_refresh_oauth_token.called) self.assertEqual(expected_access_token, actual_access_token)
def course_data(request, course_id): access_token = get_oauth_token(request) request_context = RequestContext(**settings.CANVAS_SDK_SETTINGS, auth_token=access_token) # request.user object returns the username cache_user_id_course_id = f"{request.user}{course_id}" try: assignments = get_assignments_list(request_context, course_id) assignment_ids = [assignment['id'] for assignment in assignments] submissions = get_submissions_with_rubric_assessments( request_context, course_id, assignment_ids) sections_list = get_sections_list(request_context, course_id) except CanvasAPIError as e: msg = f"Course ({course_id}) -- Canvas API error {e.status_code}" logger.exception(msg) return HttpResponseBadRequest() try: students_dict, sections_dict = students_sections_tuple(sections_list) rubric_assignments = rubric_assignments_dict(assignments) criteria_lookup = { criterion["id"]: criterion for assignment in rubric_assignments for criterion in rubric_assignments[assignment]["rubric"] } datapoints = datapoints_list(criteria_lookup, students_dict, submissions) except (StudentsSectionsError, RubricAssignmentsError, DatapointsError) as e: msg = f"Course ({course_id}) -- {e.__class__.__name__}: {e.message}" logger.exception(msg) return HttpResponseServerError() payload = { "students": students_dict, "sections": sections_dict, "rubric_assignments": rubric_assignments, "denormalized_data": datapoints } create_cache(cache_user_id_course_id, payload) return JsonResponse(payload)