-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_admin.py
More file actions
194 lines (138 loc) · 6.37 KB
/
test_admin.py
File metadata and controls
194 lines (138 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from django.contrib.sessions.backends.cache import SessionStore as CachedSessionStore
from django.contrib.sessions.backends.db import SessionStore as DBSessionStore
from django.contrib.sessions.models import Session
from django.test import override_settings
from django.urls import reverse
import pytest
from sessionprofile.models import SessionProfile
from open_api_framework.utils import get_session_store
from .factories import SessionProfileFactory
@pytest.fixture
def session_changelist_url():
return reverse("admin:sessionprofile_sessionprofile_changelist")
def test_session_profile_sanity(client, admin_user, session_changelist_url):
client.force_login(admin_user)
response = client.get(session_changelist_url)
assert response.status_code == 200
assert SessionProfile.objects.count() == 1
session = SessionProfile.objects.get()
assert client.session.session_key == session.session_key
def test_only_session_profile_of_user_shown(
client, admin_user, django_user_model, session_changelist_url
):
other_admin = django_user_model.objects.create_superuser("garry")
client.force_login(other_admin)
response = client.get(session_changelist_url)
assert response.status_code == 200
client.force_login(admin_user)
response = client.get(session_changelist_url)
assert response.status_code == 200
# two sessions, one for each user
assert SessionProfile.objects.count() == 2
# Session created after response, needs to be called again
response = client.get(session_changelist_url)
admin_user_session = SessionProfile.objects.get(user=admin_user)
assert admin_user_session.session_key in response.content.decode()
other_user_session = SessionProfile.objects.get(user=other_admin)
assert other_user_session.session_key not in response.content.decode()
# should only be able to access own page
change_url = reverse(
"admin:sessionprofile_sessionprofile_change",
args=[admin_user_session.session_key],
)
response = client.get(change_url)
assert response.status_code == 200
change_url = reverse(
"admin:sessionprofile_sessionprofile_change",
args=[other_user_session.session_key],
)
response = client.get(change_url)
assert response.status_code == 302
assert response.url == reverse("admin:index")
def test_cant_delete_other_users_session(client, admin_user, django_user_model):
client.force_login(admin_user)
other_admin = django_user_model.objects.create_superuser("garry")
other_user_session = SessionProfileFactory(user=other_admin)
delete_url = reverse(
"admin:sessionprofile_sessionprofile_delete",
args=[other_user_session.session_key],
)
response = client.post(delete_url, {"post": "yes"})
assert response.status_code == 302
SessionStore = get_session_store()
assert SessionStore().exists(other_user_session.session_key) # pyright: ignore
def test_delete_with_session_db_backend(client, admin_user, session_changelist_url):
client.force_login(admin_user)
session = SessionProfileFactory(user=admin_user)
assert SessionProfile.objects.count() == 1
# sesison created by login
assert Session.objects.count() == 2
assert DBSessionStore().exists(session.session_key)
url = reverse("admin:sessionprofile_sessionprofile_delete", args=[session.pk])
response = client.post(url, {"post": "yes"})
assert response.status_code == 302
# new session saved upon request
assert SessionProfile.objects.count() == 1
assert SessionProfile.objects.count() != session
assert Session.objects.count() == 1
assert not DBSessionStore().exists(session.session_key)
@override_settings(SESSION_ENGINE="django.contrib.sessions.backends.cache")
def test_delete_with_session_cache_backend(client, admin_user, session_changelist_url):
client.force_login(admin_user)
session = SessionProfileFactory(user=admin_user)
assert SessionProfile.objects.count() == 1
assert Session.objects.count() == 0
assert CachedSessionStore().exists(session.session_key)
url = reverse("admin:sessionprofile_sessionprofile_delete", args=[session.pk])
response = client.post(url, {"post": "yes"})
assert response.status_code == 302
# new session saved upon request
assert SessionProfile.objects.count() == 1
assert SessionProfile.objects.count() != session
assert Session.objects.count() == 0
assert not CachedSessionStore().exists(session.session_key)
def test_delete_action_with_session_db_backend(
client, admin_user, session_changelist_url
):
client.force_login(admin_user)
sessions = SessionProfileFactory.create_batch(5, user=admin_user)
# one created from user login
assert Session.objects.count() == 6
assert SessionProfile.objects.count() == 5
session_keys = [session.session_key for session in sessions]
for session_key in session_keys:
assert DBSessionStore().exists(session_key)
response = client.post(
session_changelist_url,
{"action": "delete_selected", "_selected_action": session_keys, "post": "yes"},
)
assert response.status_code == 302
# one is created as the post request is sent
assert SessionProfile.objects.count() == 1
assert Session.objects.count() == 1
for session_key in session_keys:
assert not DBSessionStore().exists(session_key)
@override_settings(SESSION_ENGINE="django.contrib.sessions.backends.cache")
def test_delete_action_with_session_cache_backend(
client, admin_user, session_changelist_url
):
client.force_login(admin_user)
sessions = SessionProfileFactory.create_batch(5, user=admin_user)
# no db sessions are created
assert Session.objects.count() == 0
assert SessionProfile.objects.count() == 5
session_keys = [session.session_key for session in sessions]
# sessions are created
for session_key in session_keys:
assert CachedSessionStore().exists(session_key)
response = client.post(
session_changelist_url,
{"action": "delete_selected", "_selected_action": session_keys, "post": "yes"},
)
assert response.status_code == 302
# one is created as the post request is sent
assert SessionProfile.objects.count() == 1
assert Session.objects.count() == 0
# sessions should be deleted
for session_key in session_keys:
assert not CachedSessionStore().exists(session_key)