Skip to content

Commit

Permalink
fixup improve coverage with signature errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dutrieuc committed Dec 19, 2024
1 parent ed616d7 commit aa1f1e1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
12 changes: 11 additions & 1 deletion auth_saml/tests/fake_idp.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"endpoints": {
"single_sign_on_service": [
("%s/sso/redirect" % BASE, BINDING_HTTP_REDIRECT),
("%s/sso/post" % BASE, BINDING_HTTP_POST),
("%s/sso/post" % BASE, BINDING_HTTP_POST),
],
},
"policy": {
Expand Down Expand Up @@ -165,3 +165,13 @@ def authn_request_endpoint(self, req, binding, relay_state):
)

return DummyResponse(**_dict)


class UnsignedFakeIDP(FakeIDP):
def create_authn_response(
self,
*args,
**kwargs,
):
kwargs["sign_assertion"] = False
return super().create_authn_response(*args, **kwargs)
36 changes: 35 additions & 1 deletion auth_saml/tests/test_pysaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
from unittest.mock import patch

import responses
from saml2.sigver import SignatureError

from odoo.exceptions import AccessDenied, UserError, ValidationError
from odoo.tests import HttpCase, tagged
from odoo.tools import mute_logger

from .fake_idp import CONFIG, FakeIDP
from .fake_idp import CONFIG, FakeIDP, UnsignedFakeIDP


@tagged("saml", "post_install", "-at_install")
Expand Down Expand Up @@ -452,3 +454,35 @@ def test_login_with_saml_metadata_key_changed(self):
body=up_to_date_metadata,
)
self.test_login_with_saml()

@responses.activate
def test_login_with_saml_unsigned_response(self):
self.add_provider_to_user()
self.saml_provider.idp_metadata_url = "http://localhost:8000/metadata"
unsigned_idp = UnsignedFakeIDP([self.saml_provider._metadata_string()])
redirect_url = self.saml_provider._get_auth_request()
self.assertIn("http://localhost:8000/sso/redirect?SAMLRequest=", redirect_url)

response = unsigned_idp.fake_login(redirect_url)
self.assertEqual(200, response.status_code)
unpacked_response = response._unpack()

responses.add(
responses.GET,
"http://localhost:8000/metadata",
status=200,
content_type="text/xml",
body=self.saml_provider.idp_metadata,
)
with (
self.assertRaises(SignatureError),
mute_logger("saml2.entity"),
mute_logger("saml2.client_base"),
):
(database, login, token) = (
self.env["res.users"]
.sudo()
.auth_saml(
self.saml_provider.id, unpacked_response.get("SAMLResponse"), None
)
)

0 comments on commit aa1f1e1

Please sign in to comment.