Esempio n. 1
0
    def test_clear_site_cache_on_db_write(self):
        """Verify the site cache is cleared whenever a SiteConfiguration instance is
        saved or deleted from the database."""
        with mock.patch.object(SiteManager, "clear_cache") as mock_clear_site_cache:
            sc = SiteConfigurationFactory()
            mock_clear_site_cache.assert_called_once()

            mock_clear_site_cache.reset_mock()
            sc.delete()
            mock_clear_site_cache.assert_called_once()
Esempio n. 2
0
 def setUp(self):
     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."
     }]
Esempio n. 3
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)
Esempio n. 4
0
 def test_invalid_site(self):
     """Verify that the view returns a 404 if user_credentials are displayed on a site
     they are not associated with.
     """
     domain = "unused.testsite"
     site_configuration = SiteConfigurationFactory(
         site__domain=domain,
     )
     test_site = site_configuration.site
     test_program_certificate = factories.ProgramCertificateFactory(site=test_site)
     test_signatory_1 = factories.SignatoryFactory()
     test_signatory_2 = factories.SignatoryFactory()
     test_program_certificate.signatories.add(test_signatory_1, test_signatory_2)
     test_user_credential = factories.UserCredentialFactory(
         username=self.MOCK_USER_DATA["username"], credential=test_program_certificate
     )
     response = self.client.get(test_user_credential.get_absolute_url())
     self.assertEqual(response.status_code, 404)
     # Change the program certificate site to the client's site and check that the
     # response returns the user's certificate.
     test_program_certificate.site = self.site
     test_program_certificate.save()
     response = self._render_user_credential(
         user_credential=test_user_credential, program_certificate=test_program_certificate
     )
     self.assertEqual(response.status_code, 200)
Esempio n. 5
0
    def test_program_uuid(self):
        """ Verify a ValidationError is raised if the program's authoring organizations have
        no certificate images. """
        sc = SiteConfigurationFactory()
        data = factory.build(dict, FACTORY_CLASS=ProgramCertificateFactory)
        data['site'] = sc.site.id

        form = ProgramCertificateAdminForm(data)
        with mock.patch.object(
                SiteConfiguration,
                'get_program',
                return_value=self.BAD_MOCK_API_PROGRAM) as mock_method:
            self.assertFalse(form.is_valid())
            mock_method.assert_called_with(data['program_uuid'],
                                           ignore_cache=True)
            self.assertEqual(
                form.errors['program_uuid'][0],
                'All authoring organizations of the program MUST have a certificate image defined!'
            )

        form = ProgramCertificateAdminForm(data)
        with mock.patch.object(
                SiteConfiguration,
                'get_program',
                return_value=self.GOOD_MOCK_API_PROGRAM) as mock_method:
            self.assertFalse(form.is_valid())
            mock_method.assert_called_with(data['program_uuid'],
                                           ignore_cache=True)
            self.assertNotIn('program_uuid', form.errors)
Esempio n. 6
0
    def setUp(self):
        super(SiteMixin, self).setUp()

        # Set the domain used for all test requests
        domain = 'testserver.fake'
        self.client = self.client_class(SERVER_NAME=domain)

        Site.objects.all().delete()
        self.site_configuration = SiteConfigurationFactory(
            site__domain=domain, site__id=settings.SITE_ID)
        self.site = self.site_configuration.site
Esempio n. 7
0
    def setUp(self):
        super().setUp()
        cache.clear()

        # Set the domain used for all test requests
        domain = "testserver.fake"
        self.client = self.client_class(SERVER_NAME=domain)

        Site.objects.all().delete()
        self.site_configuration = SiteConfigurationFactory(site__domain=domain, site__id=settings.SITE_ID)
        self.site = self.site_configuration.site

        # Clear edx rest api client cache
        TieredCache.dangerous_clear_all_tiers()
Esempio n. 8
0
    def test_program_uuid(self):
        """Verify a ValidationError is raised if the program's authoring organizations have
        no certificate images."""
        sc = SiteConfigurationFactory()
        data = factory.build(dict, FACTORY_CLASS=ProgramCertificateFactory)
        data["site"] = sc.site.id

        form = ProgramCertificateAdminForm(data)
        fake = faker.Faker()

        bad_organization = OrganizationDetails(
            uuid=fake.uuid4(),
            key=fake.word(),
            name=fake.word(),
            display_name=fake.word(),
            certificate_logo_image_url=None,
        )
        good_organization = dataclasses.replace(bad_organization, certificate_logo_image_url=fake.word())

        bad_program = ProgramDetails(
            uuid=fake.uuid4(),
            title=fake.word(),
            type=fake.word(),
            type_slug=fake.word(),
            credential_title=fake.word(),
            course_count=fake.random_digit(),
            organizations=[bad_organization],
            hours_of_effort=fake.random_digit(),
            status=fake.word(),
        )
        good_program = dataclasses.replace(bad_program, organizations=[good_organization])
        with mock.patch(
            "credentials.apps.credentials.forms.get_program_details_by_uuid", return_value=bad_program
        ) as mock_method:
            self.assertFalse(form.is_valid())
            mock_method.assert_called_with(data["program_uuid"], sc.site)
            self.assertEqual(
                form.errors["program_uuid"][0],
                "All authoring organizations of the program MUST have a certificate image defined!",
            )

        form = ProgramCertificateAdminForm(data)
        with mock.patch(
            "credentials.apps.credentials.forms.get_program_details_by_uuid", return_value=good_program
        ) as mock_method:
            self.assertFalse(form.is_valid())
            mock_method.assert_called_with(data["program_uuid"], sc.site)
            self.assertNotIn("program_uuid", form.errors)
Esempio n. 9
0
 def setUp(self):
     # pylint: disable=no-member
     super(CreateOrUpdateSiteCommandTests, self).setUp()
     self.site_configuration = SiteConfigurationFactory.build(segment_key=self.faker.word())
Esempio n. 10
0
 def test_str(self):
     """ Test the site value for site configuration model. """
     site = SiteFactory(domain="test.org", name="test")
     site_configuration = SiteConfigurationFactory(site=site)
     self.assertEqual(str(site_configuration), site.name)
Esempio n. 11
0
 def setUp(self):
     super().setUp()
     self.site_configuration = SiteConfigurationFactory.build(
         segment_key=self.faker.word())
Esempio n. 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)
Esempio n. 13
0
 def test_unicode(self):
     """ Test the site value for site configuration model. """
     site = SiteFactory(domain='test.org', name='test')
     site_configuration = SiteConfigurationFactory(site=site)
     self.assertEqual(unicode(site_configuration), site.name)
Esempio n. 14
0
 def setUp(self):
     super(CopyCatalogCommandTests, self).setUp()
     self.site_configuration = SiteConfigurationFactory.build(
         segment_key=self.faker.word())