def test_single_organization(self):
     """
     Ensure multiple orgs to be forbidden.
     """
     my_site = SiteFactory(domain='my_site.org')
     other_site = SiteFactory(domain='other_site.org')  # ensure no collision.
     my_org = OrganizationFactory.create(sites=[my_site])
     OrganizationFactory.create(sites=[other_site])  # ensure no collision.
     request = Mock(get_host=Mock(return_value=my_site.domain))
     assert my_org == get_current_organization(request)
    def setUp(self):
        """
        Test set up.
        """
        super(TestEnterpriseCustomerIdentityProviderAdminForm, self).setUp()
        self.idp_choices = (("saml-idp1", "SAML IdP 1"), ('saml-idp2', "SAML IdP 2"))

        self.first_site = SiteFactory(domain="first.localhost.com")
        self.second_site = SiteFactory(domain="second.localhost.com")
        self.enterprise_customer = EnterpriseCustomerFactory(site=self.first_site)
        self.provider_id = FAKER.slug()  # pylint: disable=no-member
示例#3
0
    def test_get_configuration_value_for_site_with_configuration(self):
        """
        ``get_configuration_value_for_site`` returns the key's value or the default in the site configuration.

        We do not test whether we get back the key's value or the default in particular, but just that
        the function returns a value through the configuration, rather than the default.
        """
        site = SiteFactory()
        site.configuration = mock.MagicMock(get_value=mock.MagicMock(
            return_value='value'))
        assert utils.get_configuration_value_for_site(site, 'key',
                                                      'default') == 'value'
示例#4
0
    def test_on_main_site_without_uuid_parameter(self, settings):
        """
        Non superusers shouldn't use the CAG on main site ( e.g. tahoe.appsembler.com/admin ).
        """
        main_site = SiteFactory.create(domain='main_site')
        settings.SITE_ID = main_site.id

        customer_site = SiteFactory.create(domain='customer_site')
        OrganizationFactory.create(sites=[customer_site
                                          ])  # Creates customer_org
        superuser = UserFactory.create(is_superuser=True)
        request = Mock(site=main_site, user=superuser, GET={})

        with pytest.raises(Organization.DoesNotExist,
                           match=r'Tahoe.*Should not find.*SITE_ID'):
            get_requested_organization(request)
 def test_no_organization(self):
     """
     Ensure no orgs are handled properly.
     """
     site = SiteFactory(domain='my_site.org')
     request = Mock(get_host=Mock(return_value=site.domain))
     with pytest.raises(Organization.DoesNotExist):
         get_current_organization(request)
示例#6
0
    def test_on_main_site_with_uuid_parameter(self, settings):
        """
        Superusers can use the `get_requested_organization` helper with `organization_uuid`.
        """
        main_site = SiteFactory.create(domain='main_site')
        settings.SITE_ID = main_site.id

        customer_site = SiteFactory.create(domain='customer_site')
        customer_org = OrganizationFactory.create(sites=[customer_site])
        superuser = UserFactory.create(is_superuser=True)
        request = Mock(site=main_site,
                       user=superuser,
                       GET={
                           'organization_uuid': customer_org.edx_uuid,
                       })

        requested_org = get_requested_organization(request)
        assert requested_org == customer_org, 'Should return the site organization'
