Skip to content

Commit c90e67c

Browse files
fix tests for werkzeug 3 (#426)
* fix tests for werkzeug 3 * Pin Pillow to 9.5 so docs can build --------- Co-authored-by: Ryan Schaffer <[email protected]>
1 parent 27add75 commit c90e67c

File tree

7 files changed

+52
-47
lines changed

7 files changed

+52
-47
lines changed

flask_dance/consumer/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ def token(self):
134134
# Update the `expires_in` value, so that requests-oauthlib
135135
# can handle automatic token refreshing. Assume that
136136
# `expires_at` is a valid Unix timestamp.
137-
expires_at = datetime.utcfromtimestamp(_token["expires_at"])
138-
expires_in = expires_at - datetime.utcnow()
137+
expires_at = datetime.fromtimestamp(_token["expires_at"], timezone.utc)
138+
expires_in = expires_at - datetime.now(timezone.utc)
139139
_token["expires_in"] = expires_in.total_seconds()
140140
return _token
141141

@@ -146,7 +146,7 @@ def token(self, value):
146146
# Set the `expires_at` value, overwriting any value
147147
# that may already be there.
148148
delta = timedelta(seconds=int(_token["expires_in"]))
149-
expires_at = datetime.utcnow() + delta
149+
expires_at = datetime.now(timezone.utc) + delta
150150
_token["expires_at"] = expires_at.replace(tzinfo=timezone.utc).timestamp()
151151
self.storage.set(self, _token)
152152
try:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ docs = [
6262
"sqlalchemy>=1.3.11",
6363
"pytest",
6464
"betamax",
65+
"pillow<=9.5"
6566
]
6667
sqla = ["sqlalchemy>=1.3.11"]
6768
signals = ["blinker"]

tests/consumer/storage/test_sqla.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def db():
6262
def app(blueprint, db, request):
6363
"Make a Flask app, attach Flask-SQLAlchemy, and establish an app context"
6464
app = flask.Flask(__name__)
65+
app.config["SERVER_NAME"] = "a.b.c"
6566
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("DATABASE_URI", "sqlite://")
6667
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
6768
app.config["CACHE_TYPE"] = "SimpleCache"

tests/consumer/test_oauth1.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def make_app(login_url=None):
3939
app = flask.Flask(__name__)
4040
app.secret_key = "secret"
4141
app.register_blueprint(blueprint, url_prefix="/login")
42+
app.config["SERVER_NAME"] = "a.b.c"
4243

4344
@app.route("/")
4445
def index():
@@ -149,11 +150,10 @@ def test_authorized_url():
149150
assert auth_header["oauth_token"] == "foobar"
150151
assert auth_header["oauth_verifier"] == "xyz"
151152
# check that we stored the access token and secret in the session
152-
with client.session_transaction() as sess:
153-
assert sess["test-service_oauth_token"] == {
154-
"oauth_token": "xxx",
155-
"oauth_token_secret": "yyy",
156-
}
153+
assert flask.session["test-service_oauth_token"] == {
154+
"oauth_token": "xxx",
155+
"oauth_token_secret": "yyy",
156+
}
157157

158158

159159
@responses.activate

tests/consumer/test_oauth2.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def make_app(login_url=None, debug=False, **kwargs):
4747
app.secret_key = "secret"
4848
app.register_blueprint(blueprint, url_prefix="/login")
4949
app.debug = debug
50+
app.config["SERVER_NAME"] = "a.b.c"
5051

5152
@app.route("/")
5253
def index():
@@ -77,8 +78,7 @@ def test_login_url():
7778
"/login/test-service", base_url="https://a.b.c", follow_redirects=False
7879
)
7980
# check that we saved the state in the session
80-
with client.session_transaction() as sess:
81-
assert sess["test-service_oauth_state"] == "random-string"
81+
assert flask.session["test-service_oauth_state"] == "random-string"
8282
# check that we redirected the client
8383
assert resp.status_code == 302
8484
location = URLObject(resp.headers["Location"])
@@ -94,28 +94,27 @@ def test_login_url():
9494

