Skip to content

Commit 8cd926c

Browse files
authored
Merge pull request #433 from MongoEngine/sessions-tests
Extended sessions.py tests
2 parents 28e8568 + 8f59018 commit 8cd926c

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

requirements-dev.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ black
55
pre-commit
66
pytest
77
pytest-cov
8+
pytest-mock
9+
tox

tests/test_session.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
from flask import session
3+
from pytest_mock import MockerFixture
34

45
from flask_mongoengine import MongoEngineSessionInterface
56

@@ -18,23 +19,60 @@ def index():
1819
def check_session():
1920
return "session: %s" % session["a"]
2021

21-
@app.route("/check-session-database")
22-
def check_session_database():
23-
sessions = app.session_interface.cls.objects.count()
24-
return "sessions: %s" % sessions
2522

23+
@pytest.fixture
24+
def permanent_session_app(app):
25+
@app.before_request
26+
def make_session_permanent():
27+
session.permanent = True
2628

27-
def test_setting_session(app):
29+
return app
30+
31+
32+
def test__save_session__called_on_session_set(app, mocker: MockerFixture):
33+
save_spy = mocker.spy(MongoEngineSessionInterface, "save_session")
34+
expiration_spy = mocker.spy(MongoEngineSessionInterface, "get_expiration_time")
2835
client = app.test_client()
2936

3037
response = client.get("/")
38+
call_args, _ = expiration_spy.call_args_list[0]
39+
3140
assert response.status_code == 200
3241
assert response.data.decode("utf-8") == "hello session"
42+
assert app.session_interface.cls.objects.count() == 1
43+
save_spy.assert_called_once()
44+
assert call_args[2].permanent is False # session object
45+
46+
47+
def test__save_session__called_on_session_set__should_respect_permanent_session_setting(
48+
permanent_session_app, mocker: MockerFixture
49+
):
50+
expiration_spy = mocker.spy(MongoEngineSessionInterface, "get_expiration_time")
51+
client = permanent_session_app.test_client()
52+
client.get("/")
53+
54+
call_args, _ = expiration_spy.call_args_list[0]
55+
assert call_args[2].permanent is True # session object
56+
57+
58+
def test__open_session_called_on_session_get(app, mocker: MockerFixture):
59+
client = app.test_client()
60+
open_spy = mocker.spy(MongoEngineSessionInterface, "open_session")
61+
client.get("/")
62+
open_spy.assert_called_once() # On init call with no session
3363

3464
response = client.get("/check-session")
65+
3566
assert response.status_code == 200
3667
assert response.data.decode("utf-8") == "session: hello session"
68+
assert open_spy.call_count == 2 # On init + get with sid
3769

38-
response = client.get("/check-session-database")
39-
assert response.status_code == 200
40-
assert response.data.decode("utf-8") == "sessions: 1"
70+
71+
@pytest.mark.parametrize("unsupported_value", (1, None, True, False, [], {}))
72+
def test_session_interface__should_raise_value_error_if_collection_name_not_string(
73+
db, unsupported_value
74+
):
75+
with pytest.raises(ValueError) as error:
76+
MongoEngineSessionInterface(db, collection=unsupported_value)
77+
78+
assert str(error.value) == "Collection argument should be string"

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ deps =
88
PyMongo>3.9.0
99
pytest
1010
pytest-cov
11+
pytest-mock
1112

1213
[testenv:lint]
1314
deps =

0 commit comments

Comments
 (0)