Projet

Général

Profil

0004-tests-add-test-to-home-views-40098.patch

Nicolas Roche, 28 février 2020 12:37

Télécharger (3,47 ko)

Voir les différences:

Subject: [PATCH 4/6] tests: add test to home views (#40098)

 tests/test_home_views.py | 98 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 98 insertions(+)
 create mode 100644 tests/test_home_views.py
tests/test_home_views.py
1
# -*- coding: utf-8 -*-
2
import json
3
import mock
4
import pytest
5
import re
6

  
7
from django.contrib.auth.models import User
8

  
9
from hobo.environment.models import Authentic
10

  
11
from test_manager import login
12

  
13
pytestmark = pytest.mark.django_db
14

  
15

  
16
@pytest.fixture
17
def user(db):
18
    return User.objects.create_user('jhon', email='jhon@doe.love', password='xxx')
19

  
20
def test_home_view(app, admin_user):
21
    app = login(app)
22
    resp = app.get('/')
23
    assert resp.html.find('h1').text == 'System'
24
    assert resp.html.find('h2').text == 'System'
25

  
26
def test_home_view_with_normal_user(app, user):
27
    app = login(app, 'jhon', 'xxx')
28
    app.get('/', status=403)
29

  
30
def test_home_view_with_not_auth(app):
31
    resp = app.get('/')
32
    assert resp.location == '/login/?next=/'
33

  
34
def test_hobo_view(app, user):
35
    app = login(app, 'jhon', 'xxx')
36
    resp = app.get('/hobos.json')
37
    assert resp.json == ['http://testserver/']
38

  
39
def test_idp_login(app, user):
40
    Authentic.objects.create(
41
        title='bar',
42
        slug='bar',
43
        base_url='http://bar.example.net',
44
        use_as_idp_for_self=True,
45
        last_operational_success_timestamp='2022-2-22',
46
        last_operational_check_timestamp='2022-2-22'
47
    )
48
    resp = app.get('/login/')
49
    assert resp.location == '/accounts/mellon/login/?'
50

  
51
@mock.patch('hobo.views.auth_logout')
52
def test_logout_view(mocked_logout, app):
53
    resp = app.get('/logout/')
54
    assert mocked_logout.called
55
    assert resp.location == 'http://testserver/'
56

  
57
    Authentic.objects.create(
58
        title='bar',
59
        slug='bar',
60
        base_url='http://bar.example.net',
61
        use_as_idp_for_self=True,
62
        last_operational_success_timestamp='2022-2-22',
63
        last_operational_check_timestamp='2022-2-22'
64
    )
65
    resp = app.get('/logout/')
66
    assert resp.location == '/accounts/mellon/logout/?'
67

  
68
def test_healt_view(app):
69
    resp = app.get('/api/health/')
70
    assert resp.json == {'data': {}}
71
    Authentic.objects.create(
72
        title='bar',
73
        slug='bar',
74
        base_url='http://bar.example.net',
75
        last_operational_success_timestamp='2022-2-22',
76
        last_operational_check_timestamp='2022-2-22'
77
    )
78
    resp = app.get('/api/health/')
79
    assert resp.json == {
80
        'data': {
81
            'bar': {
82
                'has_valid_certificate': False,
83
                'is_operational': True,
84
                'is_resolvable': False,
85
                'is_running': False
86
            }}}
87

  
88
def test_menu_view(app, admin_user):
89
    expected = [{"slug": "system", "label": "System", "url": "http://testserver/"}]
90
    app = login(app)
91
    resp = app.get('/menu.json')
92
    assert resp.content_type == 'application/json'
93
    assert resp.json == expected
94

  
95
    resp = app.get('/menu.json?callback=foo')
96
    assert resp.content_type == 'application/javascript'
97
    json_str = re.match(r'foo\((.*)\)', resp.text).group(1)
98
    assert json.loads(json_str) == expected
0
-