9595
@responses.activate
9696
def test_login_url_with_pkce():
97-
_code_challenge_method = "S256" # That should be a default value
97+
code_challenge_method = "S256" # That should be a default value
9898
app, _ = make_app(use_pkce=True)
9999
with app.test_client() as client:
100100
resp = client.get(
101101
"/login/test-service", base_url="https://a.b.c", follow_redirects=False
102102
)
103103
# check that we saved the code verifier in the session
104-
with client.session_transaction() as sess:
105-
assert "test-service_oauth_code_verifier" in sess
106-
_code_verifier = sess["test-service_oauth_code_verifier"]
107-
assert 43 <= len(_code_verifier) <= 128 # RFC7636 section 4.1
108-
_code_challenge = OAuth2Client("123").create_code_challenge(
109-
_code_verifier, _code_challenge_method
110-
)
104+
assert "test-service_oauth_code_verifier" in flask.session
105+
code_verifier = flask.session["test-service_oauth_code_verifier"]
106+
assert 43 <= len(code_verifier) <= 128 # RFC7636 section 4.1
107+
code_challenge = OAuth2Client("123").create_code_challenge(
108+
code_verifier, code_challenge_method
109+
)
111110

112111
# check that we redirected the client
113112
assert resp.status_code == 302
114113
location = URLObject(resp.headers["Location"])
115114
assert location.without_query() == "https://example.com/oauth/authorize"
116115
# check PKCE specific query parameters
117-
assert location.query_dict["code_challenge_method"] == _code_challenge_method
118-
assert location.query_dict["code_challenge"] == _code_challenge
116+
assert location.query_dict["code_challenge_method"] == code_challenge_method
117+
assert location.query_dict["code_challenge"] == code_challenge
119118

120119

121120
@responses.activate
@@ -126,8 +125,7 @@ def test_login_url_with_invalid_code_challenge_method():
126125
"/login/test-service", base_url="https://a.b.c", follow_redirects=False
127126
)
128127
# the code verifier is saved in the session ...
129-
with client.session_transaction() as sess:
130-
assert "test-service_oauth_code_verifier" in sess
128+
assert "test-service_oauth_code_verifier" in flask.session
131129

132130
location = URLObject(resp.headers["Location"])
133131
assert location.without_query() == "https://example.com/oauth/authorize"
@@ -428,6 +426,7 @@ def test_redirect_url():
428426
)
429427
app = flask.Flask(__name__)
430428
app.secret_key = "secret"
429+
app.config["SERVER_NAME"] = "a.b.c"
431430
app.register_blueprint(blueprint, url_prefix="/login")
432431

433432
with app.test_client() as client:
@@ -464,6 +463,7 @@ def test_redirect_to():
464463
)
465464
app = flask.Flask(__name__)
466465
app.secret_key = "secret"
466+
app.config["SERVER_NAME"] = "a.b.c"
467467
app.register_blueprint(blueprint, url_prefix="/login")
468468

469469
@app.route("/blargl")
@@ -503,6 +503,7 @@ def test_redirect_fallback():
503503
)
504504
app = flask.Flask(__name__)
505505
app.secret_key = "secret"
506+
app.config["SERVER_NAME"] = "a.b.c"
506507
app.register_blueprint(blueprint, url_prefix="/login")
507508

508509
@app.route("/blargl")

tests/contrib/test_gitlab.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def make_app():
1414

1515
def _make_app(*args, **kwargs):
1616
app = Flask(__name__)
17+
app.config["SERVER_NAME"] = "a.b.c"
1718
app.secret_key = "whatever"
1819
blueprint = make_gitlab_blueprint(*args, **kwargs)
1920
app.register_blueprint(blueprint)

tests/contrib/test_slack.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
from urllib.parse import parse_qs
2+
13
import pytest
24
import requests_oauthlib
35
import responses
46
from flask import Flask
57
from urlobject import URLObject
6-
from werkzeug.urls import url_decode
78

