def setUp(self): self.superuser = get_super_user() self.user = UserFactory() self.unauthorized_user = UserFactory() self.classroom = ClassroomFactory(created_by=self.user) self.enrollment = EnrollmentFactory(student=self.user, classroom=self.classroom)
def setUp(self): self.user = UserFactory() self.user_1 = UserFactory() self.user_2 = UserFactory() self.classroom = ClassroomFactory(created_by=self.user) self.enrollment_1 = EnrollmentFactory(classroom=self.classroom, student=self.user_1) self.enrollment_2 = EnrollmentFactory(classroom=self.classroom, student=self.user_2)
def setUp(self): self.superuser = get_super_user() self.user_1 = UserFactory() self.user_2 = UserFactory() self.classroom_1 = ClassroomFactory() self.classroom_2 = ClassroomFactory() self.enrollment_1 = EnrollmentFactory(student=self.user_1, classroom=self.classroom_1) self.enrollment_2 = EnrollmentFactory(student=self.user_2, classroom=self.classroom_2)
class EnrollmentTests(TestCase): def setUp(self): self.superuser = get_super_user() self.user_1 = UserFactory() self.user_2 = UserFactory() self.classroom_1 = ClassroomFactory() self.classroom_2 = ClassroomFactory() self.enrollment_1 = EnrollmentFactory(student=self.user_1, classroom=self.classroom_1) self.enrollment_2 = EnrollmentFactory(student=self.user_2, classroom=self.classroom_2) def test_enrollment_creation_between_user_and_classroom(self): self.assertEqual(Enrollment.objects.count(), 2) self.assertEqual(self.enrollment_1.student, self.user_1) self.assertEqual(self.enrollment_1.classroom, self.classroom_1) self.assertEqual(self.enrollment_2.student, self.user_2) self.assertEqual(self.enrollment_2.classroom, self.classroom_2) def test_enrollment_student_can_update_the_enrollment(self): self.assertTrue(self.enrollment_1.can_update(self.user_1)) def test_enrollement_cannot_be_updated_by_other_student(self): self.assertFalse(self.enrollment_1.can_update(self.user_2)) self.assertFalse(self.enrollment_2.can_update(self.user_1)) def test_enrollment_student_can_delete_the_enrollment(self): self.assertTrue(self.enrollment_2.can_update(self.user_2)) def test_enrollement_cannot_be_deleted_by_other_student(self): self.assertFalse(self.enrollment_1.can_delete(self.user_2)) self.assertFalse(self.enrollment_2.can_delete(self.user_1)) def test_super_user_can_update_any_enrollment(self): self.assertTrue(self.enrollment_1.can_update(self.superuser)) self.assertTrue(self.enrollment_2.can_update(self.superuser)) def test_super_user_can_delete_any_enrollment(self): self.assertTrue(self.enrollment_1.can_delete(self.superuser)) self.assertTrue(self.enrollment_2.can_delete(self.superuser))
def test_enrollment_deletion_raises_correct_message_for_non_existing_enrollment( self): self.assertEqual(Enrollment.objects.all().count(), 1) invalid_enrollment = EnrollmentFactory() invalid_enrollment_url_1 = invalid_enrollment.get_delete_url() invalid_enrollment_url_2 = f'/classroom/enroll/{uuid.uuid4()}/delete/' invalid_enrollment.delete() self.assertEqual(Enrollment.objects.all().count(), 1) self.client.force_login(self.user) response_1 = self.client.post(invalid_enrollment_url_1) response_2 = self.client.post(invalid_enrollment_url_2) self.assertEqual(response_1.status_code, 404) self.assertEqual(response_2.status_code, 404)
def setUp(self): self.super_user = get_user_model().objects.create_superuser( username='******', email='*****@*****.**', password='******') self.user_1 = UserFactory() self.user_2 = UserFactory() self.classroom_1 = ClassroomFactory() self.classroom_2 = ClassroomFactory() self.enroll_1 = EnrollmentFactory(student=self.user_1, classroom=self.classroom_1) self.enroll_2 = EnrollmentFactory(student=self.user_2, classroom=self.classroom_2) self.post_1 = PostFactory(author=self.user_1, classroom=self.classroom_1) self.post_2 = PostFactory(author=self.user_2, classroom=self.classroom_2)
def test_valid_code_for_existing_enrollment_does_not_create_new_enrollment( self): code = str(self.classroom.id) _ = EnrollmentFactory(student=self.user, classroom=self.classroom) self.client.force_login(self.user) response = self.client.post(reverse('enroll'), data={'code': code}) messages = list(get_messages(response.wsgi_request)) self.assertEqual(response.status_code, 302) self.assertEqual(len(messages), 1) self.assertIn('You are already enrolled in', str(messages[0])) self.assertEqual(Enrollment.objects.count(), 1)
class EnrollmentDeleteTests(TestCase): def setUp(self): self.superuser = get_super_user() self.user = UserFactory() self.unauthorized_user = UserFactory() self.classroom = ClassroomFactory(created_by=self.user) self.enrollment = EnrollmentFactory(student=self.user, classroom=self.classroom) def test_enrollment_delete_redirects_for_logged_out_user(self): response = self.client.get(self.enrollment.get_delete_url()) no_response = self.client.get('/enroll/1234/delete/') self.assertEqual(response.status_code, 302) self.assertTemplateNotUsed(response, 'classroom/enrollment_confirm_delete.html') self.assertEqual(no_response.status_code, 404) def test_enrollment_delete_view_works_for_logged_in_user(self): self.client.force_login(self.user) response = self.client.get(self.enrollment.get_delete_url()) no_response = self.client.get('/enroll/1234/delete/') self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'classroom/enrollment_confirm_delete.html') self.assertContains(response, 'Unenroll') self.assertNotContains(response, 'Hi I should not be on this page!') self.assertEqual(no_response.status_code, 404) def test_enrollment_delete_resolves_enrollmentdeleteview(self): view = resolve(self.enrollment.get_delete_url()) self.assertEqual(view.func.__name__, EnrollmentDelete.as_view().__name__) def test_enrollment_deletion_works_for_existing_enrollment_and_logged_in_user( self): self.assertEqual(Enrollment.objects.all().count(), 1) self.client.force_login(self.user) response = self.client.post(self.enrollment.get_delete_url()) self.assertEqual(response.status_code, 302) self.assertEqual(Enrollment.objects.all().count(), 0) messages = list(get_messages(response.wsgi_request)) self.assertEqual(len(messages), 1) self.assertEqual(str(messages[0]), 'You have unenrolled successfully!') def test_enrollment_deletion_raises_correct_message_for_non_existing_enrollment( self): self.assertEqual(Enrollment.objects.all().count(), 1) invalid_enrollment = EnrollmentFactory() invalid_enrollment_url_1 = invalid_enrollment.get_delete_url() invalid_enrollment_url_2 = f'/classroom/enroll/{uuid.uuid4()}/delete/' invalid_enrollment.delete() self.assertEqual(Enrollment.objects.all().count(), 1) self.client.force_login(self.user) response_1 = self.client.post(invalid_enrollment_url_1) response_2 = self.client.post(invalid_enrollment_url_2) self.assertEqual(response_1.status_code, 404) self.assertEqual(response_2.status_code, 404) def test_only_user_who_is_already_enrolled_can_unenroll_itself_while_others_cant( self): self.client.force_login(self.unauthorized_user) response = self.client.post(self.enrollment.get_delete_url()) self.assertEqual(response.status_code, 404) self.assertEqual(Enrollment.objects.all().count(), 1) def test_superuser_can_unenroll_any_student(self): self.client.force_login(self.superuser) response = self.client.post(self.enrollment.get_delete_url()) self.assertEqual(response.status_code, 302) self.assertEqual(Enrollment.objects.all().count(), 0) messages = list(get_messages(response.wsgi_request)) self.assertEqual(len(messages), 1) self.assertEqual(str(messages[0]), 'You have unenrolled successfully!')