From 725efed83df7e1fc91e36c890657e80cb03e4754 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sun, 30 Jun 2019 14:00:28 +0200
Subject: [PATCH] tests: adapt to changes in wcs runscript command (#34405)

---
 tests/test_wcs.py | 58 ++++++++++++++++++++++++-----------------------
 tox.ini           |  2 +-
 2 files changed, 31 insertions(+), 29 deletions(-)

diff --git a/tests/test_wcs.py b/tests/test_wcs.py
index 5f173450..6d913e27 100644
--- a/tests/test_wcs.py
+++ b/tests/test_wcs.py
@@ -7,6 +7,7 @@ import re
 import requests
 import subprocess
 import shutil
+import sys
 import tempfile
 import time
 import os
@@ -31,10 +32,10 @@ from .test_manager import login
 
 pytestmark = pytest.mark.django_db
 
-wcsctl_present = pytest.mark.skipif('WCSCTL' not in os.environ or not os.path.exists(os.environ['WCSCTL']),
-        reason='WCSCTL not defined in environment')
+wcs_present = pytest.mark.skipif('WCS_MANAGE' not in os.environ or not os.path.exists(os.environ['WCS_MANAGE']),
+        reason='WCS_MANAGE not defined in environment')
 
-WCSCTL = os.environ.get('WCSCTL')
+WCS_MANAGE = os.environ.get('WCS_MANAGE')
 
 WCS_SCRIPTS = {
         'setup-auth': """
@@ -184,14 +185,15 @@ def run_wcs_script(script, hostname):
     fd = open(script_path, 'w')
     fd.write(WCS_SCRIPTS[script])
     fd.close()
-    subprocess.check_call([WCSCTL, 'runscript', '--app-dir', WCS_DIR,
-        '--vhost', hostname, script_path])
+    subprocess.check_call([sys.executable, WCS_MANAGE, 'runscript', '--app-dir', WCS_DIR,
+        '--vhost', hostname, script_path],
+        env={'DJANGO_SETTINGS_MODULE': 'wcs.settings'})
 
 
 def setup_module(module):
     global WCS_PID
 
-    if not WCSCTL:
+    if not WCS_MANAGE:
         return
 
     for hostname in ['127.0.0.1', '127.0.0.2']:
@@ -220,10 +222,10 @@ ALLOWED_HOSTS = ['127.0.0.1', '127.0.0.2']
 
     WCS_PID = os.fork()
     if not WCS_PID:
-        os.chdir(os.path.dirname(WCSCTL))
+        os.chdir(os.path.dirname(WCS_MANAGE))
         os.environ['DJANGO_SETTINGS_MODULE'] = 'wcs.settings'
         os.environ['WCS_SETTINGS_FILE'] = os.path.join(WCS_DIR, 'local_settings.py')
-        os.execvp('python', ['python', 'manage.py', 'runserver', '--noreload', '0.0.0.0:8999'])
+        os.execvp('python', [sys.executable, 'manage.py', 'runserver', '--noreload', '0.0.0.0:8999'])
         sys.exit(0)
 
     time.sleep(5)
@@ -251,7 +253,7 @@ def context():
     ctx['request'].session = {}
     return ctx
 
-@wcsctl_present
+@wcs_present
 def test_form_cell_setup():
     cell = WcsFormCell()
     form_class = cell.get_default_form_class()
@@ -267,7 +269,7 @@ def test_form_cell_setup():
             (u'other:third-form-title', u'test2 : third form title')]
 
 
-@wcsctl_present
+@wcs_present
 def test_form_cell_save_cache():
     page = Page(title='xxx', slug='test_form_cell_save_cache', template_name='standard')
     page.save()
@@ -295,7 +297,7 @@ def test_form_cell_save_cache():
     appconfig.update_db_cache()
     assert WcsFormCell.objects.get(id=cell.id).cached_title == 'form title'
 
-@wcsctl_present
+@wcs_present
 def test_form_cell_load():
     page = Page(title='xxx', slug='test_form_cell_save_cache', template_name='standard')
     page.save()
@@ -313,7 +315,7 @@ def test_form_cell_load():
     assert cell.cached_title == 'form title'
 
 
-@wcsctl_present
+@wcs_present
 def test_category_cell_save_cache():
     page = Page(title='xxx', slug='test_category_cell_save_cache', template_name='standard')
     page.save()
@@ -324,7 +326,7 @@ def test_category_cell_save_cache():
     assert cell.cached_title == 'Test 3'
     assert cell.get_additional_label() == 'Test 3'
 
-@wcsctl_present
+@wcs_present
 def test_form_cell_render():
     page = Page(title='xxx', slug='test_form_cell_render', template_name='standard')
     page.save()
@@ -335,7 +337,7 @@ def test_form_cell_render():
     assert 'http://127.0.0.1:8999/form-title/tryauth' in result
     assert 'form title' in result
 
-@wcsctl_present
+@wcs_present
 def test_current_forms_cell_setup():
     cell = WcsCurrentFormsCell()
     form_class = cell.get_default_form_class()
@@ -356,7 +358,7 @@ def test_current_forms_cell_setup():
     cell.done_forms = True
     assert cell.get_additional_label() == 'All Sites - Done Forms'
 
-@wcsctl_present
+@wcs_present
 def test_current_forms_cell_render(context):
     page = Page(title='xxx', slug='test_current_forms_cell_render', template_name='standard')
     page.save()
@@ -418,7 +420,7 @@ def test_current_forms_cell_render(context):
     extra_context = cell.get_cell_extra_context(context)
     assert len(extra_context['forms']) == 0
 
-@wcsctl_present
+@wcs_present
 def test_current_forms_cell_render_single_site(context):
     page = Page(title='xxx', slug='test_current_forms_cell_render', template_name='standard')
     page.save()
@@ -441,7 +443,7 @@ def test_current_forms_cell_render_single_site(context):
     assert 'http://127.0.0.2:8999/form-title/1/' not in result
     assert 'http://127.0.0.2:8999/form-title/22/' not in result
 
-@wcsctl_present
+@wcs_present
 def test_current_forms_unknown_name_id(caplog, context):
     page = Page(title='xxx', slug='test_current_forms_cell_render', template_name='standard')
     page.save()
@@ -462,7 +464,7 @@ def test_current_forms_unknown_name_id(caplog, context):
     assert 'http://127.0.0.1:8999/' not in result
     assert len(caplog.records) == 0
 
-@wcsctl_present
+@wcs_present
 def test_forms_of_category_cell_setup():
     cell = WcsFormsOfCategoryCell()
     form_class = cell.get_default_form_class()
@@ -473,7 +475,7 @@ def test_forms_of_category_cell_setup():
             (u'other:test-3', u'test2 : Test 3'),
             (u'other:test-9', u'test2 : Test 9')]
 
-@wcsctl_present
+@wcs_present
 def test_forms_of_category_cell_render(context):
     page = Page(title='xxx', slug='test_forms_of_category_cell_render', template_name='standard')
     page.save()
@@ -519,7 +521,7 @@ def test_forms_of_category_cell_render(context):
     assert cell.render_for_search() == ''
     assert len(list(cell.get_external_links_data())) == 2
 
-@wcsctl_present
+@wcs_present
 def test_current_drafts_cell_render_unlogged(context):
     page = Page(title='xxx', slug='test_current_drafts_cell_render', template_name='standard')
     page.save()
@@ -529,7 +531,7 @@ def test_current_drafts_cell_render_unlogged(context):
     result = cell.render(context)
     assert not 'http://127.0.0.1:8999/third-form-title' in result # no form
 
-@wcsctl_present
+@wcs_present
 def test_current_drafts_cell_render_logged_in(context):
     page = Page(title='xxx', slug='test_current_drafts_cell_render', template_name='standard')
     page.save()
@@ -551,7 +553,7 @@ def test_current_drafts_cell_render_logged_in(context):
     assert len([x for x in extra_context['drafts'] if x['site_slug'] == 'default']) == 1
     assert len([x for x in extra_context['drafts'] if x['site_slug'] == 'other']) == 1
 
-@wcsctl_present
+@wcs_present
 def test_manager_forms_of_category_cell(app, admin_user):
     Page.objects.all().delete()
     page = Page(title='One', slug='one', template_name='standard')
@@ -571,7 +573,7 @@ def test_manager_forms_of_category_cell(app, admin_user):
     resp = resp.forms[0].submit()
     assert resp.status_int == 302
 
-@wcsctl_present
+@wcs_present
 def test_manager_current_forms(app, admin_user):
     Page.objects.all().delete()
     page = Page(title='One', slug='one', template_name='standard')
@@ -608,7 +610,7 @@ def test_manager_current_forms(app, admin_user):
         settings.KNOWN_SERVICES = temp_settings
 
 
-@wcsctl_present
+@wcs_present
 def test_tracking_code_cell(app):
     Page.objects.all().delete()
     page = Page(title='One', slug='index', template_name='standard')
@@ -678,7 +680,7 @@ def test_tracking_code_cell(app):
 
     resp = app.post(reverse('wcs-tracking-code'), params={'cell': cell.id}, status=400)
 
-@wcsctl_present
+@wcs_present
 def test_cell_assets(app, admin_user):
     page = Page(title='xxx', slug='test_cell_assets', template_name='standard')
     page.save()
@@ -710,7 +712,7 @@ def test_cell_assets(app, admin_user):
         assert u'>Logo — Test 9 (test)<' in resp.text
         assert u'>Picture — form title (test)<' in resp.text
 
-@wcsctl_present
+@wcs_present
 def test_tracking_code_search(app):
     assert len(app.get('/api/search/tracking-code/').json.get('data')) == 0
     assert len(app.get('/api/search/tracking-code/?q=123').json.get('data')) == 0
@@ -720,13 +722,13 @@ def test_tracking_code_search(app):
     assert len(app.get('/api/search/tracking-code/?q=BBCCDDFFG').json.get('data')) == 0
     assert len(app.get('/api/search/tracking-code/?q= cnphntfb').json.get('data')) == 1
 
-@wcsctl_present
+@wcs_present
 def test_wcs_search_engines(app):
     search_engines = engines.get_engines()
     assert 'tracking-code' in search_engines.keys()
     assert len([x for x in search_engines.keys() if x.startswith('formdata:')]) == 2
 
-@wcsctl_present
+@wcs_present
 def test_backoffice_submission_cell_render(context):
     page = Page(title='xxx', slug='test_backoffice_submission_cell_render', template_name='standard')
     page.save()
diff --git a/tox.ini b/tox.ini
index 71d9fec3..f56ff1e4 100644
--- a/tox.ini
+++ b/tox.ini
@@ -6,7 +6,7 @@ envlist = coverage-py2-pylint,py3
 usedevelop = True
 basepython = python2
 setenv =
-  py2: WCSCTL=wcs/wcsctl.py
+  py2: WCS_MANAGE=wcs/wcsctl.py
   DJANGO_SETTINGS_MODULE=combo.settings
   COMBO_SETTINGS_FILE=tests/settings.py
   coverage: COVERAGE=--cov-report xml --cov-report html --cov=combo/
-- 
2.20.1

