@@ -53,41 +53,47 @@ def random_user(self, **kwargs):
53
53
Generates a :class:`~canaille.core.models.User` with random values.
54
54
Any parameter will be used instead of a random value.
55
55
"""
56
- user = fake_users ()[0 ]
57
- user .update (** kwargs )
58
- user .save ()
56
+ with self .app .app_context ():
57
+ user = fake_users ()[0 ]
58
+ user .update (** kwargs )
59
+ user .save ()
60
+
59
61
return user
60
62
61
63
def random_group (self , ** kwargs ):
62
64
"""
63
65
Generates a :class:`~canaille.core.models.Group` with random values.
64
66
Any parameter will be used instead of a random value.
65
67
"""
66
- group = fake_groups (nb_users_max = 0 )[0 ]
67
- group .update (** kwargs )
68
- group .save ()
68
+ with self .app .app_context ():
69
+ group = fake_groups (nb_users_max = 0 )[0 ]
70
+ group .update (** kwargs )
71
+ group .save ()
72
+
69
73
return group
70
74
71
75
def random_token (self , subject , client , ** kwargs ):
72
76
"""
73
77
Generates a test :class:`~canaille.oidc.basemodels.Token` with random values.
74
78
Any parameter will be used instead of a random value.
75
79
"""
76
- token = self .models .Token (
77
- id = str (uuid .uuid4 ()),
78
- token_id = str (uuid .uuid4 ()),
79
- access_token = str (uuid .uuid4 ()),
80
- client = client ,
81
- subject = subject ,
82
- type = "access_token" ,
83
- refresh_token = str (uuid .uuid4 ()),
84
- scope = client .scope ,
85
- issue_date = datetime .datetime .now (datetime .timezone .utc ),
86
- lifetime = 3600 ,
87
- audience = [client ],
88
- )
89
- token .update (** kwargs )
90
- token .save ()
80
+ with self .app .app_context ():
81
+ token = self .models .Token (
82
+ id = str (uuid .uuid4 ()),
83
+ token_id = str (uuid .uuid4 ()),
84
+ access_token = str (uuid .uuid4 ()),
85
+ client = client ,
86
+ subject = subject ,
87
+ type = "access_token" ,
88
+ refresh_token = str (uuid .uuid4 ()),
89
+ scope = client .scope ,
90
+ issue_date = datetime .datetime .now (datetime .timezone .utc ),
91
+ lifetime = 3600 ,
92
+ audience = [client ],
93
+ )
94
+ token .update (** kwargs )
95
+ token .save ()
96
+
91
97
return token
92
98
93
99
def login (self , user ):
@@ -103,21 +109,23 @@ def consent(self, user, client=None):
103
109
104
110
:param client: If :const:`None`, all existing clients are consented.
105
111
"""
106
- clients = [client ] if client else models .Client .query ()
107
-
108
- consents = [
109
- self .models .Consent (
110
- consent_id = str (uuid .uuid4 ()),
111
- client = client ,
112
- subject = user ,
113
- scope = client .scope ,
114
- issue_date = datetime .datetime .now (datetime .timezone .utc ),
115
- )
116
- for client in clients
117
- ]
118
112
119
- for consent in consents :
120
- consent .save ()
113
+ with self .app .app_context ():
114
+ clients = [client ] if client else models .Client .query ()
115
+
116
+ consents = [
117
+ self .models .Consent (
118
+ consent_id = str (uuid .uuid4 ()),
119
+ client = client ,
120
+ subject = user ,
121
+ scope = client .scope ,
122
+ issue_date = datetime .datetime .now (datetime .timezone .utc ),
123
+ )
124
+ for client in clients
125
+ ]
126
+
127
+ for consent in consents :
128
+ consent .save ()
121
129
122
130
if len (consents ) > 1 :
123
131
return consents
0 commit comments