tests: add test to theme views (#40098)

This commit is contained in:
Nicolas Roche 2020-02-23 20:13:06 +01:00
parent 314123d243
commit 748be27d26
3 changed files with 101 additions and 40 deletions

View File

@ -1,3 +1,4 @@
import os
import pytest
import django_webtest
@ -16,3 +17,44 @@ def app(request):
@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))

View File

@ -18,46 +18,6 @@ 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'

59
tests/test_theme.py Normal file
View File

@ -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'