def test_whodidit(self): author = UserFactory() quiz = QuizFactory() self.assertEqual(quiz.created_by, None) self.assertEqual(quiz.modified_by, None) mw = WhoDidItMiddleware(get_response) url = reverse('quiz-detail', kwargs={'pk': quiz.pk}) # get operations should leave the object unchanged request = RequestFactory().get(url) request.user = author mw.__call__(request) quiz.refresh_from_db() self.assertEqual(quiz.created_by, None) self.assertEqual(quiz.modified_by, None) # post operations should save the request.user request = RequestFactory().post(url) request.user = author mw.__call__(request) quiz.refresh_from_db() self.assertEqual(quiz.created_by, author) self.assertEqual(quiz.modified_by, author)
def test_send_template_email(self): user = UserFactory() with self.settings(SERVER_EMAIL='*****@*****.**'): send_template_email('foo', 'main/notify_lti_course_connect.txt', {'user': user}, '*****@*****.**') self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, 'foo') self.assertEquals(mail.outbox[0].from_email, '*****@*****.**') self.assertTrue(mail.outbox[0].to, ['*****@*****.**'])
def test_post_sis_course_id(self): with self.settings( COURSEAFFILS_COURSESTRING_MAPPER=CourseStringMapper): user = UserFactory() self.client.login(username=user.username, password='******') data = { 'lms_course': '1234', 'lms_course_title': 'LTI Course', 'sis_course_id': 'SOCWT7113_010_2017_3' } response = self.client.post(reverse('lti-course-create'), data) self.assertEqual(response.status_code, 302) c = Course.objects.get(title='LTI Course') self.assertEqual( c.group.name, 't3.y2017.s010.ct7113.socw.st.course:columbia.edu') self.assertEqual( c.faculty_group.name, 't3.y2017.s010.ct7113.socw.fc.course:columbia.edu') self.assertEqual(c.info.term, 3) self.assertEqual(c.info.year, 2017) self.assertTrue(user in c.group.user_set.all()) self.assertTrue(user in c.faculty_group.user_set.all()) self.assertEqual(len(mail.outbox), 2) self.assertEqual(mail.outbox[0].subject, 'Quizzing with Confidence Course Connected') self.assertEqual(mail.outbox[0].from_email, settings.SERVER_EMAIL) self.assertEqual(mail.outbox[0].to, [settings.SERVER_EMAIL]) self.assertEqual(mail.outbox[1].subject, 'Quizzing with Confidence Course Connected') self.assertEqual(mail.outbox[1].from_email, settings.SERVER_EMAIL) self.assertEqual(mail.outbox[1].to, [user.email]) LTICourseContext.objects.get(lms_course_context='1234', group=c.group, faculty_group=c.faculty_group) # try this again and make sure there is no duplication data['lms_course_title'] = 'LTI Course With More Detail' response = self.client.post(reverse('lti-course-create'), data) self.assertEqual(response.status_code, 302) Course.objects.get(title='LTI Course') Group.objects.get(name=c.group.name) Group.objects.get(name=c.faculty_group.name) LTICourseContext.objects.get(lms_course_context='1234', group=c.group, faculty_group=c.faculty_group)
def test_permissions(self): self.setup_course() alt_course = CourseFactory.create() alt_student = UserFactory() alt_faculty = UserFactory() alt_course.group.user_set.add(alt_student) alt_course.group.user_set.add(alt_faculty) alt_course.faculty_group.user_set.add(alt_faculty) quiz = QuizFactory(course=self.course) mixin = UpdateQuizPermissionMixin() mixin.request = RequestFactory().get('/') # failure - quiz does not exist mixin.kwargs = {'pk': 42} self.assertFalse(mixin.test_func()) # success - faculty in our course can edit & delete mixin.kwargs = {'pk': quiz.pk} mixin.request.user = self.faculty self.assertTrue(mixin.test_func()) # failure - student in our course cannot edit or delete mixin.request.user = self.student self.assertFalse(mixin.test_func()) # failure - faculty from another course cannot edit or delete mixin.request.user = alt_faculty self.assertFalse(mixin.test_func()) # failure - student from another course cannot edit or delete mixin.request.user = alt_student self.assertFalse(mixin.test_func()) # failure - superusers can't randomly edit or delete either mixin.request.user = UserFactory(is_staff=True, is_superuser=True) self.assertFalse(mixin.test_func())
def test_permissions(self): self.setup_course() su = UserFactory(is_staff=True, is_superuser=True) mixin = LoggedInSuperuserMixin() mixin.request = RequestFactory().get('/') # test failure path - user is faculty returns False mixin.request.user = self.faculty self.assertFalse(mixin.test_func()) # test failure path - user is student returns False mixin.request.user = self.student self.assertFalse(mixin.test_func()) # test success path - superuser returns True mixin.request.user = su self.assertTrue(mixin.test_func())
def test_post_course_context_with_unicode(self): with self.settings( COURSEAFFILS_COURSESTRING_MAPPER=CourseStringMapper): user = UserFactory() self.client.login(username=user.username, password='******') data = { 'lms_course': '1234', 'lms_course_title': 'LTI Course "Película", rødgrød med fløde', 'sis_course_id': '20170152049' } response = self.client.post(reverse('lti-course-create'), data) self.assertEqual(response.status_code, 302) c = Course.objects.get( title='LTI Course "Película", rødgrød med fløde') self.assertEqual(c.group.name, '1234') self.assertEqual(c.faculty_group.name, '1234_faculty') self.assertEqual(c.info.term, 1) self.assertEqual(c.info.year, 2017) self.assertTrue(user in c.group.user_set.all()) self.assertTrue(user in c.faculty_group.user_set.all()) LTICourseContext.objects.get(lms_course_context='1234', group=c.group, faculty_group=c.faculty_group) # try this again and make sure there is no duplication data['lms_course_title'] = 'LTI Course With More Detail' response = self.client.post(reverse('lti-course-create'), data) self.assertEqual(response.status_code, 302) Course.objects.get( title='LTI Course "Película", rødgrød med fløde') Group.objects.get(name=c.group.name) Group.objects.get(name=c.faculty_group.name) LTICourseContext.objects.get(lms_course_context='1234', group=c.group, faculty_group=c.faculty_group)
def reset_test_models(): call_command('flush', verbosity=0, interactive=False) # Create some models UserFactory.create(username='******', first_name='Super', last_name='User', email='*****@*****.**', is_superuser=True, is_staff=True) UserFactory.create(username='******', first_name='Faculty', last_name='One', email='*****@*****.**') UserFactory.create(username='******', first_name='Student', last_name='One', email='*****@*****.**')