Example #1
0
 def setUp(self):
     super(ProgramCertificateIssuerTests, self).setUp()
     self.issuer = ProgramCertificateIssuer()
     self.program_certificate = ProgramCertificateFactory.create()
     self.username = '******'
     self.user_program_cred = self.issuer.issue_credential(self.program_certificate, self.username)
     self.attributes = [{"name": "whitelist_reason", "value": "Reason for whitelisting."}]
Example #2
0
    def test_create_credential_type_issuer_map(self):
        """ Verify the Accreditor supports only one issuer per credential type. """
        accreditor = Accreditor(
            issuers=[ProgramCertificateIssuer(),
                     ProgramCertificateIssuer()])

        expected = {self.program_credential: accreditor.issuers[0]}
        self.assertEqual(accreditor.credential_type_issuer_map, expected)
Example #3
0
    def test_constructor_with_multiple_issuers_for_same_credential_type(self):
        """ Verify the Accreditor supports a single Issuer per credential type.
        Attempts to register additional issuers for a credential type should
        result in a warning being logged.
        """
        msg = 'The issuer [{}] is already registered to issue credentials of type [{}]. [{}] will NOT be used.'.format(
            ProgramCertificateIssuer, self.program_credential, ProgramCertificateIssuer
        )

        with LogCapture(LOGGER_NAME) as l:
            accreditor = Accreditor(issuers=[ProgramCertificateIssuer(), ProgramCertificateIssuer()])
            l.check((LOGGER_NAME, 'WARNING', msg))
        expected = {self.program_credential: accreditor.issuers[0]}
        self.assertEqual(accreditor.credential_type_issuer_map, expected)
Example #4
0
 def setUp(self):
     super(ProgramCertificateIssuerTests, self).setUp()
     self.issuer = ProgramCertificateIssuer()
     self.program_certificate = ProgramCertificateFactory.create()
     self.username = '******'
     self.user_program_cred = self.issuer.issue_credential(self.program_certificate, self.username)
     self.attributes = [{"name": "whitelist_reason", "value": "Reason for whitelisting."}]
Example #5
0
 def test_issue_credential_with_invalid_type(self):
     """ Verify the method raises an error for attempts to issue an unsupported credential type. """
     accreditor = Accreditor(issuers=[ProgramCertificateIssuer()])
     course_cert = CourseCertificateFactory()
     with self.assertRaises(UnsupportedCredentialTypeError):
         accreditor.issue_credential(course_cert,
                                     'tester',
                                     attributes=self.attributes)
Example #6
0
    def test_constructor_with_multiple_issuers(self):
        """ Verify the Accreditor supports multiple Issuers """
        accreditor = Accreditor(issuers=[CourseCertificateIssuer(), ProgramCertificateIssuer()])

        expected = {
            self.course_credential: accreditor.issuers[0],
            self.program_credential: accreditor.issuers[1],
        }
        self.assertDictEqual(accreditor.credential_type_issuer_map, expected)
Example #7
0
 def test_issue_credential(self):
     """ Verify the method calls the Issuer's issue_credential method. """
     accreditor = Accreditor(issuers=[ProgramCertificateIssuer()])
     with patch.object(ProgramCertificateIssuer,
                       'issue_credential') as mock_method:
         accreditor.issue_credential(self.program_cert,
                                     'tester',
                                     attributes=self.attributes)
         mock_method.assert_called_with(self.program_cert, 'tester',
                                        'awarded', self.attributes, None)
Example #8
0
class ProgramCertificateIssuerTests(CertificateIssuerBase, TestCase):
    """ Tests for program Issuer class and its methods."""
    issuer = ProgramCertificateIssuer()
    cert_factory = ProgramCertificateFactory
    cert_type = ProgramCertificate

    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."}]