89
from flask_dance.consumer import OAuth2ConsumerBlueprint
910
from flask_dance.consumer.storage import MemoryStorage
@@ -117,12 +118,12 @@ def test_auto_token_get(make_app):
117118
"chat.postMessage",
118119
data={"channel": "#general", "text": "ping", "icon_emoji": ":robot_face:"},
119120
)
120-
request_data = url_decode(resp.request.body)
121-
assert request_data["channel"] == "#general"
122-
assert request_data["text"] == "ping"
123-
assert request_data["icon_emoji"] == ":robot_face:"
121+
request_data = parse_qs(resp.request.body)
122+
assert request_data["channel"] == ["#general"]
123+
assert request_data["text"] == ["ping"]
124+
assert request_data["icon_emoji"] == [":robot_face:"]
124125
# the `token` parameter should have been automatically added
125-
assert request_data["token"] == "abcde"
126+
assert request_data["token"] == ["abcde"]
126127

127128

128129
@responses.activate
@@ -141,12 +142,12 @@ def test_auto_token_post(make_app):
141142
"chat.postMessage",
142143
data={"channel": "#general", "text": "ping", "icon_emoji": ":robot_face:"},
143144
)
144-
request_data = url_decode(resp.request.body)
145-
assert request_data["channel"] == "#general"
146-
assert request_data["text"] == "ping"
147-
assert request_data["icon_emoji"] == ":robot_face:"
145+
request_data = parse_qs(resp.request.body)
146+
assert request_data["channel"] == ["#general"]
147+
assert request_data["text"] == ["ping"]
148+
assert request_data["icon_emoji"] == [":robot_face:"]
148149
# the `token` parameter should have been automatically added
149-
assert request_data["token"] == "abcde"
150+
assert request_data["token"] == ["abcde"]
150151

151152

152153
@responses.activate
@@ -161,10 +162,10 @@ def test_auto_token_post_no_token(make_app):
161162
"chat.postMessage",
162163
data={"channel": "#general", "text": "ping", "icon_emoji": ":robot_face:"},
163164
)
164-
request_data = url_decode(resp.request.body)
165-
assert request_data["channel"] == "#general"
166-
assert request_data["text"] == "ping"
167-
assert request_data["icon_emoji"] == ":robot_face:"
165+
request_data = parse_qs(resp.request.body)
166+
assert request_data["channel"] == ["#general"]
167+
assert request_data["text"] == ["ping"]
168+
assert request_data["icon_emoji"] == [":robot_face:"]
168169
assert "token" not in request_data
169170
url = URLObject(resp.request.url)
170171
assert "token" not in url.query_dict
@@ -191,11 +192,11 @@ def test_override_token_get(make_app):
191192
"icon_emoji": ":robot_face:",
192193
},
193194
)
194-
request_data = url_decode(resp.request.body)
195-
assert request_data["token"] == "xyz"
196-
assert request_data["channel"] == "#general"
197-
assert request_data["text"] == "ping"
198-
assert request_data["icon_emoji"] == ":robot_face:"
195+
request_data = parse_qs(resp.request.body)
196+
assert request_data["token"] == ["xyz"]
197+
assert request_data["channel"] == ["#general"]
198+
assert request_data["text"] == ["ping"]
199+
assert request_data["icon_emoji"] == [":robot_face:"]
199200
# should not be present in URL
200201
url = URLObject(resp.request.url)
201202
assert "token" not in url.query_dict
@@ -222,11 +223,11 @@ def test_override_token_post(make_app):
222223
"icon_emoji": ":robot_face:",
223224
},
224225
)
225-
request_data = url_decode(resp.request.body)
226-
assert request_data["token"] == "xyz"
227-
assert request_data["channel"] == "#general"
228-
assert request_data["text"] == "ping"
229-
assert request_data["icon_emoji"] == ":robot_face:"
226+
request_data = parse_qs(resp.request.body)
227+
assert request_data["token"] == ["xyz"]
228+
assert request_data["channel"] == ["#general"]
229+
assert request_data["text"] == ["ping"]
230+
assert request_data["icon_emoji"] == [":robot_face:"]
230231
# should not be present
231232
url = URLObject(resp.request.url)
232233
assert "token" not in url.query_dict

0 commit comments

Comments
 (0)