Project

General

Profile

Download (12.7 KB) Statistics
| Branch: | Tag: | Revision:

root / tests / test_manager.py @ 2ef32d7b

1
import logging
2
import mock
3
import os
4
import pytest
5

    
6
from django.core.urlresolvers import reverse
7
from django.contrib.auth.models import User
8
from django.test import override_settings
9

    
10
from corbo.models import Broadcast
11

    
12
pytestmark = pytest.mark.django_db
13

    
14
@pytest.fixture
15
def admin_user():
16
    try:
17
        user = User.objects.get(username='admin')
18
    except User.DoesNotExist:
19
        user = User.objects.create_superuser('admin', email='admin@example.com', password='admin')
20
    return user
21

    
22
def login(app, username='admin', password='admin'):
23
    login_page = app.get('/login/')
24
    login_form = login_page.forms[0]
25
    login_form['username'] = username
26
    login_form['password'] = password
27
    resp = login_form.submit()
28
    assert resp.status_int == 302
29
    return app
30

    
31
def test_unlogged_access(app):
32
    # connect while not being logged in
33
    assert app.get('/', status=302).location == 'http://testserver/manage/'
34
    assert app.get('/manage/', status=302).location == 'http://testserver/login/?next=/manage/'
35

    
36
def test_access(app, admin_user):
37
    app = login(app)
38
    resp = app.get('/manage/', status=200)
39
    assert 'New category' in resp.body
40

    
41
def test_logout(app, admin_user):
42
    app = login(app)
43
    app.get('/logout/')
44
    assert app.get('/', status=302).location == 'http://testserver/manage/'
45

    
46
def test_create_category(app, admin_user):
47
    app = login(app)
48
    resp = app.get(reverse('manage'))
49
    assert 'New category' in resp.content
50
    category_page = resp.click('New category')
51
    category_form = category_page.forms[0]
52
    category_form['name'] = 'Alerts'
53
    resp = category_form.submit()
54
    assert resp.status_int == 302
55
    assert resp.location == 'http://testserver/manage/'
56
    resp = app.get('http://testserver/manage/')
57
    assert 'Alerts' in resp.content
58

    
59
def test_edit_category(app, admin_user):
60
    app = login(app)
61
    resp = app.get('/manage/')
62
    assert 'New category' in resp.content
63
    category_page = resp.click('New category')
64
    category_form = category_page.forms[0]
65
    category_form['name'] = 'Alerts'
66
    resp = category_form.submit()
67
    assert resp.status_int == 302
68
    assert resp.location == 'http://testserver/manage/'
69
    resp = app.get('http://testserver/manage/')
70
    assert 'Alerts' in resp.content
71
    assert '0 announces' in resp.content
72
    assert '0 subscriptions' in resp.content
73
    resp = resp.click('Alerts')
74
    assert 'Edit' in resp.content
75
    edit_page = resp.click('Edit')
76
    edit_form = edit_page.forms[0]
77
    edit_form['name'] = 'New Alerts'
78
    resp = edit_form.submit()
79
    assert resp.status_int == 302
80
    assert resp.location == 'http://testserver/manage/category/alerts/'
81

    
82
def test_delete_category(app, admin_user):
83
    app = login(app)
84
    resp = app.get('/manage/')
85
    assert 'New category' in resp.content
86
    category_page = resp.click('New category')
87
    category_form = category_page.forms[0]
88
    category_form['name'] = 'Alerts'
89
    resp = category_form.submit()
90
    assert resp.status_int == 302
91
    assert resp.location == 'http://testserver/manage/'
92
    resp = app.get('http://testserver/manage/')
93
    assert 'Alerts' in resp.content
94
    assert '0 announces' in resp.content
95
    assert '0 subscriptions' in resp.content
96
    resp = resp.click('Alerts')
97
    assert 'Delete' in resp.content
98
    delete_page = resp.click('Delete')
99
    delete_form = delete_page.forms[0]
100
    resp = delete_form.submit()
101
    assert resp.status_int == 302
102
    assert resp.location == 'http://testserver/manage/'
103

    
104
def test_create_announce(app, admin_user):
105
    app = login(app)
106
    resp = app.get('/manage/')
107
    assert 'New category' in resp.content
108
    category_page = resp.click('New category')
109
    category_form = category_page.forms[0]
110
    category_form['name'] = 'Alerts'
111
    resp = category_form.submit()
112
    assert resp.status_int == 302
113
    assert resp.location == 'http://testserver/manage/'
114
    resp = app.get('http://testserver/manage/')
115
    assert 'Alerts' in resp.content
116
    resp = resp.click('Alerts')
117
    assert 'New announce' in resp.content
118
    announce_page = resp.click('New announce')
119
    announce_form = announce_page.forms[0]
120
    announce_form['title'] = 'First announce'
121
    announce_form['text'] = 'announce content'
122
    resp = announce_form.submit()
123
    assert resp.status_int == 302
