Projet

Général

Profil

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

Nicolas Roche, 18 mars 2020 17:31

Télécharger (9,52 ko)

Voir les différences:

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

 tests/conftest.py     |  44 +++++++++++
 tests/test_manager.py | 165 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 208 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.objects.create(content=ContentFile('foo', name='bar.txt'))
113
    pk = Mail.objects.get().pk
114
    source_type = ContentType.objects.get_for_model(Mail).pk
115

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

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

  
125

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

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

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

  
143

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

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

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

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

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

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

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

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

  
198

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

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

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