0004-matomo-manage-automatic-configuration-19743.patch
hobo/matomo/templates/hobo/matomo_home.html | ||
---|---|---|
36 | 36 | |
37 | 37 |
{% if not enabled %} |
38 | 38 |
<p> |
39 |
{% if error != '' %} |
|
40 |
<div class="errornotice"> |
|
41 |
{{ error }} |
|
42 |
</div> |
|
43 |
{% endif %} |
|
39 | 44 |
{% trans "Support is currently disabled." %} |
40 | 45 |
</p> |
41 | 46 |
<p> |
... | ... | |
43 | 48 |
<a class="button" rel="popup" href="{% url 'matomo-enable-auto' %}">{% trans 'Automatic' %}</a> |
44 | 49 |
</p> |
45 | 50 |
{% else %} |
46 | ||
47 |
{% if cnil_ack_level == 'success' %} |
|
51 |
{% if tracking_js != '' %} |
|
52 |
{% if cnil_ack_level == 'success' %}
|
|
48 | 53 |
<div class="successnotice"> |
49 | 54 |
suivi des recommandations CNIL |
50 | 55 |
</div> |
51 |
{% elif cnil_ack_level == 'warning' %} |
|
56 |
{% elif cnil_ack_level == 'warning' %}
|
|
52 | 57 |
<div class="warningnotice"> |
53 | 58 |
suivi des recommandations CNIL |
54 | 59 |
</div> |
55 |
{% elif cnil_ack_level == 'error' %} |
|
60 |
{% elif cnil_ack_level == 'error' %}
|
|
56 | 61 |
<div class="errornotice"> |
57 | 62 |
suivi des recommandations CNIL |
58 | 63 |
</div> |
59 |
{% endif %} |
|
64 |
{% endif %} |
|
65 |
{% endif %} |
|
60 | 66 | |
61 |
{% if mode == 'manual' %} |
|
62 |
<p> |
|
63 |
{% trans "Manual mode." %} |
|
64 |
</p> |
|
65 |
{% endif %} |
|
66 | ||
67 |
{% if mode == 'auto' %} |
|
68 |
<p> |
|
69 |
{% trans "Automatic mode." %} |
|
70 |
</p> |
|
71 |
{% endif %} |
|
67 |
{% if mode == 'manual' %} |
|
68 |
<h4> |
|
69 |
{% trans "Manual mode" %} |
|
70 |
</h4> |
|
72 | 71 | |
73 | 72 |
<form method="post"> |
74 |
{% csrf_token %} |
|
75 |
{{ form.as_p }} |
|
73 |
{% csrf_token %}
|
|
74 |
{{ form.as_p }}
|
|
76 | 75 | |
77 | 76 |
<div class="buttons"> |
78 |
<button class="submit-button">{% trans "Save" %}</button> |
|
77 |
<button class="submit-button">{% trans "Save" %}</button>
|
|
79 | 78 |
</div> |
80 | 79 |
</form> |
80 |
{% endif %} |
|
81 | 81 | |
82 |
{% endif %} |
|
82 |
{% if mode == 'auto' %} |
|
83 |
<p> |
|
84 |
<h4> |
|
85 |
{% trans "Automatic mode" %} |
|
86 |
</h4> |
|
87 |
</p> |
|
83 | 88 | |
89 |
<ul> |
|
90 |
<li> |
|
91 |
{% trans "Matomo login url" %} : |
|
92 |
<a href={{matomo_url}}/index.php?module=Login&action=logme&login={{matomo_user_login}}&password={{matomo_password}}> |
|
93 |
{{matomo_url}}/index.php?module=Login&action=logme&login={{matomo_user_login}}&password={{matomo_password}} |
|
94 |
</a> |
|
95 |
</li> |
|
96 |
<li> |
|
97 |
{% trans "Untrack me code" %} : |
|
98 |
<br/> |
|
99 |
<textarea readonly rows='10' cols='40' size='1024'> |
|
100 |
<iframe |
|
101 |
style="border: 0; height: 200px; width: 600px;" |
|
102 |
src="{{matomo_url}}/index.php?module=CoreAdminHome&action=optOut&language=fr&backgroundColor=&fontColor=&fontSize=&fontFamily=" |
|
103 |
> |
|
104 |
</iframe> |
|
105 |
</textarea> |
|
106 |
<br/> |
|
107 |
exemple: |
|
108 |
<br/> |
|
109 |
<iframe |
|
110 |
style="border: 0; height: 200px; width: 600px;" |
|
111 |
src="{{matomo_url}}/index.php?module=CoreAdminHome&action=optOut&language=fr&backgroundColor=&fontColor=&fontSize=&fontFamily=" |
|
112 |
> |
|
113 |
</iframe> |
|
114 |
</li> |
|
115 |
</ul> |
|
116 |
{% endif %} |
|
117 |
{% endif %} |
|
84 | 118 |
{% endblock %} |
hobo/matomo/views.py | ||
---|---|---|
14 | 14 |
# You should have received a copy of the GNU Affero General Public License |
15 | 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | 16 | |
17 |
import hashlib |
|
18 | ||
17 | 19 |
from django.core.urlresolvers import reverse_lazy |
18 | 20 |
from django.views.generic import RedirectView, FormView |
19 | 21 | |
... | ... | |
44 | 46 |
context = super(HomeView, self).get_context_data(**kwargs) |
45 | 47 |
context['enabled'] = bool(get_variable('matomo_enable').json) |
46 | 48 |
context['mode'] = get_variable('matomo_mode').json |
49 |
context['error'] = get_variable('matomo_error').value |
|
47 | 50 |
tracking_js = get_variable('cnil_compliant_visits_tracking_js').value |
48 | 51 |
context['cnil_ack_level'] = compute_cnil_acknowledgment_level(tracking_js) |
52 |
context['tracking_js'] = tracking_js |
|
53 | ||
54 |
if context['mode'] == 'auto': |
|
55 |
matomo_setings = get_matomo_settings() |
|
56 |
tenant_name, unused = get_tenant_name_and_public_urls() |
|
57 |
password = get_variable('matomo_password').value |
|
58 |
context['matomo_url'] = matomo_setings[0] |
|
59 |
context['matomo_user_login'] = tenant_name |
|
60 |
context['matomo_password'] = hashlib.md5(password).hexdigest() |
|
49 | 61 |
return context |
50 | 62 | |
51 | 63 |
home = HomeView.as_view() |
... | ... | |
74 | 86 |
success_url = reverse_lazy('matomo-home') |
75 | 87 | |
76 | 88 |
def form_valid(self, form): |
77 |
variable = get_variable('matomo_enable') |
|
78 |
variable.value = 'true' |
|
79 |
variable.save() |
|
80 | 89 |
variable = get_variable('matomo_mode') |
81 | 90 |
variable.value = 'auto' |
82 | 91 |
variable.save() |
92 |
if auto_configure_matomo(): |
|
93 |
variable = get_variable('matomo_enable') |
|
94 |
variable.value = 'true' |
|
95 |
variable.save() |
|
83 | 96 |
return super(EnableAutoView, self).form_valid(form) |
84 | 97 | |
85 | 98 |
enable_auto = EnableAutoView.as_view() |
tests/test_matomo_views.py | ||
---|---|---|
1 |
# -*- coding: utf-8 -*- |
|
2 | ||
3 |
import mock |
|
4 |
import pytest |
|
5 |
from requests import Response |
|
6 |
from webtest import TestApp |
|
7 | ||
8 |
from django.conf import settings |
|
9 |
from django.contrib.auth.models import User |
|
10 |
from django.test import override_settings |
|
11 | ||
12 |
from hobo.environment.models import Variable, Wcs, Combo, Fargo |
|
13 |
from hobo.wsgi import application |
|
14 | ||
15 |
pytestmark = pytest.mark.django_db |
|
16 | ||
17 |
CONFIG = {'URL': 'https://matomo.test', 'TOKEN_AUTH': '1234'} |
|
18 | ||
19 |
GET_SITE_NOT_ALREADY_THERE = """<?xml version="1.0" encoding="utf-8" ?> |
|
20 |
<result> |
|
21 |
<error message="An unexpected website was found in the request: website id was set to '42' ." /> |
|
22 |
</result> |
|
23 |
""" |
|
24 | ||
25 |
ADD_SITE_SUCCESS = """<?xml version="1.0" encoding="utf-8" ?> |
|
26 |
<result>42</result> |
|
27 |
""" |
|
28 | ||
29 |
ADD_USER_SUCCESS = """<?xml version="1.0" encoding="utf-8" ?> |
|
30 |
<result> |
|
31 |
<success message="ok" /> |
|
32 |
</result> |
|
33 |
""" |
|
34 | ||
35 |
JAVASCRIPT_TAG = """<?xml version="1.0" encoding="utf-8" ?> |
|
36 |
<result><!-- Matomo --> |
|
37 |
<script type="text/javascript"> |
|
38 |
var _paq = window._paq || []; |
|
39 |
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */ |
|
40 |
_paq.push(['trackPageView']); |
|
41 |
_paq.push(['enableLinkTracking']); |
|
42 |
(function() { |
|
43 |
var u="//matomo-test.entrouvert.org/"; |
|
44 |
_paq.push(['setTrackerUrl', u+'piwik.php']); |
|
45 |
_paq.push(['setSiteId', '7']); |
|
46 |
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; |
|
47 |
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s); |
|
48 |
})(); |
|
49 |
</script> |
|
50 |
<!-- End Matomo Code --> |
|
51 |
</result> |
|
52 |
""" |
|
53 | ||
54 |
@pytest.fixture |
|
55 |
def admin_user(): |
|
56 |
try: |
|
57 |
user = User.objects.get(username='admin') |
|
58 |
except User.DoesNotExist: |
|
59 |
user = User.objects.create_superuser('admin', email=None, password='admin') |
|
60 |
return user |
|
61 | ||
62 |
def login(app, username='admin', password='admin'): |
|
63 |
login_page = app.get('/login/') |
|
64 |
login_form = login_page.forms[0] |
|
65 |
login_form['username'] = username |
|
66 |
login_form['password'] = password |
|
67 |
resp = login_form.submit() |
|
68 |
assert resp.status_int == 302 |
|
69 |
return app |
|
70 | ||
71 |
def test_unlogged_access(): |
|
72 |
# connect while not being logged in |
|
73 |
app = TestApp(application) |
|
74 |
resp = app.get('/matomo/', status=302) |
|
75 |
assert resp.location.endswith('/login/?next=/matomo/') |
|
76 | ||
77 |
def test_access(admin_user): |
|
78 |
app = login(TestApp(application)) |
|
79 |
assert app.get('/matomo/', status=200) |
|
80 | ||
81 |
def test_disable(admin_user): |
|
82 |
app = login(TestApp(application)) |
|
83 |
resp1 = app.get('/matomo/disable', status=200) |
|
84 |
resp2 = resp1.form.submit() |
|
85 |
assert resp2.location.endswith('/matomo/') |
|
86 | ||
87 |
def test_enable_manual(admin_user): |
|
88 |
"""scenario where user manually paste a javascript code""" |
|
89 |
app = login(TestApp(application)) |
|
90 |
resp1 = app.get('/matomo/enable-manual', status=200) |
|
91 |
resp2 = resp1.form.submit().follow() |
|
92 |
resp2.form['tracking_js'] = '...js_code...' |
|
93 |
resp3 = resp2.form.submit().follow() |
|
94 |
assert resp3.body.find('"warningnotice">\n suivi des recommandations CNIL') != -1 |
|
95 | ||
96 |
def auto_conf_mocked_post(url, **kwargs): |
|
97 |
contents = [GET_SITE_NOT_ALREADY_THERE, |
|
98 |
ADD_SITE_SUCCESS, |
|
99 |
ADD_USER_SUCCESS, |
|
100 |
JAVASCRIPT_TAG] |
|
101 |
response = Response() |
|
102 |
response._content = contents[auto_conf_mocked_post.cpt] |
|
103 |
response.status_code = 200 |
|
104 | ||
105 |
auto_conf_mocked_post.cpt += 1 |
|
106 |
return response |
|
107 | ||
108 |
@mock.patch('requests.post', side_effect=auto_conf_mocked_post) |
|
109 |
def test_enable_auto(mocked_post, admin_user): |
|
110 |
"""automatic scenario""" |
|
111 |
Combo.objects.create(base_url='https://combo.dev.publik.love', |
|
112 |
template_name='portal-user') |
|
113 |
Wcs.objects.create(base_url='https://wcs.dev.publik.love') |
|
114 |
Fargo.objects.create(base_url='https://fargo.dev.publik.love') |
|
115 | ||
116 |
auto_conf_mocked_post.cpt = 0 |
|
117 |
with override_settings(MATOMO_FEED=CONFIG): |
|
118 |
app = login(TestApp(application)) |
|
119 |
resp1 = app.get('/matomo/enable-auto', status=200) |
|
120 |
resp2 = resp1.form.submit() |
|
121 | ||
122 |
# call utils.py::auto_configure_matomo() |
|
123 |
resp3 = resp2.follow() |
|
124 |
print resp3.body |
|
125 |
assert resp3.body.find('"successnotice">\n suivi des recommandations CNIL') != -1 |
|
0 |
- |