From 1441dca958e61d9cce98e3a85d882fe7be2b3046 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Sat, 22 Feb 2020 11:55:21 +0100
Subject: [PATCH 3/3] warnings: cannot collect test class 'TestApp' because it
 has a __init__ constructor (#40099)

---
 tests/test_matomo_views.py | 35 ++++++++++++++++-------------------
 tests/test_version.py      |  1 -
 2 files changed, 16 insertions(+), 20 deletions(-)

diff --git a/tests/test_matomo_views.py b/tests/test_matomo_views.py
index 8326e8b..b62e690 100644
--- a/tests/test_matomo_views.py
+++ b/tests/test_matomo_views.py
@@ -1,22 +1,20 @@
 # -*- coding: utf-8 -*-
 
 import json
 import mock
 import pytest
 import re
 from requests import Response
-from webtest import TestApp
 
 from django.contrib.auth.models import User
 from django.test import override_settings
 
 from hobo.environment.models import Wcs, Combo, Fargo
-from hobo.wsgi import application
 
 from test_manager import login
 
 pytestmark = pytest.mark.django_db
 
 CONFIG = {'URL': 'https://matomo.test',
           'TOKEN_AUTH': '1234',
           'EMAIL_TEMPLATE': 'noreply+%s@entrouvert.test'}
@@ -82,35 +80,34 @@ def requests_post_mocked_replies(contents):
         if content[0] == '{':
             response.json = mock.MagicMock(return_value=json.loads(content))
         response._content = content
 
         response.status_code = 200
         responses.append(response)
     return responses
 
-def test_unlogged_access():
+def test_unlogged_access(app):
     # connect while not being logged in
-    app = TestApp(application)
     resp = app.get('/visits-tracking/', status=302)
     assert resp.location.endswith('/login/?next=/visits-tracking/')
 
-def test_access(admin_user):
-    app = login(TestApp(application))
+def test_access(app, admin_user):
+    login(app)
     assert app.get('/visits-tracking/', status=200)
 
-def test_disable(admin_user):
-    app = login(TestApp(application))
+def test_disable(app, admin_user):
+    login(app)
     resp1 = app.get('/visits-tracking/disable', status=200)
     resp2 = resp1.form.submit()
     assert resp2.location.endswith('/visits-tracking/')
 
-def test_enable_manual(admin_user):
+def test_enable_manual(app, admin_user):
     """scenario where user manually paste a javascript code"""
-    app = login(TestApp(application))
+    login(app)
 
     # get matomo's validation page
     resp = app.get('/visits-tracking/enable-manual', status=200)
     assert re.search('<textarea.* name="tracking_js"', resp.body)
 
     # validate and get matomo's home page
     resp.form['tracking_js'] = '...js_code_1...'
     resp = resp.form.submit().follow()
@@ -136,96 +133,96 @@ def test_enable_manual(admin_user):
         'You should remove the surrounding &lt;script&gt; markup.</li></ul>') in resp.text
     resp.form['tracking_js'] = '<script >'
     resp = resp.form.submit()
     assert (
         '<ul class="errorlist"><li>This field should only contain the Javascript code. '
         'You should remove the surrounding &lt;script&gt; markup.</li></ul>') in resp.text
 
 
-def test_available_options(admin_user):
+def test_available_options(app, admin_user):
     """check available buttons (manual/automatic configurations)"""
+    login(app)
+    
     with override_settings(MATOMO_SERVER=CONFIG):
-        app = login(TestApp(application))
         resp = app.get('/visits-tracking/', status=200)
         assert str(resp).find('href="/visits-tracking/enable-manual"') != -1
         assert str(resp).find('href="/visits-tracking/enable-auto"') != -1
 
     # without configuration: no automatic configuration available
-    app = login(TestApp(application))
     resp = app.get('/visits-tracking/', status=200)
     assert str(resp).find('href="/visits-tracking/enable-manual"') != -1
     assert str(resp).find('href="/visits-tracking/enable-auto"') == -1
 
 @mock.patch('requests.post')