124
    assert resp.location == 'http://testserver/manage/category/alerts/'
125
    resp = app.get('http://testserver/manage/category/alerts/')
126
    assert 'First announce' in resp.content
127

    
128
def test_edit_announce(app, admin_user):
129
    app = login(app)
130
    resp = app.get('/manage/')
131
    assert 'New category' in resp.content
132
    category_page = resp.click('New category')
133
    category_form = category_page.forms[0]
134
    category_form['name'] = 'Alerts'
135
    resp = category_form.submit()
136
    assert resp.status_int == 302
137
    assert resp.location == 'http://testserver/manage/'
138
    resp = app.get(resp.location)
139
    resp = resp.click('Alerts')
140
    assert 'New announce' in resp.content
141
    announce_page = resp.click('New announce')
142
    announce_form = announce_page.forms[0]
143
    announce_form['title'] = 'First announce'
144
    announce_form['text'] = 'announce content'
145
    resp = announce_form.submit()
146
    assert resp.status_int == 302
147
    assert resp.location == 'http://testserver/manage/category/alerts/'
148
    resp = app.get(resp.location)
149
    assert 'First announce' in resp.content
150
    announce_page = resp.click('First announce')
151
    assert 'First announce' in announce_page.content
152
    assert 'Edit' in announce_page.content
153
    assert 'Send test email' in announce_page.content
154
    assert 'Delete' in announce_page.content
155
    announce_edit_page = announce_page.click('Edit')
156
    edit_form = announce_edit_page.forms[0]
157
    edit_form['publication_time'] = '2017-03-03 09:00:00'
158
    edit_form['expiration_time'] = '2017-12-31 23:00:00'
159
    resp = edit_form.submit()
160
    assert resp.status_int == 302
161
    assert resp.location == 'http://testserver/manage/announce/1/'
162

    
163
    # simulate announce deliver
164
    broadcast = Broadcast.objects.get(announce__pk=1)
165
    broadcast.deliver_time = '2017-03-30 20:00:00'
166
    broadcast.delivery_count = 1
167
    broadcast.save()
168

    
169
    resp = app.get(resp.location)
170

    
171
    assert '<strong>Publication</strong> March 3, 2017, 9 a.m.' in resp.content
172
    assert '<strong>Expiration</strong> Dec. 31, 2017, 11 p.m.' in resp.content
173
    assert '<strong>Sent</strong> March 30, 2017, 8 p.m.' in resp.content
174
    assert 'to 1 destination' in resp.content
175

    
176
    broadcast.delivery_count = 2
177
    broadcast.save()
178

    
179
    resp = app.get('http://testserver/manage/announce/1/')
180
    assert '<strong>Sent</strong> March 30, 2017, 8 p.m.' in resp.content
181
    assert 'to 2 destinations' in resp.content
182

    
183

    
184
def test_delete_announce(app, admin_user):
185
    app = login(app)
186
    resp = app.get('/manage/')
187
    assert 'New category' in resp.content
188
    category_page = resp.click('New category')
189
    category_form = category_page.forms[0]
190
    category_form['name'] = 'Alerts'
191
    resp = category_form.submit()
192
    assert resp.status_int == 302
193
    assert resp.location == 'http://testserver/manage/'
194
    resp = app.get('http://testserver/manage/')
195
    resp = resp.click('Alerts')
196
    assert 'New announce' in resp.content
197
    announce_page = resp.click('New announce')
198
    announce_form = announce_page.forms[0]
199
    announce_form['title'] = 'First announce'
200
    announce_form['text'] = 'announce content'
201
    resp = announce_form.submit()
202
    assert resp.status_int == 302
203
    assert resp.location == 'http://testserver/manage/category/alerts/'
204
    resp = app.get('http://testserver/manage/category/alerts/')
205
    assert 'First announce' in resp.content
206
    resp = resp.click('First announce')
207
    assert 'Delete' in resp.content
208
    assert 'Send test email' in resp.content
209
    announce_delete_page = resp.click('Delete')
210
    announce_delete_form = announce_delete_page.forms[0]
211
    resp = announce_delete_form.submit()
212
    assert resp.status_int == 302
213
    assert resp.location == 'http://testserver/manage/category/alerts/'
214

    
215
def test_email_announce(app, admin_user):
216
    app = login(app)
217
    resp = app.get('/manage/')
218
    assert 'New category' in resp.content
219
    category_page = resp.click('New category')
220
    category_form = category_page.forms[0]
221
    category_form['name'] = 'Alerts'
222
    resp = category_form.submit()
223
    assert resp.status_int == 302
224
    assert resp.location == 'http://testserver/manage/'
225
    resp = app.get('http://testserver/manage/')
226
    resp = resp.click('Alerts')
227
    assert 'New announce' in resp.content
228
    announce_page = resp.click('New announce')
229
    announce_form = announce_page.forms[0]
230
    announce_form['title'] = 'First announce'
