Projet

Général

Profil

0001-tests-add-tests-on-main-views.py-40816.patch

Nicolas Roche, 19 mars 2020 13:40

Télécharger (9,51 ko)

Voir les différences:

Subject: [PATCH 1/2] tests: add tests on main views.py (#40816)

 tests/conftest.py     |  44 +++++++++++
 tests/test_manager.py | 166 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 209 insertions(+), 1 deletion(-)
tests/conftest.py
46 46
    group = Group.objects.create(name='mail')
47 47
    user.groups.add(group)
48 48

  
49 49
    # define authorization of mail group on mail channel
50 50
    channel_roles = getattr(settings, 'CHANNEL_ROLES', {})
51 51
    mail_roles = channel_roles.setdefault('mail', [])
52 52
    mail_roles.append('mail')
53 53
    return group
54

  
55

  
56
@pytest.fixture
57
def phone_group(db, settings, user):
58
    from django.contrib.auth.models import Group
59

  
60
    # add mail group to default user
61
    group = Group.objects.create(name='phone')
62
    user.groups.add(group)
63

  
64
    # define authorization of phone group on phone channel
65
    channel_roles = getattr(settings, 'CHANNEL_ROLES', {})
66
    phone_roles = channel_roles.setdefault('phone', [])
67
    phone_roles.append('phone')
68
    return group
69

  
70

  
71
@pytest.fixture
72
def counter_group(db, settings, user):
73
    from django.contrib.auth.models import Group
74

  
75
    # add mail group to default user
76
    group = Group.objects.create(name='counter')
77
    user.groups.add(group)
78

  
79
    # define authorization of counter group on counter channel
80
    channel_roles = getattr(settings, 'CHANNEL_ROLES', {})
81
    counter_roles = channel_roles.setdefault('counter', [])
82
    counter_roles.append('counter')
83
    return group
84

  
85

  
86
@pytest.fixture
87
def kb_group(db, settings, user):
88
    from django.contrib.auth.models import Group
89

  
90
    # add mail group to default user
91
    group = Group.objects.create(name='kb')
92
    user.groups.add(group)
93

  
94
    # define authorization of kb group to manage then knowledge base
95
    kb_manage_roles = getattr(settings, 'KB_MANAGE_ROLES', [])
96
    kb_manage_roles.append('kb')
97
    return group
tests/test_manager.py
9 9
# This program is distributed in the hope that it will be useful,
10 10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 12
# GNU Affero General Public License for more details.
13 13
#
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17

  
17
import httmock
18
import mock
18 19
import pytest
19 20

  
21
from django.contrib.contenttypes.models import ContentType
22
from django.core.files.base import ContentFile
23

  
24
from welco.qualif.models import Association
25
from welco.sources.mail.models import Mail
26

  
27

  
20 28
def login(app, username='toto', password='toto'):
21 29
    login_page = app.get('/login/')
22 30
    login_form = login_page.forms[0]
23 31
    login_form['username'] = username
24 32
    login_form['password'] = password
25 33
    resp = login_form.submit()
26 34
    assert resp.status_int == 302
27 35
    return app
......
32 40
    return login(app)
33 41

  
34 42

  
35 43
def test_unlogged_access(app):
36 44
    # connect while not being logged in
37 45
    assert app.get('/', status=302).location.endswith('/login/?next=/')
38 46

  
39 47

  
48
def test_no_channel_access(logged_app):
49
    logged_app.get('/', status=403)
50

  
51

  
40 52
def test_access(logged_app, mail_group):
41 53
    resp = logged_app.get('/', status=302)
42 54
    assert resp.location == 'mail/'
43 55

  
44 56

  
45 57
def test_logout(logged_app):
46 58
    app = logged_app
47 59
    app.get('/logout/')
48 60
    assert app.get('/', status=302).location.endswith('/login/?next=/')
61

  
62

  
63
@mock.patch('welco.views.get_idps', return_value=[{'METADATA': '...'}])
64
@mock.patch('welco.views.resolve_url', return_value='foo-url')
65
def test_mellon_idp_redirections(mocked_resolv_url, mocked_get_idps, app):
66
    resp = app.get('/login/', status=302)
67
    assert resp.location == 'foo-url'
68
    resp = app.get('/login/?next=http://foo/?bar', status=302)
69
    assert resp.location == 'foo-url?next=http%3A//foo/%3Fbar'
70
    resp = app.get('/logout/', status=302)
71
    assert resp.location == 'foo-url'
72

  
73

  
74
def test_mail_view(app, user, mail_group):
75
    resp = app.get('/mail/', status=302)
76
    assert resp.location == '/login/?next=/mail/'
77
    app.set_user(user.username)
78
    resp = app.get('/mail/', status=200)
79
    assert resp.html.find('h2').text == 'Mails'
80

  
81

  
82
def test_no_channel_access_on_mail_view(app, user):
83
    app.set_user(user.username)
84
    app.get('/mail/', status=403)
85

  
86

  
87
def test_phone_view(app, user, phone_group):
88
    resp = app.get('/phone/', status=302)
89
    assert resp.location == '/login/?next=/phone/'
90
    app.set_user(user.username)
91
    resp = app.get('/phone/', status=200)
92
    assert resp.html.find('h2').text == 'Phone Call'
93

  
94

  
95
def test_counter_view(app, user, counter_group):
96
    resp = app.get('/counter/', status=302)
97
    assert resp.location == '/login/?next=/counter/'
98
    app.set_user(user.username)
99
    resp = app.get('/counter/', status=200)
100
    assert resp.html.find('h2').text == 'Counter'
101

  
102

  
103
def test_kb_view(app, user, kb_group):
104
    resp = app.get('/kb/', status=302)
105
    assert resp.location == '/login/?next=/kb/'
106
    app.set_user(user.username)
107
    resp = app.get('/kb/', status=200)
108
    assert resp.html.find('h2').text == 'Knowledge Base'
109

  
110

  
111
def test_wcs_summary_view(app, mail_group, user):
112
    mail = Mail.objects.create(content=ContentFile('foo', name='bar.txt'))
113
    source_type = ContentType.objects.get_for_model(Mail).pk
114

  
115
    resp = app.get(
116
        '/ajax/summary/%s/%s/?callback=spam' % (source_type, mail.pk),
117
        status=302)
118
    assert resp.location.startswith('/login/?next=')
119

  
120
    app.set_user(user.username)
121
    resp = app.get(
122
        '/ajax/summary/%s/%s/?callback=spam' % (source_type, mail.pk),
123
        status=200)
124
    assert resp.content_type == 'application/javascript'
125
    assert 'bar' in resp.text
126
    assert resp.text.startswith('spam({')
127

  
128

  
129
def test_remove_association_view(app, mail_group, user):
130
    mail = Mail.objects.create(content=ContentFile('foo', name='bar.txt'))
131
    source_type = ContentType.objects.get_for_model(Mail).pk
132
    association = Association.objects.create(
133
        source_type=ContentType.objects.get(id=source_type),
134
        source_pk=mail.pk)
135
    assert Association.objects.filter(id=association.pk).count() == 1
136

  
137
    resp = app.get('/ajax/remove-association/%s' % association.pk, status=302)
138
    assert resp.location.startswith('/login/?next=')
139

  
140
    app.set_user(user.username)
141
    resp = app.get('/ajax/remove-association/%s' % association.pk, status=302)
142
    assert resp.location == '/'
143
    assert Association.objects.filter(id=association.pk).count() == 0
144

  
145

  
146
def test_create_formdata_view(settings, app, mail_group, user):
147
    settings.KNOWN_SERVICES = {
148
        'wcs': {
149
            'demarches': {
150
                'url': 'http://wcs.example.net/',
151
            }
152
        }
153
    }
154
    mail = Mail.objects.create(content=ContentFile('foo', name='bar.txt'))
155
    source_type = ContentType.objects.get_for_model(Mail).pk
156
    association = Association.objects.create(
157
        source_type=ContentType.objects.get(id=source_type),
158
        source_pk=mail.pk)
159

  
160
    resp = app.get('/ajax/create-formdata/%s' % association.pk, status=302)
161
    assert resp.location.startswith('/login/?next=')
162

  
163
    app.set_user(user.username)
164
    resp = app.get('/ajax/create-formdata/%s' % association.pk, status=200)
165
    assert resp.content_type == 'application/json'
166
    assert resp.json == {'err': 1}
167

  
168
    resp = app.post('/ajax/create-formdata/%s' % association.pk, status=200)
169
    assert resp.json == {'err': 1, 'msg': "'NoneType' object has no attribute 'split'"}
170

  
171
    association.formdef_reference = 'demarches:bar'
172
    association.save()
173

  
174
    @httmock.urlmatch(netloc='wcs.example.net', path='/api/formdefs/bar/schema', method='GET')
175
    def response_get(url, request):
176
        headers = {'content-type': 'application/json'}
177
        content = {}
178
        return httmock.response(200, content, headers)
179

  
180
    @httmock.urlmatch(netloc='wcs.example.net', path='/api/formdefs/bar/submit', method='POST')
181
    def response_post(url, request):
182
        headers = {'content-type': 'application/json'}
183
        content = {
184
            'err': 0,
185
            'data': {
186
                'id': 42,
187
                'backoffice_url': 'http://example.net',
188
            }}
189
        return httmock.response(200, content, headers)
190

  
191
    with httmock.HTTMock(response_get, response_post):
192
        resp = app.post('/ajax/create-formdata/%s' % association.pk, status=200)
193
    assert resp.content_type == 'application/json'
194
    assert resp.json == {
195
        'result': 'ok',
196
        'url': 'http://wcs.example.net/backoffice/management/bar/42/',
197
    }
198

  
199

  
200
def test_menu_json_view(app, user, mail_group, phone_group, counter_group, kb_group):
201
    resp = app.get('/menu.json', status=302)
202
    assert resp.location.startswith('/login/?next=')
203

  
204
    app.set_user(user.username)
205
    resp = app.get('/menu.json', status=200)
206
    assert resp.content_type == 'application/json'
207
    assert sorted([x['label'] for x in resp.json]) == [
208
        'Call Center', 'Counter', 'Knowledge Base', 'Mails']
209

  
210
    resp = app.get('/menu.json?callback=foo', status=200)
211
    assert resp.content_type == 'application/javascript'
212
    assert resp.text.startswith('foo([{')
49
-