1
1
import pytest
2
- from tenantfirstaid .session import TenantSession
2
+ from flask import Flask
3
+ from tenantfirstaid .session import TenantSession , InitSessionView
4
+ from typing import Dict , Any
3
5
4
6
5
7
@pytest .fixture
6
- def mock_valkey (mocker ):
7
- """Mock the Valkey class with the db_con.ping(), db_con.get(), and db_con.set() methods."""
8
- mock_client = mocker .Mock ()
9
- mocker .patch ("tenantfirstaid.session.Valkey" , return_value = mock_client )
8
+ def mock_valkey_ping_nop (mocker ):
9
+ """Mock the Valkey class with the db_con.ping() method."""
10
+ mock_valkey_client = mocker .Mock ()
11
+ mocker .patch ("tenantfirstaid.session.Valkey" , return_value = mock_valkey_client )
12
+ mock_valkey_client .ping = mocker .Mock ()
13
+ return mock_valkey_client
14
+
10
15
11
- _data = {}
16
+ @pytest .fixture
17
+ def mock_valkey (mock_valkey_ping_nop , mocker ):
18
+ # mock_valkey_dbcon_client = mocker.Mock()
19
+ # mocker.patch("tenantfirstaid.session.Valkey", mock_valkey_dbcon_client)
20
+
21
+ _data : Dict [str , Any ] = {}
12
22
13
- mock_client .set = mocker .Mock (
23
+ mock_valkey_ping_nop .set = mocker .Mock (
14
24
side_effect = lambda key , value : _data .update ({key : value })
15
25
)
16
- mock_client .get = mocker .Mock (side_effect = lambda key : _data .get (key , None ))
17
- mock_client .ping = mocker .Mock ()
18
- return mock_client
26
+
27
+ mock_valkey_ping_nop .get = mocker .Mock (
28
+ return_value = lambda key : _data .get (key , None )
29
+ )
30
+
31
+ return mock_valkey_ping_nop
19
32
20
33
21
34
@pytest .fixture
@@ -26,24 +39,37 @@ def mock_environ(monkeypatch):
26
39
monkeypatch .setenv ("DB_USE_SSL" , "false" )
27
40
28
41
29
- def test_session_set_and_get (mock_valkey , mock_environ ):
30
- tenant_session = TenantSession ()
31
-
32
- mock_valkey .get .return_value = '"test_value"'
33
- tenant_session .set ("some_session_id" , "test_value" )
34
- value = tenant_session .get ("some_session_id" )
35
- assert value == "test_value"
42
+ def test_session_init_success (mocker , mock_environ ):
43
+ test_data = {
44
+ "city" : "Test City" ,
45
+ "state" : "Test State" ,
46
+ }
36
47
48
+ mock_valkey_client = mocker .Mock ()
49
+ mocker .patch ("tenantfirstaid.session.Valkey" , return_value = mock_valkey_client )
50
+ mock_valkey_client .ping = mocker .Mock ()
37
51
38
- def test_session_get_unknown_session_id (mock_valkey , mock_environ ):
39
52
tenant_session = TenantSession ()
53
+ app = Flask (__name__ )
54
+ app .add_url_rule (
55
+ "/api/init" ,
56
+ view_func = InitSessionView .as_view ("init" , tenant_session ),
57
+ methods = ["POST" ],
58
+ )
59
+ app .secret_key = "test_secret_key" # Set a secret key for session management
40
60
41
- mock_valkey .get .return_value = None
42
- value = tenant_session .get ("some_session_id" )
43
- assert value == []
61
+ with app .test_request_context ("/api/init" , method = "POST" , json = test_data ) as reqctx :
62
+ assert (
63
+ reqctx .session .get ("session_id" ) is None
64
+ ) # Ensure session_id is NOT set in the request context (before dispatch)
65
+ response = app .full_dispatch_request ()
66
+ assert response .status_code == 200 # Ensure the response is successful
67
+ assert (
68
+ reqctx .session .get ("session_id" ) is not None
69
+ ) # Ensure session_id is set in the request context
44
70
45
71
46
- def test_session_init_ping_exception (mocker , mock_environ , capsys ):
72
+ def test_session_init_ping_exception (mocker , capsys ):
47
73
# Patch Valkey so that ping raises an exception
48
74
mock_client = mocker .Mock ()
49
75
mock_client .ping = mocker .Mock (side_effect = Exception ("Ping failed" ))
@@ -52,3 +78,62 @@ def test_session_init_ping_exception(mocker, mock_environ, capsys):
52
78
_obj = TenantSession ()
53
79
captured = capsys .readouterr ()
54
80
assert "Ping failed" in captured .out
81
+
82
+
83
+ def test_session_get_unknown_session_id (mocker , mock_environ ):
84
+ test_data = {
85
+ "city" : "Test City" ,
86
+ "state" : "Test State" ,
87
+ }
88
+
89
+ mock_valkey_client = mocker .Mock ()
90
+ mocker .patch ("tenantfirstaid.session.Valkey" , return_value = mock_valkey_client )
91
+ mock_valkey_client .ping = mocker .Mock ()
92
+
93
+ tenant_session = TenantSession ()
94
+ app = Flask (__name__ )
95
+ app .add_url_rule (
96
+ "/api/init" ,
97
+ view_func = InitSessionView .as_view ("init" , tenant_session ),
98
+ methods = ["POST" ],
99
+ )
100
+ app .secret_key = "test_secret_key" # Set a secret key for session management
101
+
102
+ with app .test_request_context ("/api/init" , method = "POST" , json = test_data ) as reqctx :
103
+ assert (
104
+ reqctx .session .get ("session_id" ) is None
105
+ ) # Ensure session_id is NOT set in the request context (before dispatch)
106
+ assert tenant_session .get () == {
107
+ "city" : "" ,
108
+ "state" : "" ,
109
+ "messages" : [],
110
+ }
111
+
112
+
113
+ # def test_session_set_and_get(mocker, mock_environ, mock_valkey):
114
+ # test_data: Dict[str, Any] = {
115
+ # "city": "Test City",
116
+ # "state": "Test State",
117
+ # "messages": ["this is message 1", "this is message 2"],
118
+ # }
119
+
120
+ # mock_valkey_client = mocker.Mock()
121
+ # mocker.patch("tenantfirstaid.session.Valkey", return_value=mock_valkey_client)
122
+ # mock_valkey_client.ping = mocker.Mock()
123
+
124
+ # tenant_session = TenantSession()
125
+ # app = Flask(__name__)
126
+ # app.add_url_rule(
127
+ # "/api/init",
128
+ # view_func=InitSessionView.as_view("init", tenant_session),
129
+ # methods=["POST"],
130
+ # )
131
+ # app.secret_key = "test_secret_key" # Set a secret key for session management
132
+
133
+ # with app.test_request_context("/api/init", method="POST", json=test_data):
134
+ # response = app.full_dispatch_request()
135
+ # assert response.status_code == 200 # Ensure the response is successful
136
+ # session_id = response.json["session_id"]
137
+
138
+ # tenant_session.set(session_id, test_data)
139
+ # assert tenant_session.get() == test_data
0 commit comments