77import jwt
88
99from odoo import tests
10+ from odoo .tools import convert , file_path
11+
12+
13+ class TestAuthJwtDemo (tests .common .HttpCase ):
14+ @classmethod
15+ def setUpClass (cls ):
16+ super ().setUpClass ()
1017
1118
1219@tests .tagged ("post_install" , "-at_install" )
1320class TestRegisterHook (tests .HttpCase ):
21+ def setUp (self ):
22+ super ().setUp ()
23+ demo_user = self .env .ref ("base.user_demo" , raise_if_not_found = False )
24+ if not demo_user :
25+ demo_user = self .env ["res.users" ].create (
26+ {
27+ "name" : "Demo User" ,
28+ "login" : "demo" ,
29+ "email" : "demo@example.com" ,
30+ "group_ids" : [(6 , 0 , [self .env .ref ("base.group_user" ).id ])],
31+ }
32+ )
33+ self .env ["ir.model.data" ].create (
34+ {
35+ "name" : "user_demo" ,
36+ "module" : "base" ,
37+ "model" : "res.users" ,
38+ "res_id" : demo_user .id ,
39+ "noupdate" : True ,
40+ }
41+ )
42+ module_name = "auth_jwt_demo"
43+ xml_path_in_module = "demo/auth_jwt_validator.xml"
44+ try :
45+ full_path = file_path (f"{ module_name } /{ xml_path_in_module } " )
46+ except FileNotFoundError :
47+ self .fail (f"No se pudo encontrar el fichero demo: { xml_path_in_module } " )
48+ full_path = None
49+ if full_path :
50+ with open (full_path , "rb" ) as f :
51+ convert .convert_xml_import (
52+ self .env , module_name , f , idref = {}, mode = "demo" , noupdate = False
53+ )
54+
1455 def test_auth_method_exists (self ):
1556 validator = self .env ["auth.jwt.validator" ].search ([("name" , "=" , "demo" )])
1657 self .assertEqual (len (validator ), 1 )
@@ -19,6 +60,40 @@ def test_auth_method_exists(self):
1960
2061@tests .tagged ("post_install" , "-at_install" )
2162class TestEndToEnd (tests .HttpCase ):
63+ def setUp (self ):
64+ super ().setUp ()
65+ demo_user = self .env .ref ("base.user_demo" , raise_if_not_found = False )
66+ if not demo_user :
67+ demo_user = self .env ["res.users" ].create (
68+ {
69+ "name" : "Demo User" ,
70+ "login" : "demo" ,
71+ "email" : "demo@example.com" ,
72+ "group_ids" : [(6 , 0 , [self .env .ref ("base.group_user" ).id ])],
73+ }
74+ )
75+ self .env ["ir.model.data" ].create (
76+ {
77+ "name" : "user_demo" ,
78+ "module" : "base" ,
79+ "model" : "res.users" ,
80+ "res_id" : demo_user .id ,
81+ "noupdate" : True ,
82+ }
83+ )
84+ module_name = "auth_jwt_demo"
85+ xml_path_in_module = "demo/auth_jwt_validator.xml"
86+ try :
87+ full_path = file_path (f"{ module_name } /{ xml_path_in_module } " )
88+ except FileNotFoundError :
89+ self .fail (f"No se pudo encontrar el fichero demo: { xml_path_in_module } " )
90+ full_path = None
91+ if full_path :
92+ with open (full_path , "rb" ) as f :
93+ convert .convert_xml_import (
94+ self .env , module_name , f , idref = {}, mode = "demo" , noupdate = False
95+ )
96+
2297 def _get_token (self , aud = None , email = None ):
2398 validator = self .env ["auth.jwt.validator" ].search ([("name" , "=" , "demo" )])
2499 payload = {
@@ -74,7 +149,7 @@ def test_whoami_cookie(self):
74149 self .assertTrue (cookie )
75150 # Try again with the cookie.
76151 resp = self .url_open (
77- "/auth_jwt_demo_cookie/whoami" , headers = {"Cookie " : f"demo_auth= { cookie } " }
152+ "/auth_jwt_demo_cookie/whoami" , cookies = {"demo_auth " : cookie }
78153 )
79154 resp .raise_for_status ()
80155 whoami = resp .json ()
@@ -159,7 +234,7 @@ def test_public_cookie_mode(self):
159234 token = self ._get_token (email = partner .email )
160235 resp = self .url_open (
161236 "/auth_jwt_demo_cookie/whoami-public-or-jwt" ,
162- headers = {"Cookie " : f"demo_auth= { cookie } " },
237+ cookies = {"demo_auth " : cookie },
163238 )
164239 resp .raise_for_status ()
165240 whoami = resp .json ()
0 commit comments