def test_update_event(self):
     event = EventSession()
     event.testing_center = "new center"
     event.course_id = "new/course/id"
     event.course_event_id = "new event"
     event.proctor = self.user
     event.save()
     self.assertEqual(event.end_date, None)
     factory = APIRequestFactory()
     event_data = {
         'status': EventSession.ARCHIVED,
         'notify': 'new notify',
         'comment': 'new comment'
     }
     request = factory.patch(
         '/api/event_session/%s' % event.pk, data=event_data)
     force_authenticate(request, user=self.user)
     view = views_ui.EventSessionViewSet.as_view(
         {'patch': 'partial_update'})
     response = view(request, pk=event.pk)
     response.render()
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     data = json.loads(response.content)
     self.assertEqual(type(data), dict)
     event = ArchivedEventSession.objects.get(pk=data['id'])
     self.assertDictContainsSubset({
         "status": event.status,
         "notify": event.notify,
     },
         event_data
     )
     self.assertNotEqual(event.end_date, None)
 def setUp(self):
     self.user = User.objects.create_user(
         'test', '*****@*****.**', 'testpassword'
     )
     Permission.objects.create(
         user=self.user,
         object_id='*',
         object_type='*'
     )
     event = EventSession()
     event.testing_center = "new center"
     event.course_id = "new course"
     event.course_event_id = "new event"
     event.proctor = self.user
     event.save()
     exam = Exam()
     exam.exam_code = 'examCode'
     exam.organization = 'organization'
     exam.duration = 1
     exam.reviewed_exam = 'reviewedExam'
     exam.reviewer_notes = 'reviewerNotes'
     exam.exam_password = '******'
     exam.exam_sponsor = 'examSponsor'
     exam.exam_name = 'examName'
     exam.ssi_product = 'ssiProduct'
     exam.first_name = 'firstName'
     exam.last_name = 'lastName'
     exam.username = '******'
     exam.user_id = 1
     exam.email = '*****@*****.**'
     exam.exam_id = event.course_event_id
     exam.course_id = event.course_id
     exam.event = event
     exam.save()
     self.exam = exam
 def setUp(self):
     self.event = EventSession()
     self.event.testing_center = 'testing center'
     self.event.course_id = 'org1/course1/run1'
     self.event.course_event_id = '1'
     self.event.status = EventSession.IN_PROGRESS
     self.user = User.objects.create_user(
         'test',
         '*****@*****.**',
         'password'
     )
     self.event.proctor = self.user
     self.event.save()
     Permission.objects.create(
         user=self.user,
         object_type="*",
         object_id="*",
         role=Permission.ROLE_PROCTOR
     )
     exam = Exam()
     exam.exam_code = 'examCode'
     exam.organization = 'organization'
     exam.duration = 1
     exam.reviewed_exam = 'reviewedExam'
     exam.reviewer_notes = 'reviewerNotes'
     exam.exam_password = '******'
     exam.exam_sponsor = 'examSponsor'
     exam.exam_name = 'examName'
     exam.ssi_product = 'ssiProduct'
     exam.first_name = 'firstName'
     exam.last_name = 'lastName'
     exam.username = '******'
     exam.user_id = '1'
     exam.email = '*****@*****.**'
     exam.exam_id = self.event.course_event_id
     exam.course_id = self.event.course_id
     exam.event = self.event
     exam.save()
     self.exam = exam
class ExamViewSetTestCase(TestCase):
    def setUp(self):
        self.event = EventSession()
        self.event.testing_center = 'testing center'
        self.event.course_id = 'org1/course1/run1'
        self.event.course_event_id = '1'
        self.event.status = EventSession.IN_PROGRESS
        self.user = User.objects.create_user(
            'test',
            '*****@*****.**',
            'password'
        )
        self.event.proctor = self.user
        self.event.save()
        Permission.objects.create(
            user=self.user,
            object_type="*",
            object_id="*",
            role=Permission.ROLE_PROCTOR
        )
        exam = Exam()
        exam.exam_code = 'examCode'
        exam.organization = 'organization'
        exam.duration = 1
        exam.reviewed_exam = 'reviewedExam'
        exam.reviewer_notes = 'reviewerNotes'
        exam.exam_password = '******'
        exam.exam_sponsor = 'examSponsor'
        exam.exam_name = 'examName'
        exam.ssi_product = 'ssiProduct'
        exam.first_name = 'firstName'
        exam.last_name = 'lastName'
        exam.username = '******'
        exam.user_id = '1'
        exam.email = '*****@*****.**'
        exam.exam_id = self.event.course_event_id
        exam.course_id = self.event.course_id
        exam.event = self.event
        exam.save()
        self.exam = exam

    def test_get_exam_by_session(self):
        factory = APIRequestFactory()
        request = factory.get(
            '/api/exam_register/?session=%s' % self.event.hash_key)
        force_authenticate(request, user=self.user)
        view = ExamViewSet.as_view({'get': 'list'})

        response = view(request)
        response.render()
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        data = json.loads(response.content)
        self.assertEqual(type(data), list)
        self.assertTrue(len(data) > 0)

    def test_register_exam(self):
        factory = APIRequestFactory()
        exam_data = {
            "examCode": "newEamCode",
            "duration ": 2,
            "organization ": "newOrganization",
            "reviewedExam ": "newReviewedExam",
            "reviewerNotes ": "newReviewerNotes",
            "examPassword ": "******",
            "examSponsor ": "newExamSponsor",
            "examName ": "newExamName",
            "ssiProduct ": "newSsiProduct",
            "orgExtra": '''{
                "examStartDate": "2015-10-10 11:00",
                "examEndDate": "2015-10-10 15:00",
                "noOfStudents": 1,
                "examID": "wrong",
                "courseID": "wrong/course/id",
                "firstName": "first_name",
                "lastName": "last_name",
                "email": "*****@*****.**",
                "username": "******",
                "userID": "1"
            }'''
        }
        with patch('api.views_edx.send_ws_msg') as send_ws:
            send_ws.return_value = None
            request = factory.post('/api/exam_register/',
                                   data=exam_data,
                                   )
            view = ExamViewSet.as_view({'post': 'create'})
            response = view(request)
            response.render()
            self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

            exam_data['orgExtra'] = '''{
                "examStartDate": "2015-10-10 11:00",
                "examEndDate": "2015-10-10 15:00",
                "noOfStudents": 1,
                "examID": "1",
                "courseID": "org1/course1/run1",
                "firstName": "first_name",
                "lastName": "last_name",
                "email": "*****@*****.**",
                "username": "******",
                "userID": "1"
            }'''
            request = factory.post(
                '/api/exam_register/',
                data=exam_data,
            )
            view = ExamViewSet.as_view({'post': 'create'})
            response = view(request)
            response.render()
            data = json.loads(response.content)
            self.assertEqual(type(data), dict)
            exam = Exam.objects.get(exam_code=exam_data['examCode'])
            self.assertDictContainsSubset({
                "examCode": exam.exam_code,
                "duration ": exam.duration,
                "organization ": exam.organization,
                "reviewedExam ": exam.reviewed_exam,
                "reviewerNotes ": exam.reviewer_notes,
                "examPassword ": exam.exam_password,
                "examSponsor ": exam.exam_sponsor,
                "examName ": exam.exam_name,
                "ssiProduct ": exam.ssi_product,
            },
                exam_data
            )
            self.assertListEqual(
                ["org1", "org1/course1", "org1/course1/run1"],
                [exam.course_organization, exam.course_identify,
                 exam.course_run]
            )
            self.assertEqual(data['ID'], exam.generate_key())
            self.assertEqual(Exam.objects.count(), 2)