def test_to_internal_value(self): Request = namedtuple("Request", ["site"]) serializer = UserGradeSerializer( context={"request": Request(site=self.site)}) data = { "username": "******", "course_run": "nope", "letter_grade": "A", "percent_grade": 0.9, "verified": True, } with self.assertRaisesMessage(ValidationError, "No CourseRun exists for key [nope]"): serializer.to_internal_value(data) course = CourseFactory(site=self.site) course_run = CourseRunFactory(course=course) data["course_run"] = course_run.key grade = serializer.to_internal_value(data) self.assertEqual(grade["username"], "alice") self.assertEqual(grade["course_run"], course_run) self.assertEqual(grade["verified"], True) self.assertEqual(grade["letter_grade"], "A") self.assertEqual(str(grade["percent_grade"]), "0.9000")
def test_course_run_order(self): """ Test that the course_runs are returned in the program order """ new_course_run = CourseRunFactory() self.program.course_runs.add(new_course_run) # pylint: disable=no-member UserGradeFactory(username=self.MOCK_USER_DATA['username'], course_run=new_course_run, letter_grade='C', percent_grade=.70) new_course_cert = CourseCertificateFactory( course_id=new_course_run.key, site=self.site) UserCredentialFactory( username=self.MOCK_USER_DATA['username'], credential_content_type=self.credential_content_type, credential=new_course_cert) response = self.client.get( reverse('records:programs', kwargs={'uuid': self.program.uuid.hex})) grades = json.loads(response.context_data['record'])['grades'] expected_course_run_keys = [ course_run.key for course_run in [self.course_runs[1], new_course_run] ] actual_course_run_keys = [grade['course_id'] for grade in grades] self.assertEqual(expected_course_run_keys, actual_course_run_keys)
def test_grade_view_throttling(self): """ Verify requests are throttled and a message is logged after limit. """ with LogCapture() as log: course = CourseFactory(site=self.site) course_run = CourseRunFactory(course=course) data = { 'username': '******', 'course_run': course_run.key, 'letter_grade': 'A', 'percent_grade': 0.9, 'verified': True, } grade = UserGradeFactory( course_run=course_run, username=self.user.username, letter_grade='C', ) path = reverse('api:v2:grades-detail', kwargs={'pk': grade.id}) self.authenticate_user(self.user) self.add_user_permission(self.user, 'change_usergrade') # All requests up to limit should be acceptable self.hit_rate_limit('grade_view', path, data) # Request after limit should NOT be acceptable response = getattr(self.client, 'put')(path, data=data) self.assertEqual(response.status_code, 429) assert_log_correct( log, 'credentials.apps.api.v2.views', 'WARNING', 'Credentials API endpoint GradeViewSet is being throttled.', )
def setUp(self): super().setUp() self.site = Site.objects.get(domain="example.com") new_course = CourseFactory.create(site=self.site) self.new_course_run = CourseRunFactory.create(course=new_course) self.orgs = [ OrganizationFactory.create(name=name, site=self.site) for name in ["TestOrg1", "TestOrg2"] ]
def setUp(self): super().setUp() dump_random_state() self.user = UserFactory(username=self.MOCK_USER_DATA["username"]) self.client.login(username=self.user.username, password=USER_PASSWORD) self.course = CourseFactory(site=self.site) self.course_runs = CourseRunFactory.create_batch(3, course=self.course) self.user_grade_low = UserGradeFactory( username=self.MOCK_USER_DATA["username"], course_run=self.course_runs[0], letter_grade="A", percent_grade=0.70, ) self.user_grade_high = UserGradeFactory( username=self.MOCK_USER_DATA["username"], course_run=self.course_runs[1], letter_grade="C", percent_grade=1.00, ) self.user_grade_revoked_cert = UserGradeFactory( username=self.MOCK_USER_DATA["username"], course_run=self.course_runs[2], letter_grade="B", percent_grade=0.80, ) self.course_certs = [ CourseCertificateFactory(course_id=course_run.key, site=self.site) for course_run in self.course_runs ] self.credential_content_type = ContentType.objects.get(app_label="credentials", model="coursecertificate") self.program_cert = ProgramCertificateFactory(site=self.site) self.program_content_type = ContentType.objects.get(app_label="credentials", model="programcertificate") self.user_credentials = [ UserCredentialFactory( username=self.MOCK_USER_DATA["username"], credential_content_type=self.credential_content_type, credential=course_cert, ) for course_cert in self.course_certs ] self.user_credentials[2].status = UserCredential.REVOKED self.user_credentials[2].save() self.org_names = ["CCC", "AAA", "BBB"] self.orgs = [OrganizationFactory(name=name, site=self.site) for name in self.org_names] self.program = ProgramFactory( course_runs=self.course_runs, authoring_organizations=self.orgs, site=self.site, uuid=self.program_cert.program_uuid, ) self.pcr = ProgramCertRecordFactory(program=self.program, user=self.user) self.pathway = PathwayFactory(site=self.site) self.pathway.programs.set([self.program])
def setUp(self): super().setUp() self.user = UserFactory() self.course = CourseFactory(site=self.site) self.course_run = CourseRunFactory(course=self.course) self.data = { "username": "******", "course_run": self.course_run.key, "letter_grade": "A", "percent_grade": 0.9, "verified": True, }
def setUp(self): super(GradeViewSetTests, self).setUp() self.user = UserFactory() self.course = CourseFactory(site=self.site) self.course_run = CourseRunFactory(course=self.course) self.data = { 'username': '******', 'course_run': self.course_run.key, 'letter_grade': 'A', 'percent_grade': 0.9, 'verified': True, }
def test_invalid_course_key(self): """Test Validation Error occurs for invalid course key.""" course_run = CourseRunFactory() with self.assertRaises(ValidationError) as context: CourseCertificate( site=self.site, is_active=True, course_id="test_invalid", certificate_type=constants.CertificateType.HONOR, course_run=course_run, ).full_clean() self.assertEqual(context.exception.message_dict, {"course_id": ["Invalid course key."]})
def test_course_run_no_credential(self): """ Adds a course run with no credential and tests that it does appear in the results """ new_course_run = CourseRunFactory() self.program.course_runs.add(new_course_run) UserGradeFactory( username=self.MOCK_USER_DATA["username"], course_run=new_course_run, letter_grade="F", percent_grade=0.05 ) CourseCertificateFactory(course_id=new_course_run.key, site=self.site) response = self.client.get(reverse("records:private_programs", kwargs={"uuid": self.program.uuid.hex})) grades = json.loads(response.context_data["record"])["grades"] self.assertEqual(len(grades), 2) self.assertEqual(new_course_run.course.title, grades[1]["name"])
def test_create_course_certificate(self): course_run = CourseRunFactory() course_certificate = CourseCertificateFactory(course_run=course_run) actual = CourseCertificateSerializer(course_certificate).data expected = { "id": course_certificate.id, "course_id": course_certificate.course_id, "course_run": course_certificate.course_run.key, "certificate_type": course_certificate.certificate_type, "certificate_available_date": course_certificate.certificate_available_date, "is_active": course_certificate.is_active, } self.assertEqual(actual, expected)
def test_multiple_programs(self): """ Test that multiple programs can appear, in progress and completed """ # Create a second program, and delete the first one's certificate new_course = CourseFactory.create(site=self.site) new_course_run = CourseRunFactory.create(course=new_course) new_program = ProgramFactory.create(title='ZTestProgram', course_runs=[new_course_run], authoring_organizations=self.orgs, site=self.site) new_course_cert = CourseCertificateFactory.create(course_id=new_course_run.key, site=self.site) new_program_cert = ProgramCertificateFactory.create(program_uuid=new_program.uuid, site=self.site) # Make a new user credential UserCredentialFactory.create( username=self.user.username, credential_content_type=self.program_credential_content_type, credential=new_course_cert ) # Make a new program credential UserCredentialFactory.create( username=self.user.username, credential_content_type=self.program_credential_content_type, credential=new_program_cert ) self.program_user_credential.delete() response = self.client.get(reverse('records:index')) self.assertEqual(response.status_code, 200) program_data = json.loads(response.context_data['programs']) expected_program_data = [ { 'name': self.program.title, 'partner': 'TestOrg1, TestOrg2', 'uuid': self.program.uuid.hex, 'type': slugify(self.program.type), 'completed': False, 'empty': False, }, { 'name': new_program.title, 'partner': 'TestOrg1, TestOrg2', 'uuid': new_program.uuid.hex, 'type': slugify(new_program.type), 'completed': True, 'empty': False, } ] self.assertEqual(program_data, expected_program_data)
def setUp(self): super().setUp() self.user = UserFactory(username=self.MOCK_USER_DATA['username']) self.client.login(username=self.user.username, password=USER_PASSWORD) self.course = CourseFactory(site=self.site) self.course_runs = [ CourseRunFactory(course=self.course) for _ in range(3) ] self.user_grade_low = UserGradeFactory( username=self.MOCK_USER_DATA['username'], course_run=self.course_runs[0], letter_grade='A', percent_grade=0.70) self.user_grade_high = UserGradeFactory( username=self.MOCK_USER_DATA['username'], course_run=self.course_runs[1], letter_grade='C', percent_grade=1.00) self.user_grade_revoked_cert = UserGradeFactory( username=self.MOCK_USER_DATA['username'], course_run=self.course_runs[2], letter_grade='B', percent_grade=.80) self.course_certs = [ CourseCertificateFactory(course_id=course_run.key, site=self.site) for course_run in self.course_runs ] self.credential_content_type = ContentType.objects.get( app_label='credentials', model='coursecertificate') self.user_credentials = [ UserCredentialFactory( username=self.MOCK_USER_DATA['username'], credential_content_type=self.credential_content_type, credential=course_cert) for course_cert in self.course_certs ] self.user_credentials[2].status = UserCredential.REVOKED self.org_names = ['CCC', 'AAA', 'BBB'] self.orgs = [ OrganizationFactory(name=name, site=self.site) for name in self.org_names ] self.program = ProgramFactory(course_runs=self.course_runs, authoring_organizations=self.orgs, site=self.site)
def course_run_no_credential(self): """ Adds a course run with no credential and tests that it doesn't appear in the results """ new_course_run = CourseRunFactory() self.program.course_runs.add(new_course_run) # pylint: disable=no-member UserGradeFactory(username=self.MOCK_USER_DATA['username'], course_run=new_course_run, letter_grade='F', percent_grade=.05) CourseCertificateFactory(course_id=new_course_run.key, site=self.site) response = self.client.get( reverse('records:programs', kwargs={'uuid': self.program.uuid.hex})) grades = json.loads(response.context_data['record'])['grades'] self.assertEqual(len(grades), 1) self.assertEqual(self.course_runs[1].key, grades[0]['course_id'])
def test_multiple_attempts_no_cert(self): """ Adds a course with two failed course_run attempts (no cert) and verifies that the course only shows up once """ # Only superusers can view an empty program (we could add a real cert too here, but this is a more direct test) self.user.is_superuser = True self.user.save() new_course = CourseFactory(site=self.site) new_course_runs = CourseRunFactory.create_batch(2, course=new_course) _ = [UserGradeFactory(username=self.MOCK_USER_DATA['username'], course_run=course_run, letter_grade='F', percent_grade=0.20) for course_run in new_course_runs] self.program.course_runs = new_course_runs response = self.client.get(reverse('records:private_programs', kwargs={'uuid': self.program.uuid.hex})) grades = json.loads(response.context_data['record'])['grades'] self.assertEqual(len(grades), 1) self.assertEqual(new_course.title, grades[0]['name'])
def test_course_run_order(self): """ Test that the course_runs are returned in the program order """ new_course_run = CourseRunFactory() self.program.course_runs.add(new_course_run) UserGradeFactory( username=self.MOCK_USER_DATA["username"], course_run=new_course_run, letter_grade="C", percent_grade=0.70 ) new_course_cert = CourseCertificateFactory(course_id=new_course_run.key, site=self.site) UserCredentialFactory( username=self.MOCK_USER_DATA["username"], credential_content_type=self.credential_content_type, credential=new_course_cert, ) response = self.client.get(reverse("records:private_programs", kwargs={"uuid": self.program.uuid.hex})) grades = json.loads(response.context_data["record"])["grades"] expected_course_run_keys = [course_run.key for course_run in [self.course_runs[1], new_course_run]] actual_course_run_keys = [grade["course_id"] for grade in grades] self.assertEqual(expected_course_run_keys, actual_course_run_keys)
def setUp(self): super().setUp() dump_random_state() self.user = UserFactory(username=self.MOCK_USER_DATA["username"]) self.orgs = [OrganizationFactory.create(name=name, site=self.site) for name in ["TestOrg1", "TestOrg2"]] self.course = CourseFactory.create(site=self.site) self.course_runs = CourseRunFactory.create_batch(2, course=self.course) self.program = ProgramFactory( title="TestProgram1", course_runs=self.course_runs, authoring_organizations=self.orgs, site=self.site ) self.course_certs = [ CourseCertificateFactory.create( course_id=course_run.key, site=self.site, ) for course_run in self.course_runs ] self.program_cert = ProgramCertificateFactory.create(program_uuid=self.program.uuid, site=self.site) self.course_credential_content_type = ContentType.objects.get( app_label="credentials", model="coursecertificate" ) self.program_credential_content_type = ContentType.objects.get( app_label="credentials", model="programcertificate" ) self.course_user_credentials = [ UserCredentialFactory.create( username=self.user.username, credential_content_type=self.course_credential_content_type, credential=course_cert, ) for course_cert in self.course_certs ] self.program_user_credential = UserCredentialFactory.create( username=self.user.username, credential_content_type=self.program_credential_content_type, credential=self.program_cert, ) self.client.login(username=self.user.username, password=USER_PASSWORD)
def setUp(self): super().setUp() self.user = UserFactory(username=self.MOCK_USER_DATA['username']) self.orgs = [ OrganizationFactory.create(name=name, site=self.site) for name in ['TestOrg1', 'TestOrg2'] ] self.course = CourseFactory.create(site=self.site) self.course_runs = [ CourseRunFactory.create(course=self.course) for _ in range(2) ] self.program = ProgramFactory(course_runs=self.course_runs, authoring_organizations=self.orgs, site=self.site) self.course_certs = [ CourseCertificateFactory.create( course_id=course_run.key, site=self.site, ) for course_run in self.course_runs ] self.program_cert = ProgramCertificateFactory.create( program_uuid=self.program.uuid, site=self.site) self.course_credential_content_type = ContentType.objects.get( app_label='credentials', model='coursecertificate') self.program_credential_content_type = ContentType.objects.get( app_label='credentials', model='programcertificate') self.course_user_credentials = [ UserCredentialFactory.create( username=self.user.username, credential_content_type=self.course_credential_content_type, credential=course_cert) for course_cert in self.course_certs ] self.program_user_credentials = UserCredentialFactory.create( username=self.user.username, credential_content_type=self.program_credential_content_type, credential=self.program_cert) self.client.login(username=self.user.username, password=USER_PASSWORD)
def test_protected_deletion(self): course_run = CourseRunFactory() UserGradeFactory(course_run=course_run) with self.assertRaises(ProtectedError): course_run.delete()