Projet

Général

Profil

0001-tests-add-test-to-theme-views-40098.patch

Nicolas Roche, 24 février 2020 10:15

Télécharger (5,9 ko)

Voir les différences:

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
tests/conftest.py
1
import os
1 2
import pytest
2 3

  
3 4
import django_webtest
4 5

  
5 6
from django.contrib.auth.models import User
6 7

  
7 8

  
8 9
@pytest.fixture
......
11 12
    wtm._patch_settings()
12 13
    request.addfinalizer(wtm._unpatch_settings)
13 14
    return django_webtest.DjangoTestApp()
14 15

  
15 16

  
16 17
@pytest.fixture
17 18
def admin_user(db):
18 19
    return User.objects.create_superuser('admin', email=None, password='password')
20

  
21

  
22
@pytest.fixture
23
def fake_themes(settings, tmpdir):
24
    THEMES="""
25
[
26
  {
27
    "id": "alfortville",
28
    "label": "Alfortville",
29
    "variables": {
30
      "css_variant": "alfortville",
31
      "no_extra_js": false,
32
      "theme_color": "#FFFFFF",
33
      "foo": "bar"
34
    },
35
    "overlay": "foobar"
36
  },
37
  {
38
    "id": "publik",
39
    "label": "Publik",
40
    "variables": {
41
      "css_variant": "publik",
42
      "no_extra_js": false,
43
      "theme_color": "#E80E89"
44
    }
45
  }
46
]
47
"""
48
    base_dir = str(tmpdir.mkdir('themes'))
49
    settings.THEMES_DIRECTORY = base_dir
50

  
51
    themes_dir = os.path.join(base_dir, 'publik-base')
52
    os.mkdir(themes_dir)
53
    with open(os.path.join(themes_dir, 'themes.json'), 'w') as handler:
54
        handler.write(THEMES)
55

  
56
    # populate 'foobar' overlay
57
    themes_dir = os.path.join(base_dir, 'foobar')
58
    os.mkdir(themes_dir)
59
    for part in ('static', 'templates'):
60
        os.mkdir(os.path.join(themes_dir, part))
tests/test_hobo_deploy.py
11 11

  
12 12
from hobo.agent.common.management.commands.hobo_deploy import (
13 13
    replace_file, Command, CommandError)
14 14
from hobo.agent.hobo.management.commands.hobo_deploy import Command as HoboCommand
15 15
from hobo.environment.models import Variable, Combo, Hobo, Wcs
16 16
from hobo.multitenant.middleware import TenantNotFound
17 17

  
18 18

  
19
@pytest.fixture
20
def fake_themes(settings, tmpdir):
21
    THEMES="""
22
[
23
  {
24
    "id": "alfortville",
25
    "label": "Alfortville",
26
    "variables": {
27
      "css_variant": "alfortville",
28
      "no_extra_js": false,
29
      "theme_color": "#804697"
30
    },
31
    "overlay": "foobar"
32
  },
33
  {
34
    "id": "publik",
35
    "label": "Publik",
36
    "variables": {
37
      "css_variant": "publik",
38
      "no_extra_js": false,
39
      "theme_color": "#E80E89"
40
    }
41
  }
42
]
43
"""
44
    base_dir = str(tmpdir.mkdir('themes'))
45
    settings.THEMES_DIRECTORY = base_dir
46

  
47
    themes_dir = os.path.join(base_dir, 'publik-base')
48
    os.mkdir(themes_dir)
49
    with open(os.path.join(themes_dir, 'themes.json'), 'w') as handler:
50
        handler.write(THEMES)
51

  
52
    # populate 'foobar' overlay
53
    themes_dir = os.path.join(base_dir, 'foobar')
54
    os.mkdir(themes_dir)
55
    for part in ('static', 'templates'):
56
        os.mkdir(os.path.join(themes_dir, part))
57

  
58

  
59 19
def test_replace_file(tmpdir):
60 20
    path = str(tmpdir) + '/my_file.txt'
61 21

  
62 22
    content = 'content of my new file'
63 23
    replace_file(path, content)
64 24
    with open(path, 'r') as handler:
65 25
        assert handler.read() == content
66 26

  
tests/test_theme.py
1
# -*- coding: utf-8 -*-
2
from mock import patch
3
import pytest
4

  
5
from django.core.exceptions import ValidationError
6

  
7
from hobo.environment.models import Variable
8

  
9
from test_manager import login
10

  
11
pytestmark = pytest.mark.django_db
12

  
13

  
14
@patch('hobo.theme.views.random.random', return_value=0.1)
15
def test_theme_view(mocked_random, app, admin_user, fake_themes):
16
    app = login(app)
17
    resp = app.get('/theme').follow()
18
    assert [x['value'] for x in resp.html.findAll('input', {'type': 'radio'})] == ['alfortville', 'publik']
19
    resp.form['theme'].value = 'alfortville'
20
    resp = resp.form.submit()
21
    assert Variable.objects.filter(name='theme')[0].value == 'alfortville'
22
    assert Variable.objects.filter(name='foo')[0].value == 'bar'
23
    assert resp.location == '/theme/'
24
    assert "The theme has been changed" in dict(resp.headers)['Set-Cookie']
25
    resp = resp.follow()
26
    assert resp.form['theme'].value == 'alfortville'
27

  
28
    resp.form['theme'].value = 'publik'
29
    resp = resp.form.submit()
30
    assert resp.location == '/theme/'
31
    assert Variable.objects.filter(name='theme')[0].value == 'publik'
32
    assert Variable.objects.filter(name='foo')[0].value == ''
33

  
34
    # easter egg, sometimes it gets sorted by colour
35
    mocked_random.return_value = 0.09
36
    resp = app.get('/theme').follow()
37
    assert [x['value'] for x in resp.html.findAll('input', {'type': 'radio'})] == ['publik', 'alfortville']
38

  
39

  
40
def test_thme_view_empty(app, admin_user, settings):
41
    del settings.THEMES_DIRECTORY
42
    app = login(app)
43
    resp = app.get('/theme').follow()
44
    assert 'Theme' in resp.text
45

  
46

  
47
def test_theme_option_view(app, admin_user):
48
    app = login(app)
49
    resp = app.get('/theme').follow()
50
    resp = resp.click('Options')
51
    assert resp.html.find('label').text == 'Global Title:'
52
    resp.form['global_title'] = 'foo'
53
    resp = resp.form.submit()
54
    assert Variable.objects.all()[0].name == 'global_title'
55
    assert Variable.objects.all()[0].value == 'foo'
56

  
57
    assert resp.location == '.'
58
    resp = resp.follow()
59
    assert resp.html.find('li').text == 'foo'
0
-