def setUp(self): super(CredentialFieldTests, self).setUp() self.program_certificate = ProgramCertificateFactory(site=self.site) self.course_certificate = CourseCertificateFactory( site=self.site, certificate_type='verified') self.field_instance = CredentialField() self.field_instance.context['request'] = namedtuple( 'HttpRequest', ['site'])(self.site)
def setUp(self): super(CredentialFieldTests, self).setUp() self.program_certificate = ProgramCertificateFactory(site=self.site) self.course_certificate = CourseCertificateFactory(site=self.site, certificate_type='verified') self.field_instance = CredentialField() # see: https://github.com/encode/django-rest-framework/blob/3.9.x/rest_framework/fields.py#L610 # pylint: disable=protected-access self.field_instance._context = { 'request': namedtuple('HttpRequest', ['site'])(self.site), }
class CredentialFieldTests(TestCase): def setUp(self): super(CredentialFieldTests, self).setUp() self.program_certificate = ProgramCertificateFactory() self.field_instance = CredentialField() def assert_program_uuid_validation_error_raised(self, program_uuid): try: self.field_instance.to_internal_value({'program_uuid': program_uuid}) except ValidationError as ex: expected = {'program_uuid': 'No active ProgramCertificate exists for program [{}]'.format(program_uuid)} self.assertEqual(ex.detail, expected) def test_to_internal_value_with_empty_program_uuid(self): """ Verify an error is raised if no program UUID is provided. """ with self.assertRaisesRegex(ValidationError, 'Credential identifier is missing'): self.field_instance.to_internal_value({'program_uuid': ''}) def test_to_internal_value_with_invalid_program_uuid(self, ): """ Verify the method raises a ValidationError if the passed program UUID does not correspond to a ProgramCertificate. """ self.assert_program_uuid_validation_error_raised(uuid4()) def test_to_internal_value_with_inactive_program_certificate(self, ): """ Verify the method raises a ValidationError if the ProgramCertificate is NOT active. """ self.program_certificate.is_active = False self.program_certificate.save() self.assert_program_uuid_validation_error_raised(self.program_certificate.program_uuid) def test_to_internal_value_with_valid_program_credential(self): """ Verify the method returns the ProgramCertificate corresponding to the specified UUID. """ self.assertEqual( self.field_instance.to_internal_value({'program_uuid': self.program_certificate.program_uuid}), self.program_certificate ) def test_to_representation_data_with_program(self): """ Verify the method serializes the credential details to a dict. """ expected = { 'program_uuid': self.program_certificate.program_uuid, 'credential_id': self.program_certificate.id } self.assertEqual(self.field_instance.to_representation(self.program_certificate), expected)
class CredentialFieldTests(SiteMixin, TestCase): def setUp(self): super().setUp() self.program_certificate = ProgramCertificateFactory(site=self.site) self.course_certificate = CourseCertificateFactory( site=self.site, certificate_type='verified') self.field_instance = CredentialField() # see: https://github.com/encode/django-rest-framework/blob/3.9.x/rest_framework/fields.py#L610 # pylint: disable=protected-access self.field_instance._context = { 'request': namedtuple('HttpRequest', ['site'])(self.site), } def assert_program_uuid_validation_error_raised(self, program_uuid): try: self.field_instance.to_internal_value( {'program_uuid': program_uuid}) except ValidationError as ex: expected = { 'program_uuid': f'No active ProgramCertificate exists for program [{program_uuid}]' } self.assertEqual(ex.detail, expected) def assert_course_run_key_validation_error_raised(self, course_run_key): try: self.field_instance.to_internal_value({ 'course_run_key': course_run_key, 'mode': 'verified' }) except ValidationError as ex: expected = { 'course_run_key': f'No active CourseCertificate exists for course run [{course_run_key}]' } self.assertEqual(ex.detail, expected) def test_to_internal_value_with_empty_program_uuid(self): """ Verify an error is raised if no program UUID is provided. """ with self.assertRaisesMessage(ValidationError, 'Credential identifier is missing'): self.field_instance.to_internal_value({'program_uuid': ''}) def test_to_internal_value_with_invalid_program_uuid(self, ): """ Verify the method raises a ValidationError if the passed program UUID does not correspond to a ProgramCertificate. """ self.assert_program_uuid_validation_error_raised(uuid4()) def test_to_internal_value_with_invalid_site(self, ): """ Verify the method raises a ValidationError if the passed program UUID belongs to a different site. """ certificate = ProgramCertificateFactory( ) # without setting site=self.site self.assert_program_uuid_validation_error_raised( certificate.program_uuid) def test_to_internal_value_with_inactive_program_certificate(self, ): """ Verify the method raises a ValidationError if the ProgramCertificate is NOT active. """ self.program_certificate.is_active = False self.program_certificate.save() self.assert_program_uuid_validation_error_raised( self.program_certificate.program_uuid) def test_to_internal_value_with_valid_program_credential(self): """ Verify the method returns the ProgramCertificate corresponding to the specified UUID. """ self.assertEqual( self.field_instance.to_internal_value( {'program_uuid': self.program_certificate.program_uuid}), self.program_certificate) def test_to_internal_value_with_created_course_credential(self): """ Verify the method creates a course credential if needed. """ credential = self.field_instance.to_internal_value({ 'course_run_key': 'create-me', 'mode': 'verified' }) self.assertEqual(credential, CourseCertificate.objects.get(course_id='create-me')) def test_to_internal_value_with_created_course_credential_read_only(self): """ Verify the method refuses to create a course credential when read-only. """ self.field_instance.read_only = True self.assert_course_run_key_validation_error_raised('create-me') def test_to_internal_value_with_created_course_credential_no_type_change( self): """ Verify the method won't update cert information when creating a course credential. """ credential = self.field_instance.to_internal_value({ 'course_run_key': self.course_certificate.course_id, 'mode': 'honor' }) credential.refresh_from_db() # just in case self.assertEqual(credential.certificate_type, 'verified') def test_to_internal_value_with_inactive_course_credential(self): """ Verify the method raises a ValidationError if the CourseCertificate is NOT active. """ self.course_certificate.is_active = False self.course_certificate.save() self.assert_course_run_key_validation_error_raised( self.course_certificate.course_id) def test_to_internal_value_with_valid_course_credential(self): """ Verify the method serializes the course credential details to a dict. """ self.assertEqual( self.field_instance.to_internal_value({ 'course_run_key': self.course_certificate.course_id, 'mode': 'verified' }), self.course_certificate) def test_to_representation_data_with_program(self): """ Verify the method serializes the credential details to a dict. """ expected = { 'type': 'program', 'program_uuid': self.program_certificate.program_uuid, 'credential_id': self.program_certificate.id } self.assertEqual( self.field_instance.to_representation(self.program_certificate), expected) def test_to_representation_data_with_course(self): """ Verify the method serializes the credential details to a dict. """ expected = { 'type': 'course-run', 'course_run_key': self.course_certificate.course_id, 'mode': self.course_certificate.certificate_type, } self.assertEqual( self.field_instance.to_representation(self.course_certificate), expected)
def setUp(self): super(CredentialFieldTests, self).setUp() self.program_certificate = ProgramCertificateFactory() self.field_instance = CredentialField()