Skip to content

Commit

Permalink
fastapi_auth_partner: clean test and add missing index
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienbeau committed Sep 10, 2023
1 parent 80af604 commit 819aeee
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
1 change: 0 additions & 1 deletion fastapi_auth_partner/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,4 @@ def __call__(


auth_partner_authenticated_partner = AuthPartner()

auth_partner_optionally_authenticated_partner = AuthPartner(allow_unauthenticated=True)
15 changes: 10 additions & 5 deletions fastapi_auth_partner/models/fastapi_auth_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion fastapi_auth_partner/security/res_group.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<odoo>

<record id="group_partner_auth_manager" model="res.groups">
<field name="name">Manage Partner Auth</field>
<field name="name">FastAPI Partner Auth Manager</field>
<field name="category_id" ref="base.module_category_hidden" />
<field
name="users"
Expand Down
55 changes: 28 additions & 27 deletions fastapi_auth_partner/tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
from contextlib import contextmanager
from functools import partial

from requests import Response
Expand All @@ -14,26 +13,21 @@
from ..routers.auth import auth_router


@tagged("post_install", "-at_install")
class TestAuth(FastAPITransactionCase):
class CommonTestAuth(FastAPITransactionCase):
@classmethod
def setUpClass(cls) -> 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(
Expand All @@ -46,35 +40,42 @@ def _register_partner(self):
)
return response

def _login(self, test_client):
response: Response = test_client.post(
"/auth/login",
content=json.dumps(
{
"login": "[email protected]",
"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)
self.assertEqual(response.json(), {"login": "[email protected]"})

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": "[email protected]",
"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": "[email protected]"})

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": "[email protected]"}),
Expand Down

0 comments on commit 819aeee

Please sign in to comment.