Esempio n. 1
0
    def test_render_response_with_no_processor_and_post_binding(self):
        html_response = {"type": "POST", "data": "<html></html>"}
        response = IdPHandlerViewMixin().render_response(
            HttpRequest(), html_response)

        assert response.content.decode() == "<html></html>"
        assert isinstance(response, HttpResponse)
Esempio n. 2
0
    def test_render_response_with_no_processor_and_redirect_binding(self):
        html_response = {"type": "REDIRECT", "data": "https://example.com"}
        response = IdPHandlerViewMixin().render_response(
            HttpRequest(), html_response)

        assert response.url == "https://example.com"
        assert isinstance(response, HttpResponseRedirect)
Esempio n. 3
0
    def test_set_sp_works_if_sp_defined(self, settings):
        mixin = IdPHandlerViewMixin()
        mixin.set_sp('test_generic_sp')

        assert mixin.sp == {
            'id': 'test_generic_sp',
            'config': settings.SAML_IDP_SPCONFIG['test_generic_sp']
        }
Esempio n. 4
0
    def test_fetch_cutom_template_returns_default_if_not_found(self):
        custom_name = "hello"
        default_name = "djangosaml2idp/login.html"

        mixin = IdPHandlerViewMixin()

        template = mixin._fetch_custom_template(custom_name, default_name)

        assert default_name in template.origin.name
Esempio n. 5
0
    def test_render_login_hto_to_string_returns_result_of_render(
            self, mock_get_template):

        mixin = IdPHandlerViewMixin()
        rendered = mixin.render_login_html_to_string()

        assert rendered == mock_get_template.return_value.render.return_value
        mock_get_template.return_value.render.assert_called_once_with(
            None, None)
Esempio n. 6
0
 def test_create_html_response_with_get(self):
     mixin = IdPHandlerViewMixin()
     try:
         mixin.dispatch(HttpRequest())
     except AttributeError:
         html_response = mixin.create_html_response(
             HttpRequest(), BINDING_HTTP_REDIRECT, "SAMLResponse",
             "https://sp.example.com/SAML2", "")
         assert isinstance(html_response['data'], str)
Esempio n. 7
0
    def test_get_authn_returns_correctly_when_no_req_info(self):
        mixin = IdPHandlerViewMixin()

        assert mixin.get_authn() == {
            'authn_auth': '',
            'class_ref': 'urn:oasis:names:tc:SAML:2.0:ac:classes:Password',
            'level': 0,
            'method': ''
        }
Esempio n. 8
0
    def test_render_login_html_to_string_renders_default_if_custom_not_found(
            self, mock_get_template):

        mixin = IdPHandlerViewMixin()

        _ = mixin.render_login_html_to_string()

        mock_get_template.assert_called_once_with('djangosaml2idp/login.html',
                                                  using=None)
Esempio n. 9
0
    def test_fetch_custom_template_returns_custom_if_found(
            self, mock_get_template):
        custom_name = "hello"
        default_name = "default"

        mixin = IdPHandlerViewMixin()

        _ = mixin._fetch_custom_template(custom_name, default_name)

        mock_get_template.assert_called_once_with(custom_name, using=None)
Esempio n. 10
0
 def test_build_authn_response(self):
     mixin = IdPHandlerViewMixin()
     try:
         mixin.dispatch(HttpRequest())
     except AttributeError:
         mixin.set_sp('test_generic_sp')
         mixin.set_processor()
         user = User()
         authn = mixin.get_authn()
         resp_args = {
             "in_response_to": "SP_Initiated_Login",
             "destination": "https://sp.example.com/SAML2",
         }
         assert isinstance(
             mixin.build_authn_response(user, authn, resp_args), Response)
Esempio n. 11
0
    def test_fetch_custom_template_returns_default_if_syntax_error(
            self, mock_get_template, mocker):
        mock_get_template.side_effect = [
            TemplateSyntaxError("hello"),
            mocker.Mock()
        ]
        custom_name = "hello"
        default_name = "djangosaml2idp/login.html"

        mixin = IdPHandlerViewMixin()

        _ = mixin._fetch_custom_template(custom_name, default_name)

        first_call = mocker.call(custom_name, using=None)
        second_call = mocker.call('djangosaml2idp/login.html', using=None)
        mock_get_template.assert_has_calls((first_call, second_call))
