def dispatch(self, request, *args, **kwargs): lti = LTI(self.request_type, self.role_type) # validate the user via oauth user = authenticate(request=request, lti=lti) if user is None: lti.clear_session(request) return render(request, 'lti_auth/fail_auth.html', {}) # login login(request, user) # check if course is configured try: ctx = LTICourseContext.objects.get( lms_course_context=lti.course_context()) except (KeyError, ValueError, LTICourseContext.DoesNotExist): return render( request, 'lti_auth/fail_course_configuration.html', { 'is_instructor': lti.is_instructor(), 'is_administrator': lti.is_administrator(), 'user': user, 'lms_course': lti.course_context(), 'lms_course_title': lti.course_title(), 'sis_course_id': lti.sis_course_id(), 'domain': lti.canvas_domain() }) # add user to the course self.join_groups(lti, ctx, user) self.lti = lti return super(LTIAuthMixin, self).dispatch(request, *args, **kwargs)
def test_verify_session(self): lti = LTI('session', 'any') request = RequestFactory().post('/lti/') with self.assertRaises(LTINotInSessionException): request.session = {} lti.verify(request) request.session = {LTI_SESSION_KEY: True} self.assertTrue(lti.verify(request))
def test_verify_any(self): lti = LTI('any', 'any') request = generate_lti_request() with self.settings(PYLTI_CONFIG={'consumers': CONSUMERS}): # test_verify_request lti.verify(request) self.assertTrue(request.session[LTI_SESSION_KEY]) # test_verify_session self.assertTrue(lti.verify(request))
def test_user_fullname(self): lti = LTI('initial', 'any') self.assertEquals(lti.user_fullname(), '') lti.lti_params = {'user_id': 'student_one'} self.assertEquals(lti.user_fullname(), 'student_one') lti.lti_params = BASE_LTI_PARAMS self.assertEquals(lti.user_fullname(), 'Foo Bar Baz')
def dispatch(self, request, *args, **kwargs): lti = LTI(self.request_type, self.role_type) # validate the user via oauth user = authenticate(request=request, lti=lti) if user is None: lti.clear_session(request) return render_to_response( 'lti_auth/fail_auth.html', {}, context_instance=RequestContext(request)) # check if course is configured try: ctx = lti.custom_course_context() except (KeyError, ValueError, LTICourseContext.DoesNotExist): lti.clear_session(request) return render_to_response( 'lti_auth/fail_course_configuration.html', {}, context_instance=RequestContext(request)) # add user to the course self.join_groups(lti, ctx, user) # login login(request, user) return super(LTIAuthMixin, self).dispatch(request, *args, **kwargs)
def test_user_roles(self): lti = LTI('initial', 'any') self.assertEquals(lti.user_roles(), []) lti.lti_params = BASE_LTI_PARAMS self.assertEquals(lti.user_roles(), [ u'urn:lti:instrole:ims/lis/instructor', u'urn:lti:instrole:ims/lis/staff' ]) self.assertTrue(lti.is_instructor()) self.assertFalse(lti.is_administrator())
def test_user_roles(self): lti = LTI('initial', 'any') self.assertEquals(lti.user_roles(), []) lti.lti_params = BASE_LTI_PARAMS self.assertEquals(lti.user_roles(), [ u'urn:lti:instrole:ims/lis/instructor', u'urn:lti:instrole:ims/lis/staff']) self.assertTrue(lti.is_instructor()) self.assertFalse(lti.is_administrator())
def test_verify_request(self): with self.settings(PYLTI_CONFIG={'consumers': CONSUMERS}): request = generate_lti_request() lti = LTI('initial', 'any') lti.verify(request) self.assertTrue(request.session[LTI_SESSION_KEY])
def test_consumers(self): lti = LTI('any', 'any') with self.settings(PYLTI_CONFIG={'consumers': CONSUMERS}): self.assertEquals(lti._consumers(), CONSUMERS)
def test_user_email(self): lti = LTI('initial', 'any') self.assertIsNone(lti.user_email()) lti.lti_params = BASE_LTI_PARAMS self.assertEquals(lti.user_email(), '*****@*****.**')
def test_custom_course_context(self): lti = LTI('initial', 'any') with self.assertRaises(KeyError): lti.custom_course_context() lti.lti_params = BASE_LTI_PARAMS lti.lti_params['custom_course_context'] = 434 with self.assertRaises(LTICourseContext.DoesNotExist): lti.custom_course_context() ctx = LTICourseContextFactory() lti.lti_params['custom_course_context'] = 'abc' with self.assertRaises(ValueError): lti.custom_course_context() lti.lti_params['custom_course_context'] = ctx.uuid with self.assertRaises(LTICourseContext.DoesNotExist): lti.custom_course_context() ctx.enable = True ctx.save() self.assertEquals(lti.custom_course_context(), ctx)
def test_consumer_user_id(self): lti = LTI('initial', 'any') lti.lti_params = BASE_LTI_PARAMS.copy() lti.lti_params['oauth_consumer_key'] = '1234567890' self.assertEquals(lti.consumer_user_id(), '1234567890-student')
def test_init(self): lti = LTI('initial', 'any') self.assertEquals(lti.request_type, 'initial') self.assertEquals(lti.role_type, 'any')
def setUp(self): self.lti = LTI('initial', 'any') self.lti.lti_params = BASE_LTI_PARAMS.copy()
def test_user_roles(self): lti = LTI('initial', 'any') self.assertEquals(lti.user_roles(), []) lti.lti_params = BASE_LTI_PARAMS self.assertEquals(lti.user_roles(), ['Instructor', 'Staff'])