Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ black
pre-commit
pytest
pytest-cov
pytest-mock
tox
54 changes: 46 additions & 8 deletions tests/test_session.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from flask import session
from pytest_mock import MockerFixture

from flask_mongoengine import MongoEngineSessionInterface

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

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

@pytest.fixture
def permanent_session_app(app):
@app.before_request
def make_session_permanent():
session.permanent = True

def test_setting_session(app):
return app


def test__save_session__called_on_session_set(app, mocker: MockerFixture):
save_spy = mocker.spy(MongoEngineSessionInterface, "save_session")
expiration_spy = mocker.spy(MongoEngineSessionInterface, "get_expiration_time")
client = app.test_client()

response = client.get("/")
call_args, _ = expiration_spy.call_args_list[0]

assert response.status_code == 200
assert response.data.decode("utf-8") == "hello session"
assert app.session_interface.cls.objects.count() == 1
save_spy.assert_called_once()
assert call_args[2].permanent is False # session object


def test__save_session__called_on_session_set__should_respect_permanent_session_setting(
permanent_session_app, mocker: MockerFixture
):
expiration_spy = mocker.spy(MongoEngineSessionInterface, "get_expiration_time")
client = permanent_session_app.test_client()
client.get("/")

call_args, _ = expiration_spy.call_args_list[0]
assert call_args[2].permanent is True # session object


def test__open_session_called_on_session_get(app, mocker: MockerFixture):
client = app.test_client()
open_spy = mocker.spy(MongoEngineSessionInterface, "open_session")
client.get("/")
open_spy.assert_called_once() # On init call with no session

response = client.get("/check-session")

assert response.status_code == 200
assert response.data.decode("utf-8") == "session: hello session"
assert open_spy.call_count == 2 # On init + get with sid

response = client.get("/check-session-database")
assert response.status_code == 200
assert response.data.decode("utf-8") == "sessions: 1"

@pytest.mark.parametrize("unsupported_value", (1, None, True, False, [], {}))
def test_session_interface__should_raise_value_error_if_collection_name_not_string(
db, unsupported_value
):
with pytest.raises(ValueError) as error:
MongoEngineSessionInterface(db, collection=unsupported_value)

assert str(error.value) == "Collection argument should be string"
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ deps =
PyMongo>3.9.0
pytest
pytest-cov
pytest-mock

[testenv:lint]
deps =
Expand Down