From a65875cdbedb2139eb7d38f9954f574b3d45f0d8 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Sun, 23 Feb 2020 20:13:06 +0100
Subject: [PATCH 1/3] tests: add test to theme views (#40098)

---
 tests/conftest.py         | 42 ++++++++++++++++++++++++++++
 tests/test_hobo_deploy.py | 40 --------------------------
 tests/test_theme.py       | 59 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 101 insertions(+), 40 deletions(-)
 create mode 100644 tests/test_theme.py

diff --git a/tests/conftest.py b/tests/conftest.py
index deb8447..5b544c3 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,8 +1,9 @@
+import os
 import pytest
 
 import django_webtest
 
 from django.contrib.auth.models import User
 
 
 @pytest.fixture
@@ -11,8 +12,49 @@ def app(request):
     wtm._patch_settings()
     request.addfinalizer(wtm._unpatch_settings)
     return django_webtest.DjangoTestApp()
 
 
 @pytest.fixture
 def admin_user(db):
     return User.objects.create_superuser('admin', email=None, password='password')
+
+
+@pytest.fixture
+def fake_themes(settings, tmpdir):
+    THEMES="""
+[
+  {
+    "id": "alfortville",
+    "label": "Alfortville",
+    "variables": {
+      "css_variant": "alfortville",
+      "no_extra_js": false,
+      "theme_color": "#FFFFFF",
+      "foo": "bar"
+    },
+    "overlay": "foobar"
+  },
+  {
+    "id": "publik",
+    "label": "Publik",
+    "variables": {
+      "css_variant": "publik",
+      "no_extra_js": false,
+      "theme_color": "#E80E89"
+    }
+  }
+]
+"""
+    base_dir = str(tmpdir.mkdir('themes'))
+    settings.THEMES_DIRECTORY = base_dir
+
+    themes_dir = os.path.join(base_dir, 'publik-base')
+    os.mkdir(themes_dir)
+    with open(os.path.join(themes_dir, 'themes.json'), 'w') as handler:
+        handler.write(THEMES)
+
+    # populate 'foobar' overlay
+    themes_dir = os.path.join(base_dir, 'foobar')
+    os.mkdir(themes_dir)
+    for part in ('static', 'templates'):
+        os.mkdir(os.path.join(themes_dir, part))
diff --git a/tests/test_hobo_deploy.py b/tests/test_hobo_deploy.py
index 2b8fc95..efcacba 100644
--- a/tests/test_hobo_deploy.py
+++ b/tests/test_hobo_deploy.py
@@ -11,56 +11,16 @@ from requests import Response, exceptions
 
 from hobo.agent.common.management.commands.hobo_deploy import (
     replace_file, Command, CommandError)
 from hobo.agent.hobo.management.commands.hobo_deploy import Command as HoboCommand
 from hobo.environment.models import Variable, Combo, Hobo, Wcs
 from hobo.multitenant.middleware import TenantNotFound
 
 
-@pytest.fixture
-def fake_themes(settings, tmpdir):
-    THEMES="""
-[
-  {
-    "id": "alfortville",
-    "label": "Alfortville",
-    "variables": {
-      "css_variant": "alfortville",
-      "no_extra_js": false,
-      "theme_color": "#804697"
-    },
-    "overlay": "foobar"
-  },
-  {
-    "id": "publik",
-    "label": "Publik",
-    "variables": {
-      "css_variant": "publik",
-      "no_extra_js": false,
-      "theme_color": "#E80E89"
-    }
-  }
-]
-"""
-    base_dir = str(tmpdir.mkdir('themes'))
-    settings.THEMES_DIRECTORY = base_dir
-
-    themes_dir = os.path.join(base_dir, 'publik-base')
-    os.mkdir(themes_dir)
-    with open(os.path.join(themes_dir, 'themes.json'), 'w') as handler:
-        handler.write(THEMES)
-
-    # populate 'foobar' overlay
-    themes_dir = os.path.join(base_dir, 'foobar')
-    os.mkdir(themes_dir)
-    for part in ('static', 'templates'):
-        os.mkdir(os.path.join(themes_dir, part))
-
-
 def test_replace_file(tmpdir):
     path = str(tmpdir) + '/my_file.txt'
 
     content = 'content of my new file'
     replace_file(path, content)
     with open(path, 'r') as handler:
         assert handler.read() == content
 
diff --git a/tests/test_theme.py b/tests/test_theme.py
new file mode 100644
index 0000000..792d421
--- /dev/null
+++ b/tests/test_theme.py
@@ -0,0 +1,59 @@
+# -*- coding: utf-8 -*-
+from mock import patch
+import pytest
+
+from django.core.exceptions import ValidationError
+
+from hobo.environment.models import Variable
+
+from test_manager import login
+
+pytestmark = pytest.mark.django_db
+
+
+@patch('hobo.theme.views.random.random', return_value=0.1)
+def test_theme_view(mocked_random, app, admin_user, fake_themes):
+    app = login(app)
+    resp = app.get('/theme').follow()
+    assert [x['value'] for x in resp.html.findAll('input', {'type': 'radio'})] == ['alfortville', 'publik']
+    resp.form['theme'].value = 'alfortville'
+    resp = resp.form.submit()
+    assert Variable.objects.filter(name='theme')[0].value == 'alfortville'
+    assert Variable.objects.filter(name='foo')[0].value == 'bar'
+    assert resp.location == '/theme/'
+    assert "The theme has been changed" in dict(resp.headers)['Set-Cookie']
+    resp = resp.follow()
+    assert resp.form['theme'].value == 'alfortville'
+
+    resp.form['theme'].value = 'publik'
+    resp = resp.form.submit()
+    assert resp.location == '/theme/'
+    assert Variable.objects.filter(name='theme')[0].value == 'publik'
+    assert Variable.objects.filter(name='foo')[0].value == ''
+
+    # easter egg, sometimes it gets sorted by colour
+    mocked_random.return_value = 0.09
+    resp = app.get('/theme').follow()
+    assert [x['value'] for x in resp.html.findAll('input', {'type': 'radio'})] == ['publik', 'alfortville']
+
+
+def test_thme_view_empty(app, admin_user, settings):
+    del settings.THEMES_DIRECTORY
+    app = login(app)
+    resp = app.get('/theme').follow()
+    assert 'Theme' in resp.text
+
+
+def test_theme_option_view(app, admin_user):
+    app = login(app)
+    resp = app.get('/theme').follow()
+    resp = resp.click('Options')
+    assert resp.html.find('label').text == 'Global Title:'
+    resp.form['global_title'] = 'foo'
+    resp = resp.form.submit()
+    assert Variable.objects.all()[0].name == 'global_title'
+    assert Variable.objects.all()[0].value == 'foo'
+
+    assert resp.location == '.'
+    resp = resp.follow()
+    assert resp.html.find('li').text == 'foo'
-- 
2.25.0

