Skip to content

Commit

Permalink
[MIG] auth_jwt: convert unit tests to integration tests
Browse files Browse the repository at this point in the history
The unit tests were broken for non-functional reasons (interaction with
the mock) and is easier to implement as integration test.
  • Loading branch information
sbidoul committed Jun 7, 2023
1 parent 845ffac commit 35f4fd6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
17 changes: 2 additions & 15 deletions auth_jwt/tests/test_auth_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def _create_validator(
issuer="http://the.issuer",
secret_key="thesecret",
partner_id_required=False,
static_user_id=1,
):
return self.env["auth.jwt.validator"].create(
dict(
Expand All @@ -82,6 +83,7 @@ def _create_validator(
audience=audience,
issuer=issuer,
user_id_strategy="static",
static_user_id=static_user_id,
partner_id_strategy="email",
partner_id_required=partner_id_required,
)
Expand Down Expand Up @@ -258,13 +260,6 @@ def test_invalid_validation_auto_chain(self):
"Validators mustn't make a closed chain: " "validator -> validator.",
)

def test_user_id_strategy(self):
validator = self._create_validator("validator5")
authorization = "Bearer " + self._create_token()
with self._mock_request(authorization=authorization) as request:
self.env["ir.http"]._auth_method_jwt_validator5()
self.assertEqual(request.env.uid, validator.static_user_id.id)

def test_partner_id_strategy_email_found(self):
partner = self.env["res.partner"].search([("email", "!=", False)])[0]
self._create_validator("validator6")
Expand Down Expand Up @@ -396,14 +391,6 @@ def test_name_check(self):
with self.assertRaises(ValidationError):
self._create_validator(name="not an identifier")

def test_public_or_jwt_no_token(self):
with self._mock_request(authorization=None) as request:
self.env["ir.http"]._auth_method_public_or_jwt()
request.update_env.assert_called_once_with(
user=self.env.ref("base.public_user").id
)
assert not hasattr(request, "jwt_payload")

def test_public_or_jwt_valid_token(self):
self._create_validator("validator")
authorization = "Bearer " + self._create_token()
Expand Down
6 changes: 3 additions & 3 deletions auth_jwt_demo/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def whoami(self):
data = {}
if request.jwt_partner_id:
partner = request.env["res.partner"].browse(request.jwt_partner_id)
data.update(name=partner.name, email=partner.email)
data.update(name=partner.name, email=partner.email, uid=request.env.uid)
return Response(json.dumps(data), content_type="application/json", status=200)

@route(
Expand Down Expand Up @@ -62,8 +62,8 @@ def whoami_public_or_keycloak(self):
data = {}
if hasattr(request, "jwt_partner_id") and request.jwt_partner_id:
partner = request.env["res.partner"].browse(request.jwt_partner_id)
data.update(name=partner.name, email=partner.email)
data.update(name=partner.name, email=partner.email, uid=request.env.uid)
else:
# public
data.update(name="Anonymous")
data.update(name="Anonymous", uid=request.env.uid)
return Response(json.dumps(data), content_type="application/json", status=200)
2 changes: 1 addition & 1 deletion auth_jwt_demo/demo/auth_jwt_validator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<field name="secret_algorithm">HS256</field>
<field name="secret_key">thesecret</field>
<field name="user_id_strategy">static</field>
<field name="static_user_id">1</field>
<field name="static_user_id" ref="base.user_demo" />
<field name="partner_id_strategy">email</field>
<field name="partner_id_required" eval="False" />
</record>
Expand Down
7 changes: 7 additions & 0 deletions auth_jwt_demo/tests/test_auth_jwt_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_whoami(self):
whoami = resp.json()
self.assertEqual(whoami.get("name"), partner.name)
self.assertEqual(whoami.get("email"), partner.email)
self.assertEqual(whoami.get("uid"), self.env.ref("base.user_demo").id)
# Try again in a user session, it will be rejected because auth_jwt
# is not designed to work in user session.
self.authenticate("demo", "demo")
Expand All @@ -49,3 +50,9 @@ def test_forbidden(self):
token = self._get_token(aud="invalid")
resp = self.url_open("/auth_jwt_demo/whoami", headers={"Authorization": token})
self.assertEqual(resp.status_code, 401)

def test_public(self):
"""A end-to-end test for anonymous/public access."""
resp = self.url_open("/auth_jwt_demo/keycloak/whoami-public-or-jwt")
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.json()["uid"], self.ref("base.public_user"))

0 comments on commit 35f4fd6

Please sign in to comment.