Esempio n. 12
0
    def compile_data_for_render_response(self):
        mixin = IdPHandlerViewMixin()
        mixin.set_sp("test_generic_sp")
        mixin.set_processor()

        user = User.objects.create()
        user.email = "*****@*****.**",
        user.first_name = 'First Name',
        user.last_name = 'Last Name',
        user.is_staff = True
        user.is_superuser = False

        request = HttpRequest()
        request.user = user
        request.session = {}

        html_response = {"type": "POST", "data": "<html></html>"}
        return mixin, request, html_response
Esempio n. 13
0
    def compile_data_for_render_response(self):
        ServiceProvider.objects.create(entity_id='test_generic_sp',
                                       local_metadata=sp_metadata_xml)

        mixin = IdPHandlerViewMixin()
        _ = get_sp_config("test_generic_sp")

        user = User.objects.create()
        user.email = "*****@*****.**",
        user.first_name = 'First Name',
        user.last_name = 'Last Name',
        user.is_staff = True
        user.is_superuser = False

        request = HttpRequest()
        request.user = user
        request.session = {}

        html_response = {"type": "POST", "data": "<html></html>"}
        return mixin, request, html_response
Esempio n. 14
0
 def test_dispatch_gets_to_super_call_if_everything_correct(self):
     # If it gets this far, it worked b/c no dispatch method on the mixin
     with pytest.raises(AttributeError):
         IdPHandlerViewMixin().dispatch(HttpRequest())
Esempio n. 15
0
 def test_create_html_response_with_post(self):
     html_response = IdPHandlerViewMixin().create_html_response(
         HttpRequest(), BINDING_HTTP_POST, "SAMLResponse",
         "https://sp.example.com/SAML2", "")
     assert isinstance(html_response['data'], str)
Esempio n. 16
0
 def test_check_access_works(self):
     mixin = IdPHandlerViewMixin()
     mixin.set_sp('test_generic_sp')
     mixin.set_processor()
     mixin.check_access(HttpRequest())
Esempio n. 17
0
    def test_get_processor_loads_custom_processor(self):
        mixin = IdPHandlerViewMixin()
        mixin.set_sp('test_sp_with_custom_processor')
        mixin.set_processor()

        assert isinstance(mixin.processor, CustomProcessor)
Esempio n. 18
0
 def test_check_access_fails_when_it_should(self):
     mixin = IdPHandlerViewMixin()
     mixin.set_sp('test_sp_with_custom_processor_that_doesnt_allow_access')
     mixin.set_processor()
     with pytest.raises(PermissionDenied):
         mixin.check_access(HttpRequest())
Esempio n. 19
0
    def test_set_processor_defaults_to_base_processor(self):
        mixin = IdPHandlerViewMixin()
        mixin.set_sp('test_sp_with_no_processor')
        mixin.set_processor()

        assert isinstance(mixin.processor, BaseProcessor)
Esempio n. 20
0
    def test_set_processor_errors_if_processor_cannot_be_loaded(self):
        mixin = IdPHandlerViewMixin()
        mixin.set_sp('test_sp_with_bad_processor')

        with pytest.raises(Exception):
            mixin.set_processor()
Esempio n. 21
0
 def test_dispatch_correctly_assigns_a_conf_object(self):
     mixin = IdPHandlerViewMixin()
     try:
         mixin.dispatch(HttpRequest())
     except AttributeError:
         assert isinstance(mixin.IDP, Server)
Esempio n. 22
0
 def test_get_identity_provides_extra_config(self):
     IdPHandlerViewMixin()
Esempio n. 23
0
    def test_get_processor_defaults_to_base_processor(self):
        sp_config = {}

        assert isinstance(
            IdPHandlerViewMixin().get_processor('entity_id', sp_config),
            BaseProcessor)
Esempio n. 24
0
    def test_get_processor_errors_if_processor_cannot_be_loaded(self):
        sp_config = {'processor': 'this.does.not.exist'}

        with pytest.raises(Exception):
            IdPHandlerViewMixin().get_processor('entity_id', sp_config)
Esempio n. 25
0
    def test_dispatch_fails_if_IDP_config_undefined_in_settings(
            self, settings):
        del settings.SAML_IDP_CONF

        with pytest.raises(Exception):
            IdPHandlerViewMixin().dispatch(HttpRequest())
Esempio n. 26
0
    def test_get_processor_loads_custom_processor(self):
        sp_config = {'processor': 'tests.test_views.CustomProcessor'}

        assert isinstance(
            IdPHandlerViewMixin().get_processor('entity_id', sp_config),
            CustomProcessor)
Esempio n. 27
0
    def test_set_sp_errors_if_sp_not_defined(self):
        mixin = IdPHandlerViewMixin()

        with pytest.raises(ImproperlyConfigured):
            mixin.set_sp('this_sp_does_not_exist')