1
+ from django .contrib .sessions .backends .cache import SessionStore as CachedSessionStore
2
+ from django .contrib .sessions .backends .db import SessionStore as DBSessionStore
1
3
from django .contrib .sessions .models import Session
4
+ from django .test import override_settings
2
5
from django .urls import reverse
3
6
4
7
import pytest
5
- from pytest_django .fixtures import admin_user
6
8
from sessionprofile .models import SessionProfile
7
9
10
+ from open_api_framework .utils import get_session_store
11
+
8
12
from .factories import SessionProfileFactory
9
13
10
14
@@ -25,9 +29,6 @@ def test_session_profile_sanity(client, admin_user, session_changelist_url):
25
29
assert client .session .session_key == session .session_key
26
30
27
31
28
- admin_user2 = admin_user
29
-
30
-
31
32
def test_only_session_profile_of_user_shown (
32
33
client , admin_user , django_user_model , session_changelist_url
33
34
):
@@ -54,18 +55,52 @@ def test_only_session_profile_of_user_shown(
54
55
other_user_session = SessionProfile .objects .get (user = other_admin )
55
56
assert other_user_session .session_key not in response .content .decode ()
56
57
58
+ # should only be able to access own page
59
+ change_url = reverse (
60
+ "admin:sessionprofile_sessionprofile_change" ,
61
+ args = [admin_user_session .session_key ],
62
+ )
63
+ response = client .get (change_url )
64
+ assert response .status_code == 200
57
65
58
- def test_delete_with_session_db_backend (
59
- client , admin_user , session_changelist_url , db_session_store
60
- ):
66
+ change_url = reverse (
67
+ "admin:sessionprofile_sessionprofile_change" ,
68
+ args = [other_user_session .session_key ],
69
+ )
70
+ response = client .get (change_url )
71
+ assert response .status_code == 302
72
+ assert response .url == reverse ("admin:index" )
73
+
74
+
75
+ def test_cant_delete_other_users_session (client , admin_user , django_user_model ):
76
+ client .force_login (admin_user )
77
+
78
+ other_admin = django_user_model .objects .create_superuser ("garry" )
79
+
80
+ other_user_session = SessionProfileFactory (user = other_admin )
81
+
82
+ delete_url = reverse (
83
+ "admin:sessionprofile_sessionprofile_delete" ,
84
+ args = [other_user_session .session_key ],
85
+ )
86
+
87
+ response = client .post (delete_url , {"post" : "yes" })
88
+ assert response .status_code == 302
89
+
90
+ SessionStore = get_session_store ()
91
+
92
+ assert SessionStore ().exists (other_user_session .session_key )
93
+
94
+
95
+ def test_delete_with_session_db_backend (client , admin_user , session_changelist_url ):
61
96
client .force_login (admin_user )
62
97
63
98
session = SessionProfileFactory (user = admin_user )
64
99
65
100
assert SessionProfile .objects .count () == 1
66
101
# sesison created by login
67
102
assert Session .objects .count () == 2
68
- assert db_session_store ().exists (session .session_key )
103
+ assert DBSessionStore ().exists (session .session_key )
69
104
70
105
url = reverse ("admin:sessionprofile_sessionprofile_delete" , args = [session .pk ])
71
106
@@ -76,19 +111,18 @@ def test_delete_with_session_db_backend(
76
111
assert SessionProfile .objects .count () == 1
77
112
assert SessionProfile .objects .count () != session
78
113
assert Session .objects .count () == 1
79
- assert not db_session_store ().exists (session .session_key )
114
+ assert not DBSessionStore ().exists (session .session_key )
80
115
81
116
82
- def test_delete_with_session_cache_backend (
83
- client , admin_user , session_changelist_url , cache_session_store
84
- ):
117
+ @override_settings (SESSION_ENGINE = "django.contrib.sessions.backends.cache" )
118
+ def test_delete_with_session_cache_backend (client , admin_user , session_changelist_url ):
85
119
client .force_login (admin_user )
86
120
87
121
session = SessionProfileFactory (user = admin_user )
88
122
89
123
assert SessionProfile .objects .count () == 1
90
124
assert Session .objects .count () == 0
91
- assert cache_session_store ().exists (session .session_key )
125
+ assert CachedSessionStore ().exists (session .session_key )
92
126
93
127
url = reverse ("admin:sessionprofile_sessionprofile_delete" , args = [session .pk ])
94
128
@@ -99,11 +133,11 @@ def test_delete_with_session_cache_backend(
99
133
assert SessionProfile .objects .count () == 1
100
134
assert SessionProfile .objects .count () != session
101
135
assert Session .objects .count () == 0
102
- assert not cache_session_store ().exists (session .session_key )
136
+ assert not CachedSessionStore ().exists (session .session_key )
103
137
104
138
105
139
def test_delete_action_with_session_db_backend (
106
- client , admin_user , session_changelist_url , db_session_store
140
+ client , admin_user , session_changelist_url
107
141
):
108
142
client .force_login (admin_user )
109
143
sessions = SessionProfileFactory .create_batch (5 , user = admin_user )
@@ -114,7 +148,7 @@ def test_delete_action_with_session_db_backend(
114
148
115
149
session_keys = [session .session_key for session in sessions ]
116
150
for session_key in session_keys :
117
- assert db_session_store ().exists (session_key )
151
+ assert DBSessionStore ().exists (session_key )
118
152
119
153
response = client .post (
120
154
session_changelist_url ,
@@ -127,11 +161,12 @@ def test_delete_action_with_session_db_backend(
127
161
assert Session .objects .count () == 1
128
162
129
163
for session_key in session_keys :
130
- assert not db_session_store ().exists (session_key )
164
+ assert not DBSessionStore ().exists (session_key )
131
165
132
166
167
+ @override_settings (SESSION_ENGINE = "django.contrib.sessions.backends.cache" )
133
168
def test_delete_action_with_session_cache_backend (
134
- client , admin_user , session_changelist_url , cache_session_store
169
+ client , admin_user , session_changelist_url
135
170
):
136
171
137
172
client .force_login (admin_user )
@@ -145,7 +180,7 @@ def test_delete_action_with_session_cache_backend(
145
180
146
181
# sessions are created
147
182
for session_key in session_keys :
148
- assert cache_session_store ().exists (session_key )
183
+ assert CachedSessionStore ().exists (session_key )
149
184
150
185
response = client .post (
151
186
session_changelist_url ,
@@ -159,4 +194,4 @@ def test_delete_action_with_session_cache_backend(
159
194
160
195
# sessions should be deleted
161
196
for session_key in session_keys :
162
- assert not cache_session_store ().exists (session_key )
197
+ assert not CachedSessionStore ().exists (session_key )
0 commit comments