Skip to content

Commit 8a6f258

Browse files
committed
fix: ensure model instances are created within the Canaille application context
1 parent 3eae2ee commit 8a6f258

File tree

2 files changed

+49
-35
lines changed

2 files changed

+49
-35
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
Versions follow [Semantic Versioning](https://semver.org/>) (<major>.<minor>.<patch>).
99

10+
## [0.0.4]
11+
12+
### Fixed
13+
14+
- Ensure model instances are created within the Canaille application context.
15+
1016
## [0.0.3] - 2023-08-31
1117

1218
### Added

pytest_iam/__init__.py

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -53,41 +53,47 @@ def random_user(self, **kwargs):
5353
Generates a :class:`~canaille.core.models.User` with random values.
5454
Any parameter will be used instead of a random value.
5555
"""
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+
5961
return user
6062

6163
def random_group(self, **kwargs):
6264
"""
6365
Generates a :class:`~canaille.core.models.Group` with random values.
6466
Any parameter will be used instead of a random value.
6567
"""
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+
6973
return group
7074

7175
def random_token(self, subject, client, **kwargs):
7276
"""
7377
Generates a test :class:`~canaille.oidc.basemodels.Token` with random values.
7478
Any parameter will be used instead of a random value.
7579
"""
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+
9197
return token
9298

9399
def login(self, user):
@@ -103,21 +109,23 @@ def consent(self, user, client=None):
103109
104110
:param client: If :const:`None`, all existing clients are consented.
105111
"""
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-
]
118112

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()
121129

122130
if len(consents) > 1:
123131
return consents

0 commit comments

Comments
 (0)