def setUp(self): super(UserCredentialSerializerTests, self).setUp() self.program_cert = ProgramCertificateFactory() self.program_credential = UserCredentialFactory(credential=self.program_cert) self.program_cert_attr = UserCredentialAttributeFactory(user_credential=self.program_credential) self.course_cert = CourseCertificateFactory.create() self.course_credential = UserCredentialFactory.create(credential=self.course_cert) self.course_cert_attr = UserCredentialAttributeFactory(user_credential=self.course_credential) self.request = APIRequestFactory().get('/')
def test_course_credential(self): request = APIRequestFactory().get("/") course_certificate = CourseCertificateFactory() user_credential = UserCredentialFactory(credential=course_certificate) user_credential_attribute = UserCredentialAttributeFactory(user_credential=user_credential) expected_url = "http://testserver{}".format( reverse("credentials:render", kwargs={"uuid": user_credential.uuid.hex}) ) expected = { "username": user_credential.username, "uuid": str(user_credential.uuid), "credential": { "type": "course-run", "course_run_key": course_certificate.course_id, "mode": course_certificate.certificate_type, }, "download_url": user_credential.download_url, "status": user_credential.status, "attributes": [{"name": user_credential_attribute.name, "value": user_credential_attribute.value}], "created": user_credential.created.strftime(api_settings.DATETIME_FORMAT), "modified": user_credential.modified.strftime(api_settings.DATETIME_FORMAT), "certificate_url": expected_url, } actual = UserCredentialSerializer(user_credential, context={"request": request}).data self.assertEqual(actual, expected)
def test_course_credential(self): request = APIRequestFactory().get('/') course_certificate = CourseCertificateFactory() user_credential = UserCredentialFactory(credential=course_certificate) user_credential_attribute = UserCredentialAttributeFactory(user_credential=user_credential) expected_url = 'http://testserver{}'.format( reverse('credentials:render', kwargs={'uuid': user_credential.uuid.hex})) expected = { 'username': user_credential.username, 'uuid': str(user_credential.uuid), 'credential': { 'type': 'course-run', 'course_run_key': course_certificate.course_id, 'mode': course_certificate.certificate_type, }, 'download_url': user_credential.download_url, 'status': user_credential.status, 'attributes': [ { 'name': user_credential_attribute.name, 'value': user_credential_attribute.value } ], 'created': user_credential.created.strftime(api_settings.DATETIME_FORMAT), 'modified': user_credential.modified.strftime(api_settings.DATETIME_FORMAT), 'certificate_url': expected_url } actual = UserCredentialSerializer(user_credential, context={'request': request}).data self.assertEqual(actual, expected)
def test_list_visible_filtering(self): """ Verify the endpoint can filter by visible date. """ program_certificate = ProgramCertificateFactory(site=self.site) course_certificate = CourseCertificateFactory(site=self.site) course_cred = UserCredentialFactory(credential=course_certificate) program_cred = UserCredentialFactory(credential=program_certificate) UserCredentialAttributeFactory( user_credential=program_cred, name="visible_date", value="9999-01-01T01:01:01Z", ) self.authenticate_user(self.user) self.add_user_permission(self.user, "view_usercredential") both = [course_cred, program_cred] response = self.client.get(self.list_path) self.assertEqual(response.status_code, 200) self.assertEqual(response.data["results"], self.serialize_user_credential(both, many=True)) response = self.client.get(self.list_path + "?only_visible=True") self.assertEqual(response.status_code, 200) self.assertEqual( response.data["results"], self.serialize_user_credential([course_cred], many=True)) response = self.client.get(self.list_path + "?only_visible=False") self.assertEqual(response.status_code, 200) self.assertEqual(response.data["results"], self.serialize_user_credential(both, many=True))
def test_data(self): program_certificate = ProgramCertificateFactory() user_credential = UserCredentialFactory(credential=program_certificate) program_certificate_attr = UserCredentialAttributeFactory(user_credential=user_credential) expected = {"name": program_certificate_attr.name, "value": program_certificate_attr.value} actual = UserCredentialAttributeSerializer(program_certificate_attr).data self.assertEqual(actual, expected)
def test_visible_date_as_issue_date(self): """ Verify that we show visible_date when available """ UserCredentialAttributeFactory(user_credential=self.user_credentials[1], name='visible_date', value='2017-07-31T09:32:46Z') 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(grades[0]['issue_date'], '2017-07-31T09:32:46+00:00')
def test_visible_date_as_issue_date(self): """ Verify that we show visible_date when available """ UserCredentialAttributeFactory( user_credential=self.user_credentials[1], name="visible_date", value="2017-07-31T09:32:46Z" ) 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(grades[0]["issue_date"], "2017-07-31T09:32:46+00:00")
def test_not_visible_from_db(self): """ Test that the program's visible_date is considered """ UserCredentialAttributeFactory( user_credential=self.program_user_credential, name="visible_date", value="9999-01-01T01:01:01Z", ) response = self.client.get(reverse("records:index")) self.assertFalse(json.loads(response.context_data["programs"])[0]["completed"])
def test_future_visible_date_not_shown(self): """ Verify that we don't show certificates with a visible_date in the future """ UserCredentialAttributeFactory(user_credential=self.user_credentials[1], name='visible_date', value=datetime.datetime.max.strftime('%Y-%m-%dT%H:%M:%SZ')) 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(grades[0]['course_id'], self.course_runs[0].key) # 0 instead of 1 now that 1 is in future self.assertEqual(grades[0]['issue_date'], self.user_credentials[0].created.isoformat())
def test_program_visible_date(self, date, completed): """ Test that the program's visible_date is considered """ program_credential = UserCredentialFactory( username=self.MOCK_USER_DATA['username'], credential_content_type=self.program_content_type, credential=self.program_cert) if date: UserCredentialAttributeFactory( user_credential=program_credential, name='visible_date', value=date, ) response = self.client.get(reverse('records:private_programs', kwargs={'uuid': self.program.uuid.hex})) self.assertEqual(json.loads(response.context_data['record'])['program']['completed'], completed)
def test_program_visible_date(self, date, completed): """ Test that the program's visible_date is considered """ program_credential = UserCredentialFactory( username=self.MOCK_USER_DATA["username"], credential_content_type=self.program_content_type, credential=self.program_cert, ) if date: UserCredentialAttributeFactory( user_credential=program_credential, name="visible_date", value=date, ) response = self.client.get(reverse("records:private_programs", kwargs={"uuid": self.program.uuid.hex})) self.assertEqual(json.loads(response.context_data["record"])["program"]["completed"], completed)
def setUp(self): super(UserCredentialAttributeSerializerTests, self).setUp() self.program_cert = ProgramCertificateFactory() self.program_credential = UserCredentialFactory(credential=self.program_cert) self.program_cert_attr = UserCredentialAttributeFactory(user_credential=self.program_credential)