From 819aeee2f6b46e243820bea016167e03a6177530 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20BEAU?= Date: Wed, 6 Sep 2023 23:17:04 +0200 Subject: [PATCH] fastapi_auth_partner: clean test and add missing index --- fastapi_auth_partner/dependencies.py | 1 - .../models/fastapi_auth_partner.py | 15 +++-- fastapi_auth_partner/security/res_group.xml | 2 +- fastapi_auth_partner/tests/test_auth.py | 55 ++++++++++--------- 4 files changed, 39 insertions(+), 34 deletions(-) diff --git a/fastapi_auth_partner/dependencies.py b/fastapi_auth_partner/dependencies.py index aa3e182a..9eb3ba59 100644 --- a/fastapi_auth_partner/dependencies.py +++ b/fastapi_auth_partner/dependencies.py @@ -64,5 +64,4 @@ def __call__( auth_partner_authenticated_partner = AuthPartner() - auth_partner_optionally_authenticated_partner = AuthPartner(allow_unauthenticated=True) diff --git a/fastapi_auth_partner/models/fastapi_auth_partner.py b/fastapi_auth_partner/models/fastapi_auth_partner.py index 1da9cbd2..0f1bfe3e 100644 --- a/fastapi_auth_partner/models/fastapi_auth_partner.py +++ b/fastapi_auth_partner/models/fastapi_auth_partner.py @@ -32,20 +32,25 @@ class FastApiAuthPartner(models.Model): _description = "FastApi Auth Partner" _rec_name = "login" - partner_id = fields.Many2one("res.partner", "Partner", required=True) - directory_id = fields.Many2one("fastapi.auth.directory", "Directory", required=True) - login = fields.Char(compute="_compute_login", store=True, required=True) + partner_id = fields.Many2one( + "res.partner", "Partner", required=True, ondelete="cascade", index=True + ) + directory_id = fields.Many2one( + "fastapi.auth.directory", "Directory", required=True, index=True + ) + login = fields.Char(compute="_compute_login", store=True, required=True, index=True) password = fields.Char(compute="_compute_password", inverse="_inverse_password") - encrypted_password = fields.Char() + encrypted_password = fields.Char(index=True) token_set_password_encrypted = fields.Char() token_expiration = fields.Datetime() nbr_pending_reset_sent = fields.Integer( + index=True, help=( "Number of pending reset sent from your customer." "This field is usefull when after a migration from an other system " "you ask all you customer to reset their password and you send" "different mail depending on the number of reminder" - ) + ), ) date_last_request_reset_pwd = fields.Datetime( help="Date of the last password reset request" diff --git a/fastapi_auth_partner/security/res_group.xml b/fastapi_auth_partner/security/res_group.xml index 5d255c82..6d07b501 100644 --- a/fastapi_auth_partner/security/res_group.xml +++ b/fastapi_auth_partner/security/res_group.xml @@ -2,7 +2,7 @@ - Manage Partner Auth + FastAPI Partner Auth Manager None: super().setUpClass() + cls.demo_app = cls.env.ref("fastapi_auth_partner.fastapi_endpoint_demo") cls.env = cls.env(context=dict(cls.env.context, test_queue_job_no_delay=True)) cls.default_fastapi_router = auth_router - - @contextmanager - def _create_test_auth_client(self): - demo_app = self.env.ref("fastapi_auth_partner.fastapi_endpoint_demo") - with self._create_test_client( - demo_app._get_app(), - dependency_overrides={fastapi_endpoint: partial(lambda a: a, demo_app)}, - env=self.env, - ) as test_client: - yield test_client + cls.default_fastapi_app = cls.demo_app._get_app() + cls.default_fastapi_dependency_overrides = { + fastapi_endpoint: partial(lambda a: a, cls.demo_app) + } + cls.default_fastapi_odoo_env = cls.env def _register_partner(self): - with self._create_test_auth_client() as test_client: + with self._create_test_client() as test_client: response: Response = test_client.post( "/auth/register", content=json.dumps( @@ -46,6 +40,22 @@ def _register_partner(self): ) return response + def _login(self, test_client): + response: Response = test_client.post( + "/auth/login", + content=json.dumps( + { + "login": "loriot@example.org", + "password": "supersecret", + } + ), + ) + self.assertEqual(response.status_code, status.HTTP_200_OK) + return response + + +@tagged("post_install", "-at_install") +class TestAuth(CommonTestAuth): def test_register(self): response = self._register_partner() self.assertEqual(response.status_code, status.HTTP_201_CREATED) @@ -53,28 +63,19 @@ def test_register(self): def test_login(self): self._register_partner() - with self._create_test_auth_client() as test_client: - response: Response = test_client.post( - "/auth/login", - content=json.dumps( - { - "login": "loriot@example.org", - "password": "supersecret", - } - ), - ) - self.assertEqual(response.status_code, status.HTTP_200_OK) + with self._create_test_client() as test_client: + response = self._login(test_client) self.assertEqual(response.json(), {"login": "loriot@example.org"}) def test_logout(self): self._register_partner() - with self._create_test_auth_client() as test_client: + with self._create_test_client() as test_client: response: Response = test_client.post("/auth/logout") self.assertEqual(response.status_code, status.HTTP_205_RESET_CONTENT) def test_request_reset_password(self): self._register_partner() - with self._create_test_auth_client() as test_client: + with self._create_test_client() as test_client: response: Response = test_client.post( "/auth/request_reset_password", content=json.dumps({"login": "loriot@example.org"}),