示例#7
0
    def test_send_email_notification_message_with_site_from_email_override(
            self, site_config_from_email_address, expected_from_email_address):
        """
        Test that we can successfully override a from email address per site.
        """
        user = UserFactory(username='******',
                           email='*****@*****.**',
                           first_name='sal')
        enrolled_in = {
            'name': 'Demo Course',
            'url': 'http://lms.example.com/courses',
            'type': 'course',
            'start': '2017-01-01'
        }
        enrolled_in['start'] = datetime.datetime.strptime(
            enrolled_in['start'], '%Y-%m-%d')

        site = SiteFactory()
        if site_config_from_email_address:
            site.configuration = mock.MagicMock(get_value=mock.MagicMock(
                return_value=site_config_from_email_address))

        enterprise_customer = mock.MagicMock(
            name='Example Corporation',
            enterprise_enrollment_template=mock.MagicMock(
                render_all_templates=mock.MagicMock(return_value=((
                    'plaintext_value',
                    '<b>HTML value</b>',
                ))),
                subject_line='New course! {course_name}!'),
            site=site)

        conn = mail.get_connection()
        utils.send_email_notification_message(
            user,
            enrolled_in,
            enterprise_customer,
            email_connection=conn,
        )

        assert len(mail.outbox) == 1
        assert getattr(mail.outbox[0],
                       'from_email') == expected_from_email_address
        assert mail.outbox[0].connection is conn
示例#8
0
    def test_two_organizations(self):
        """
        Ensure multiple orgs to be forbidden.
        """
        site = SiteFactory.create(domain='my_site.org')
        OrganizationFactory.create_batch(2, sites=[site])
        request = Mock(site=site)

        with pytest.raises(Organization.MultipleObjectsReturned):
            get_current_organization(request)
示例#9
0
 def test_no_organization(self):
     """
     Ensure no orgs are handled properly.
     """
     site = SiteFactory.create(domain='my_site.org')
     request = Mock(site=site)
     with pytest.raises(
             Organization.DoesNotExist,
             match=r'Organization matching query does not exist'):
         get_current_organization(request)
示例#10
0
 def test_organization_main_site(self, settings):
     """
     Ensure no orgs are handled properly.
     """
     site = SiteFactory.create(domain='my_site.org')
     settings.SITE_ID = site.id
     request = Mock(site=site)
     with pytest.raises(Organization.DoesNotExist,
                        match=r'Tahoe.*Should not find.*SITE_ID'):
         get_current_organization(request)
示例#11
0
    def test_on_customer_site(self):
        """
        Customer sites can use CAG APIs.
        """
        site = SiteFactory.create(domain='my_site.org')
        expected_org = OrganizationFactory.create(sites=[site])
        non_superuser = UserFactory.create()
        request = Mock(site=site, user=non_superuser, GET={})

        requested_org = get_requested_organization(request)
        assert requested_org == expected_org, 'Should return the site organization'
示例#12
0
 def setup(self, client):
     client.defaults['SERVER_NAME'] = self.domain
     self.user = UserFactory.create(username='******')
     self.site = SiteFactory.create(domain=self.domain)
     self.my_org = OrganizationFactory.create(name='my_org', sites=[self.site])
     self.other_org = OrganizationFactory.create(name='other_org')
     self.staff = UserOrganizationMapping.objects.create(
         user=self.user,
         organization=self.my_org,
         is_amc_admin=True,
     )
     client.force_login(self.user)
示例#13
0
    def test_on_main_site_with_uuid_parameter_non_staff(self, settings):
        """
        Non superusers shouldn't be able to use the `organization_uuid` parameters.
        """
        main_site = SiteFactory.create(domain='main_site')
        settings.SITE_ID = main_site.id

        customer_site = SiteFactory.create(domain='customer_site')
        customer_org = OrganizationFactory.create(sites=[customer_site])
        non_superuser = UserFactory.create()
        request = Mock(site=main_site,
                       user=non_superuser,
                       GET={
                           'organization_uuid': customer_org.edx_uuid,
                       })

        with pytest.raises(
                PermissionDenied,
                match=r'Not permitted to use the `organization_uuid` parameter.'
        ):
            get_requested_organization(request)
示例#14
0
    def test_get_theme_when_theme_does_not_exist(self):  # pylint: disable=invalid-name
        """
        Validate get_theme returns default theme if site has no associated theme.
        """
        site = SiteFactory(domain='blue-theme')

        # Make sure there is not theme with the above site
        Theme.objects.filter(site=site).delete()  # pylint: disable=no-member

        self.assertEqual(
            Theme.get_theme(site),
            Theme(name=settings.THEMING['DEFAULT'], site=site)
        )
