55import unittest
66from contextlib import contextmanager
77
8- from odoo import sql_db
8+ from odoo import Command , sql_db
99from odoo .tests .common import HttpCase
1010from odoo .tools import mute_logger
1111
@@ -19,13 +19,71 @@ class FastAPIHttpCase(HttpCase):
1919 @classmethod
2020 def setUpClass (cls ):
2121 super ().setUpClass ()
22- cls .fastapi_demo_app = cls .env .ref ("fastapi.fastapi_endpoint_demo" )
23- cls .fastapi_multi_demo_app = cls .env .ref ("fastapi.fastapi_endpoint_multislash_demo" )
22+ cls .fastapi_demo_app = cls ._get_or_create_demo_endpoint (
23+ "fastapi.fastapi_endpoint_demo" ,
24+ name = "Fastapi Demo Endpoint" ,
25+ root_path = "/fastapi_demo" ,
26+ )
27+ cls .fastapi_multi_demo_app = cls ._get_or_create_demo_endpoint (
28+ "fastapi.fastapi_endpoint_multislash_demo" ,
29+ name = "Fastapi Multi-Slash Demo Endpoint" ,
30+ root_path = "/fastapi/demo-multi" ,
31+ )
2432 cls .fastapi_apps = cls .fastapi_demo_app + cls .fastapi_multi_demo_app
2533 cls .fastapi_apps ._handle_registry_sync ()
2634 lang = cls .env ["res.lang" ].with_context (active_test = False ).search ([("code" , "=" , "fr_BE" )])
2735 lang .active = True
2836
37+ @classmethod
38+ def _get_or_create_demo_endpoint (cls , xml_id , name , root_path ):
39+ """Get demo endpoint from XML ID or create it if missing (demo data not loaded)."""
40+ try :
41+ return cls .env .ref (xml_id )
42+ except ValueError :
43+ demo_user = cls ._get_or_create_demo_user ()
44+ endpoint = cls .env ["fastapi.endpoint" ].create (
45+ {
46+ "name" : name ,
47+ "app" : "demo" ,
48+ "root_path" : root_path ,
49+ "demo_auth_method" : "http_basic" ,
50+ "user_id" : demo_user .id ,
51+ }
52+ )
53+ cls .env ["ir.model.data" ].create (
54+ {
55+ "name" : xml_id .split ("." )[- 1 ],
56+ "module" : "fastapi" ,
57+ "model" : "fastapi.endpoint" ,
58+ "res_id" : endpoint .id ,
59+ }
60+ )
61+ return endpoint
62+
63+ @classmethod
64+ def _get_or_create_demo_user (cls ):
65+ """Get or create the demo app user."""
66+ try :
67+ return cls .env .ref ("fastapi.my_demo_app_user" )
68+ except ValueError :
69+ runner_group = cls .env .ref ("fastapi.group_fastapi_endpoint_runner" )
70+ user = cls .env ["res.users" ].create (
71+ {
72+ "name" : "My Demo Endpoint User" ,
73+ "login" : "my_demo_app_user" ,
74+ "groups_id" : [Command .set ([runner_group .id ])],
75+ }
76+ )
77+ cls .env ["ir.model.data" ].create (
78+ {
79+ "name" : "my_demo_app_user" ,
80+ "module" : "fastapi" ,
81+ "model" : "res.users" ,
82+ "res_id" : user .id ,
83+ }
84+ )
85+ return user
86+
2987 @contextmanager
3088 def _mocked_commit (self ):
3189 with unittest .mock .patch .object (sql_db .TestCursor , "commit" , return_value = None ) as mocked_commit :
@@ -78,9 +136,7 @@ def assert_exception_processed(
78136 expected_status_code : int ,
79137 ) -> None :
80138 with self ._mocked_commit () as mocked_commit :
81- route = (
82- "/fastapi_demo/demo/exception?" f"exception_type={ exception_type .value } &error_message={ error_message } "
83- )
139+ route = f"/fastapi_demo/demo/exception?exception_type={ exception_type .value } &error_message={ error_message } "
84140 response = self .url_open (route , timeout = 200 )
85141 mocked_commit .assert_not_called ()
86142 self .assertDictEqual (
0 commit comments