1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
""" |
4
|
|
|
Created on Tue Jul 17 14:52:13 2018 |
5
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
from pyUSIrest.exceptions import USIConnectionError |
10
|
|
|
|
11
|
|
|
from unittest.mock import Mock, patch |
12
|
|
|
|
13
|
|
|
from django.contrib.auth import get_user_model |
14
|
|
|
from django.contrib.messages import get_messages |
15
|
|
|
from django.test import Client, TestCase |
16
|
|
|
from django.urls import resolve, reverse |
17
|
|
|
|
18
|
|
|
from ..forms import GenerateTokenForm |
19
|
|
|
from ..models import Account, ManagedTeam |
20
|
|
|
from ..views import GenerateTokenView |
21
|
|
|
from .session_enabled_test_case import SessionEnabledTestCase |
22
|
|
|
from .common import generate_token |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
class BaseTest(SessionEnabledTestCase): |
26
|
|
|
fixtures = [ |
27
|
|
|
"biosample/managedteam" |
28
|
|
|
] |
29
|
|
|
|
30
|
|
View Code Duplication |
def setUp(self): |
|
|
|
|
31
|
|
|
User = get_user_model() |
32
|
|
|
|
33
|
|
|
# create a testuser |
34
|
|
|
user = User.objects.create_user( |
35
|
|
|
username='test', |
36
|
|
|
password='test', |
37
|
|
|
email="[email protected]") |
38
|
|
|
|
39
|
|
|
team = ManagedTeam.objects.get(name="subs.test-team-1") |
40
|
|
|
Account.objects.create( |
41
|
|
|
user=user, team=team, name="image-test") |
42
|
|
|
|
43
|
|
|
self.client = Client() |
44
|
|
|
self.client.login(username='test', password='test') |
45
|
|
|
|
46
|
|
|
# get the url for dashboard |
47
|
|
|
self.url = reverse('biosample:token-generation') |
48
|
|
|
self.response = self.client.get(self.url) |
49
|
|
|
|
50
|
|
View Code Duplication |
def check_messages(self, response, tag, message_text): |
|
|
|
|
51
|
|
|
"""Check that a response has warnings""" |
52
|
|
|
|
53
|
|
|
# each element is an instance |
54
|
|
|
# of django.contrib.messages.storage.base.Message |
55
|
|
|
all_messages = [msg for msg in get_messages(response.wsgi_request)] |
56
|
|
|
|
57
|
|
|
found = False |
58
|
|
|
|
59
|
|
|
# I can have moltiple message, and maybe I need to find a specific one |
60
|
|
|
for message in all_messages: |
61
|
|
|
if tag in message.tags and message_text in message.message: |
62
|
|
|
found = True |
63
|
|
|
|
64
|
|
|
self.assertTrue(found) |
65
|
|
|
|
66
|
|
|
|
67
|
|
View Code Duplication |
class CreateAuthViewTest(BaseTest): |
|
|
|
|
68
|
|
|
def test_redirection(self): |
69
|
|
|
'''Non Authenticated user are directed to login page''' |
70
|
|
|
|
71
|
|
|
login_url = reverse("login") |
72
|
|
|
client = Client() |
73
|
|
|
response = client.get(self.url) |
74
|
|
|
|
75
|
|
|
self.assertRedirects( |
76
|
|
|
response, '{login_url}?next={url}'.format( |
77
|
|
|
login_url=login_url, url=self.url) |
78
|
|
|
) |
79
|
|
|
|
80
|
|
|
def test_status_code(self): |
81
|
|
|
self.assertEqual(self.response.status_code, 200) |
82
|
|
|
|
83
|
|
|
def test_url_resolves_view(self): |
84
|
|
|
view = resolve('/biosample/token/generate/') |
85
|
|
|
self.assertIsInstance(view.func.view_class(), GenerateTokenView) |
86
|
|
|
|
87
|
|
|
def test_csrf(self): |
88
|
|
|
self.assertContains(self.response, 'csrfmiddlewaretoken') |
89
|
|
|
|
90
|
|
|
def test_contains_form(self): |
91
|
|
|
form = self.response.context.get('form') |
92
|
|
|
|
93
|
|
|
self.assertIsInstance(form, GenerateTokenForm) |
94
|
|
|
|
95
|
|
|
def test_form_inputs(self): |
96
|
|
|
''' |
97
|
|
|
The view must contain eleven inputs: csrf, username, first_name, |
98
|
|
|
last_name, email, password1, password2, affiliation, role, |
99
|
|
|
organization and agree_gdpr checkbox |
100
|
|
|
''' |
101
|
|
|
|
102
|
|
|
# total input is n of form fields + (CSRF) |
103
|
|
|
self.assertContains(self.response, '<input', 3) |
104
|
|
|
self.assertContains(self.response, 'type="password"', 1) |
105
|
|
|
|
106
|
|
|
def test_contains_navigation_links(self): |
107
|
|
|
dashboard_url = reverse('uid:dashboard') |
108
|
|
|
|
109
|
|
|
self.assertContains(self.response, 'href="{0}"'.format(dashboard_url)) |
110
|
|
|
self.assertContains(self.response, '<button type="submit"') |
111
|
|
|
|
112
|
|
|
|
113
|
|
View Code Duplication |
class UnregisteredAuthViewTest(TestCase): |
|
|
|
|
114
|
|
|
"""Test that a non register biosample user is redirected to biosample |
115
|
|
|
registration page""" |
116
|
|
|
|
117
|
|
|
def setUp(self): |
118
|
|
|
User = get_user_model() |
119
|
|
|
|
120
|
|
|
# create a testuser |
121
|
|
|
User.objects.create_user( |
122
|
|
|
username='test', |
123
|
|
|
password='test', |
124
|
|
|
email="[email protected]") |
125
|
|
|
|
126
|
|
|
self.client = Client() |
127
|
|
|
self.client.login(username='test', password='test') |
128
|
|
|
|
129
|
|
|
# this user is not registered in biosample. Get generate token view |
130
|
|
|
self.url = reverse('biosample:token-generation') |
131
|
|
|
self.response = self.client.get(self.url) |
132
|
|
|
|
133
|
|
|
def test_redirection(self): |
134
|
|
|
'''Non Authenticated user are directed to login page''' |
135
|
|
|
|
136
|
|
|
login_url = reverse("login") |
137
|
|
|
client = Client() |
138
|
|
|
response = client.get(self.url) |
139
|
|
|
|
140
|
|
|
self.assertRedirects( |
141
|
|
|
response, '{login_url}?next={url}'.format( |
142
|
|
|
login_url=login_url, url=self.url) |
143
|
|
|
) |
144
|
|
|
|
145
|
|
|
def test_registered(self): |
146
|
|
|
"""A non registered user is redirected to "activate complete""" |
147
|
|
|
|
148
|
|
|
target_url = reverse('accounts:registration_activation_complete') |
149
|
|
|
|
150
|
|
|
self.assertRedirects(self.response, target_url) |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
class InvalidCreateAuthViewTest(BaseTest): |
154
|
|
|
def setUp(self): |
155
|
|
|
# create a test user |
156
|
|
|
super().setUp() |
157
|
|
|
|
158
|
|
|
# submit an empty dictionary |
159
|
|
|
self.response = self.client.post(self.url, {}) |
160
|
|
|
|
161
|
|
|
def test_signup_status_code(self): |
162
|
|
|
''' |
163
|
|
|
An invalid form submission should return to the same page |
164
|
|
|
''' |
165
|
|
|
self.assertEqual(self.response.status_code, 200) |
166
|
|
|
|
167
|
|
|
def test_form_errors(self): |
168
|
|
|
form = self.response.context.get('form') |
169
|
|
|
self.assertGreater(len(form.errors), 0) |
170
|
|
|
|
171
|
|
|
def test_form_messages(self): |
172
|
|
|
self.check_messages( |
173
|
|
|
self.response, |
174
|
|
|
"error", |
175
|
|
|
"Please correct the errors below") |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
class SuccessFullCreateAuthViewTest(BaseTest): |
179
|
|
|
@classmethod |
180
|
|
|
def setUpClass(cls): |
181
|
|
|
# calling my base class setup |
182
|
|
|
super().setUpClass() |
183
|
|
|
|
184
|
|
|
cls.mock_get_patcher = patch('pyUSIrest.auth.requests.get') |
185
|
|
|
cls.mock_get = cls.mock_get_patcher.start() |
186
|
|
|
|
187
|
|
|
@classmethod |
188
|
|
|
def tearDownClass(cls): |
189
|
|
|
cls.mock_get_patcher.stop() |
190
|
|
|
super().tearDownClass() |
191
|
|
|
|
192
|
|
|
def setUp(self): |
193
|
|
|
# create a test user |
194
|
|
|
super().setUp() |
195
|
|
|
|
196
|
|
|
# generate tocken |
197
|
|
|
self.mock_get.return_value = Mock() |
198
|
|
|
self.mock_get.return_value.text = generate_token() |
199
|
|
|
self.mock_get.return_value.status_code = 200 |
200
|
|
|
|
201
|
|
|
self.data = { |
202
|
|
|
'password': 'image-password', |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
self.response = self.client.post(self.url, self.data) |
206
|
|
|
self.dashboard_url = reverse('uid:dashboard') |
207
|
|
|
|
208
|
|
|
def test_redirection(self): |
209
|
|
|
''' |
210
|
|
|
A valid form submission should redirect the user to the home page |
211
|
|
|
''' |
212
|
|
|
|
213
|
|
|
self.assertRedirects(self.response, self.dashboard_url) |
214
|
|
|
self.check_messages(self.response, "success", "Token generated!") |
215
|
|
|
|
216
|
|
|
def test_next_redirection(self): |
217
|
|
|
"""A valid form submission with a next parameter in request""" |
218
|
|
|
|
219
|
|
|
# construct url |
220
|
|
|
next_url = reverse("about") |
221
|
|
|
url = self.url + "?next=%s" % (next_url) |
222
|
|
|
|
223
|
|
|
# get response |
224
|
|
|
response = self.client.post(url, self.data) |
225
|
|
|
|
226
|
|
|
self.assertRedirects(response, next_url) |
227
|
|
|
|
228
|
|
|
@patch("pyUSIrest.auth.Auth", side_effect=USIConnectionError("test")) |
229
|
|
|
def test_error_with_biosample(self, my_auth): |
230
|
|
|
response = self.client.post(self.url, self.data) |
231
|
|
|
self.assertEqual(response.status_code, 200) |
232
|
|
|
|
233
|
|
|
self.check_messages( |
234
|
|
|
response, |
235
|
|
|
"error", |
236
|
|
|
"Unable to generate token: test") |
237
|
|
|
|
238
|
|
|
@patch("pyUSIrest.auth.Auth", |
239
|
|
|
side_effect=USIConnectionError( |
240
|
|
|
"""Got status 401: \'{"timestamp":1582726881584,""" |
241
|
|
|
""""status":401,"error":"Unauthorized","message":""" |
242
|
|
|
""""Bad credentials","path":"/auth"}\'""")) |
243
|
|
|
def test_error_bad_credentials(self, my_auth): |
244
|
|
|
response = self.client.post(self.url, self.data) |
245
|
|
|
self.assertEqual(response.status_code, 200) |
246
|
|
|
|
247
|
|
|
self.check_messages( |
248
|
|
|
response, |
249
|
|
|
"error", |
250
|
|
|
"Unable to generate token: Bad credentials") |
251
|
|
|
|