Project

General

Profile

0001-tests-share-login-and-admin_user-32876.patch

Benjamin Dauvergne, 09 May 2019 08:36 PM

Download (7.14 KB)

View differences:

Subject: [PATCH 1/2] tests: share login() and admin_user() (#32876)

 tests/conftest.py          |  7 +++++++
 tests/test_emails.py       | 16 ++++++++--------
 tests/test_manager.py      |  7 -------
 tests/test_matomo_views.py | 19 ++-----------------
 4 files changed, 17 insertions(+), 32 deletions(-)
tests/conftest.py
2 2

  
3 3
import django_webtest
4 4

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

  
5 7

  
6 8
@pytest.fixture
7 9
def app(request):
......
9 11
    wtm._patch_settings()
10 12
    request.addfinalizer(wtm._unpatch_settings)
11 13
    return django_webtest.DjangoTestApp()
14

  
15

  
16
@pytest.fixture
17
def admin_user(db):
18
    return User.objects.create_superuser('admin', email=None, password='admin')
tests/test_emails.py
95 95

  
96 96

  
97 97
def test_invalid_address(client, admin_user):
98
    client.post('/login/', {'username': 'admin', 'password': 'password'})
98
    client.post('/login/', {'username': 'admin', 'password': 'admin'})
99 99
    response = client.post('/emails/', {'default_from_email': 'foobar'})
100 100
    assert response.status_code == 200
101 101
    assert 'Enter a valid email address' in response.content
102 102

  
103 103

  
104 104
def test_unkown_address(client, admin_user, dns_resolver, smtp_server):
105
    client.post('/login/', {'username': 'admin', 'password': 'password'})
105
    client.post('/login/', {'username': 'admin', 'password': 'admin'})
106 106
    response = client.post('/emails/', {'default_from_email': 'john.doe@unknown.com'})
107 107
    assert response.status_code == 200
108 108
    assert 'Email address not found' in response.content
109 109

  
110 110

  
111 111
def test_kown_address_nospf(client, admin_user, dns_resolver, smtp_server):
112
    client.post('/login/', {'username': 'admin', 'password': 'password'})
112
    client.post('/login/', {'username': 'admin', 'password': 'admin'})
113 113
    response = client.post('/emails/', {'default_from_email': 'john.doe@example.com'}, follow=True)
114 114
    assert response.status_code == 200
115 115
    assert 'Emails settings have been updated. It will take a few seconds to be effective.' in response.content
116 116

  
117 117

  
118 118
def test_spf_allow_all_mail(client, admin_user, dns_resolver, smtp_server, settings):
119
    client.post('/login/', {'username': 'admin', 'password': 'password'})
119
    client.post('/login/', {'username': 'admin', 'password': 'admin'})
120 120
    response = client.post('/emails/', {'default_from_email': 'john.doe@example-spf-allow-all.com'}, follow=True)
121 121
    assert response.status_code == 200
122 122
    assert 'Emails settings have been updated. It will take a few seconds to be effective.' in response.content
......
124 124

  
125 125
def test_invalid_spf(client, admin_user, dns_resolver, smtp_server, settings):
126 126
    settings.ALLOWED_SPF_RECORDS = ['include:example.com']
127
    client.post('/login/', {'username': 'admin', 'password': 'password'})
127
    client.post('/login/', {'username': 'admin', 'password': 'admin'})
128 128
    response = client.post('/emails/', {'default_from_email': 'john.doe@example-invalid-spf.com'})
129 129
    assert response.status_code == 200
130 130
    assert 'No suitable SPF record found' in response.content
......
133 133
def test_strict_nospf(client, admin_user, dns_resolver, smtp_server, monkeypatch, settings):
134 134
    settings.ALLOWED_SPF_RECORDS = ['include:allowed_mx.com']
135 135
    monkeypatch.setattr('hobo.emails.validators.validate_email_spf.__defaults__', (True,))
136
    client.post('/login/', {'username': 'admin', 'password': 'password'})
136
    client.post('/login/', {'username': 'admin', 'password': 'admin'})
137 137
    response = client.post('/emails/', {'default_from_email': 'john.doe@example.com'}, follow=True)
138 138
    assert response.status_code == 200
139 139
    assert 'No suitable SPF record found' in response.content
......
141 141

  
142 142
def test_valid_spf(client, admin_user, dns_resolver, smtp_server, settings):
143 143
    settings.ALLOWED_SPF_RECORDS = ['include:allowed_mx.com']
144
    client.post('/login/', {'username': 'admin', 'password': 'password'})
144
    client.post('/login/', {'username': 'admin', 'password': 'admin'})
145 145
    response = client.post('/emails/', {'default_from_email': 'john.doe@example-spf.com'}, follow=True)
146 146
    assert response.status_code == 200
147 147
    assert 'Emails settings have been updated. It will take a few seconds to be effective.' in response.content
......
149 149

  
150 150
def test_no_spf_validation(client, admin_user, dns_resolver, smtp_server, settings):
151 151
    settings.ALLOWED_SPF_RECORDS = []
152
    client.post('/login/', {'username': 'admin', 'password': 'password'})
152
    client.post('/login/', {'username': 'admin', 'password': 'admin'})
153 153
    response = client.post('/emails/', {'default_from_email': 'john.doe@example-invalid-spf.com'}, follow=True)
154 154
    assert response.status_code == 200
155 155
    assert 'Emails settings have been updated. It will take a few seconds to be effective.' in response.content
tests/test_manager.py
12 12

  
13 13
pytestmark = pytest.mark.django_db
14 14

  
15
@pytest.fixture
16
def admin_user():
17
    try:
18
        user = User.objects.get(username='admin')
19
    except User.DoesNotExist:
20
        user = User.objects.create_superuser('admin', email=None, password='admin')
21
    return user
22 15

  
23 16
def login(app, username='admin', password='admin'):
24 17
    login_page = app.get('/login/')
tests/test_matomo_views.py
13 13
from hobo.environment.models import Variable, Wcs, Combo, Fargo
14 14
from hobo.wsgi import application
15 15

  
16
from test_manager import login
17

  
16 18
pytestmark = pytest.mark.django_db
17 19

  
18 20
CONFIG = {'URL': 'https://matomo.test',
......
62 64
</result>
63 65
"""
64 66

  
65
@pytest.fixture
66
def admin_user():
67
    try:
68
        user = User.objects.get(username='admin')
69
    except User.DoesNotExist:
70
        user = User.objects.create_superuser('admin', email=None, password='admin')
71
    return user
72

  
73
def login(app, username='admin', password='admin'):
74
    login_page = app.get('/login/')
75
    login_form = login_page.forms[0]
76
    login_form['username'] = username
77
    login_form['password'] = password
78
    resp = login_form.submit()
79
    assert resp.status_int == 302
80
    return app
81

  
82 67
def test_unlogged_access():
83 68
    # connect while not being logged in
84 69
    app = TestApp(application)
85
-