2 |
2 |
import mock
|
3 |
3 |
import os
|
4 |
4 |
import pytest
|
5 |
5 |
|
6 |
6 |
from django.urls import reverse
|
7 |
7 |
from django.contrib.auth.models import User
|
8 |
8 |
from django.test import override_settings
|
9 |
9 |
|
10 |
|
from corbo.models import Broadcast, Subscription
|
|
10 |
from corbo.models import Announce, Broadcast, Subscription
|
11 |
11 |
from corbo.utils import format_phonenumber
|
12 |
12 |
|
13 |
13 |
pytestmark = pytest.mark.django_db
|
14 |
14 |
|
15 |
15 |
@pytest.fixture
|
16 |
16 |
def admin_user():
|
17 |
17 |
try:
|
18 |
18 |
user = User.objects.get(username='admin')
|
... | ... | |
155 |
155 |
assert 'Send test email' in announce_page
|
156 |
156 |
assert 'Delete' in announce_page
|
157 |
157 |
announce_edit_page = announce_page.click('Edit')
|
158 |
158 |
edit_form = announce_edit_page.forms[0]
|
159 |
159 |
edit_form['publication_time'] = '2017-03-03 09:00:00'
|
160 |
160 |
edit_form['expiration_time'] = '2017-12-31 23:00:00'
|
161 |
161 |
resp = edit_form.submit()
|
162 |
162 |
assert resp.status_int == 302
|
163 |
|
assert resp.location.endswith(reverse('view_announce', kwargs={'pk': 1}))
|
|
163 |
announce = Announce.objects.get(title='First announce')
|
|
164 |
assert resp.location.endswith(reverse('view_announce', kwargs={'pk': announce.id}))
|
164 |
165 |
|
165 |
166 |
# simulate announce deliver
|
166 |
|
broadcast = Broadcast.objects.get(announce__pk=1)
|
|
167 |
broadcast = Broadcast.objects.get(announce__pk=announce.id)
|
167 |
168 |
broadcast.deliver_time = '2017-03-30 20:00:00'
|
168 |
169 |
broadcast.delivery_count = 1
|
169 |
170 |
broadcast.save()
|
170 |
171 |
|
171 |
172 |
resp = app.get(resp.location)
|
172 |
173 |
|
173 |
174 |
assert '<strong>Publication</strong> March 3, 2017, 9 a.m.' in resp
|
174 |
175 |
assert '<strong>Expiration</strong> Dec. 31, 2017, 11 p.m.' in resp
|
175 |
176 |
assert '<strong>Sent</strong> March 30, 2017, 8 p.m.' in resp
|
176 |
177 |
assert 'to 1 destination' in resp
|
177 |
178 |
|
178 |
179 |
broadcast.delivery_count = 2
|
179 |
180 |
broadcast.save()
|
180 |
181 |
|
181 |
|
resp = app.get('http://testserver/manage/announce/1/')
|
|
182 |
resp = app.get('http://testserver/manage/announce/%s/' % announce.id)
|
182 |
183 |
assert '<strong>Sent</strong> March 30, 2017, 8 p.m.' in resp
|
183 |
184 |
assert 'to 2 destinations' in resp
|
184 |
185 |
|
185 |
186 |
|
186 |
187 |
def test_delete_announce(app, admin_user):
|
187 |
188 |
app = login(app)
|
188 |
189 |
resp = app.get('/manage/')
|
189 |
190 |
assert 'New category' in resp
|
... | ... | |
243 |
244 |
send_form = resp.forms[0]
|
244 |
245 |
assert send_form.method == 'post'
|
245 |
246 |
assert 'email' in send_form.fields
|
246 |
247 |
assert send_form.fields['email'][0].value == admin_user.email
|
247 |
248 |
assert 'Send' in resp
|
248 |
249 |
assert 'Cancel' in resp
|
249 |
250 |
resp = send_form.submit()
|
250 |
251 |
assert resp.status_int == 302
|
251 |
|
assert resp.location.endswith(reverse('view_announce', kwargs={'pk': '1'}))
|
|
252 |
announce = Announce.objects.get(title='First announce')
|
|
253 |
assert resp.location.endswith(reverse('view_announce', kwargs={'pk': announce.id}))
|
252 |
254 |
|
253 |
255 |
@mock.patch('corbo.utils.requests.post')
|
254 |
256 |
def test_sms_announce(mocked_post, app, admin_user, settings):
|
255 |
257 |
app = login(app)
|
256 |
258 |
resp = app.get('/manage/')
|
257 |
259 |
assert 'New category' in resp
|
258 |
260 |
category_page = resp.click('New category')
|
259 |
261 |
category_form = category_page.forms[0]
|
... | ... | |
302 |
304 |
|
303 |
305 |
form = resp.forms[0]
|
304 |
306 |
form['mobile'] = '0607080900'
|
305 |
307 |
# simulate response from passerelle
|
306 |
308 |
mocked_response = mock.Mock()
|
307 |
309 |
mocked_response.json.return_value = {'err': 0, 'data': True}
|
308 |
310 |
mocked_post.return_value = mocked_response
|
309 |
311 |
resp = form.submit()
|
310 |
|
assert resp.location.endswith(reverse('view_announce', kwargs={'pk': 1}))
|
|
312 |
announce = Announce.objects.get(title='First announce')
|
|
313 |
assert resp.location.endswith(reverse('view_announce', kwargs={'pk': announce.id}))
|
311 |
314 |
resp = resp.follow()
|
312 |
315 |
# make sure the form informs about the success
|
313 |
316 |
assert 'SMS successfully sent' in resp
|
314 |
317 |
|
315 |
318 |
resp = resp.click('Send test SMS')
|
316 |
319 |
form = resp.forms[0]
|
317 |
320 |
form['mobile'] = '0607080900'
|
318 |
321 |
# simulate error from passerelle
|
319 |
|
-
|