|
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 mock
|
|
19 |
import pytest
|
|
20 |
import requests
|
|
21 |
|
|
22 |
from django.contrib.contenttypes.models import ContentType
|
|
23 |
from django.core.files.base import ContentFile
|
|
24 |
|
|
25 |
from welco.sources.mail.models import Mail
|
|
26 |
|
|
27 |
|
|
28 |
def test_get_contacts_zone_view(app, db):
|
|
29 |
resp = app.get('/ajax/contacts', status=200)
|
|
30 |
assert resp.html.find('button')['data-url'] == '/contacts/add/'
|
|
31 |
|
|
32 |
mail = Mail.objects.create(
|
|
33 |
content=ContentFile('foo', name='bar.txt'),
|
|
34 |
contact_id='42')
|
|
35 |
source_type = ContentType.objects.get_for_model(Mail).pk
|
|
36 |
resp = app.get(
|
|
37 |
'/ajax/contacts',
|
|
38 |
params={'source_type': source_type, 'source_pk': mail.pk},
|
|
39 |
status=200)
|
|
40 |
assert resp.html.find('a').text == '...'
|
|
41 |
assert resp.html.find('a')['data-page-slug'] == '42'
|
|
42 |
|
|
43 |
|
|
44 |
def test_post_contacts_zone_view(app, db):
|
|
45 |
mail = Mail.objects.create(content=ContentFile('foo', name='bar.txt'))
|
|
46 |
assert not mail.contact_id
|
|
47 |
source_type = ContentType.objects.get_for_model(Mail).pk
|
|
48 |
resp = app.post(
|
|
49 |
'/ajax/contacts',
|
|
50 |
params={'source_type': source_type, 'source_pk': mail.pk, 'user_id': 42},
|
|
51 |
status=200)
|
|
52 |
assert resp.text == 'ok'
|
|
53 |
assert Mail.objects.get(id=mail.pk).contact_id == '42'
|
|
54 |
|
|
55 |
|
|
56 |
def test_search_json_view_without_channel(app):
|
|
57 |
app.get('/contacts/search/json/', status=403)
|
|
58 |
|
|
59 |
|
|
60 |
def test_search_json_view_without_query(app, user, mail_group):
|
|
61 |
app.set_user(user.username)
|
|
62 |
resp = app.get('/contacts/search/json/', status=200)
|
|
63 |
assert resp.content_type == 'application/json'
|
|
64 |
assert resp.json == {'data': []}
|
|
65 |
|
|
66 |
|
|
67 |
def test_search_json_view(settings, app, user, mail_group):
|
|
68 |
settings.KNOWN_SERVICES = {
|
|
69 |
'wcs': {
|
|
70 |
'demarches': {
|
|
71 |
'url': 'http://wcs.example.net/',
|
|
72 |
}
|
|
73 |
}
|
|
74 |
}
|
|
75 |
app.set_user(user.username)
|
|
76 |
|
|
77 |
@httmock.urlmatch(netloc='wcs.example.net', path='/api/users/', method='GET')
|
|
78 |
def response(url, request):
|
|
79 |
headers = {'content-type': 'application/json'}
|
|
80 |
content = {'err': 1, 'msg': 'oups'}
|
|
81 |
return httmock.response(200, content, headers)
|
|
82 |
|
|
83 |
with httmock.HTTMock(response):
|
|
84 |
with pytest.raises(Exception, match='oups'):
|
|
85 |
resp = app.get('/contacts/search/json/', params={'q': 'Doe'})
|
|
86 |
|
|
87 |
@httmock.urlmatch(netloc='wcs.example.net', path='/api/users/', method='GET')
|
|
88 |
def response(url, request):
|
|
89 |
headers = {'content-type': 'application/json'}
|
|
90 |
content = {
|
|
91 |
'data': [{
|
|
92 |
'user_display_name': 'John Doe',
|
|
93 |
'user_email': 'john@example.net',
|
|
94 |
'user_var_phone': '0123456789',
|
|
95 |
'user_var_mobile': '0612345789',
|
|
96 |
'user_id': '42',
|
|
97 |
'user_roles': [{
|
|
98 |
'name': 'Agent',
|
|
99 |
'text': 'Agent',
|
|
100 |
'slug': 'agent',
|
|
101 |
'id': '8d73434814484aa0b8555ac9c68a9300'
|
|
102 |
}],
|
|
103 |
}],
|
|
104 |
'err': 0
|
|
105 |
}
|
|
106 |
return httmock.response(200, content, headers)
|
|
107 |
|
|
108 |
with httmock.HTTMock(response):
|
|
109 |
resp = app.get('/contacts/search/json/', params={'q': 'Doe'}, status=200)
|
|
110 |
assert resp.content_type == 'application/json'
|
|
111 |
assert resp.json['data'][0]['user_display_name'] == 'John Doe'
|
|
112 |
|
|
113 |
|
|
114 |
def test_contact_detail_fragment_view(settings, app, db):
|
|
115 |
settings.KNOWN_SERVICES = {
|
|
116 |
'wcs': {
|
|
117 |
'demarches': {
|
|
118 |
'url': 'http://wcs.example.net/',
|
|
119 |
}
|
|
120 |
}
|
|
121 |
}
|
|
122 |
@httmock.urlmatch(netloc='wcs.example.net', path='/api/users/42/', method='GET')
|
|
123 |
def response(url, request):
|
|
124 |
headers = {'content-type': 'application/json'}
|
|
125 |
content = {
|
|
126 |
'user_display_name': 'John Doe',
|
|
127 |
'user_email': 'john@example.net',
|
|
128 |
'user_var_phone': '0123456789',
|
|
129 |
'user_var_mobile': '0612345789',
|
|
130 |
'user_id': '42',
|
|
131 |
'user_roles': [{
|
|
132 |
'name': 'Agent',
|
|
133 |
'text': 'Agent',
|
|
134 |
'slug': 'agent',
|
|
135 |
'id': '8d73434814484aa0b8555ac9c68a9300'
|
|
136 |
}],
|
|
137 |
}
|
|
138 |
return httmock.response(200, content, headers)
|
|
139 |
|
|
140 |
with httmock.HTTMock(response):
|
|
141 |
resp = app.get('/ajax/contacts/42/', status=200)
|
|
142 |
assert resp.html.find('h3').text == 'John Doe'
|
|
143 |
assert resp.html.find('p').text == 'Agent'
|
|
144 |
assert resp.html.find('li').text == 'Phone: 0123456789'
|
|
145 |
|
|
146 |
# unused 'is_pinned_user' context
|
|
147 |
mail = Mail.objects.create(
|
|
148 |
content=ContentFile('foo', name='bar.txt'),
|
|
149 |
contact_id='42')
|
|
150 |
source_type = ContentType.objects.get_for_model(Mail).pk
|
|
151 |
with httmock.HTTMock(response):
|
|
152 |
resp = app.get(
|
|
153 |
'/ajax/contacts/42/',
|
|
154 |
params={'source_type': source_type, 'source_pk': mail.pk},
|
|
155 |
status=200)
|
|
156 |
assert resp.html.find('h3').text == 'John Doe'
|
|
157 |
|
|
158 |
|
|
159 |
def test_get_contact_add_view(app):
|
|
160 |
resp = app.get('/contacts/add/', status=200)
|
|
161 |
assert resp.html.find('select')['name'] == 'title'
|
|
162 |
assert resp.html.find('input', {'id': 'id_first_name'})['name'] == 'first_name'
|
|
163 |
|
|
164 |
|
|
165 |
@mock.patch('welco.contacts.views.time.sleep')
|
|
166 |
def test_post_contact_add_view(mocked_sleep, settings, app, db):
|
|
167 |
settings.CONTACT_SEND_REGISTRATION_EMAIL = True
|
|
168 |
settings.KNOWN_SERVICES = {
|
|
169 |
'authentic': {
|
|
170 |
'connexion': {
|
|
171 |
'url': 'http://authentic.example.net/',
|
|
172 |
'orig': 'http://welco.example.net/',
|
|
173 |
'secret': 'xxx',
|
|
174 |
}
|
|
175 |
},
|
|
176 |
'wcs': {
|
|
177 |
'demarches': {
|
|
178 |
'url': 'http://wcs.example.net/',
|
|
179 |
}
|
|
180 |
}
|
|
181 |
}
|
|
182 |
|
|
183 |
# normal case
|
|
184 |
@httmock.urlmatch(netloc='authentic.example.net', path='/api/users/', method='POST')
|
|
185 |
def authentic_response(url, request):
|
|
186 |
headers = {'content-type': 'application/json'}
|
|
187 |
content = {'uuid': '42'}
|
|
188 |
return httmock.response(200, content, headers)
|
|
189 |
|
|
190 |
@httmock.urlmatch(netloc='wcs.example.net', path='/api/users/42/', method='GET')
|
|
191 |
def wcs_response(url, request):
|
|
192 |
headers = {'content-type': 'application/json'}
|
|
193 |
content = {
|
|
194 |
'user_display_name': 'John Doe',
|
|
195 |
'id': '43',
|
|
196 |
}
|
|
197 |
return httmock.response(200, content, headers)
|
|
198 |
|
|
199 |
with httmock.HTTMock(authentic_response, wcs_response):
|
|
200 |
resp = app.post(
|
|
201 |
'/contacts/add/',
|
|
202 |
params={
|
|
203 |
'title': 'Mr',
|
|
204 |
'first_name': 'John',
|
|
205 |
'last_name': 'Doe',
|
|
206 |
},
|
|
207 |
status=200)
|
|
208 |
assert resp.content_type == 'application/json'
|
|
209 |
assert resp.json['data']['user_id'] == '43'
|
|
210 |
|
|
211 |
# timeout
|
|
212 |
@httmock.urlmatch(netloc='wcs.example.net', path='/api/users/42/', method='GET')
|
|
213 |
def wcs_no_response(url, request):
|
|
214 |
return httmock.response(404)
|
|
215 |
|
|
216 |
with httmock.HTTMock(authentic_response, wcs_no_response):
|
|
217 |
resp = app.post('/contacts/add/', status=200)
|
|
218 |
assert resp.content_type == 'application/json'
|
|
219 |
assert resp.json['err'] == 1
|
|
220 |
assert resp.json['data'] == 'timeout when calling wcs'
|
|
221 |
|
|
222 |
# error
|
|
223 |
@httmock.urlmatch(netloc='wcs.example.net', path='/api/users/42/', method='GET')
|
|
224 |
def wcs_no_response(url, request):
|
|
225 |
return httmock.response(500)
|
|
226 |
|
|
227 |
with httmock.HTTMock(authentic_response, wcs_no_response):
|
|
228 |
with pytest.raises(requests.HTTPError):
|
|
229 |
resp = app.post('/contacts/add/', status=200)
|
0 |
|
-
|