1 |
1 |
# -*- coding: utf-8 -*-
|
2 |
2 |
|
3 |
3 |
import json
|
4 |
4 |
import mock
|
5 |
5 |
import pytest
|
6 |
6 |
import re
|
7 |
7 |
from requests import Response
|
8 |
|
from webtest import TestApp
|
9 |
8 |
|
10 |
9 |
from django.contrib.auth.models import User
|
11 |
10 |
from django.test import override_settings
|
12 |
11 |
|
13 |
12 |
from hobo.environment.models import Wcs, Combo, Fargo
|
14 |
|
from hobo.wsgi import application
|
15 |
13 |
|
16 |
14 |
from test_manager import login
|
17 |
15 |
|
18 |
16 |
pytestmark = pytest.mark.django_db
|
19 |
17 |
|
20 |
18 |
CONFIG = {'URL': 'https://matomo.test',
|
21 |
19 |
'TOKEN_AUTH': '1234',
|
22 |
20 |
'EMAIL_TEMPLATE': 'noreply+%s@entrouvert.test'}
|
... | ... | |
82 |
80 |
if content[0] == '{':
|
83 |
81 |
response.json = mock.MagicMock(return_value=json.loads(content))
|
84 |
82 |
response._content = content
|
85 |
83 |
|
86 |
84 |
response.status_code = 200
|
87 |
85 |
responses.append(response)
|
88 |
86 |
return responses
|
89 |
87 |
|
90 |
|
def test_unlogged_access():
|
|
88 |
def test_unlogged_access(app):
|
91 |
89 |
# connect while not being logged in
|
92 |
|
app = TestApp(application)
|
93 |
90 |
resp = app.get('/visits-tracking/', status=302)
|
94 |
91 |
assert resp.location.endswith('/login/?next=/visits-tracking/')
|
95 |
92 |
|
96 |
|
def test_access(admin_user):
|
97 |
|
app = login(TestApp(application))
|
|
93 |
def test_access(app, admin_user):
|
|
94 |
login(app)
|
98 |
95 |
assert app.get('/visits-tracking/', status=200)
|
99 |
96 |
|
100 |
|
def test_disable(admin_user):
|
101 |
|
app = login(TestApp(application))
|
|
97 |
def test_disable(app, admin_user):
|
|
98 |
login(app)
|
102 |
99 |
resp1 = app.get('/visits-tracking/disable', status=200)
|
103 |
100 |
resp2 = resp1.form.submit()
|
104 |
101 |
assert resp2.location.endswith('/visits-tracking/')
|
105 |
102 |
|
106 |
|
def test_enable_manual(admin_user):
|
|
103 |
def test_enable_manual(app, admin_user):
|
107 |
104 |
"""scenario where user manually paste a javascript code"""
|
108 |
|
app = login(TestApp(application))
|
|
105 |
login(app)
|
109 |
106 |
|
110 |
107 |
# get matomo's validation page
|
111 |
108 |
resp = app.get('/visits-tracking/enable-manual', status=200)
|
112 |
109 |
assert re.search('<textarea.* name="tracking_js"', resp.body)
|
113 |
110 |
|
114 |
111 |
# validate and get matomo's home page
|
115 |
112 |
resp.form['tracking_js'] = '...js_code_1...'
|
116 |
113 |
resp = resp.form.submit().follow()
|
... | ... | |
136 |
133 |
'You should remove the surrounding <script> markup.</li></ul>') in resp.text
|
137 |
134 |
resp.form['tracking_js'] = '<script >'
|
138 |
135 |
resp = resp.form.submit()
|
139 |
136 |
assert (
|
140 |
137 |
'<ul class="errorlist"><li>This field should only contain the Javascript code. '
|
141 |
138 |
'You should remove the surrounding <script> markup.</li></ul>') in resp.text
|
142 |
139 |
|
143 |
140 |
|
144 |
|
def test_available_options(admin_user):
|
|
141 |
def test_available_options(app, admin_user):
|
145 |
142 |
"""check available buttons (manual/automatic configurations)"""
|
|
143 |
login(app)
|
|
144 |
|
146 |
145 |
with override_settings(MATOMO_SERVER=CONFIG):
|
147 |
|
app = login(TestApp(application))
|
148 |
146 |
resp = app.get('/visits-tracking/', status=200)
|
149 |
147 |
assert str(resp).find('href="/visits-tracking/enable-manual"') != -1
|
150 |
148 |
assert str(resp).find('href="/visits-tracking/enable-auto"') != -1
|
151 |
149 |
|
152 |
150 |
# without configuration: no automatic configuration available
|
153 |
|
app = login(TestApp(application))
|
154 |
151 |
resp = app.get('/visits-tracking/', status=200)
|
155 |
152 |
assert str(resp).find('href="/visits-tracking/enable-manual"') != -1
|
156 |
153 |
assert str(resp).find('href="/visits-tracking/enable-auto"') == -1
|
157 |
154 |
|
158 |
155 |
@mock.patch('requests.post')
|
159 |
|
def test_enable_auto(mocked_post, admin_user):
|
|
156 |
def test_enable_auto(mocked_post, app, admin_user):
|
160 |
157 |
"""succesfull automatic scenario"""
|
161 |
158 |
Combo.objects.create(base_url='https://combo.dev.publik.love',
|
162 |
159 |
template_name='portal-user')
|
163 |
160 |
Wcs.objects.create(base_url='https://wcs.dev.publik.love')
|
164 |
161 |
Fargo.objects.create(base_url='https://fargo.dev.publik.love')
|
|
162 |
login(app)
|
165 |
163 |
|
166 |
164 |
contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS, ADD_SITE_ALIAS_URLS_SUCCESS,
|
167 |
165 |
DEL_UNKNOWN_USER, MATOMO_SUCCESS,
|
168 |
166 |
JAVASCRIPT_TAG, PING_SUCCESS]
|
169 |
167 |
|
170 |
168 |
mocked_post.side_effect = requests_post_mocked_replies(contents)
|
171 |
169 |
with override_settings(MATOMO_SERVER=CONFIG):
|
172 |
|
app = login(TestApp(application))
|
173 |
170 |
resp1 = app.get('/visits-tracking/enable-auto', status=200)
|
174 |
171 |
resp2 = resp1.form.submit()
|
175 |
172 |
|
176 |
173 |
# call utils.py::auto_configure_matomo()
|
177 |
174 |
resp3 = resp2.follow()
|
178 |
175 |
|
179 |
176 |
# expect the CNIL compliance message is displayed
|
180 |
177 |
assert resp3.body.find('Excellent respect of user rights') != -1
|
181 |
178 |
|
182 |
179 |
@mock.patch('requests.post')
|
183 |
|
def test_enable_auto_warning(mocked_post, admin_user):
|
|
180 |
def test_enable_auto_warning(mocked_post, app, admin_user):
|
184 |
181 |
"""succesfull automatic scenario having final ping failure"""
|
185 |
182 |
Combo.objects.create(base_url='https://combo.dev.publik.love',
|
186 |
183 |
template_name='portal-user')
|
187 |
184 |
Wcs.objects.create(base_url='https://wcs.dev.publik.love')
|
188 |
185 |
Fargo.objects.create(base_url='https://fargo.dev.publik.love')
|
|
186 |
login(app)
|
189 |
187 |
|
190 |
188 |
contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS, ADD_SITE_ALIAS_URLS_SUCCESS,
|
191 |
189 |
DEL_UNKNOWN_USER, MATOMO_SUCCESS,
|
192 |
190 |
JAVASCRIPT_TAG, PING_ERROR]
|
193 |
191 |
|
194 |
192 |
mocked_post.side_effect = requests_post_mocked_replies(contents)
|
195 |
193 |
with override_settings(MATOMO_SERVER=CONFIG):
|
196 |
|
app = login(TestApp(application))
|
197 |
194 |
resp1 = app.get('/visits-tracking/enable-auto', status=200)
|
198 |
195 |
resp2 = resp1.form.submit()
|
199 |
196 |
|
200 |
197 |
# call utils.py::auto_configure_matomo()
|
201 |
198 |
resp3 = resp2.follow()
|
202 |
199 |
|
203 |
200 |
# expect 'ping fails' warning
|
204 |
201 |
assert resp3.body.find('class="warning">ping: ping fails') != -1
|
205 |
202 |
|
206 |
203 |
# expect the CNIL compliance message is displayed
|
207 |
204 |
assert resp3.body.find('Excellent respect of user rights') != -1
|
208 |
205 |
|
209 |
206 |
@mock.patch('requests.post')
|
210 |
|
def test_enable_auto_error(mocked_post, admin_user):
|
|
207 |
def test_enable_auto_error(mocked_post, app, admin_user):
|
211 |
208 |
"""error on automatic scenario"""
|
212 |
209 |
Combo.objects.create(base_url='https://combo.dev.publik.love',
|
213 |
210 |
template_name='portal-user')
|
214 |
211 |
Wcs.objects.create(base_url='https://wcs.dev.publik.love')
|
215 |
212 |
Fargo.objects.create(base_url='https://fargo.dev.publik.love')
|
|
213 |
login(app)
|
216 |
214 |
|
217 |
215 |
contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS, ADD_SITE_ALIAS_URLS_SUCCESS,
|
218 |
216 |
DEL_UNKNOWN_USER, MATOMO_SUCCESS,
|
219 |
217 |
JAVASCRIPT_TAG_BAD_RESPONSE]
|
220 |
218 |
|
221 |
219 |
mocked_post.side_effect = requests_post_mocked_replies(contents)
|
222 |
220 |
with override_settings(MATOMO_SERVER=CONFIG):
|
223 |
|
app = login(TestApp(application))
|
224 |
221 |
resp1 = app.get('/visits-tracking/enable-auto', status=200)
|
225 |
222 |
resp2 = resp1.form.submit()
|
226 |
223 |
|
227 |
224 |
# call utils.py::auto_configure_matomo()
|
228 |
225 |
resp3 = resp2.follow()
|
229 |
226 |
|
230 |
227 |
# expect a Django error message is displayed
|
231 |
228 |
assert resp3.body.find('class="error">matomo: get_javascript_tag fails') != -1
|