示例#15
0
    def test_get_theme_when_theme_does_not_exist_and_no_default_theme(self):  # pylint: disable=invalid-name
        """
        Validate get_theme returns Noneif site has no associated theme and there is not default theme.
        """
        site = SiteFactory(domain='blue-theme')

        # Make sure there is not theme with the above site
        Theme.objects.filter(site=site).delete()  # pylint: disable=no-member

        with override_settings(THEMING=dict(settings.THEMING, DEFAULT=None)):
            self.assertEqual(
                Theme.get_theme(site),
                None,
            )
示例#16
0
    def test_create_new_identity_provider_link(self, mock_idp_choices,
                                               mock_url, mock_saml_config,
                                               mock_method):
        """
        Test create new identity provider link in help text.
        """
        provider_id = FAKER.slug()  # pylint: disable=no-member
        name = FAKER.name()

        # pylint: disable=invalid-name
        enterprise_customer_identity_provider = EnterpriseCustomerIdentityProviderFactory(
            enterprise_customer=EnterpriseCustomerFactory(site=SiteFactory(
                domain="site.localhost.com")))
        mock_method.return_value = mock.Mock(pk=1,
                                             name=name,
                                             provider_id=provider_id)
        mock_saml_config._meta.app_label = 'test_app'
        mock_saml_config._meta.model_name = 'test_model'
        mock_url.return_value = '/test_saml_app/test_saml_model/add/'
        mock_idp_choices.return_value = self.idp_choices
        form = EnterpriseCustomerIdentityProviderAdminForm(
            {
                'provider_id': provider_id,
                'enterprise_customer': self.enterprise_customer
            },
            instance=enterprise_customer_identity_provider)
        assert '/test_saml_app/test_saml_model/add/?source=1' in form.fields[
            'provider_id'].help_text
        assert form.fields['provider_id'].choices == list(self.idp_choices)

        # Without provider id information.
        form = EnterpriseCustomerIdentityProviderAdminForm(
            {'enterprise_customer': self.enterprise_customer}, instance=None)
        assert 'Create a new identity provider' in form.fields[
            'provider_id'].help_text
        assert '/test_saml_app/test_saml_model/add/?source=1' not in form.fields[
            'provider_id'].help_text
        assert form.fields['provider_id'].choices == list(self.idp_choices)

        mock_method.return_value = None
        # Invalid provider id.
        form = EnterpriseCustomerIdentityProviderAdminForm(
            {'enterprise_customer': self.enterprise_customer},
            instance=enterprise_customer_identity_provider)
        assert 'Make sure you have added a valid provider_id' in form.fields[
            'provider_id'].help_text
示例#17
0
 def setup(self, db, monkeypatch, standard_test_users):
     self.site = SiteFactory.create()
     self.organization = OrganizationFactory(sites=[self.site])
     self.callers = [
         UserFactory.create(username='******'),
         UserFactory.create(username='******'),
         UserFactory.create(username='******'),
     ]
     self.user_organization_mappings = [
         UserOrganizationMapping.objects.create(
             user=self.callers[0], organization=self.organization),
         UserOrganizationMapping.objects.create(
             user=self.callers[1],
             organization=self.organization,
             is_amc_admin=True)
     ]
     self.callers += standard_test_users
     self.request = APIRequestFactory().get('/')
     self.request.site = self.site
     monkeypatch.setattr(sites_shortcuts, 'get_current_site',
                         self.get_test_site)
示例#18
0
 def test_get_configuration_value_for_site_without_configuration(self):
     """
     ``get_configuration_value_for_site`` returns the default because of no site configuration.
     """
     assert utils.get_configuration_value_for_site(SiteFactory(), 'key',
                                                   'default') == 'default'