Example #9
0
class ProgramCertificateIssuerTests(CertificateIssuerBase, TestCase):
    """ Tests for program Issuer class and its methods."""
    issuer = ProgramCertificateIssuer()
    cert_factory = ProgramCertificateFactory
    cert_type = ProgramCertificate

    def setUp(self):
        super().setUp()
        self.site = SiteFactory()
        self.site_config = SiteConfigurationFactory(site=self.site)
        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 test_records_enabled_is_unchecked(self):
        """Verify that if SiteConfiguration.records_enabled is unchecked then don't send
        updated email to a pathway org.
        """
        self.site_config.records_enabled = False
        self.site_config.save()

        with mock.patch(
                'credentials.apps.credentials.issuers.send_updated_emails_for_program'
        ) as mock_method:
            self.issuer.issue_credential(self.certificate,
                                         'testuser3',
                                         attributes=self.attributes)
            self.assertEqual(mock_method.call_count, 0)

    def test_records_enabled_is_checked(self):
        """Verify that if SiteConfiguration.records_enabled is checked and new record is created
        then updated email is sent to a pathway org.
        """
        with mock.patch(
                'credentials.apps.credentials.issuers.send_updated_emails_for_program'
        ) as mock_method:
            self.issuer.issue_credential(self.certificate,
                                         'testuser4',
                                         attributes=self.attributes)
            self.assertEqual(mock_method.call_count, 1)
Example #10
0
 def __init__(self, issuers=None):
     self.issuers = issuers or [
         CourseCertificateIssuer(),
         ProgramCertificateIssuer()
     ]
     self._create_credential_type_issuer_map()
Example #11
0
class ProgramCertificateIssuerTests(CertificateIssuerBase, TestCase):
    """ Tests for program Issuer class and its methods."""
    issuer = ProgramCertificateIssuer()
    cert_factory = ProgramCertificateFactory
    cert_type = ProgramCertificate
Example #12
0
class ProgramCertificateIssuerTests(CertificateIssuerBase, TestCase):
    """Tests for program Issuer class and its methods."""

    issuer = ProgramCertificateIssuer()
    cert_factory = ProgramCertificateFactory
    cert_type = ProgramCertificate

    def setUp(self):
        super().setUp()
        self.site = SiteFactory()
        self.site_config = SiteConfigurationFactory(site=self.site)
        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 test_records_enabled_is_unchecked(self):
        """Verify that if SiteConfiguration.records_enabled is unchecked then don't send
        updated email to a pathway org.
        """
        self.site_config.records_enabled = False
        self.site_config.save()

        with mock.patch(
                "credentials.apps.credentials.issuers.send_updated_emails_for_program"
        ) as mock_method:
            self.issuer.issue_credential(self.certificate,
                                         "testuser3",
                                         attributes=self.attributes)
            self.assertEqual(mock_method.call_count, 0)

    def test_records_enabled_is_checked(self):
        """Verify that if SiteConfiguration.records_enabled is checked and new record is created
        then updated email is sent to a pathway org.
        """
        with mock.patch(
                "credentials.apps.credentials.issuers.send_updated_emails_for_program"
        ) as mock_method:
            self.issuer.issue_credential(self.certificate,
                                         "testuser4",
                                         attributes=self.attributes)
            self.assertEqual(mock_method.call_count, 1)

    @override_settings(SEND_EMAIL_ON_PROGRAM_COMPLETION=True)
    @mock.patch(
        "credentials.apps.credentials.issuers.send_program_certificate_created_message"
    )
    def test_send_learner_email_when_issuing_program_cert(
            self, mock_send_learner_email):
        self.site_config.records_enabled = False
        self.site_config.save()

        self.issuer.issue_credential(self.certificate, "testuser5")
        self.assertEqual(mock_send_learner_email.call_count, 1)

    @override_settings(SEND_EMAIL_ON_PROGRAM_COMPLETION=True)
    @mock.patch(
        "credentials.apps.credentials.issuers.send_program_certificate_created_message"
    )
    def test_send_learner_email_only_once(self, mock_send_learner_email):
        """
        Verify that we call `send_program_certificate_created_message` only once if a
        certificate already exists and is being awarded again after being revoked.
        """
        username = "******"
        user = UserFactory(username=username)

        self.site_config.records_enabled = False
        self.site_config.save()

        self.issuer.issue_credential(self.certificate, user.username)
        # revoke the user credential
        user_credential = UserCredential.objects.get(username=username)
        user_credential.revoke()
        # issue the credential again, make sure that we haven't tried to send the email again
        self.issuer.issue_credential(self.certificate, user.username)
        self.assertEqual(mock_send_learner_email.call_count, 1)

    @override_settings(SEND_EMAIL_ON_PROGRAM_COMPLETION=False)
    @mock.patch(
        "credentials.apps.credentials.issuers.send_program_certificate_created_message"
    )
    def test_do_not_send_learner_email_when_feature_disabled(
            self, mock_send_learner_email):
        """
        Verify that we do NOT try to send an email to the learner when a Program Cert is issued
        if the feature is disabled.
        """
        self.site_config.records_enabled = False
        self.site_config.save()

        self.issuer.issue_credential(self.certificate, "testuser6")
        self.assertEqual(mock_send_learner_email.call_count, 0)
