def setUp(self): self.course = CourseFactory() self.user = BaseUserFactory() self.user.is_active = True self.user.save() self.course_description = CourseDescriptionFactory(course=self.course) self.application_info = ApplicationInfoFactory(course=self.course_description)
def setUp(self): self.user = BaseUserFactory() self.user.is_active = True self.user.save() self.application = ApplicationFactory(user=self.user) self.interview = InterviewFactory(application=self.application, has_confirmed=False) self.client = Client()
def test_baseuser_not_access_generate_interview(self): user = BaseUserFactory() user.is_active = True user.save() with self.login(username=user.email, password=BaseUserFactory.password): response = self.get('interview_system:generate_interviews') self.assertEquals(response.status_code, 302)
class CreateStudentNoteTests(TestCase): def setUp(self): self.baseuser = BaseUserFactory() self.baseuser.is_active = True self.baseuser.save() self.baseuser2 = BaseUserFactory() self.baseuser2.is_active = True self.baseuser2.save() self.student = BaseUser.objects.promote_to_student(self.baseuser2) self.course = CourseFactory() self.course_assignment = CourseAssignmentFactory(course=self.course, user=self.student) def test_cannot_create_student_notes_if_no_login(self): response = self.post('education:student-note-create', course=self.course.id) self.assertEquals(response.status_code, 302) def test_student_cannot_create_student_notes(self): with self.login(email=self.student.email, password=BaseUserFactory.password): response = self.post('education:student-note-create', course=self.course.id) self.assertEquals(response.status_code, 403) def test_base_cannot_create_student_notes(self): with self.login(email=self.baseuser.email, password=BaseUserFactory.password): response = self.post('education:student-note-create', course=self.course.id) self.assertEquals(response.status_code, 403) def test_teacher_can_create_student_notes(self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) teacher.teached_courses = [self.course] with self.login(email=teacher.email, password=BaseUserFactory.password): data = { 'text': faker.text(), 'assignment': self.course_assignment.id } response = self.post('education:student-note-create', course=self.course.id, data=data, follow=True) self.assertEquals(response.status_code, 200) self.assertEqual( 1, StudentNote.objects.filter( assignment=self.course_assignment).count()) self.assertEqual( teacher, StudentNote.objects.filter( assignment=self.course_assignment).first().author)
def test_access_choose_from_user_without_application(self): user_without_app = BaseUserFactory() user_without_app.is_active = True user_without_app.save() with self.login(username=user_without_app.email, password=BaseUserFactory.password): response = self.get('interview_system:choose_interview', self.application.id, self.interview.uuid) self.assertEqual(response.status_code, 404)
class DropStudentTests(TestCase): def setUp(self): self.baseuser = BaseUserFactory() self.baseuser.is_active = True self.baseuser.save() self.baseuser2 = BaseUserFactory() self.baseuser2.is_active = True self.baseuser2.save() self.student = BaseUser.objects.promote_to_student(self.baseuser2) self.course = CourseFactory() self.course_assignment = CourseAssignmentFactory(course=self.course, user=self.student) def test_cannot_drop_student_if_not_logged_in(self): response = self.post('education:drop-student', course=self.course.id, ca=self.course_assignment.id) self.assertEqual(response.status_code, 302) def test_non_teachers_cannot_drop_student(self): with self.login(email=self.baseuser2.email, password=BaseUserFactory.password): response = self.post('education:drop-student', course=self.course.id, ca=self.course_assignment.id) self.assertEqual(response.status_code, 403) def test_teacher_cannot_drop_students_from_other_courses(self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser2) course = CourseFactory() teacher.teached_courses = [course] teacher.save() with self.login(email=teacher.email, password=BaseUserFactory.password): response = self.post('education:drop-student', course=self.course.id, ca=self.course_assignment.id) self.assertEqual(response.status_code, 403) def test_teacher_can_drop_student_from_course_if_he_teaches_it(self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser2) teacher.teached_courses = [self.course] teacher.save() self.assertTrue(self.course_assignment.is_attending) with self.login(email=teacher.email, password=BaseUserFactory.password): response = self.post('education:drop-student', course=self.course.id, ca=self.course_assignment.id) self.assertEqual(response.status_code, 302) self.course_assignment.refresh_from_db() self.assertFalse(self.course_assignment.is_attending)
def setUp(self): self.baseuser = BaseUserFactory() self.baseuser.is_active = True self.baseuser.save() self.student = BaseUser.objects.promote_to_student(self.baseuser) self.course = CourseFactory() self.course_assignment = CourseAssignmentFactory(course=self.course, user=self.student) self.certificate = CertificateFactory( assignment=self.course_assignment)
def setUp(self): self.baseuser = BaseUserFactory() self.baseuser.is_active = True self.baseuser.save() self.baseuser2 = BaseUserFactory() self.baseuser2.is_active = True self.baseuser2.save() self.student = BaseUser.objects.promote_to_student(self.baseuser2) self.course = CourseFactory() self.task = TaskFactory(course=self.course) self.course_assignment = CourseAssignmentFactory(course=self.course, user=self.student)
def get_valid_token(self): self.user = BaseUserFactory() self.user.is_active = True self.user.save() data = {'email': self.user.email, 'password': BaseUserFactory.password} response = self.client.post(self.reverse('hack_fmi:api-login'), data=data, format='json') self.response_200(response) token = response.data.get('token') self.assertIsNotNone(token) return token
def test_create_file_if_there_is_db(self): baseuser = BaseUserFactory() baseuser.is_active = True baseuser.save() student = BaseUser.objects.promote_to_student(baseuser) course = CourseFactory() CourseAssignmentFactory(course=course, user=student) course1 = CourseFactory() course2 = CourseFactory() self.assertFalse(os.path.exists('working_ats.csv')) call_command('create_csv_with_all_workingats', "{}, {}".format(course1.id, course2.id)) self.assertTrue(os.path.exists('working_ats.csv'))
def test_teacher_can_see_student_list_only_for_his_course(self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) teacher.teached_courses = [self.course] baseuser3 = BaseUserFactory() baseuser3.is_active = True baseuser3.save() student = BaseUser.objects.promote_to_student(baseuser3) course2 = CourseFactory() course_assignment_for_baseuser3 = CourseAssignmentFactory( course=course2, user=student) with self.login(email=teacher.email, password=BaseUserFactory.password): response = self.get('education:student-list', course=self.course.id) self.assertEqual(response.status_code, 200) self.assertIn(self.course_assignment, response.context['object_list']) self.assertNotIn(course_assignment_for_baseuser3, response.context['object_list'])
def setUp(self): self.baseuser = BaseUserFactory() self.baseuser.is_active = True self.baseuser.save() self.baseuser2 = BaseUserFactory() self.baseuser2.is_active = True self.baseuser2.save() self.student = BaseUser.objects.promote_to_student(self.baseuser2) self.student.mac = "22:44:55:66:77" self.course = CourseFactory() self.ca = CourseAssignmentFactory(course=self.course, user=self.student) self.task = TaskFactory(course=self.course, gradable=True) self.baseuser3 = BaseUserFactory() self.baseuser3.is_active = True self.baseuser3.save()
class TestInvitationConsumer(ChannelTestCase, TestCase): "On post save of Invitation object, we send a massive message to group 'Invitations'" def get_valid_token(self): self.user = BaseUserFactory() self.user.is_active = True self.user.save() data = {'email': self.user.email, 'password': BaseUserFactory.password} response = self.client.post(self.reverse('hack_fmi:api-login'), data=data, format='json') self.response_200(response) token = response.data.get('token') self.assertIsNotNone(token) return token # Test signal works properly def test_server_group_send_message_to_client_on_post_save_of_invitation( self): competitor = CompetitorFactory() # Add test-channel to Invitation example group expected_group_name = settings.INVITATION_GROUP_NAME.format( id=competitor.id) Group(expected_group_name).add("test-channel") team = TeamFactory(season__is_active=True) TeamMembershipFactory(team=team, competitor=competitor, is_leader=True) InvitationFactory(team=team, competitor=competitor) # Get the message that is transferred into the channel result = self.get_next_message("test-channel") result = json.loads(result.get('text')) self.assertEqual(result['message'], "New invitation was created.") self.assertEqual(result['leader'], competitor.full_name) def test_connection_is_closed_when_token_is_empty_string(self): # In receive message we expect to receive token field channel_name = u"websocket.receive" text = {"token": ""} Channel(channel_name).send({ 'path': '/', 'text': json.dumps(text), "reply_channel": "angular_client" }) message = self.get_next_message(channel_name, require=True) InvitationConsumer(message) result = self.get_next_message(message.reply_channel.name, require=True) self.assertTrue(result.get('close')) def test_non_authenticated_user_is_not_added_to_group(self): channel_name = u"websocket.receive" invalid_token = self.get_valid_token() + "invalid" text = {"token": invalid_token} Channel(channel_name).send({ 'path': '/', 'text': json.dumps(text), "reply_channel": "angular_client" }) message = self.get_next_message(channel_name, require=True) InvitationConsumer(message) result = self.get_next_message(message.reply_channel.name, require=True) self.assertTrue(result.get('close')) def test_authenticated_user_is_added_to_group(self): token = self.get_valid_token() # Channel name maps the method that is expected to be run -> InvitationConsumer.method_mapping channel_name = u"websocket.receive" text = {"token": token} # Open a channel connection called 'connection' and pass data to server from reply_channel # reply_channel is the other hand of the connection /angular client/ Channel(channel_name).send({ 'path': '/', 'text': json.dumps(text), "reply_channel": "angular_client" }) message = self.get_next_message(channel_name, require=True) InvitationConsumer(message) result = self.get_next_message(message.reply_channel.name, require=True) self.assertEquals(result.get("text"), InvitationConsumer.USER_ADDED_TO_GROUP_MESSAGE) def test_authentication_failed_if_there_is_no_token_key_in_sended_messsage( self): channel_name = u"websocket.receive" text = {"non_token_field": faker.word()} Channel(channel_name).send({ 'path': '/', 'text': json.dumps(text), "reply_channel": "angular_client" }) message = self.get_next_message(channel_name, require=True) InvitationConsumer(message) result = self.get_next_message(message.reply_channel.name, require=True) self.assertTrue(result.get('close'))
def test_non_hackfmi_user_can_not_access(self): user = BaseUserFactory() request = make_mock_object(user=user) self.assertFalse( self.permission.has_permission(request=request, view=None))
def setUp(self): self.base_user = BaseUserFactory() self.course = CourseFactory()
def setUp(self): self.baseuser = BaseUserFactory() self.baseuser.is_active = True self.baseuser.save()
class TeacherTaskListViewTests(TestCase): def setUp(self): self.baseuser = BaseUserFactory() self.baseuser.is_active = True self.baseuser.save() self.teacher = BaseUser.objects.promote_to_teacher(self.baseuser) self.baseuser2 = BaseUserFactory() self.baseuser2.is_active = True self.baseuser2.save() self.student = BaseUser.objects.promote_to_student(self.baseuser2) self.course = CourseFactory() self.course_assignment = CourseAssignmentFactory(course=self.course, user=self.student) self.task = TaskFactory(course=self.course) def test_no_access_to_task_list_without_login(self): response = self.get('education:student-task-list', course=self.course.id, student=faker.random_int()) self.assertEquals(response.status_code, 302) def test_baseuser_cannot_access_task_list(self): with self.login(email=self.baseuser.email, password=BaseUserFactory.password): response = self.get('education:student-task-list', course=self.course.id, student=faker.random_int()) self.assertEqual(response.status_code, 403) def test_student_cannot_access_task_list(self): with self.login(email=self.student.email, password=BaseUserFactory.password): response = self.get('education:student-task-list', course=self.course.id, student=self.student.id) self.assertEqual(response.status_code, 403) def test_teacher_cannot_access_course_student_task_list_if_he_doesnt_teach_it( self): with self.login(email=self.teacher.email, password=BaseUserFactory.password): response = self.get('education:student-task-list', course=self.course.id, student=self.student.id) self.assertEqual(response.status_code, 403) def test_teacher_can_access_course_student_task_list_if_he_teaches_it( self): self.teacher.teached_courses = [self.course] with self.login(email=self.teacher.email, password=BaseUserFactory.password): response = self.get('education:student-task-list', course=self.course.id, student=self.student.id) self.assertEqual(response.status_code, 200) self.assertIn(self.task, response.context['object_list']) def test_teacher_can_see_student_task_only_course_if_he_teaches_it(self): course2 = CourseFactory() TaskFactory(course=course2) self.teacher.teached_courses = [self.course] with self.login(email=self.teacher.email, password=BaseUserFactory.password): response = self.get('education:student-task-list', course=course2.id, student=self.student.id) self.assertEqual(response.status_code, 403) def test_teacher_can_see_student_task_list_only_if_he_teaches_course(self): course2 = CourseFactory() task2 = TaskFactory(course=course2) self.teacher.teached_courses = [self.course] with self.login(email=self.teacher.email, password=BaseUserFactory.password): response = self.get('education:student-task-list', course=self.course.id, student=self.student.id) self.assertEqual(response.status_code, 200) self.assertIn(self.task, response.context['object_list']) self.assertNotIn(task2, response.context['object_list'])
class TeacherSolutionListViewTests(TestCase): def setUp(self): self.baseuser = BaseUserFactory() self.baseuser.is_active = True self.baseuser.save() self.teacher = BaseUser.objects.promote_to_teacher(self.baseuser) self.baseuser2 = BaseUserFactory() self.baseuser2.is_active = True self.baseuser2.save() self.student = BaseUser.objects.promote_to_student(self.baseuser2) self.course = CourseFactory() self.course_assignment = CourseAssignmentFactory(course=self.course, user=self.student) self.task = TaskFactory(course=self.course) self.task2 = TaskFactory(course=self.course) def test_no_access_to_solution_list_without_login(self): response = self.get('education:student-solution-list', course=self.course.id, student=self.student.id, task=self.task.id) self.assertEquals(response.status_code, 302) def test_baseuser_cannot_access_solution_list(self): with self.login(email=self.baseuser.email, password=BaseUserFactory.password): response = self.get('education:student-solution-list', course=self.course.id, student=self.student.id, task=self.task.id) self.assertEqual(response.status_code, 403) def test_student_cannot_access_solution_list(self): with self.login(email=self.student.email, password=BaseUserFactory.password): response = self.get('education:student-solution-list', course=self.course.id, student=self.student.id, task=self.task.id) self.assertEqual(response.status_code, 403) def test_teacher_cannot_access_course_student_solution_list_if_he_doesnt_teach_it( self): with self.login(email=self.teacher.email, password=BaseUserFactory.password): response = self.get('education:student-solution-list', course=self.course.id, student=self.student.id, task=self.task.id) self.assertEqual(response.status_code, 403) def test_teacher_can_access_course_student_solution_list_if_he_teach_it( self): self.teacher.teached_courses = [self.course] solution = SolutionFactory(task=self.task, student=self.student) solution2 = SolutionFactory(task=self.task, student=self.student) with self.login(email=self.teacher.email, password=BaseUserFactory.password): response = self.get('education:student-solution-list', course=self.course.id, student=self.student.id, task=self.task.id) self.assertEqual(response.status_code, 200) self.assertIn(solution, response.context['object_list']) self.assertIn(solution, response.context['solution_list']) self.assertIn(solution2, response.context['object_list']) self.assertIn(solution2, response.context['solution_list'])
class ChooseInterviewTests(TestCase): def setUp(self): self.user = BaseUserFactory() self.user.is_active = True self.user.save() self.application = ApplicationFactory(user=self.user) self.interview = InterviewFactory(application=self.application, has_confirmed=False) self.client = Client() def test_access_choose_new_interview(self): confirmed_interviews = Interview.objects.filter(application__isnull=False, has_confirmed=True) confirmed_interviews_count = confirmed_interviews.count() self.assertEqual(confirmed_interviews_count + 1, Interview.objects.count()) self.assertEqual(self.interview.application.user, self.user) self.client.login(email=self.user.email, password=BaseUserFactory.password) url = reverse('interview_system:choose_interview', kwargs={"application": self.application.id, "interview_token": self.interview.uuid}) self.assertEqual(self.interview.application.id, self.application.id) response = self.client.get(url) self.assertEqual(response.status_code, 200) def test_choose_new_interview(self): confirmed_interviews = Interview.objects.filter(application__isnull=False, has_confirmed=True) self.assertEqual(0, confirmed_interviews.count()) self.assertEqual(self.interview.application.user, self.user) self.client.login(email=self.user.email, password=BaseUserFactory.password) url = reverse('interview_system:choose_interview', kwargs={"application": self.application.id, "interview_token": self.interview.uuid}) self.assertEqual(self.interview.application.id, self.application.id) free_interview = InterviewFactory(application=None) data = { 'application': self.application.id, 'interview_token': free_interview.uuid } url = reverse('interview_system:choose_interview', kwargs={"application": self.application.id, "interview_token": free_interview.uuid}) response = self.client.post(url, data) self.assertEqual(response.status_code, 302) self.assertRedirects(response, reverse('interview_system:confirm_interview', kwargs={"application": self.application.id, "interview_token": free_interview.uuid})) def test_access_confirm_interview(self): with self.login(username=self.user.email, password=BaseUserFactory.password): url = reverse('interview_system:confirm_interview', kwargs={"application": self.application.id, "interview_token": self.interview.uuid}) response = self.client.get(url) self.assertEqual(response.status_code, 200) def test_confirm_interview_from_confirm_page(self): with self.login(username=self.user.email, password=BaseUserFactory.password): url = reverse('interview_system:confirm_interview', kwargs={"application": self.application.id, "interview_token": self.interview.uuid}) response = self.client.post(url) self.assertEqual(response.status_code, 200) self.interview.refresh_from_db() self.assertEqual(self.interview.has_confirmed, True) def test_unsigned_user_access_confirm_page_redirects_to_login(self): url = reverse('interview_system:confirm_interview', kwargs={"application": self.application.id, "interview_token": self.interview.uuid}) response = self.client.get(url) self.assertEqual(response.status_code, 302) def test_unsigned_user_access_choose_page_redirects_to_login(self): url = reverse('interview_system:choose_interview', kwargs={"application": self.application.id, "interview_token": self.interview.uuid}) response = self.client.get(url) self.assertEqual(response.status_code, 302) def test_choose_new_interview_after_already_confirmed(self): with self.login(username=self.user.email, password=BaseUserFactory.password): self.post('interview_system:confirm_interview', self.application.id, self.interview.uuid) self.interview.refresh_from_db() self.assertEqual(Interview.objects.filter( has_confirmed=True).count(), 1) response = self.get('interview_system:choose_interview', self.application.id, self.interview.uuid, follow=True) red_url = reverse('interview_system:confirm_interview', kwargs={"application": self.application.id, "interview_token": self.interview.uuid}) self.assertRedirects(response, red_url) def test_confirm_non_existing_interview(self): uuid1 = uuid.uuid4() self.assertEqual(self.interview.uuid != uuid1, True) with self.login(username=self.user.email, password=BaseUserFactory.password): response = self.get('interview_system:confirm_interview', self.application.id, uuid1) self.assertEqual(response.status_code, 404) def test_confirm_non_existing_application(self): app = ApplicationFactory(has_interview_date=False) self.assertEqual(self.application.user != app.user, True) with self.login(username=self.user.email, password=BaseUserFactory.password): response = self.get('interview_system:confirm_interview', app.id, self.interview.uuid) self.assertEqual(response.status_code, 404) def test_access_confirm_from_user_without_application(self): user_without_app = BaseUserFactory() user_without_app.is_active = True user_without_app.save() with self.login(username=user_without_app.email, password=BaseUserFactory.password): response = self.get('interview_system:confirm_interview', self.application.id, self.interview.uuid) self.assertEqual(response.status_code, 404) def test_access_choose_from_user_without_application(self): user_without_app = BaseUserFactory() user_without_app.is_active = True user_without_app.save() with self.login(username=user_without_app.email, password=BaseUserFactory.password): response = self.get('interview_system:choose_interview', self.application.id, self.interview.uuid) self.assertEqual(response.status_code, 404)
class MaterialListViewTests(TestCase): def setUp(self): self.baseuser = BaseUserFactory() self.baseuser.is_active = True self.baseuser.save() self.baseuser2 = BaseUserFactory() self.baseuser2.is_active = True self.baseuser2.save() self.student = BaseUser.objects.promote_to_student(self.baseuser2) self.course = CourseFactory() self.course_assignment = CourseAssignmentFactory(course=self.course, user=self.student) def test_no_access_to_material_list_without_login(self): week = WeekFactory() LectureFactory(week=week, course=self.course) MaterialFactory(week=week, course=self.course) response = self.get('education:material-list', course=self.course.id) self.assertEquals(response.status_code, 302) def test_student_can_access_course_materials(self): week = WeekFactory() LectureFactory(week=week, course=self.course) MaterialFactory(week=week, course=self.course) with self.login(email=self.student.email, password=BaseUserFactory.password): response = self.get('education:material-list', course=self.course.id) self.assertEqual(response.status_code, 200) def test_student_cannot_access_other_courses_materials(self): course2 = CourseFactory() week = WeekFactory() LectureFactory(week=week, course=course2) MaterialFactory(week=week, course=course2) with self.login(email=self.student.email, password=BaseUserFactory.password): response = self.get('education:material-list', course=course2.id) self.assertEqual(response.status_code, 403) def test_baseuser_cannot_access_course_materials(self): week = WeekFactory() LectureFactory(week=week, course=self.course) MaterialFactory(week=week, course=self.course) with self.login(email=self.baseuser.email, password=BaseUserFactory.password): response = self.get('education:material-list', course=self.course.id) self.assertEqual(response.status_code, 403) def test_teacher_can_access_course_materials(self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) teacher.teached_courses = [self.course] week = WeekFactory() LectureFactory(week=week, course=self.course) MaterialFactory(week=week, course=self.course) with self.login(email=teacher.email, password=BaseUserFactory.password): response = self.get('education:material-list', course=self.course.id) self.assertEqual(response.status_code, 200) def test_student_can_access_course_materials_if_no_materials(self): with self.login(email=self.student.email, password=BaseUserFactory.password): response = self.get('education:material-list', course=self.course.id) self.assertEqual(response.status_code, 200) def test_baseuser_cannot_access_course_materials_if_no_materials(self): with self.login(email=self.baseuser.email, password=BaseUserFactory.password): response = self.get('education:material-list', course=self.course.id) self.assertEqual(response.status_code, 403) def test_teacher_can_access_course_materials_if_no_materials(self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) teacher.teached_courses = [self.course] with self.login(email=teacher.email, password=BaseUserFactory.password): response = self.get('education:material-list', course=self.course.id) self.assertEqual(response.status_code, 200) def test_teacher_cannot_access_no_materials_of_course_not_in_teached_courses( self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) teacher.teached_courses = [self.course] course2 = CourseFactory() with self.login(email=teacher.email, password=BaseUserFactory.password): response = self.get('education:material-list', course=course2.id) self.assertEqual(response.status_code, 403) def test_teacher_cannot_access_materials_of_course_not_in_teached_courses( self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) teacher.teached_courses = [self.course] week = WeekFactory() course2 = CourseFactory() LectureFactory(week=week, course=self.course) MaterialFactory(week=week, course=self.course) with self.login(email=teacher.email, password=BaseUserFactory.password): response = self.get('education:material-list', course=course2.id) self.assertEqual(response.status_code, 403)
class CertificatesTests(TestCase): def setUp(self): self.baseuser = BaseUserFactory() self.baseuser.is_active = True self.baseuser.save() self.student = BaseUser.objects.promote_to_student(self.baseuser) self.course = CourseFactory() self.course_assignment = CourseAssignmentFactory(course=self.course, user=self.student) self.certificate = CertificateFactory( assignment=self.course_assignment) def test_everyone_can_see_certificate(self): response = self.get("education:certificate-detail", token=self.certificate.token) self.assertEqual(200, response.status_code) def test_bad_request_is_returned_if_id_is_passed_instead_of_uuid(self): response = self.get('education:certificate-detail', token=self.certificate.id) self.response_404(response) def test_context_of_the_certificate(self): url_tasks = TaskFactory.create_batch(5, course=self.course, gradable=False) gradable_task1 = TaskFactory(course=self.course, gradable=True) gradable_task2 = TaskFactory(course=self.course, gradable=True) SolutionFactory(task=gradable_task1, student=self.student, status=3) SolutionFactory(task=gradable_task1, student=self.student, status=2) SolutionFactory(task=gradable_task2, student=self.student, status=3) [ SolutionFactory(task=task, status=6, student=self.student) for task in url_tasks ] response = self.get("education:certificate-detail", token=self.certificate.token) self.assertEqual(200, response.status_code) self.assertEqual(2, len(response.context["gradable_tasks"])) self.assertEqual(5, len(response.context["url_tasks"])) url_solutions_statuses = [ task['solution'] for task in response.context["url_tasks"] if task['solution'] != "Not sent" ] gradable_passed_solutions = [ task['solution_status'] for task in response.context["gradable_tasks"] if task['solution_status'] == "PASS" ] gradable_failed_solutions = [ task['solution_status'] for task in response.context["gradable_tasks"] if task['solution_status'] == "FAIL" ] self.assertEqual(5, len(url_solutions_statuses)) self.assertEqual(1, len(gradable_passed_solutions)) self.assertEqual(1, len(gradable_failed_solutions)) def tearDown(self): delete_cache_for_courseassingment(self.course_assignment)
class CourseListViewTests(TestCase): def setUp(self): self.baseuser = BaseUserFactory() self.baseuser.is_active = True self.baseuser.save() def test_not_access_course_list_without_login(self): response = self.get('education:course-list') self.assertEquals(response.status_code, 302) def test_baseuser_not_access_courselist(self): with self.login(username=self.baseuser.email, password=BaseUserFactory.password): response = self.get('education:course-list') self.assertEqual(response.status_code, 403) def test_student_can_access_courselist(self): student = BaseUser.objects.promote_to_student(self.baseuser) with self.login(username=student.email, password=BaseUserFactory.password): response = self.get('education:course-list') self.assertEqual(response.status_code, 200) def test_teacher_can_access_courselist(self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) with self.login(username=teacher.email, password=BaseUserFactory.password): response = self.get('education:course-list') self.assertEqual(response.status_code, 200) def test_student_can_see_only_courses_for_which_have_courseassignments( self): student = BaseUser.objects.promote_to_student(self.baseuser) course = CourseFactory() course2 = CourseFactory() CourseAssignmentFactory(course=course, user=student) with self.login(username=student.email, password=BaseUserFactory.password): response = self.get('education:course-list') self.assertEqual(response.status_code, 200) self.assertIn(course, response.context['student_courses']) self.assertNotIn(course2, response.context['student_courses']) self.assertNotIn('teacher_courses', response.context) def test_teacher_can_see_only_courses_for_which_is_teacher(self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) course = CourseFactory() teacher.teached_courses = [course] course2 = CourseFactory() with self.login(username=teacher.email, password=BaseUserFactory.password): response = self.get('education:course-list') self.assertEqual(response.status_code, 200) self.assertIn(course, response.context['teacher_courses']) self.assertNotIn(course2, response.context['teacher_courses']) self.assertNotIn('student_courses', response.context) def test_common_teacher_and_student_user_can_see_courses_for_which_ca_or_is_teacher( self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) student = BaseUser.objects.promote_to_student(self.baseuser) course = CourseFactory() teacher.teached_courses = [course] course2 = CourseFactory() CourseAssignmentFactory(course=course2, user=student) with self.login(username=teacher.email, password=BaseUserFactory.password): response = self.get('education:course-list') self.assertEqual(response.status_code, 200) self.assertIn(course, response.context['teacher_courses']) self.assertNotIn(course2, response.context['teacher_courses']) self.assertIn(course2, response.context['student_courses']) self.assertNotIn(course, response.context['student_courses'])
class StudentListViewTests(TestCase): def setUp(self): self.baseuser = BaseUserFactory() self.baseuser.is_active = True self.baseuser.save() self.baseuser2 = BaseUserFactory() self.baseuser2.is_active = True self.baseuser2.save() self.student = BaseUser.objects.promote_to_student(self.baseuser2) self.course = CourseFactory() self.course_assignment = CourseAssignmentFactory(course=self.course, user=self.student) def test_cannot_access_student_list_if_no_login(self): response = self.get('education:student-list', course=self.course.id) self.assertEquals(response.status_code, 302) def test_student_cannot_access_student_list(self): with self.login(email=self.student.email, password=BaseUserFactory.password): response = self.get('education:student-list', course=self.course.id) self.assertEquals(response.status_code, 403) def test_teacher_can_access_student_list(self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) teacher.teached_courses = [self.course] with self.login(email=teacher.email, password=BaseUserFactory.password): response = self.get('education:student-list', course=self.course.id) self.assertEqual(response.status_code, 200) self.assertIn(self.course_assignment, response.context['object_list']) def test_teacher_cannot_access_student_list_if_not_teached_it(self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) with self.login(email=teacher.email, password=BaseUserFactory.password): response = self.get('education:student-list', course=self.course.id) self.assertEqual(response.status_code, 403) def test_teacher_can_see_student_list_only_for_his_course(self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) teacher.teached_courses = [self.course] baseuser3 = BaseUserFactory() baseuser3.is_active = True baseuser3.save() student = BaseUser.objects.promote_to_student(baseuser3) course2 = CourseFactory() course_assignment_for_baseuser3 = CourseAssignmentFactory( course=course2, user=student) with self.login(email=teacher.email, password=BaseUserFactory.password): response = self.get('education:student-list', course=self.course.id) self.assertEqual(response.status_code, 200) self.assertIn(self.course_assignment, response.context['object_list']) self.assertNotIn(course_assignment_for_baseuser3, response.context['object_list'])
class StudentDetailView(TestCase): def setUp(self): self.baseuser = BaseUserFactory() self.baseuser.is_active = True self.baseuser.save() self.baseuser2 = BaseUserFactory() self.baseuser2.is_active = True self.baseuser2.save() self.student = BaseUser.objects.promote_to_student(self.baseuser2) self.student.mac = "22:44:55:66:77" self.course = CourseFactory() self.ca = CourseAssignmentFactory(course=self.course, user=self.student) self.task = TaskFactory(course=self.course, gradable=True) self.baseuser3 = BaseUserFactory() self.baseuser3.is_active = True self.baseuser3.save() def test_cannot_access_student_list_if_no_login(self): response = self.get('education:student-detail', course=self.course.id, ca=self.ca.id) self.assertEquals(response.status_code, 302) def test_student_cannot_access_student_list(self): with self.login(email=self.student.email, password=BaseUserFactory.password): response = self.get('education:student-detail', course=self.course.id, ca=self.ca.id) self.assertEquals(response.status_code, 403) def test_teacher_can_see_student_detail_information(self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) teacher.teached_courses = [self.course] solution1 = SolutionFactory(task=self.task, student=self.student) solution1.status = 3 solution1.save() solution2 = SolutionFactory(task=self.task, student=self.student) solution2.status = 2 solution2.save() solution3 = SolutionFactory(task=self.task, student=self.student) solution3.status = 2 solution3.save() url_task = TaskFactory(course=self.course, gradable=False) SolutionFactory(task=url_task, student=self.student) LectureFactory(course=self.course, date=datetime.now().date() - timedelta(days=9)) LectureFactory(course=self.course, date=datetime.now().date() - timedelta(days=7)) LectureFactory(course=self.course, date=datetime.now().date() - timedelta(days=5)) LectureFactory(course=self.course, date=datetime.now().date() - timedelta(days=3)) check_in1 = CheckInFactory(mac=self.student.mac, user=self.student) check_in1.date = datetime.now().date() - timedelta(days=9) check_in1.save() check_in2 = CheckInFactory(mac=self.student.mac, user=self.student) check_in2.date = datetime.now().date() - timedelta(days=7) check_in2.save() with self.login(email=teacher.email, password=BaseUserFactory.password): response = self.get('education:student-detail', course=self.course.id, ca=self.ca.id) self.assertEqual(response.status_code, 200) self.assertEqual(self.ca, response.context['object']) self.assertEqual(2, response.context['passed_solutions']) self.assertEqual(1, response.context['failed_solutions']) self.assertEqual(1, response.context['url_solutions']) def test_teacher_cannot_see_other_students_detail_information(self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) teacher.teached_courses = [self.course] other_student = BaseUser.objects.promote_to_student(self.baseuser3) other_student_course = CourseFactory() other_student_ca = CourseAssignmentFactory(course=other_student_course, user=other_student) self.assertFalse(other_student_course in teacher.teached_courses.all()) with self.login(email=teacher.email, password=BaseUserFactory.password): response = self.get('education:student-detail', course=other_student_course.id, ca=other_student_ca.id) self.assertEqual(response.status_code, 403)
class CourseDetailViewTests(TestCase): def setUp(self): self.baseuser = BaseUserFactory() self.baseuser.is_active = True self.baseuser.save() self.teacher = BaseUser.objects.promote_to_teacher(self.baseuser) self.baseuser2 = BaseUserFactory() self.baseuser2.is_active = True self.baseuser2.save() self.student = BaseUser.objects.promote_to_student(self.baseuser2) self.course = CourseFactory() self.course_assignment = CourseAssignmentFactory(course=self.course, user=self.student) self.task = TaskFactory(course=self.course, gradable=True) def test_not_access_course_detail_without_login(self): response = self.get( 'education:course-detail', course=self.course.id, ) self.assertEquals(response.status_code, 302) def test_baseuser_cannot_access_course_detail(self): with self.login(username=self.baseuser.email, password=BaseUserFactory.password): response = self.get('education:course-detail', course=self.course.id) self.assertEqual(response.status_code, 403) def test_student_cannot_access_course_detail(self): student = BaseUser.objects.promote_to_student(self.baseuser) with self.login(username=student.email, password=BaseUserFactory.password): response = self.get('education:course-detail', course=self.course.id) self.assertEqual(response.status_code, 403) def test_teacher_cannot_access_course_detail_if_he_is_not_teacher(self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) with self.login(username=teacher.email, password=BaseUserFactory.password): response = self.get('education:course-detail', course=self.course.id) self.assertEqual(response.status_code, 403) def test_teacher_can_access_course_detail_if_he_is_teacher(self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) teacher.teached_courses = [self.course] with self.login(username=teacher.email, password=BaseUserFactory.password): response = self.get('education:course-detail', course=self.course.id) self.assertEqual(response.status_code, 200) def test_teacher_can_see_only_his_course(self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) teacher.teached_courses = [self.course] course2 = CourseFactory() with self.login(username=teacher.email, password=BaseUserFactory.password): response = self.get('education:course-detail', course=course2.id) self.assertEqual(response.status_code, 403) def test_check_count_gradable_and_notgradable_tasks(self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) teacher.teached_courses = [self.course] SolutionFactory(task=self.task, student=self.student) TaskFactory(course=self.course, gradable=False) with self.login(username=teacher.email, password=BaseUserFactory.password): response = self.get('education:course-detail', course=self.course.id) self.assertEqual(response.status_code, 200) self.assertEqual(1, response.context['not_gradable_tasks']) self.assertEqual(1, response.context['gradable_tasks']) self.assertEqual(1, response.context['count_solutions']) def test_check_count_gradable_and_notgradable_solutions(self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) teacher.teached_courses = [self.course] solution1 = SolutionFactory(task=self.task, student=self.student) solution1.status = 3 solution1.save() solution2 = SolutionFactory(task=self.task, student=self.student) solution2.status = 2 solution2.save() url_task = TaskFactory(course=self.course, gradable=False) SolutionFactory(task=url_task, student=self.student) with self.login(username=teacher.email, password=BaseUserFactory.password): response = self.get('education:course-detail', course=self.course.id) self.assertEqual(response.status_code, 200) self.assertEqual(1, response.context['not_gradable_tasks']) self.assertEqual(1, response.context['gradable_tasks']) self.assertEqual(3, response.context['count_solutions']) self.assertEqual(1, response.context['not_gradable_tasks']) self.assertEqual(1, response.context['passed_solutions']) self.assertEqual(1, response.context['url_solutions']) self.assertEqual(1, response.context['failed_solutions'])
class TestApplicationViews(TestCase): def setUp(self): self.course = CourseFactory() self.user = BaseUserFactory() self.user.is_active = True self.user.save() self.course_description = CourseDescriptionFactory(course=self.course) self.application_info = ApplicationInfoFactory(course=self.course_description) def test_non_registered_user_cannot_see_apply_overview(self): self.get('applications:apply_overview') self.response_200() def test_registered_user_can_see_apply_overview(self): with self.login(username=self.user.email, password=BaseUserFactory.password): self.get('applications:apply_overview') self.response_200() def test_applying_for_non_existing_course_should_raise_404(self): self.assertEqual(0, Application.objects.count()) with self.login(username=self.user.email, password=BaseUserFactory.password): data = {} self.post('applications:apply_course', course_url=self.course_description.url + faker.word(), data=data) self.response_404() self.assertEqual(0, Application.objects.count()) def test_register_user_can_see_course_apply_form(self): with self.login(username=self.user.email, password=BaseUserFactory.password): self.get('applications:apply_course', course_url=self.course_description.url) self.response_200() def test_apply_for_invalid_course_url(self): with self.login(username=self.user.email, password=BaseUserFactory.password): self.post('applications:edit_application', course_url=faker.word) self.response_404() def test_applying_for_course_with_inconsistent_data(self): self.assertEqual(0, Application.objects.count()) with self.login(username=self.user.email, password=BaseUserFactory.password): data = {"phone": faker.random_number(), "skype": faker.word()} self.post('applications:apply_course', course_url=self.course_description.url, data=data) form = self.get_context('form') errors = {'studies_at': ['Това поле е задължително.'], 'works_at': ['Това поле е задължително.'], 'task_field_count': ['Това поле е задължително.']} self.assertEquals(errors, form.errors) self.response_200() self.assertEqual(0, Application.objects.count()) def test_applying_for_course(self): self.assertEqual(0, Application.objects.count()) app_problem1 = ApplicationProblemFactory() app_problem2 = ApplicationProblemFactory() self.application_info.applicationproblem_set.add(app_problem1) self.application_info.applicationproblem_set.add(app_problem2) with self.login(username=self.user.email, password=BaseUserFactory.password): data = {"phone": faker.random_number(), "skype": faker.word(), "studies_at": faker.word(), "works_at": faker.word(), "task_field_count": 2, "task_1": faker.url(), "task_2": faker.url()} self.post('applications:apply_course', course_url=self.course_description.url, data=data, follow=True) """ TODO: Finish test """ # form = self.get_context('apply_form') self.response_200() application = Application.objects.filter(user=self.user) self.assertEqual(2, ApplicationProblemSolution.objects.filter(application=application).count()) self.assertEqual(1, application.count()) def test_applying_for_the_same_course(self): self.assertEqual(0, Application.objects.count()) ApplicationFactory(user=self.user, application_info=self.application_info) with self.login(username=self.user.email, password=BaseUserFactory.password): self.post('applications:apply_course', course_url=self.course_description.url) self.response_302() def test_non_registered_can_not_see_apply_edit(self): self.post('applications:edit_application', course_url=self.course_description.url) self.response_302() def test_registered_user_editing_apply_form(self): self.assertEqual(0, Application.objects.count()) app_problem1 = ApplicationProblemFactory() app_problem2 = ApplicationProblemFactory() self.application_info.applicationproblem_set.add(app_problem1) self.application_info.applicationproblem_set.add(app_problem2) application = ApplicationFactory(application_info=self.application_info, user=self.user) solution_problem1 = ApplicationProblemSolutionFactory(application=application) solution_problem2 = ApplicationProblemSolutionFactory(application=application) solution_problem1.problem = app_problem1 solution_problem1.save() solution_problem2.problem = app_problem2 solution_problem2.save() self.assertEqual(1, Application.objects.filter(user=self.user).count()) self.assertEqual(2, ApplicationProblemSolution.objects.filter(application=application).count()) with self.login(username=self.user.email, password=BaseUserFactory.password): data = {"phone": faker.random_number(), "skype": faker.word(), "studies_at": faker.word(), "works_at": faker.word(), "task_field_count": 2, "task_1": faker.url(), "task_2": faker.url()} self.post('applications:edit_application', course_url=self.course_description.url, data=data) self.response_302() app_problem_solutions = ApplicationProblemSolution.objects.filter(application=application).all() self.assertEqual(1, Application.objects.filter(user=self.user).count()) self.assertEqual(2, app_problem_solutions.count()) self.assertTrue([solution_problem1, solution_problem2] != app_problem_solutions)
class TaskListViewTests(TestCase): def setUp(self): self.baseuser = BaseUserFactory() self.baseuser.is_active = True self.baseuser.save() self.baseuser2 = BaseUserFactory() self.baseuser2.is_active = True self.baseuser2.save() self.student = BaseUser.objects.promote_to_student(self.baseuser2) self.course = CourseFactory() self.task = TaskFactory(course=self.course) self.course_assignment = CourseAssignmentFactory(course=self.course, user=self.student) def test_no_access_to_task_list_without_login(self): response = self.get('education:task-list', course=self.course.id) self.assertEquals(response.status_code, 302) def test_baseuser_cannot_access_task_list(self): with self.login(email=self.baseuser.email, password=BaseUserFactory.password): response = self.get('education:task-list', course=self.course.id) self.assertEqual(response.status_code, 403) def test_teacher_cannot_access_task_list(self): teacher = BaseUser.objects.promote_to_teacher(self.baseuser) with self.login(email=teacher.email, password=BaseUserFactory.password): response = self.get('education:task-list', course=self.course.id) self.assertEqual(response.status_code, 403) def test_student_access_task_list(self): with self.login(email=self.student.email, password=BaseUserFactory.password): response = self.get('education:task-list', course=self.course.id) self.assertEqual(response.status_code, 200) def test_student_cannot_access_task_list_of_course_without_tasks(self): course2 = CourseFactory() CourseAssignmentFactory(course=course2, user=self.student) with self.login(email=self.student.email, password=BaseUserFactory.password): response = self.get('education:task-list', course=course2.id) self.assertEqual(response.status_code, 404) def test_student_see_only_tasks_for_his_course(self): task2 = TaskFactory(course=self.course) course2 = CourseFactory() task_for_course2 = TaskFactory(course=course2) with self.login(email=self.student.email, password=BaseUserFactory.password): response = self.get('education:task-list', course=self.course.id) self.assertEqual(response.status_code, 200) self.assertIn(self.task, response.context['object_list']) self.assertIn(task2, response.context['object_list']) self.assertNotIn(task_for_course2, response.context['object_list']) def test_cannot_submit_solutions_if_date_is_not_in_deadline(self): self.course.deadline_date = datetime.now().date() - timedelta(days=2) self.course.save() with self.login(email=self.student.email, password=BaseUserFactory.password): response = self.get('education:task-list', course=self.course.id) self.assertEqual(response.status_code, 404)