Projet

Général

Profil

0001-tests-make-a-fixture-of-EmailsMocking-fixes-15555.patch

Benjamin Dauvergne, 24 mars 2017 10:54

Télécharger (7,97 ko)

Voir les différences:

Subject: [PATCH] tests: make a fixture of EmailsMocking (fixes #15555)

 tests/conftest.py              |  8 ++++++++
 tests/test_backoffice_pages.py |  6 +++---
 tests/test_form_pages.py       |  7 +++----
 tests/test_register.py         |  8 ++++----
 tests/test_workflows.py        |  8 ++++----
 tests/utilities.py             | 25 +++++++++++++++++--------
 6 files changed, 39 insertions(+), 23 deletions(-)
tests/conftest.py
3 3

  
4 4
import pytest
5 5

  
6
from utilities import EmailsMocking
7

  
6 8

  
7 9
def pytest_addoption(parser):
8 10
    parser.addoption('--without-postgresql-tests', action='store_true',
......
49 51
@pytest.fixture
50 52
def welco_url(request, pub):
51 53
    return site_options(request, pub, 'options', 'welco_url', 'http://welco.example.net')
54

  
55

  
56
@pytest.fixture
57
def emails():
58
    with EmailsMocking() as mock:
59
        yield mock
tests/test_backoffice_pages.py
32 32
from wcs import fields
33 33

  
34 34
from utilities import (get_app, login, create_temporary_pub,
35
        clean_temporary_pub, emails, sms_mocking)
35
        clean_temporary_pub, sms_mocking)
36

  
36 37

  
37 38
def pytest_generate_tests(metafunc):
38 39
    if 'pub' in metafunc.fixturenames:
......
2787 2788
    resp = app.get('/backoffice/management/users/%s/' % user.id)
2788 2789
    assert resp.body.count('<li') == (count_li - 2)
2789 2790

  
2790
def test_360_user_view_tracking_code(pub):
2791
def test_360_user_view_tracking_code(pub, emails):
2791 2792
    if not pub.is_using_postgresql():
2792 2793
        pytest.skip('this requires SQL')
2793 2794
        return
2794 2795

  
2795
    emails.empty()
2796 2796
    sms_mocking.empty()
2797 2797

  
2798 2798
    user = create_user(pub)
tests/test_form_pages.py
32 32
from wcs import fields
33 33
from wcs.sessions import BasicSession
34 34

  
35
from utilities import get_app, login, create_temporary_pub, clean_temporary_pub, emails
35
from utilities import get_app, login, create_temporary_pub, clean_temporary_pub
36 36

  
37 37

  
38 38
def assert_equal_zip(stream1, stream2):
......
809 809
    resp = resp.forms[0].submit('submit')
810 810
    assert 'Check values then click submit.' in resp.body
811 811

  
812
def test_form_submit_with_user(pub):
812
def test_form_submit_with_user(pub, emails):
813 813
    create_user(pub)
814 814
    formdef = create_formdef()
815 815
    page = login(get_app(pub), username='foo', password='foo').get('/test/')
......
1053 1053
    assert resp.location == 'http://example.net/code/%s/load' % tracking_code
1054 1054
    resp = resp.follow(status=404)
1055 1055

  
1056
def test_form_tracking_code_email(pub):
1057
    emails.empty()
1056
def test_form_tracking_code_email(pub, emails):
1058 1057
    formdef = create_formdef()
1059 1058
    formdef.data_class().wipe()
1060 1059
    formdef.fields = [fields.StringField(id='0', label='string')]
tests/test_register.py
7 7
from wcs.qommon.http_request import HTTPRequest
8 8
from wcs.qommon.ident.password_accounts import PasswordAccount
9 9

  
10
from utilities import get_app, create_temporary_pub, clean_temporary_pub, emails
10
from utilities import get_app, create_temporary_pub, clean_temporary_pub
11 11

  
12 12
def pytest_generate_tests(metafunc):
13 13
    if 'pub' in metafunc.fixturenames:
......
117 117
    account = PasswordAccount.get('foo')
118 118
    assert account.password == password
119 119

  
120
def test_admin_notification(pub):
120
def test_admin_notification(pub, emails):
121 121
    pub.cfg['identities'] = {'creation': 'self', 'notify-on-register': True}
122 122
    pub.write_cfg()
123 123
    pub.user_class.wipe()
......
134 134
    assert emails.get('New Registration')
135 135
    assert emails.get('New Registration').get('email_rcpt') == ['admin@localhost']
136 136

  
137
def test_user_notification(pub):
137
def test_user_notification(pub, emails):
138 138
    pub.cfg['identities'] = {'creation': 'self', 'notify-on-register': False,
139 139
            'email-as-username': True}
140 140
    pub.write_cfg()
......
179 179
    resp = resp.forms[0].submit()
180 180
    assert resp.location == 'http://example.net/'
181 181

  
182
def test_forgotten(pub):
182
def test_forgotten(pub, emails):
183 183
    pub.cfg['identities'] = {'creation': 'self', 'notify-on-register': False}
184 184
    pub.user_class.wipe()
185 185
    PasswordAccount.wipe()
tests/test_workflows.py
37 37
from wcs.wf.backoffice_fields import SetBackofficeFieldsWorkflowStatusItem
38 38
from wcs.wf.redirect_to_url import RedirectToUrlWorkflowStatusItem
39 39

  
40
from utilities import (create_temporary_pub, MockSubstitutionVariables, emails,
40
from utilities import (create_temporary_pub, MockSubstitutionVariables,
41 41
        http_requests, clean_temporary_pub, sms_mocking)
42 42

  
43

  
43 44
def setup_module(module):
44 45
    cleanup()
45 46

  
......
558 559
    assert len(os.listdir(os.path.join(get_publisher().app_dir, 'attachments'))) == 1
559 560
    assert url1 == url2
560 561

  
561
def test_email(pub):
562
def test_email(pub, emails):
562 563
    pub.substitutions.feed(MockSubstitutionVariables())
563 564

  
564 565
    formdef = FormDef()
......
583 584
    role2.emails = ['bar@localhost', 'baz@localhost']
584 585
    role2.store()
585 586

  
586
    emails.empty()
587 587
    # send using an uncompleted element
588 588
    item = SendmailWorkflowStatusItem()
589 589
    item.perform(formdata) # nothing
......
1492 1492
    assert 'd1' in formdata.get_workflow_messages()
1493 1493
    assert 'd2' in formdata.get_workflow_messages()
1494 1494

  
1495
def test_workflow_roles(pub):
1495
def test_workflow_roles(pub, emails):
1496 1496
    pub.substitutions.feed(MockSubstitutionVariables())
1497 1497

  
1498 1498
    user = pub.user_class(name='foo')
tests/utilities.py
175 175
    assert resp.status_int == 302
176 176
    return app
177 177

  
178
class EmailsMocking(object):
179
    def __init__(self):
180
        self.emails = {}
181
        import wcs.qommon.emails
182
        import qommon.emails
183
        wcs.qommon.emails.create_smtp_server = self.create_smtp_server
184
        qommon.emails.create_smtp_server = self.create_smtp_server
185 178

  
179
class EmailsMocking(object):
186 180
    def create_smtp_server(self, *args, **kwargs):
187 181
        class MockSmtplibSMTP(object):
188 182
            def __init__(self, emails):
......
216 210
    def count(self):
217 211
        return len(self.emails)
218 212

  
219
emails = EmailsMocking()
213
    def __enter__(self):
214
        import wcs.qommon.emails
215
        import qommon.emails
216
        self.wcs_create_smtp_server = wcs.qommon.emails.create_smtp_server
217
        self.qommon_create_smtp_server = qommon.emails.create_smtp_server
218

  
219
        wcs.qommon.emails.create_smtp_server = self.create_smtp_server
220
        qommon.emails.create_smtp_server = self.create_smtp_server
221

  
222
        self.emails = {}
223
        return self
224

  
225
    def __exit__(self, exc_type, exc_value, tb):
226
        del self.emails
227
        wcs.qommon.emails.create_smtp_server = self.wcs_create_smtp_server
228
        qommon.emails.create_smtp_server = self.qommon_create_smtp_server
220 229

  
221 230

  
222 231
class MockSubstitutionVariables(object):
223
-