Example #13
0
class ProgramCertificateIssuerTests(TestCase):
    """ Tests for Issuer class and its methods."""
    def setUp(self):
        super(ProgramCertificateIssuerTests, self).setUp()
        self.issuer = ProgramCertificateIssuer()
        self.program_certificate = ProgramCertificateFactory.create()
        self.username = '******'
        self.user_program_cred = self.issuer.issue_credential(
            self.program_certificate, self.username)
        self.attributes = [{
            "name": "whitelist_reason",
            "value": "Reason for whitelisting."
        }]

    def test_issued_credential_type(self):
        """ Verify issued_credential_type returns the correct credential type."""
        self.assertEqual(self.issuer.issued_credential_type,
                         ProgramCertificate)

    def test_issue_credential_without_attributes(self):
        """ Verify credentials can be issued without attributes."""

        user_credential = self.issuer.issue_credential(
            self.program_certificate, self.username)
        self._assert_usercredential_fields(user_credential,
                                           self.program_certificate,
                                           self.username, [])

    def test_issue_credential_with_attributes(self):
        """ Verify credentials can be issued with attributes."""

        user_credential = self.issuer.issue_credential(
            self.program_certificate, 'testuser2', self.attributes)
        self._assert_usercredential_fields(user_credential,
                                           self.program_certificate,
                                           'testuser2', self.attributes)

    def _assert_usercredential_fields(self, user_credential, expected_credential, expected_username, expected_attrs):  # pylint: disable=line-too-long
        """ Verify the fields on a UserCredential object match expectations. """
        self.assertEqual(user_credential.username, expected_username)
        self.assertEqual(user_credential.credential, expected_credential)
        actual_attributes = [{
            'name': attr.name,
            'value': attr.value
        } for attr in user_credential.attributes.all()]
        self.assertEqual(actual_attributes, expected_attrs)

    def test_set_credential_without_attributes(self):
        """ Verify that if no attributes given then None will return."""
        self.assertEqual(
            self.issuer.set_credential_attributes(self.user_program_cred,
                                                  None), None)

    def test_set_credential_with_attributes(self):
        """ Verify that it adds the given attributes against user credential."""

        self.issuer.set_credential_attributes(self.user_program_cred,
                                              self.attributes)
        self._assert_usercredential_fields(self.user_program_cred,
                                           self.program_certificate,
                                           self.user_program_cred.username,
                                           self.attributes)

    def test_set_credential_with_duplicate_attributes_by_util(self):
        """Verify in case of duplicate attributes utils method will return False and
        exception will be raised.
        """

        self.attributes.append({
            "name": "whitelist_reason",
            "value": "Reason for whitelisting."
        })

        with self.assertRaises(DuplicateAttributeError):
            self.issuer.set_credential_attributes(self.user_program_cred,
                                                  self.attributes)

    def test_existing_credential_with_duplicate_attributes(self):
        """Verify if user credential attributes already exists in db then method will
        update existing attributes values."""

        # add the attribute in db and then try to create the credential
        # with same data "names but value is different"

        attribute_db = {
            "name": "whitelist_reason",
            "value": "Reason for whitelisting."
        }

        UserCredentialAttribute.objects.create(
            user_credential=self.user_program_cred,
            name=attribute_db.get("name"),
            value=attribute_db.get("value"))
        self.issuer.set_credential_attributes(self.user_program_cred,
                                              self.attributes)

        # first attribute value will be changed to 0.5
        self._assert_usercredential_fields(self.user_program_cred,
                                           self.program_certificate,
                                           self.user_program_cred.username,
                                           self.attributes)

    def test_existing_attributes_with_empty_attributes_list(self):
        """Verify if user credential attributes already exists in db then in case of empty
        attributes list it will return without changing any data."""

        self.issuer.set_credential_attributes(self.user_program_cred,
                                              self.attributes)
        self._assert_usercredential_fields(self.user_program_cred,
                                           self.program_certificate,
                                           self.user_program_cred.username,
                                           self.attributes)

        # create same credential without attributes.
        self.assertIsNone(
            self.issuer.set_credential_attributes(self.user_program_cred, []))
        self._assert_usercredential_fields(self.user_program_cred,
                                           self.program_certificate,
                                           self.user_program_cred.username,
                                           self.attributes)
