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)
Пример #2
0
class CredentialFieldTests(TestCase):
    """ CredentialField tests. """

    def setUp(self):
        super(CredentialFieldTests, self).setUp()
        self.program_cert = ProgramCertificateFactory()
        self.field_instance = serializers.CredentialField()

    @ddt.data(
        {"program_id": ""},
        {"course_id": ""},
        {"course_id": 404, 'certificate_type': ''},
    )
    def test_to_internal_value_with_empty_credential(self, credential_data):
        """Verify that it will return error message if credential-id attributes are empty."""

        with self.assertRaisesRegexp(ValidationError, "Credential ID is missing"):
            self.field_instance.to_internal_value(credential_data)

    def test_to_internal_value_with_invalid_program_credential(self,):
        """Verify that it will return error message if program-id does not exist in db."""

        with self.assertRaisesRegexp(ValidationError, "ProgramCertificate matching query does not exist."):
            self.field_instance.to_internal_value({"program_id": 404})

    def test_to_internal_value_with_in_active_program_credential(self,):
        """Verify that it will return error message if program is not active in db."""
        self.program_cert.is_active = False
        self.program_cert.save()

        with self.assertRaisesRegexp(ValidationError, "ProgramCertificate matching query does not exist."):
            self.field_instance.to_internal_value({"program_id": 404})

    def test_to_internal_value_with_invalid_course_credential(self):
        """Verify that it will return error message if course-id does not exist in db."""

        with self.assertRaisesRegexp(ValidationError, "CourseCertificate matching query does not exist."):
            self.field_instance.to_internal_value({"course_id": 404, 'certificate_type': "honor"})

    def test_to_internal_value_with_valid_program_credential(self):
        """Verify that it will return credential object if program-id found in db."""

        self.assertEqual(
            self.program_cert,
            self.field_instance.to_internal_value({"program_id": self.program_cert.program_id})
        )

    def test_to_internal_value_with_valid_course_credential(self):
        """Verify that it will return credential object if course-id and certificate type
        found in db."""

        course_cert = CourseCertificateFactory()
        self.assertEqual(
            course_cert,
            self.field_instance.to_internal_value({"course_id": course_cert.course_id, "certificate_type": "honor"})
        )

    def test_to_representation_data_with_program(self):
        """Verify that it will return program certificate credential object in dict format."""

        expected_data = {"program_id": self.program_cert.program_id, "credential_id": self.program_cert.id}
        self.assertEqual(
            expected_data,
            self.field_instance.to_representation(
                self.program_cert
            )
        )

    def test_to_representation_with_course(self):
        """Verify that it will return course certificate credential object in dict format."""

        course_cert = CourseCertificateFactory()
        expected_data = {
            "course_id": course_cert.course_id,
            "credential_id": course_cert.id,
            "certificate_type": course_cert.certificate_type
        }
        self.assertEqual(
            expected_data,
            self.field_instance.to_representation(
                course_cert
            )
        )
Пример #3
0
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)