Beispiel #1
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)
Beispiel #2
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)