-def test_enable_auto(mocked_post, admin_user):
+def test_enable_auto(mocked_post, app, admin_user):
     """succesfull automatic scenario"""
     Combo.objects.create(base_url='https://combo.dev.publik.love',
                          template_name='portal-user')
     Wcs.objects.create(base_url='https://wcs.dev.publik.love')
     Fargo.objects.create(base_url='https://fargo.dev.publik.love')
+    login(app)
 
     contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS, ADD_SITE_ALIAS_URLS_SUCCESS,
                 DEL_UNKNOWN_USER, MATOMO_SUCCESS,
                 JAVASCRIPT_TAG, PING_SUCCESS]
 
     mocked_post.side_effect = requests_post_mocked_replies(contents)
     with override_settings(MATOMO_SERVER=CONFIG):
-        app = login(TestApp(application))
         resp1 = app.get('/visits-tracking/enable-auto', status=200)
         resp2 = resp1.form.submit()
 
         # call utils.py::auto_configure_matomo()
         resp3 = resp2.follow()
 
         # expect the CNIL compliance message is displayed
         assert resp3.body.find('Excellent respect of user rights') != -1
 
 @mock.patch('requests.post')
-def test_enable_auto_warning(mocked_post, admin_user):
+def test_enable_auto_warning(mocked_post, app, admin_user):
     """succesfull automatic scenario having final ping failure"""
     Combo.objects.create(base_url='https://combo.dev.publik.love',
                          template_name='portal-user')
     Wcs.objects.create(base_url='https://wcs.dev.publik.love')
     Fargo.objects.create(base_url='https://fargo.dev.publik.love')
+    login(app)
 
     contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS, ADD_SITE_ALIAS_URLS_SUCCESS,
                 DEL_UNKNOWN_USER, MATOMO_SUCCESS,
                 JAVASCRIPT_TAG, PING_ERROR]
 
     mocked_post.side_effect = requests_post_mocked_replies(contents)
     with override_settings(MATOMO_SERVER=CONFIG):
-        app = login(TestApp(application))
         resp1 = app.get('/visits-tracking/enable-auto', status=200)
         resp2 = resp1.form.submit()
 
         # call utils.py::auto_configure_matomo()
         resp3 = resp2.follow()
 
         # expect 'ping fails' warning
         assert resp3.body.find('class="warning">ping: ping fails') != -1
 
         # expect the CNIL compliance message is displayed
         assert resp3.body.find('Excellent respect of user rights') != -1
 
 @mock.patch('requests.post')
-def test_enable_auto_error(mocked_post, admin_user):
+def test_enable_auto_error(mocked_post, app, admin_user):
     """error on automatic scenario"""
     Combo.objects.create(base_url='https://combo.dev.publik.love',
                          template_name='portal-user')
     Wcs.objects.create(base_url='https://wcs.dev.publik.love')
     Fargo.objects.create(base_url='https://fargo.dev.publik.love')
+    login(app)
 
     contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS, ADD_SITE_ALIAS_URLS_SUCCESS,
                 DEL_UNKNOWN_USER, MATOMO_SUCCESS,
                 JAVASCRIPT_TAG_BAD_RESPONSE]
 
     mocked_post.side_effect = requests_post_mocked_replies(contents)
     with override_settings(MATOMO_SERVER=CONFIG):
-        app = login(TestApp(application))
         resp1 = app.get('/visits-tracking/enable-auto', status=200)
         resp2 = resp1.form.submit()
 
         # call utils.py::auto_configure_matomo()
         resp3 = resp2.follow()
 
         # expect a Django error message is displayed
         assert resp3.body.find('class="error">matomo: get_javascript_tag fails') != -1
diff --git a/tests/test_version.py b/tests/test_version.py
index 70e5527..2e193ef 100644
--- a/tests/test_version.py
+++ b/tests/test_version.py
@@ -1,11 +1,10 @@
 import pytest
 import json
-from webtest import TestApp
 
 import hobo.scrutiny.wsgi.middleware
 
 pytestmark = pytest.mark.django_db
 
 def test_version_middleware(settings, client):
     hobo.scrutiny.wsgi.middleware.VersionMiddleware.ENTROUVERT_PACKAGES = ['pytest', 'pytest-django']
     # disable apt_cache to get stable test results
-- 
2.25.0

