forked from DefectDojo/django-DefectDojo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_remote_user.py
219 lines (201 loc) · 10.3 KB
/
test_remote_user.py
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from django.test import Client, override_settings
from netaddr import IPSet
from dojo.models import Dojo_Group, Dojo_Group_Member, User
from dojo.remote_user import RemoteUserScheme
from .dojo_test_case import DojoTestCase
class TestRemoteUser(DojoTestCase):
client1 = Client()
client2 = Client()
def setUp(self):
self.user, _ = User.objects.get_or_create(
username='test_remote_user',
first_name='original_first',
last_name='original_last',
email='[email protected]',
)
self.group1, _ = Dojo_Group.objects.get_or_create(name="group1", social_provider=Dojo_Group.REMOTE)
self.group2, _ = Dojo_Group.objects.get_or_create(name="group2", social_provider=Dojo_Group.REMOTE)
@override_settings(AUTH_REMOTEUSER_ENABLED=False)
def test_disabled(self):
resp = self.client1.get('/profile')
self.assertEqual(resp.status_code, 302)
@override_settings(
AUTH_REMOTEUSER_ENABLED=True,
AUTH_REMOTEUSER_USERNAME_HEADER="HTTP_REMOTE_USER",
)
def test_basic(self):
resp = self.client1.get('/profile',
# TODO - This can be replaced by following lines in the future
# Using of "headers" is supported since Django 4.2
HTTP_REMOTE_USER=self.user.username,
# headers={
# "Remote-User": self.user.username
# }
)
self.assertEqual(resp.status_code, 200)
@override_settings(
AUTH_REMOTEUSER_ENABLED=True,
AUTH_REMOTEUSER_USERNAME_HEADER="HTTP_REMOTE_USER",
AUTH_REMOTEUSER_FIRSTNAME_HEADER="HTTP_REMOTE_FIRSTNAME",
AUTH_REMOTEUSER_LASTNAME_HEADER="HTTP_REMOTE_LASTNAME",
AUTH_REMOTEUSER_EMAIL_HEADER="HTTP_REMOTE_EMAIL",
)
def test_update_user(self):
resp = self.client1.get('/profile',
# TODO - This can be replaced by following lines in the future
# Using of "headers" is supported since Django 4.2
HTTP_REMOTE_USER=self.user.username,
HTTP_REMOTE_FIRSTNAME="new_first",
HTTP_REMOTE_LASTNAME="new_last",
HTTP_REMOTE_EMAIL="[email protected]",
# headers = {
# "Remote-User": self.user.username,
# "Remote-Firstname": "new_first",
# "Remote-Lastname": "new_last",
# "Remote-Email": "[email protected]",
# }
)
self.assertEqual(resp.status_code, 200)
updated_user = User.objects.get(pk=self.user.pk)
self.assertEqual(updated_user.first_name, "new_first")
self.assertEqual(updated_user.last_name, "new_last")
self.assertEqual(updated_user.email, "[email protected]")
@override_settings(
AUTH_REMOTEUSER_ENABLED=True,
AUTH_REMOTEUSER_USERNAME_HEADER="HTTP_REMOTE_USER",
AUTH_REMOTEUSER_GROUPS_HEADER="HTTP_REMOTE_GROUPS",
AUTH_REMOTEUSER_GROUPS_CLEANUP=True,
)
def test_update_groups_cleanup(self):
resp = self.client1.get('/profile',
# TODO - This can be replaced by following lines in the future
# Using of "headers" is supported since Django 4.2
HTTP_REMOTE_USER=self.user.username,
HTTP_REMOTE_GROUPS=self.group1.name,
# headers = {
# "Remote-User": self.user.username,
# "Remote-Groups": self.group1.name,
# }
)
self.assertEqual(resp.status_code, 200)
dgms = Dojo_Group_Member.objects.filter(user=self.user)
self.assertEqual(dgms.count(), 1)
self.assertEqual(dgms.first().group.name, self.group1.name)
resp = self.client2.get('/profile',
# TODO - This can be replaced by following lines in the future
# Using of "headers" is supported since Django 4.2
HTTP_REMOTE_USER=self.user.username,
HTTP_REMOTE_GROUPS=self.group2.name,
# headers = {
# "Remote-User": self.user.username,
# "Remote-Groups": self.group2.name,
# }
)
self.assertEqual(resp.status_code, 200)
dgms = Dojo_Group_Member.objects.all().filter(user=self.user)
self.assertEqual(dgms.count(), 1)
self.assertEqual(dgms.first().group.name, self.group2.name)
@override_settings(
AUTH_REMOTEUSER_ENABLED=True,
AUTH_REMOTEUSER_USERNAME_HEADER="HTTP_REMOTE_USER",
AUTH_REMOTEUSER_GROUPS_HEADER="HTTP_REMOTE_GROUPS",
AUTH_REMOTEUSER_GROUPS_CLEANUP=True,
)
def test_update_multiple_groups_cleanup(self):
resp = self.client1.get('/profile',
# TODO - This can be replaced by following lines in the future
# Using of "headers" is supported since Django 4.2
HTTP_REMOTE_USER=self.user.username,
HTTP_REMOTE_GROUPS=f"{self.group1.name},{self.group2.name}",
# headers = {
# "Remote-User": self.user.username,
# "Remote-Groups": f"{self.group1.name},{self.group2.name}",
# }
)
self.assertEqual(resp.status_code, 200)
dgms = Dojo_Group_Member.objects.filter(user=self.user)
self.assertEqual(dgms.count(), 2)
@override_settings(
AUTH_REMOTEUSER_ENABLED=True,
AUTH_REMOTEUSER_USERNAME_HEADER="HTTP_REMOTE_USER",
AUTH_REMOTEUSER_GROUPS_HEADER="HTTP_REMOTE_GROUPS",
AUTH_REMOTEUSER_GROUPS_CLEANUP=False,
)
def test_update_groups_no_cleanup(self):
resp = self.client1.get('/profile',
# TODO - This can be replaced by following lines in the future
# Using of "headers" is supported since Django 4.2
HTTP_REMOTE_USER=self.user.username,
HTTP_REMOTE_GROUPS=self.group1.name,
# headers = {
# "Remote-User": self.user.username,
# "Remote-Groups": self.group1.name,
# }
)
self.assertEqual(resp.status_code, 200)
resp = self.client2.get('/profile',
# TODO - This can be replaced by following lines in the future
# Using of "headers" is supported since Django 4.2
HTTP_REMOTE_USER=self.user.username,
HTTP_REMOTE_GROUPS=self.group2.name,
# headers = {
# "Remote-User": self.user.username,
# "Remote-Groups": self.group2.name,
# }
)
self.assertEqual(resp.status_code, 200)
dgms = Dojo_Group_Member.objects.filter(user=self.user)
self.assertEqual(dgms.count(), 2)
@override_settings(
AUTH_REMOTEUSER_ENABLED=True,
AUTH_REMOTEUSER_USERNAME_HEADER="HTTP_REMOTE_USER",
AUTH_REMOTEUSER_TRUSTED_PROXY=IPSet(['192.168.0.0/24', '192.168.2.0/24']),
)
def test_trusted_proxy(self):
resp = self.client1.get('/profile',
REMOTE_ADDR='192.168.0.42',
# TODO - This can be replaced by following lines in the future
# Using of "headers" is supported since Django 4.2
HTTP_REMOTE_USER=self.user.username,
# headers = {
# "Remote-User": self.user.username,
# }
)
self.assertEqual(resp.status_code, 200)
@override_settings(
AUTH_REMOTEUSER_ENABLED=True,
AUTH_REMOTEUSER_USERNAME_HEADER="HTTP_REMOTE_USER",
AUTH_REMOTEUSER_TRUSTED_PROXY=IPSet(['192.168.0.0/24', '192.168.2.0/24']),
)
def test_untrusted_proxy(self):
with self.assertLogs('dojo.remote_user', level='DEBUG') as cm:
resp = self.client1.get('/profile',
REMOTE_ADDR='192.168.1.42',
# TODO - This can be replaced by following lines in the future
# Using of "headers" is supported since Django 4.2
HTTP_REMOTE_USER=self.user.username,
# headers = {
# "Remote-User": self.user.username,
# }
)
self.assertEqual(resp.status_code, 302)
self.assertIn('Requested came from untrusted proxy', cm.output[0])
@override_settings(
AUTH_REMOTEUSER_ENABLED=True,
AUTH_REMOTEUSER_USERNAME_HEADER="HTTP_OUR_REMOTE_USER",
AUTH_REMOTEUSER_VISIBLE_IN_SWAGGER=True,
)
def test_api_schema_visible(self):
security_definition = RemoteUserScheme.get_security_definition(None, None)
self.assertEqual(security_definition, {
"type": "apiKey",
"in": "header",
"name": "Our-remote-user",
})
@override_settings(
AUTH_REMOTEUSER_ENABLED=True,
AUTH_REMOTEUSER_VISIBLE_IN_SWAGGER=False,
)
def test_api_schema_hidden(self):
security_definition = RemoteUserScheme.get_security_definition(None, None)
self.assertEqual(security_definition, {})