231
    announce_form['text'] = 'announce content'
232
    resp = announce_form.submit()
233
    assert resp.status_int == 302
234
    assert resp.location == 'http://testserver/manage/category/alerts/'
235
    resp = app.get(resp.location)
236
    assert 'First announce' in resp.content
237
    resp = resp.click('First announce')
238
    assert 'Send test email' in resp.content
239
    assert 'Send test SMS' not in resp.content
240
    resp = resp.click('Send test email')
241
    send_form = resp.forms[0]
242
    assert send_form.method == 'post'
243
    assert 'email' in send_form.fields
244
    assert send_form.fields['email'][0].value == admin_user.email
245
    assert 'Send' in resp.content
246
    assert 'Cancel' in resp.content
247
    resp = send_form.submit()
248
    assert resp.status_int == 302
249
    assert resp.location == 'http://testserver/manage/announce/1/'
250

    
251
@mock.patch('corbo.utils.requests.post')
252
def test_sms_announce(mocked_post, app, admin_user, settings):
253
    app = login(app)
254
    resp = app.get('/manage/')
255
    assert 'New category' in resp.content
256
    category_page = resp.click('New category')
257
    category_form = category_page.forms[0]
258
    category_form['name'] = 'Alerts'
259
    resp = category_form.submit()
260
    assert resp.status_int == 302
261
    assert resp.location.endswith(reverse('manage'))
262
    resp = resp.follow()
263
    resp = resp.click('Alerts')
264

    
265
    # create new announce
266
    assert 'New announce' in resp.content
267
    announce_page = resp.click('New announce')
268
    announce_form = announce_page.forms[0]
269
    announce_form['title'] = 'First announce'
270
    announce_form['text'] = 'announce content'
271
    resp = announce_form.submit()
272
    assert resp.status_int == 302
273
    assert resp.location.endswith(reverse('view_category', kwargs={'slug': 'alerts'}))
274
    resp = resp.follow()
275

    
276
    # view announce
277
    assert 'First announce' in resp.content
278
    settings.SMS_GATEWAY_URL = 'http:/passerelle.com'
279
    resp = resp.click('First announce')
280
    assert 'Send test SMS' in resp.content
281

    
282
    # open send sms form
283
    resp = resp.click('Send test SMS')
284
    send_form = resp.forms[0]
285
    assert 'mobile' in send_form.fields
286
    assert send_form.fields['mobile'][0].value == ''
287
    # submit with no mobile
288
    resp = send_form.submit()
289
    assert resp.status_int == 200
290

    
291
    form = resp.forms[0]
292
    form['mobile'] = '0607080900'
293
    # simulate response from passerelle
294
    mocked_response = mock.Mock()
295
    mocked_response.json.return_value = {'err': 0, 'data': True}
296
    mocked_post.return_value = mocked_response
297
    resp = form.submit()
298
    assert resp.location.endswith(reverse('view_announce', kwargs={'pk': 1}))
299
    resp = resp.follow()
300
    # make sure the form informs about the success
301
    assert 'SMS successfully sent' in resp.content
302

    
303
    resp = resp.click('Send test SMS')
304
    form = resp.forms[0]
305
    form['mobile'] = '0607080900'
306
    # simulate error from passerelle
307
    mocked_response.json.return_value = {'err': 1, 'data': None, 'err_desc': 'Destination error'}
308
    resp = form.submit()
309
    resp = resp.follow()
310
    assert 'Error occured while sending SMS' in resp.content
311

    
312
def test_sms_announce_with_invalid_gateway_url(app, admin_user, settings, caplog):
313
    app = login(app)
314
    resp = app.get('/manage/')
315
    assert 'New category' in resp.content
316
    category_page = resp.click('New category')
317
    category_form = category_page.forms[0]
318
    category_form['name'] = 'Alerts'
319
    resp = category_form.submit()
320
    resp = resp.follow()
321
    resp = resp.click('Alerts')
322
    assert 'New announce' in resp.content
323
    announce_page = resp.click('New announce')
324
    announce_form = announce_page.forms[0]
325
    announce_form['title'] = 'First announce'
326
    announce_form['text'] = 'announce content'
327
    resp = announce_form.submit()
328
    assert resp.status_int == 302
329
    assert resp.location == 'http://testserver/manage/category/alerts/'
330
    resp = resp.follow()
331
    assert 'First announce' in resp.content
332
    settings.SMS_GATEWAY_URL='invalid_url'
333
    resp = resp.click('First announce')
334
    assert 'Send test SMS' in resp.content
335
    resp = resp.click('Send test SMS')
336
    form = resp.forms[0]
337
    form['mobile'] = '0607080900'
338
    resp = form.submit()
339
    records = caplog.records
340
    assert len(records) == 1
341
    for record in records:
342
        assert record.name == 'corbo.utils'
343
        assert record.levelno == logging.WARNING
344
        assert 'Invalid URL' in record.getMessage()
(7-7/9)