Example #14
0
class ProgramCertificateIssuerTests(TestCase):
    """ Tests for Issuer class and its methods."""

    def setUp(self):
        super(ProgramCertificateIssuerTests, self).setUp()
        self.issuer = ProgramCertificateIssuer()
        self.program_certificate = ProgramCertificateFactory.create()
        self.username = '******'
        self.user_program_cred = self.issuer.issue_credential(self.program_certificate, self.username)
        self.attributes = [{"name": "whitelist_reason", "value": "Reason for whitelisting."}]

    def test_issued_credential_type(self):
        """ Verify issued_credential_type returns the correct credential type."""
        self.assertEqual(self.issuer.issued_credential_type, ProgramCertificate)

    def test_issue_credential_without_attributes(self):
        """ Verify credentials can be issued without attributes."""

        user_credential = self.issuer.issue_credential(self.program_certificate, self.username)
        self._assert_usercredential_fields(user_credential, self.program_certificate, self.username, [])

    def test_issue_credential_with_attributes(self):
        """ Verify credentials can be issued with attributes."""

        user_credential = self.issuer.issue_credential(self.program_certificate, 'testuser2', self.attributes)
        self._assert_usercredential_fields(user_credential, self.program_certificate, 'testuser2', self.attributes)

    def _assert_usercredential_fields(self, user_credential, expected_credential, expected_username, expected_attrs):  # pylint: disable=line-too-long
        """ Verify the fields on a UserCredential object match expectations. """
        self.assertEqual(user_credential.username, expected_username)
        self.assertEqual(user_credential.credential, expected_credential)
        actual_attributes = [{'name': attr.name, 'value': attr.value} for attr in user_credential.attributes.all()]
        self.assertEqual(actual_attributes, expected_attrs)

    def test_set_credential_without_attributes(self):
        """ Verify that if no attributes given then None will return."""
        self.assertEqual(
            self.issuer.set_credential_attributes(self.user_program_cred, None),
            None
        )

    def test_set_credential_with_attributes(self):
        """ Verify that it adds the given attributes against user credential."""

        self.issuer.set_credential_attributes(self.user_program_cred, self.attributes)
        self._assert_usercredential_fields(
            self.user_program_cred, self.program_certificate, self.user_program_cred.username, self.attributes
        )

    def test_set_credential_with_duplicate_attributes_by_util(self):
        """Verify in case of duplicate attributes utils method will return False and
        exception will be raised.
        """

        self.attributes.append({"name": "whitelist_reason", "value": "Reason for whitelisting."})

        with self.assertRaises(DuplicateAttributeError):
            self.issuer.set_credential_attributes(self.user_program_cred, self.attributes)

    def test_existing_credential_with_duplicate_attributes(self):
        """Verify if user credential attributes already exists in db then method will
        update existing attributes values."""

        # add the attribute in db and then try to create the credential
        # with same data "names but value is different"

        attribute_db = {"name": "whitelist_reason", "value": "Reason for whitelisting."}

        UserCredentialAttribute.objects.create(
            user_credential=self.user_program_cred,
            name=attribute_db.get("name"),
            value=attribute_db.get("value")
        )
        self.issuer.set_credential_attributes(self.user_program_cred, self.attributes)

        # first attribute value will be changed to 0.5
        self._assert_usercredential_fields(
            self.user_program_cred,
            self.program_certificate,
            self.user_program_cred.username,
            self.attributes
        )

    def test_existing_attributes_with_empty_attributes_list(self):
        """Verify if user credential attributes already exists in db then in case of empty
        attributes list it will return without changing any data."""

        self.issuer.set_credential_attributes(self.user_program_cred, self.attributes)
        self._assert_usercredential_fields(
            self.user_program_cred, self.program_certificate, self.user_program_cred.username, self.attributes
        )

        # create same credential without attributes.
        self.assertIsNone(self.issuer.set_credential_attributes(self.user_program_cred, []))
        self._assert_usercredential_fields(
            self.user_program_cred, self.program_certificate, self.user_program_cred.username, self.attributes
        )