|
1 |
# welco - multichannel request processing
|
|
2 |
# Copyright (C) 2020 Entr'ouvert
|
|
3 |
#
|
|
4 |
# This program is free software: you can redistribute it and/or modify it
|
|
5 |
# under the terms of the GNU Affero General Public License as published
|
|
6 |
# by the Free Software Foundation, either version 3 of the License, or
|
|
7 |
# (at your option) any later version.
|
|
8 |
#
|
|
9 |
# This program is distributed in the hope that it will be useful,
|
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
# GNU Affero General Public License for more details.
|
|
13 |
#
|
|
14 |
# You should have received a copy of the GNU Affero General Public License
|
|
15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
16 |
|
|
17 |
import httmock
|
|
18 |
import json
|
|
19 |
import requests
|
|
20 |
from webtest import Upload
|
|
21 |
|
|
22 |
from django.contrib.contenttypes.models import ContentType
|
|
23 |
from django.core.files.base import ContentFile
|
|
24 |
from django.utils.encoding import force_text
|
|
25 |
|
|
26 |
from welco.sources.mail.models import Mail
|
|
27 |
|
|
28 |
|
|
29 |
def test_viewer_view(app):
|
|
30 |
resp = app.get('/mail/viewer/', status=302)
|
|
31 |
assert resp.location == '?file='
|
|
32 |
resp = resp.follow()
|
|
33 |
assert resp.html.find('title').text == 'PDF.js viewer'
|
|
34 |
|
|
35 |
resp = app.get('/mail/viewer/', {'file': 'tests/test.pdf'}, status=200)
|
|
36 |
assert resp.html.find('title').text == 'PDF.js viewer'
|
|
37 |
|
|
38 |
|
|
39 |
def test_get_feeder_view(app, user):
|
|
40 |
resp = app.get('/mail/feeder/', status=302)
|
|
41 |
assert resp.location.startswith('/login/?next=')
|
|
42 |
app.set_user(user.username)
|
|
43 |
resp = app.get('/mail/feeder/', status=200)
|
|
44 |
assert resp.html.find('h2').text == 'Mail Feeder'
|
|
45 |
|
|
46 |
|
|
47 |
def test_post_feeder_view(app, user):
|
|
48 |
app.set_user(user.username)
|
|
49 |
resp = app.post(
|
|
50 |
'/mail/feeder/',
|
|
51 |
params={'mail': Upload('filename.txt', b'contents')},
|
|
52 |
status=302)
|
|
53 |
assert resp.location == '/mail/feeder/'
|
|
54 |
resp = resp.follow()
|
|
55 |
assert resp.html.find('li', {'class': 'info'}).text == '1 files uploaded successfully.'
|
|
56 |
|
|
57 |
|
|
58 |
def test_qualification_save_view(settings, app, db):
|
|
59 |
settings.KNOWN_SERVICES = {
|
|
60 |
'wcs': {
|
|
61 |
'demarches': {
|
|
62 |
'url': 'http://wcs.example.net/',
|
|
63 |
}
|
|
64 |
}
|
|
65 |
}
|
|
66 |
mail = Mail.objects.create(
|
|
67 |
content=ContentFile('foo', name='bar.txt'),
|
|
68 |
subject='spam')
|
|
69 |
assert not mail.contact_id
|
|
70 |
source_type = ContentType.objects.get_for_model(Mail).pk
|
|
71 |
resp = app.post(
|
|
72 |
'/ajax/qualification-mail-save',
|
|
73 |
params={'source_type': source_type, 'source_pk': mail.pk, 'subject': 'eggs'},
|
|
74 |
status=302)
|
|
75 |
assert resp.location == '/ajax/qualification?source_type=%s&source_pk=%s' % (
|
|
76 |
source_type, mail.pk)
|
|
77 |
|
|
78 |
@httmock.urlmatch(netloc='wcs.example.net', path='/api/formdefs/', method='GET')
|
|
79 |
def response_get(url, request):
|
|
80 |
headers = {'content-type': 'application/json'}
|
|
81 |
content = {
|
|
82 |
"err": 0,
|
|
83 |
"data": [{
|
|
84 |
"title": "Foo",
|
|
85 |
"slug": "foo",
|
|
86 |
}]}
|
|
87 |
return httmock.response(200, content, headers)
|
|
88 |
|
|
89 |
with httmock.HTTMock(response_get):
|
|
90 |
resp = resp.follow()
|
|
91 |
assert resp.html.find('option', {'value': 'demarches:foo'}).text == 'Foo'
|
|
92 |
assert Mail.objects.get(id=mail.pk).subject == 'eggs'
|
|
93 |
|
|
94 |
|
|
95 |
def test_edit_note_view(app, user):
|
|
96 |
resp = app.get('/ajax/mail/edit-note/', status=302)
|
|
97 |
assert resp.location.startswith('/login/?next=')
|
|
98 |
|
|
99 |
app.set_user(user.username)
|
|
100 |
mail = Mail.objects.create(
|
|
101 |
content=ContentFile('foo', name='bar.txt'),
|
|
102 |
note='spam')
|
|
103 |
resp = app.get('/ajax/mail/edit-note/', params={'mail': mail.pk}, status=200)
|
|
104 |
assert resp.html.find('h2').text == 'Note'
|
|
105 |
assert resp.html.find('textarea', {'name': 'note'}).text == 'spam'
|
|
106 |
|
|
107 |
resp.form['note'] = 'eggs'
|
|
108 |
resp = resp.form.submit()
|
|
109 |
assert resp.content_type == 'text/html'
|
|
110 |
assert resp.text == '{"result": "ok"}'
|
|
111 |
assert Mail.objects.get(id=mail.pk).note == 'eggs'
|
|
112 |
|
|
113 |
|
|
114 |
def test_note_view(app, user):
|
|
115 |
mail = Mail.objects.create(content=ContentFile('foo', name='bar.txt'))
|
|
116 |
resp = app.get('/ajax/mail/note/%s' % mail.pk, status=302)
|
|
117 |
assert resp.location.startswith('/login/?next=')
|
|
118 |
|
|
119 |
app.set_user(user.username)
|
|
120 |
resp = app.get('/ajax/mail/note/%s' % mail.pk, status=200)
|
|
121 |
assert resp.text == '+'
|
|
122 |
assert not Mail.objects.get(id=mail.pk).note # mail object is unchanged
|
|
123 |
|
|
124 |
|
|
125 |
def test_reject_view(settings, app, user):
|
|
126 |
settings.MAARCH_FEED= {
|
|
127 |
'URL': 'http://maarch.example.net',
|
|
128 |
'ENABLE': True,
|
|
129 |
'USERNAME': 'xxx',
|
|
130 |
'PASSWORD': 'yyy',
|
|
131 |
'STATUS_REFUSED': 'FOO',
|
|
132 |
}
|
|
133 |
resp = app.post('/ajax/mail/reject', status=302)
|
|
134 |
assert resp.location.startswith('/login/?next=')
|
|
135 |
|
|
136 |
app.set_user(user.username)
|
|
137 |
mail = Mail.objects.create(
|
|
138 |
content=ContentFile('foo', name='bar.txt'),
|
|
139 |
external_id='maarch-42')
|
|
140 |
|
|
141 |
@httmock.urlmatch(netloc='maarch.example.net', path='/rest/res/resource/status', method='PUT')
|
|
142 |
def response_ok(url, request):
|
|
143 |
assert json.loads(force_text(request.body)) == {'status': 'FOO', 'resId': ['42']}
|
|
144 |
headers = {'content-type': 'application/json'}
|
|
145 |
content = {"maarch_say": "ok"}
|
|
146 |
return httmock.response(200, content, headers)
|
|
147 |
|
|
148 |
with httmock.HTTMock(response_ok):
|
|
149 |
resp = app.post('/ajax/mail/reject', params={'source_pk': mail.pk}, status=200)
|
|
150 |
assert Mail.objects.count() == 0
|
|
151 |
|
|
152 |
# errors
|
|
153 |
mail = Mail.objects.create(
|
|
154 |
content=ContentFile('foo', name='bar.txt'),
|
|
155 |
external_id='maarch-42')
|
|
156 |
|
|
157 |
@httmock.urlmatch(netloc='maarch.example.net', path='/rest/res/resource/status', method='PUT')
|
|
158 |
def response_error1(url, request):
|
|
159 |
raise requests.RequestException
|
|
160 |
|
|
161 |
with httmock.HTTMock(response_error1):
|
|
162 |
resp = app.post('/ajax/mail/reject', params={'source_pk': mail.pk})
|
|
163 |
assert Mail.objects.get(id=mail.pk)
|
|
164 |
|
|
165 |
@httmock.urlmatch(netloc='maarch.example.net', path='/rest/res/resource/status', method='PUT')
|
|
166 |
def response_error2(url, request):
|
|
167 |
return httmock.response(500)
|
|
168 |
|
|
169 |
with httmock.HTTMock(response_error2):
|
|
170 |
resp = app.post('/ajax/mail/reject', params={'source_pk': mail.pk})
|
|
171 |
assert Mail.objects.get(id=mail.pk)
|
|
172 |
|
|
173 |
@httmock.urlmatch(netloc='maarch.example.net', path='/rest/res/resource/status', method='PUT')
|
|
174 |
def response_error3(url, request):
|
|
175 |
return httmock.response(200, 'not a json')
|
|
176 |
|
|
177 |
with httmock.HTTMock(response_error3):
|
|
178 |
resp = app.post('/ajax/mail/reject', params={'source_pk': mail.pk})
|
|
179 |
assert Mail.objects.get(id=mail.pk)
|
|
180 |
|
|
181 |
|
|
182 |
def test_mail_count_view(app, user):
|
|
183 |
resp = app.get('/ajax/count/mail/', status=302)
|
|
184 |
assert resp.location.startswith('/login/?next=')
|
|
185 |
|
|
186 |
Mail.objects.create(content=ContentFile('foo', name='bar.txt'), status='done-42')
|
|
187 |
Mail.objects.create(content=ContentFile('foo', name='bar.txt'), status='43')
|
|
188 |
app.set_user(user.username)
|
|
189 |
resp = app.get('/ajax/count/mail/', status=200)
|
|
190 |
assert resp.content_type == 'application/json'
|
|
191 |
assert resp.json == {'count': 1}
|
0 |
|
-
|