Skip to content

Commit 40daedb

Browse files
committed
tests: use 'handle_error_response' for error management in unit tests
follow-up to #39
1 parent 0c46fcc commit 40daedb

6 files changed

+28
-26
lines changed

docs/django/2/authorization-server.rst

+7-4
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,13 @@ The ``AuthorizationServer`` has provided built-in methods to handle these endpoi
152152

153153
def authorize(request):
154154
if request.method == 'GET':
155-
grant = server.get_consent_grant(request, end_user=request.user)
156-
client = grant.client
157-
scope = client.get_allowed_scope(grant.request.scope)
158-
context = dict(grant=grant, client=client, scope=scope, user=request.user)
155+
try:
156+
grant = server.get_consent_grant(request, end_user=request.user)
157+
except OAuth2Error as error:
158+
return server.handle_error_response(request, error)
159+
160+
scope = grant.client.get_allowed_scope(grant.request.scope)
161+
context = dict(grant=grant, client=grant.client, scope=scope, user=request.user)
159162
return render(request, 'authorize.html', context)
160163

161164
if is_user_confirmed(request):

tests/flask/test_oauth2/oauth2_server.py

+9-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from authlib.common.encoding import to_bytes
99
from authlib.common.encoding import to_unicode
1010
from authlib.common.security import generate_token
11-
from authlib.common.urls import url_encode
1211
from authlib.integrations.flask_oauth2 import AuthorizationServer
1312
from authlib.integrations.sqla_oauth2 import create_query_client_func
1413
from authlib.integrations.sqla_oauth2 import create_save_token_func
@@ -39,23 +38,20 @@ def create_authorization_server(app, lazy=False):
3938

4039
@app.route("/oauth/authorize", methods=["GET", "POST"])
4140
def authorize():
41+
user_id = request.values.get("user_id")
42+
if user_id:
43+
end_user = db.session.get(User, int(user_id))
44+
else:
45+
end_user = None
46+
4247
if request.method == "GET":
43-
user_id = request.args.get("user_id")
44-
if user_id:
45-
end_user = db.session.get(User, int(user_id))
46-
else:
47-
end_user = None
4848
try:
4949
grant = server.get_consent_grant(end_user=end_user)
5050
return grant.prompt or "ok"
5151
except OAuth2Error as error:
52-
return url_encode(error.get_body())
53-
user_id = request.form.get("user_id")
54-
if user_id:
55-
grant_user = db.session.get(User, int(user_id))
56-
else:
57-
grant_user = None
58-
return server.create_authorization_response(grant_user=grant_user)
52+
return server.handle_error_response(request, error)
53+
54+
return server.create_authorization_response(grant_user=end_user)
5955

6056
@app.route("/oauth/token", methods=["GET", "POST"])
6157
def issue_token():

tests/flask/test_oauth2/test_authorization_code_grant.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def test_invalid_authorize(self):
9494
def test_unauthorized_client(self):
9595
self.prepare_data(True, "token")
9696
rv = self.client.get(self.authorize_url)
97-
self.assertIn(b"unauthorized_client", rv.data)
97+
self.assertIn("unauthorized_client", rv.location)
9898

9999
def test_invalid_client(self):
100100
self.prepare_data()
@@ -254,8 +254,11 @@ def test_invalid_multiple_request_parameters(self):
254254
+ "&scope=profile&state=bar&redirect_uri=https%3A%2F%2Fa.b&response_type=code"
255255
)
256256
rv = self.client.get(url)
257-
self.assertIn(b"invalid_request", rv.data)
258-
self.assertIn(b"Multiple+%27response_type%27+in+request.", rv.data)
257+
resp = json.loads(rv.data)
258+
self.assertEqual(resp["error"], "invalid_request")
259+
self.assertEqual(
260+
resp["error_description"], "Multiple 'response_type' in request."
261+
)
259262

260263
def test_client_secret_post(self):
261264
self.app.config.update({"OAUTH2_REFRESH_TOKEN_GENERATOR": True})

tests/flask/test_oauth2/test_code_challenge.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def prepare_data(self, token_endpoint_auth_method="none"):
6161
def test_missing_code_challenge(self):
6262
self.prepare_data()
6363
rv = self.client.get(self.authorize_url + "&code_challenge_method=plain")
64-
self.assertIn(b"Missing", rv.data)
64+
self.assertIn("Missing", rv.location)
6565

6666
def test_has_code_challenge(self):
6767
self.prepare_data()
@@ -76,13 +76,13 @@ def test_invalid_code_challenge(self):
7676
rv = self.client.get(
7777
self.authorize_url + "&code_challenge=abc&code_challenge_method=plain"
7878
)
79-
self.assertIn(b"Invalid", rv.data)
79+
self.assertIn("Invalid", rv.location)
8080

8181
def test_invalid_code_challenge_method(self):
8282
self.prepare_data()
8383
suffix = "&code_challenge=Zhs2POMonIVVHZteWfoU7cSXQSm0YjghikFGJSDI2_s&code_challenge_method=invalid"
8484
rv = self.client.get(self.authorize_url + suffix)
85-
self.assertIn(b"Unsupported", rv.data)
85+
self.assertIn("Unsupported", rv.location)
8686

8787
def test_supported_code_challenge_method(self):
8888
self.prepare_data()

tests/flask/test_oauth2/test_implicit_grant.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_confidential_client(self):
5656
def test_unsupported_client(self):
5757
self.prepare_data(response_type="code")
5858
rv = self.client.get(self.authorize_url)
59-
self.assertIn(b"unauthorized_client", rv.data)
59+
self.assertIn("unauthorized_client", rv.location)
6060

6161
def test_invalid_authorize(self):
6262
self.prepare_data()

tests/flask/test_oauth2/test_openid_implict_grant.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ def test_consent_view(self):
7373
},
7474
)
7575
)
76-
self.assertIn(b"error=invalid_request", rv.data)
77-
self.assertIn(b"nonce", rv.data)
76+
self.assertIn("error=invalid_request", rv.location)
77+
self.assertIn("nonce", rv.location)
7878

7979
def test_require_nonce(self):
8080
self.prepare_data()

0 commit comments

Comments
 (0)