def test_error_if_ec_and_idp_site_does_not_match(self, mock_method):
        """
        Test error message when enterprise customer's site and identity provider's site are not same.

        Test validation error message when the site of selected identity provider does not match with
        enterprise customer's site.
        """
        mock_method.return_value = mock.Mock(site=self.second_site)

        form = EnterpriseCustomerIdentityProviderAdminForm(data={
            "provider_id":
            self.provider_id,
            "enterprise_customer":
            self.enterprise_customer.uuid
        }, )

        error_message = (
            "The site for the selected identity provider "
            "({identity_provider_site}) does not match the site for "
            "this enterprise customer ({enterprise_customer_site}). "
            "To correct this problem, select a site that has a domain "
            "of '{identity_provider_site}', or update the identity "
            "provider to '{enterprise_customer_site}'.").format(
                enterprise_customer_site=self.first_site,
                identity_provider_site=self.second_site,
            )

        # Validate and clean form data
        assert not form.is_valid()
        assert error_message in form.errors["__all__"]
    def test_no_errors(self, mock_method):
        """
        Test clean method on form runs without any errors.

        Test that form's clean method runs fine when there are not errors or missing fields.
        """
        mock_method.return_value = mock.Mock(site=self.first_site)

        form = EnterpriseCustomerIdentityProviderAdminForm(
            data={"provider_id": self.provider_id, "enterprise_customer": self.enterprise_customer.uuid},
        )

        # Validate and clean form data
        assert form.is_valid()
    def test_clean_runs_without_errors(self, mock_method):
        """
        Test clean method on form runs without any errors.

        Test that form's clean method runs fine in situations where a previous error on some field has already
        raised validation errors.
        """
        mock_method.return_value = mock.Mock(site=self.first_site)

        form = EnterpriseCustomerIdentityProviderAdminForm(
            data={"provider_id": self.provider_id}, )
        error_message = "This field is required."

        # Validate and clean form data
        assert not form.is_valid()
        assert error_message in form.errors['enterprise_customer']
 def test_no_idp_choices(self):
     """
     Test that a CharField is displayed when get_idp_choices returns None.
     """
     with mock.patch("enterprise.admin.forms.utils.get_idp_choices", mock.Mock(return_value=None)):
         form = EnterpriseCustomerIdentityProviderAdminForm()
         assert not hasattr(form.fields['provider_id'], 'choices')
Exemple #5
0
    def test_error_if_identity_provider_does_not_exist(self, mock_method):
        """
        Test error message when identity provider does not exist.

        Test validation error when selected identity provider does not exists in the system.
        """
        mock_method.side_effect = ObjectDoesNotExist

        form = EnterpriseCustomerIdentityProviderAdminForm(
            data={"provider_id": self.provider_id, "enterprise_customer": self.enterprise_customer.uuid},
        )
        error_message = "Selected Identity Provider does not exist, please contact system administrator for more info."

        # Validate and clean form data
        assert not form.is_valid()
        assert error_message in form.errors["__all__"]
    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
 def test_with_idp_choices(self):
     """
     Test that a TypedChoiceField is displayed when get_idp_choices returns choice tuples.
     """
     with mock.patch("enterprise.admin.forms.utils.get_idp_choices", mock.Mock(return_value=self.idp_choices)):
         form = EnterpriseCustomerIdentityProviderAdminForm()
         assert hasattr(form.fields['provider_id'], 'choices')
         assert form.fields['provider_id'].choices == list(self.idp_choices)
Exemple #8
0
    def test_error_if_identity_provider_does_not_exist(self, mock_method):
        """
        Test error message when identity provider does not exist.

        Test validation error when selected identity provider does not exists in the system.
        """
        mock_method.return_value = None

        form = EnterpriseCustomerIdentityProviderAdminForm(
            data={"provider_id": self.provider_id, "enterprise_customer": self.enterprise_customer.uuid},
        )
        error_message = (
            "The specified Identity Provider does not exist. For "
            "more information, contact a system administrator."
        )
        # Validate and clean form data
        assert not form.is_valid()
        assert error_message in form.errors["__all__"]
Exemple #9
0
    def test_error_if_ec_and_idp_site_does_not_match(self, mock_method):
        """
        Test error message when enterprise customer's site and identity provider's site are not same.

        Test validation error message when the site of selected identity provider does not match with
        enterprise customer's site.
        """
        mock_method.return_value = mock.Mock(site=self.second_site)

        form = EnterpriseCustomerIdentityProviderAdminForm(
            data={"provider_id": self.provider_id, "enterprise_customer": self.enterprise_customer.uuid},
        )

        error_message = "Site ({identity_provider_site}) of selected identity provider does not match with " \
                        "enterprise customer's site ({enterprise_customer_site}).Please either select site with " \
                        "domain '{identity_provider_site}' or update identity provider's site to " \
                        "'{enterprise_customer_site}'.".format(
                            enterprise_customer_site=self.first_site,
                            identity_provider_site=self.second_site,
                        )
        # Validate and clean form data
        assert not form.is_valid()
        assert error_message in form.errors["__all__"]