def setUp(self): super().setUp() dump_random_state() user = UserFactory(username=self.USERNAME) self.client.login(username=user.username, password=USER_PASSWORD) self.program = ProgramFactory(site=self.site)
def test_list_program_uuid_filtering(self): """ Verify the endpoint returns data for all UserCredentials in the given program. """ # Course run 1 is in a program, course run 2 is not course1_run = CourseRunFactory() course2_run = CourseRunFactory() program = ProgramFactory(course_runs=[course1_run]) program_certificate = ProgramCertificateFactory( site=self.site, program_uuid=program.uuid) course1_certificate = CourseCertificateFactory( site=self.site, course_id=course1_run.key) course2_certificate = CourseCertificateFactory( site=self.site, course_id=course2_run.key) # Create some credentials related to the program course1_cred = UserCredentialFactory(credential=course1_certificate) program_creds = UserCredentialFactory.create_batch( 3, credential=program_certificate) expected = [course1_cred] + program_creds # Create some more credentials that we don't expect to see returned UserCredentialFactory.create_batch(3) UserCredentialFactory(credential=course2_certificate) self.authenticate_user(self.user) self.add_user_permission(self.user, 'view_usercredential') response = self.client.get(self.list_path + '?program_uuid={}'.format(program.uuid)) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['results'], self.serialize_user_credential(expected, many=True))
def _setup_program_and_program_cert(self, program_type): self.program = ProgramFactory(site=self.site) self.program.type = program_type self.program.type_slug = slugify(program_type) self.program.save() self.program_cert = ProgramCertificateFactory( site=self.site, program_uuid=self.program.uuid)
def setUp(self): self.site = SiteFactory() self.program = ProgramFactory(site=self.site) self.certificate = self.cert_factory.create(program_uuid=self.program.uuid, site=self.site) self.username = '******' self.user = UserFactory(username=self.username) self.user_cred = self.issuer.issue_credential(self.certificate, self.username) self.attributes = [{"name": "whitelist_reason", "value": "Reason for whitelisting."}]
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(username=self.USERNAME) self.client.login(username=self.user.username, password=USER_PASSWORD) self.program = ProgramFactory(site=self.site) self.pathway = PathwayFactory(site=self.site, programs=[self.program]) self.pc = ProgramCertificateFactory(site=self.site, program_uuid=self.program.uuid) self.user_credential = UserCredentialFactory(username=self.USERNAME, credential=self.pc) self.data = {"username": self.USERNAME, "pathway_id": self.pathway.id} self.url = reverse("records:send_program", kwargs={"uuid": self.program.uuid.hex}) mail.outbox = []
def setUp(self): super().setUp() dump_random_state() self.user = UserFactory(username=self.USERNAME) self.client.login(username=self.user.username, password=USER_PASSWORD) self.program = ProgramFactory(site=self.site) self.pathway = PathwayFactory(site=self.site, programs=[self.program]) self.pc = ProgramCertificateFactory(site=self.site, program_uuid=self.program.uuid) self.pcr = ProgramCertRecordFactory(program=self.program, user=self.user) self.data = {'username': self.USERNAME, 'pathway_id': self.pathway.id} self.url = reverse('records:share_program', kwargs={'uuid': self.program.uuid.hex}) mail.outbox = []
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 test_create(self): program = ProgramFactory(site=self.site) program_certificate = ProgramCertificateFactory( site=self.site, program_uuid=program.uuid) expected_username = self.user.username expected_attribute_name = "fake-name" expected_attribute_value = "fake-value" data = { "username": expected_username, "credential": { "program_uuid": str(program_certificate.program_uuid) }, "status": "awarded", "attributes": [{ "name": expected_attribute_name, "value": expected_attribute_value, }], } # Verify users without the add permission are denied access self.assert_access_denied(self.user, "post", self.list_path, data=data) self.authenticate_user(self.user) self.add_user_permission(self.user, "add_usercredential") response = self.client.post(self.list_path, data=json.dumps(data), content_type=JSON_CONTENT_TYPE) user_credential = UserCredential.objects.last() self.assertEqual(response.status_code, 201) self.assertEqual(response.data, self.serialize_user_credential(user_credential)) self.assertEqual(user_credential.username, expected_username) self.assertEqual(user_credential.credential, program_certificate) self.assertEqual(user_credential.attributes.count(), 1) attribute = user_credential.attributes.first() self.assertEqual(attribute.name, expected_attribute_name) self.assertEqual(attribute.value, expected_attribute_value)
def test_create(self): program = ProgramFactory(site=self.site) program_certificate = ProgramCertificateFactory( site=self.site, program_uuid=program.uuid) expected_username = self.user.username expected_attribute_name = 'fake-name' expected_attribute_value = 'fake-value' data = { 'username': expected_username, 'credential': { 'program_uuid': str(program_certificate.program_uuid) }, 'status': 'awarded', 'attributes': [{ 'name': expected_attribute_name, 'value': expected_attribute_value, }], } # Verify users without the add permission are denied access self.assert_access_denied(self.user, 'post', self.list_path, data=data) self.authenticate_user(self.user) self.add_user_permission(self.user, 'add_usercredential') response = self.client.post(self.list_path, data=json.dumps(data), content_type=JSON_CONTENT_TYPE) user_credential = UserCredential.objects.last() self.assertEqual(response.status_code, 201) self.assertEqual(response.data, self.serialize_user_credential(user_credential)) self.assertEqual(user_credential.username, expected_username) self.assertEqual(user_credential.credential, program_certificate) self.assertEqual(user_credential.attributes.count(), 1) attribute = user_credential.attributes.first() self.assertEqual(attribute.name, expected_attribute_name) self.assertEqual(attribute.value, expected_attribute_value)
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 test_keep_old_data(self): """ Verify that we don't destroy existing but obsolete data. """ # org with a uuid that won't be in data we get back org = OrganizationFactory(site=self.site, uuid='44f0dded-fee9-4dec-a333-b9d8c2c82bd2', key='OldX') program = ProgramFactory(site=self.site, uuid='33f0dded-fee9-4dec-a333-c9d8c2c82bd5', title='Old Program') program.authoring_organizations.add(org) # pylint: disable=no-member self.mock_access_token_response() self.mock_programs_response(self.wrap_programs(self.PROGRAMS)) self.mock_pathways_response(self.wrap_pathways(self.PATHWAYS)) self.call_command() org = Organization.objects.get(uuid='44f0dded-fee9-4dec-a333-b9d8c2c82bd2', key='OldX') # But note that it will no longer be connected to our program program = Program.objects.get(uuid='33f0dded-fee9-4dec-a333-c9d8c2c82bd5') self.assertNotIn(org, program.authoring_organizations.all())
def test_update(self): """ Verify the command updates existing data. """ OrganizationFactory(site=self.site, uuid='33f0dded-fee9-4dec-a333-b9d8c2c82bd2', key='OldX') ProgramFactory(site=self.site, uuid='33f0dded-fee9-4dec-a333-c9d8c2c82bd5', title='Old Program') self.mock_access_token_response() self.mock_programs_response(self.wrap_programs(self.PROGRAMS)) self.mock_pathways_response(self.wrap_pathways(self.PATHWAYS)) self.call_command() self.assertProgramsSaved() self.assertPathwaysSaved() org = Organization.objects.get(uuid='33f0dded-fee9-4dec-a333-b9d8c2c82bd2') self.assertNotEqual(org.key, 'OldX') program = Program.objects.get(uuid='33f0dded-fee9-4dec-a333-c9d8c2c82bd5') self.assertNotEqual(program.title, 'Old Program')
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): program = ProgramFactory() ProgramCertRecordFactory(program=program) with self.assertRaises(ProtectedError): program.delete()