Skip to content

Commit c39d65b

Browse files
committed
Extract Test acces token generation to a helper function
1 parent 890478a commit c39d65b

File tree

1 file changed

+30
-42
lines changed

1 file changed

+30
-42
lines changed

tests/conftest.py

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,19 @@ def rp_settings(oauth2_settings):
154154
return oauth2_settings
155155

156156

157-
@pytest.fixture
158-
def oidc_tokens(oauth2_settings, application, test_user, client):
159-
oauth2_settings.update(presets.OIDC_SETTINGS_RW)
157+
def generate_access_token(oauth2_settings, application, test_user, client, settings, scope, redirect_uri):
158+
"""
159+
A helper function that generates an access_token and ID Token for a given Application and User.
160+
"""
161+
oauth2_settings.update(settings)
160162
client.force_login(test_user)
161163
auth_rsp = client.post(
162164
reverse("oauth2_provider:authorize"),
163165
data={
164166
"client_id": application.client_id,
165167
"state": "random_state_string",
166-
"scope": "openid",
167-
"redirect_uri": "http://example.org",
168+
"scope": scope,
169+
"redirect_uri": redirect_uri,
168170
"response_type": "code",
169171
"allow": True,
170172
},
@@ -177,10 +179,10 @@ def oidc_tokens(oauth2_settings, application, test_user, client):
177179
data={
178180
"grant_type": "authorization_code",
179181
"code": code,
180-
"redirect_uri": "http://example.org",
182+
"redirect_uri": redirect_uri,
181183
"client_id": application.client_id,
182184
"client_secret": CLEARTEXT_SECRET,
183-
"scope": "openid",
185+
"scope": scope,
184186
},
185187
)
186188
assert token_rsp.status_code == 200
@@ -195,40 +197,26 @@ def oidc_tokens(oauth2_settings, application, test_user, client):
195197

196198

197199
@pytest.fixture
198-
def oidc_email_scope_tokens(oauth2_settings, application, test_user, client):
199-
oauth2_settings.update(presets.OIDC_SETTINGS_EMAIL_SCOPE)
200-
client.force_login(test_user)
201-
auth_rsp = client.post(
202-
reverse("oauth2_provider:authorize"),
203-
data={
204-
"client_id": application.client_id,
205-
"state": "random_state_string",
206-
"scope": "openid email",
207-
"redirect_uri": "http://example.org",
208-
"response_type": "code",
209-
"allow": True,
210-
},
211-
)
212-
assert auth_rsp.status_code == 302
213-
code = parse_qs(urlparse(auth_rsp["Location"]).query)["code"]
214-
client.logout()
215-
token_rsp = client.post(
216-
reverse("oauth2_provider:token"),
217-
data={
218-
"grant_type": "authorization_code",
219-
"code": code,
220-
"redirect_uri": "http://example.org",
221-
"client_id": application.client_id,
222-
"client_secret": CLEARTEXT_SECRET,
223-
"scope": "openid email",
224-
},
200+
def oidc_tokens(oauth2_settings, application, test_user, client):
201+
return generate_access_token(
202+
oauth2_settings,
203+
application,
204+
test_user,
205+
client,
206+
presets.OIDC_SETTINGS_RW,
207+
"openid",
208+
"http://example.org",
225209
)
226-
assert token_rsp.status_code == 200
227-
token_data = token_rsp.json()
228-
return SimpleNamespace(
229-
user=test_user,
230-
application=application,
231-
access_token=token_data["access_token"],
232-
id_token=token_data["id_token"],
233-
oauth2_settings=oauth2_settings,
210+
211+
212+
@pytest.fixture
213+
def oidc_email_scope_tokens(oauth2_settings, application, test_user, client):
214+
return generate_access_token(
215+
oauth2_settings,
216+
application,
217+
test_user,
218+
client,
219+
presets.OIDC_SETTINGS_EMAIL_SCOPE,
220+
"openid email",
221+
"http://example.org",
234222
)

0 commit comments

Comments
 (0)