Merge lp:~soren/surveilr/messaging into lp:surveilr
- messaging
- Merge into trunk
Proposed by
Soren Hansen
Status: | Merged |
---|---|
Approved by: | Soren Hansen |
Approved revision: | 15 |
Merged at revision: | 11 |
Proposed branch: | lp:~soren/surveilr/messaging |
Merge into: | lp:surveilr |
Diff against target: |
466 lines (+336/-16) 11 files modified
surveilr/api/server.py (+22/-1) surveilr/defaults.cfg (+1/-0) surveilr/messaging/__init__.py (+34/-0) surveilr/messaging/fake.py (+32/-0) surveilr/messaging/sms.py (+52/-0) surveilr/models.py (+4/-0) surveilr/tests/__init__.py (+1/-2) surveilr/tests/test_api_server.py (+36/-13) surveilr/tests/test_messaging.cfg (+5/-0) surveilr/tests/test_messaging.py (+114/-0) surveilr/tests/utils.py (+35/-0) |
To merge this branch: | bzr merge lp:~soren/surveilr/messaging |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Soren Hansen | Pending | ||
Review via email: mp+83866@code.launchpad.net |
Commit message
Add basic messaging subsystem.
Description of the change
To post a comment you must log in.
lp:~soren/surveilr/messaging
updated
- 11. By Soren Hansen
-
Merge trunk
- 12. By Soren Hansen
-
Add support for sender id in SMS messaging driver. Add tests for Clickatell client instantiation.
- 13. By Soren Hansen
-
Only define User model once.
- 14. By Soren Hansen
-
Include messaging settings in GET /users/{id} response
- 15. By Soren Hansen
-
PEP-8 fixes
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'surveilr/api/server.py' | |||
2 | --- surveilr/api/server.py 2011-11-29 20:07:19 +0000 | |||
3 | +++ surveilr/api/server.py 2011-11-30 09:36:24 +0000 | |||
4 | @@ -35,10 +35,25 @@ | |||
5 | 35 | from webob.dec import wsgify | 35 | from webob.dec import wsgify |
6 | 36 | from webob.exc import HTTPNotFound | 36 | from webob.exc import HTTPNotFound |
7 | 37 | 37 | ||
8 | 38 | from surveilr import messaging | ||
9 | 38 | from surveilr import models | 39 | from surveilr import models |
10 | 39 | from surveilr import utils | 40 | from surveilr import utils |
11 | 40 | 41 | ||
12 | 41 | 42 | ||
13 | 43 | class NotificationController(object): | ||
14 | 44 | """Routes style controller for notifications""" | ||
15 | 45 | |||
16 | 46 | def create(self, req, user_id): | ||
17 | 47 | """Called for POST requests to /user/{id}/notifications | ||
18 | 48 | |||
19 | 49 | Sends a notification to the given user""" | ||
20 | 50 | |||
21 | 51 | user = models.User.get(key=user_id) | ||
22 | 52 | messaging.send(user, json.loads(req.body)) | ||
23 | 53 | response = {} | ||
24 | 54 | return Response(json.dumps(response)) | ||
25 | 55 | |||
26 | 56 | |||
27 | 42 | class UserController(object): | 57 | class UserController(object): |
28 | 43 | """Routes style controller for actions related to users""" | 58 | """Routes style controller for actions related to users""" |
29 | 44 | 59 | ||
30 | @@ -59,7 +74,10 @@ | |||
31 | 59 | Returns information for the given service""" | 74 | Returns information for the given service""" |
32 | 60 | try: | 75 | try: |
33 | 61 | user = models.User.get(id) | 76 | user = models.User.get(id) |
35 | 62 | return Response({'id': user.key}) | 77 | resp_dict = {'id': user.key, |
36 | 78 | 'messaging_driver': user.messaging_driver, | ||
37 | 79 | 'messaging_address': user.messaging_address} | ||
38 | 80 | return Response(json.dumps(resp_dict)) | ||
39 | 63 | except riakalchemy.NoSuchObjectError: | 81 | except riakalchemy.NoSuchObjectError: |
40 | 64 | return HTTPNotFound() | 82 | return HTTPNotFound() |
41 | 65 | 83 | ||
42 | @@ -138,6 +156,9 @@ | |||
43 | 138 | path_prefix='/services/{service_name}') | 156 | path_prefix='/services/{service_name}') |
44 | 139 | map.resource("service", "services", controller='ServiceController') | 157 | map.resource("service", "services", controller='ServiceController') |
45 | 140 | map.resource("user", "users", controller='UserController') | 158 | map.resource("user", "users", controller='UserController') |
46 | 159 | map.resource("notification", "notifications", | ||
47 | 160 | controller='NotificationController', | ||
48 | 161 | path_prefix='/users/{user_id}') | ||
49 | 141 | 162 | ||
50 | 142 | @wsgify | 163 | @wsgify |
51 | 143 | def __call__(self, req): | 164 | def __call__(self, req): |
52 | 144 | 165 | ||
53 | === modified file 'surveilr/defaults.cfg' | |||
54 | --- surveilr/defaults.cfg 2011-11-28 22:05:12 +0000 | |||
55 | +++ surveilr/defaults.cfg 2011-11-30 09:36:24 +0000 | |||
56 | @@ -4,3 +4,4 @@ | |||
57 | 4 | username = | 4 | username = |
58 | 5 | password = | 5 | password = |
59 | 6 | api_id = | 6 | api_id = |
60 | 7 | sender = | ||
61 | 7 | 8 | ||
62 | === added directory 'surveilr/messaging' | |||
63 | === added file 'surveilr/messaging/__init__.py' | |||
64 | --- surveilr/messaging/__init__.py 1970-01-01 00:00:00 +0000 | |||
65 | +++ surveilr/messaging/__init__.py 2011-11-30 09:36:24 +0000 | |||
66 | @@ -0,0 +1,34 @@ | |||
67 | 1 | """ | ||
68 | 2 | Surveilr - Log aggregation, analysis and visualisation | ||
69 | 3 | |||
70 | 4 | Copyright (C) 2011 Linux2Go | ||
71 | 5 | |||
72 | 6 | This program is free software: you can redistribute it and/or | ||
73 | 7 | modify it under the terms of the GNU Affero General Public License | ||
74 | 8 | as published by the Free Software Foundation, either version 3 of | ||
75 | 9 | the License, or (at your option) any later version. | ||
76 | 10 | |||
77 | 11 | This program is distributed in the hope that it will be useful, | ||
78 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
79 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
80 | 14 | GNU Affero General Public License for more details. | ||
81 | 15 | |||
82 | 16 | You should have received a copy of the GNU Affero General Public | ||
83 | 17 | License along with this program. If not, see | ||
84 | 18 | <http://www.gnu.org/licenses/>. | ||
85 | 19 | """ | ||
86 | 20 | |||
87 | 21 | import surveilr.messaging.fake | ||
88 | 22 | import surveilr.messaging.sms | ||
89 | 23 | |||
90 | 24 | from surveilr import drivers | ||
91 | 25 | |||
92 | 26 | |||
93 | 27 | def _get_driver(recipient): | ||
94 | 28 | return drivers.get_driver('messaging', | ||
95 | 29 | getattr(recipient, 'messaging_driver', 'fake')) | ||
96 | 30 | |||
97 | 31 | |||
98 | 32 | def send(recipient, info): | ||
99 | 33 | driver = _get_driver(recipient) | ||
100 | 34 | driver.send(recipient, info) | ||
101 | 0 | 35 | ||
102 | === added file 'surveilr/messaging/fake.py' | |||
103 | --- surveilr/messaging/fake.py 1970-01-01 00:00:00 +0000 | |||
104 | +++ surveilr/messaging/fake.py 2011-11-30 09:36:24 +0000 | |||
105 | @@ -0,0 +1,32 @@ | |||
106 | 1 | """ | ||
107 | 2 | Surveilr - Log aggregation, analysis and visualisation | ||
108 | 3 | |||
109 | 4 | Copyright (C) 2011 Linux2Go | ||
110 | 5 | |||
111 | 6 | This program is free software: you can redistribute it and/or | ||
112 | 7 | modify it under the terms of the GNU Affero General Public License | ||
113 | 8 | as published by the Free Software Foundation, either version 3 of | ||
114 | 9 | the License, or (at your option) any later version. | ||
115 | 10 | |||
116 | 11 | This program is distributed in the hope that it will be useful, | ||
117 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
118 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
119 | 14 | GNU Affero General Public License for more details. | ||
120 | 15 | |||
121 | 16 | You should have received a copy of the GNU Affero General Public | ||
122 | 17 | License along with this program. If not, see | ||
123 | 18 | <http://www.gnu.org/licenses/>. | ||
124 | 19 | |||
125 | 20 | Fake messaging driver | ||
126 | 21 | """ | ||
127 | 22 | |||
128 | 23 | import surveilr.drivers | ||
129 | 24 | |||
130 | 25 | |||
131 | 26 | class FakeMessaging(object): | ||
132 | 27 | messages = [] | ||
133 | 28 | |||
134 | 29 | def send(self, recipient, info): | ||
135 | 30 | self.messages.append((recipient, info)) | ||
136 | 31 | |||
137 | 32 | surveilr.drivers.register_driver('messaging', 'fake', FakeMessaging()) | ||
138 | 0 | 33 | ||
139 | === added file 'surveilr/messaging/sms.py' | |||
140 | --- surveilr/messaging/sms.py 1970-01-01 00:00:00 +0000 | |||
141 | +++ surveilr/messaging/sms.py 2011-11-30 09:36:24 +0000 | |||
142 | @@ -0,0 +1,52 @@ | |||
143 | 1 | """ | ||
144 | 2 | Surveilr - Log aggregation, analysis and visualisation | ||
145 | 3 | |||
146 | 4 | Copyright (C) 2011 Linux2Go | ||
147 | 5 | |||
148 | 6 | This program is free software: you can redistribute it and/or | ||
149 | 7 | modify it under the terms of the GNU Affero General Public License | ||
150 | 8 | as published by the Free Software Foundation, either version 3 of | ||
151 | 9 | the License, or (at your option) any later version. | ||
152 | 10 | |||
153 | 11 | This program is distributed in the hope that it will be useful, | ||
154 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
155 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
156 | 14 | GNU Affero General Public License for more details. | ||
157 | 15 | |||
158 | 16 | You should have received a copy of the GNU Affero General Public | ||
159 | 17 | License along with this program. If not, see | ||
160 | 18 | <http://www.gnu.org/licenses/>. | ||
161 | 19 | |||
162 | 20 | SMS messaging driver | ||
163 | 21 | """ | ||
164 | 22 | |||
165 | 23 | from surveilr import config | ||
166 | 24 | from surveilr import drivers | ||
167 | 25 | |||
168 | 26 | from clickatell.api import Clickatell | ||
169 | 27 | from clickatell import constants as cc | ||
170 | 28 | |||
171 | 29 | |||
172 | 30 | class SMSMessaging(object): | ||
173 | 31 | def __init__(self): | ||
174 | 32 | username = config.get_str('sms', 'username') | ||
175 | 33 | password = config.get_str('sms', 'password') | ||
176 | 34 | api_id = config.get_str('sms', 'api_id') | ||
177 | 35 | self.client = Clickatell(username, password, api_id, | ||
178 | 36 | sendmsg_defaults={ | ||
179 | 37 | 'callback': cc.YES, | ||
180 | 38 | 'msg_type': cc.SMS_DEFAULT, | ||
181 | 39 | 'deliv_ack': cc.YES, | ||
182 | 40 | 'req_feat': (cc.FEAT_ALPHA + | ||
183 | 41 | cc.FEAT_NUMER + | ||
184 | 42 | cc.FEAT_DELIVACK) | ||
185 | 43 | }) | ||
186 | 44 | |||
187 | 45 | def send(self, recipient, info): | ||
188 | 46 | sender = config.get_str('sms', 'sender') | ||
189 | 47 | self.client.sendmsg(recipients=[recipient.messaging_address], | ||
190 | 48 | sender=sender, | ||
191 | 49 | text=str(info)) | ||
192 | 50 | |||
193 | 51 | |||
194 | 52 | drivers.register_driver('messaging', 'sms', SMSMessaging()) | ||
195 | 0 | 53 | ||
196 | === modified file 'surveilr/models.py' | |||
197 | --- surveilr/models.py 2011-11-29 10:27:40 +0000 | |||
198 | +++ surveilr/models.py 2011-11-30 09:36:24 +0000 | |||
199 | @@ -35,12 +35,16 @@ | |||
200 | 35 | 35 | ||
201 | 36 | name = String() | 36 | name = String() |
202 | 37 | most_recent_log_entry = RelatedObjects() | 37 | most_recent_log_entry = RelatedObjects() |
203 | 38 | user = RelatedObjects(backref=True) | ||
204 | 38 | 39 | ||
205 | 39 | 40 | ||
206 | 40 | class User(RiakObject): | 41 | class User(RiakObject): |
207 | 41 | """A user of the service""" | 42 | """A user of the service""" |
208 | 42 | bucket_name = 'users' | 43 | bucket_name = 'users' |
209 | 43 | 44 | ||
210 | 45 | messaging_driver = String() | ||
211 | 46 | messaging_address = String() | ||
212 | 47 | |||
213 | 44 | 48 | ||
214 | 45 | class LogEntry(RiakObject): | 49 | class LogEntry(RiakObject): |
215 | 46 | """A log entry holding one or more metrics | 50 | """A log entry holding one or more metrics |
216 | 47 | 51 | ||
217 | === modified file 'surveilr/tests/__init__.py' | |||
218 | --- surveilr/tests/__init__.py 2011-11-29 23:14:20 +0000 | |||
219 | +++ surveilr/tests/__init__.py 2011-11-30 09:36:24 +0000 | |||
220 | @@ -24,6 +24,7 @@ | |||
221 | 24 | 24 | ||
222 | 25 | from surveilr import config | 25 | from surveilr import config |
223 | 26 | 26 | ||
224 | 27 | |||
225 | 27 | class TestCase(unittest.TestCase): | 28 | class TestCase(unittest.TestCase): |
226 | 28 | def config_files(self): | 29 | def config_files(self): |
227 | 29 | module = self.__module__ | 30 | module = self.__module__ |
228 | @@ -49,5 +50,3 @@ | |||
229 | 49 | 50 | ||
230 | 50 | def tearDown(self): | 51 | def tearDown(self): |
231 | 51 | config.load_default_config() | 52 | config.load_default_config() |
232 | 52 | |||
233 | 53 | |||
234 | 54 | 53 | ||
235 | === modified file 'surveilr/tests/test_api_server.py' | |||
236 | --- surveilr/tests/test_api_server.py 2011-11-29 10:27:40 +0000 | |||
237 | +++ surveilr/tests/test_api_server.py 2011-11-30 09:36:24 +0000 | |||
238 | @@ -38,23 +38,46 @@ | |||
239 | 38 | """Create, retrieve, delete, attempt to retrieve again""" | 38 | """Create, retrieve, delete, attempt to retrieve again""" |
240 | 39 | req = Request.blank('/users', | 39 | req = Request.blank('/users', |
241 | 40 | method='POST', | 40 | method='POST', |
242 | 41 | POST=json.dumps({'messaging_driver': 'fake', | ||
243 | 42 | 'messaging_address': 'foo'})) | ||
244 | 43 | resp = application(req) | ||
245 | 44 | self.assertEquals(resp.status_int, 200) | ||
246 | 45 | |||
247 | 46 | service_id = json.loads(resp.body)['id'] | ||
248 | 47 | |||
249 | 48 | req = Request.blank('/users/%s' % service_id) | ||
250 | 49 | resp = application(req) | ||
251 | 50 | self.assertEquals(resp.status_int, 200) | ||
252 | 51 | |||
253 | 52 | print resp.body, type(resp.body) | ||
254 | 53 | user = json.loads(resp.body) | ||
255 | 54 | self.assertEquals(user['messaging_driver'], 'fake') | ||
256 | 55 | self.assertEquals(user['messaging_address'], 'foo') | ||
257 | 56 | |||
258 | 57 | req = Request.blank('/users/%s' % service_id, method='DELETE') | ||
259 | 58 | resp = application(req) | ||
260 | 59 | self.assertEquals(resp.status_int, 200) | ||
261 | 60 | |||
262 | 61 | req = Request.blank('/users/%s' % service_id) | ||
263 | 62 | resp = application(req) | ||
264 | 63 | self.assertEquals(resp.status_int, 404) | ||
265 | 64 | |||
266 | 65 | def test_send_notification(self): | ||
267 | 66 | req = Request.blank('/users', | ||
268 | 67 | method='POST', | ||
269 | 41 | POST=json.dumps({})) | 68 | POST=json.dumps({})) |
270 | 42 | resp = application(req) | 69 | resp = application(req) |
271 | 43 | self.assertEquals(resp.status_int, 200) | 70 | self.assertEquals(resp.status_int, 200) |
272 | 44 | 71 | ||
286 | 45 | service_id = json.loads(resp.body)['id'] | 72 | user_id = json.loads(resp.body)['id'] |
287 | 46 | 73 | req = Request.blank('/users/%s/notifications' % user_id, | |
288 | 47 | req = Request.blank('/users/%s' % service_id) | 74 | method='POST', |
289 | 48 | resp = application(req) | 75 | POST=json.dumps({ |
290 | 49 | self.assertEquals(resp.status_int, 200) | 76 | 'timestamp': 13217362355575, |
291 | 50 | 77 | 'metrics': {'duration': 85000, | |
292 | 51 | req = Request.blank('/users/%s' % service_id, method='DELETE') | 78 | 'response_size': 12435}})) |
293 | 52 | resp = application(req) | 79 | resp = application(req) |
294 | 53 | self.assertEquals(resp.status_int, 200) | 80 | self.assertEquals(resp.status_int, 200) |
282 | 54 | |||
283 | 55 | req = Request.blank('/users/%s' % service_id) | ||
284 | 56 | resp = application(req) | ||
285 | 57 | self.assertEquals(resp.status_int, 404) | ||
295 | 58 | 81 | ||
296 | 59 | def test_create_retrieve_service(self): | 82 | def test_create_retrieve_service(self): |
297 | 60 | """Create, retrieve, delete, attempt to retrieve again""" | 83 | """Create, retrieve, delete, attempt to retrieve again""" |
298 | 61 | 84 | ||
299 | === added file 'surveilr/tests/test_messaging.cfg' | |||
300 | --- surveilr/tests/test_messaging.cfg 1970-01-01 00:00:00 +0000 | |||
301 | +++ surveilr/tests/test_messaging.cfg 2011-11-30 09:36:24 +0000 | |||
302 | @@ -0,0 +1,5 @@ | |||
303 | 1 | [sms] | ||
304 | 2 | username = testuser | ||
305 | 3 | password = testpassword | ||
306 | 4 | api_id = testapiid | ||
307 | 5 | sender = testsender | ||
308 | 0 | 6 | ||
309 | === added file 'surveilr/tests/test_messaging.py' | |||
310 | --- surveilr/tests/test_messaging.py 1970-01-01 00:00:00 +0000 | |||
311 | +++ surveilr/tests/test_messaging.py 2011-11-30 09:36:24 +0000 | |||
312 | @@ -0,0 +1,114 @@ | |||
313 | 1 | """ | ||
314 | 2 | Surveilr - Log aggregation, analysis and visualisation | ||
315 | 3 | |||
316 | 4 | Copyright (C) 2011 Linux2Go | ||
317 | 5 | |||
318 | 6 | This program is free software: you can redistribute it and/or | ||
319 | 7 | modify it under the terms of the GNU Affero General Public License | ||
320 | 8 | as published by the Free Software Foundation, either version 3 of | ||
321 | 9 | the License, or (at your option) any later version. | ||
322 | 10 | |||
323 | 11 | This program is distributed in the hope that it will be useful, | ||
324 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
325 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
326 | 14 | GNU Affero General Public License for more details. | ||
327 | 15 | |||
328 | 16 | You should have received a copy of the GNU Affero General Public | ||
329 | 17 | License along with this program. If not, see | ||
330 | 18 | <http://www.gnu.org/licenses/>. | ||
331 | 19 | |||
332 | 20 | Tests for messaging layer | ||
333 | 21 | """ | ||
334 | 22 | |||
335 | 23 | import mock | ||
336 | 24 | import unittest | ||
337 | 25 | |||
338 | 26 | from surveilr import drivers | ||
339 | 27 | from surveilr import messaging | ||
340 | 28 | from surveilr import tests | ||
341 | 29 | from surveilr.messaging import sms | ||
342 | 30 | from surveilr.tests import utils | ||
343 | 31 | |||
344 | 32 | |||
345 | 33 | class FakeMessagingDriverTests(unittest.TestCase): | ||
346 | 34 | def setUp(self): | ||
347 | 35 | import surveilr.messaging.fake | ||
348 | 36 | # This does nothing, but pyflakes gets upset if we import it | ||
349 | 37 | # and never "use" it. | ||
350 | 38 | surveilr.messaging.fake | ||
351 | 39 | self.driver = drivers.get_driver('messaging', 'fake') | ||
352 | 40 | |||
353 | 41 | def test_send(self): | ||
354 | 42 | user = utils.get_test_user() | ||
355 | 43 | info = {'service': 'service_id', | ||
356 | 44 | 'state': 'normal', | ||
357 | 45 | 'previous_state': 'unexpected high'} | ||
358 | 46 | self.driver.send(user, info) | ||
359 | 47 | |||
360 | 48 | |||
361 | 49 | class SMSMessagingDriverTests(tests.TestCase): | ||
362 | 50 | @mock.patch('surveilr.messaging.sms.Clickatell') | ||
363 | 51 | def test_instantiate_client(self, clickatell): | ||
364 | 52 | self.driver = sms.SMSMessaging() | ||
365 | 53 | |||
366 | 54 | sendmsg_defaults = {'callback': 1, | ||
367 | 55 | 'req_feat': 8240, | ||
368 | 56 | 'deliv_ack': 1, | ||
369 | 57 | 'msg_type': 'SMS_TEXT'} | ||
370 | 58 | clickatell.assert_called_with('testuser', 'testpassword', 'testapiid', | ||
371 | 59 | sendmsg_defaults=sendmsg_defaults) | ||
372 | 60 | |||
373 | 61 | def test_send(self): | ||
374 | 62 | driver = sms.SMSMessaging() | ||
375 | 63 | driver.client = mock.Mock() | ||
376 | 64 | |||
377 | 65 | msisdn = '12345678' | ||
378 | 66 | user = utils.get_test_user(messaging_driver='sms', | ||
379 | 67 | messaging_address=msisdn) | ||
380 | 68 | info = utils.get_test_notification_info() | ||
381 | 69 | |||
382 | 70 | driver.send(user, info) | ||
383 | 71 | |||
384 | 72 | driver.client.sendmsg.assert_called_with(recipients=[msisdn], | ||
385 | 73 | sender='testsender', | ||
386 | 74 | text=str(info)) | ||
387 | 75 | |||
388 | 76 | |||
389 | 77 | class MessagingAPITests(unittest.TestCase): | ||
390 | 78 | def test_send(self): | ||
391 | 79 | user = utils.get_test_user() | ||
392 | 80 | info = {'service': 'service_id', | ||
393 | 81 | 'state': 'normal', | ||
394 | 82 | 'previous_state': 'unexpected high'} | ||
395 | 83 | |||
396 | 84 | with mock.patch('surveilr.messaging._get_driver') as _get_driver: | ||
397 | 85 | messaging.send(user, info) | ||
398 | 86 | |||
399 | 87 | # Check that _get_driver gets called with the user as its argument | ||
400 | 88 | _get_driver.assert_called_with(user) | ||
401 | 89 | |||
402 | 90 | # Check that _get_driver's return value (i.e. the driver) | ||
403 | 91 | # gets its .send() method called with user and info as its | ||
404 | 92 | # arguments | ||
405 | 93 | _get_driver.return_value.send.assert_called_with(user, info) | ||
406 | 94 | |||
407 | 95 | def test_get_driver_default(self): | ||
408 | 96 | user = utils.get_test_user() | ||
409 | 97 | expected_driver = drivers.get_driver('messaging', 'fake') | ||
410 | 98 | |||
411 | 99 | actual_driver = messaging._get_driver(user) | ||
412 | 100 | self.assertEquals(actual_driver, expected_driver) | ||
413 | 101 | |||
414 | 102 | def test_get_driver_sms(self): | ||
415 | 103 | user = utils.get_test_user(messaging_driver='sms') | ||
416 | 104 | expected_driver = drivers.get_driver('messaging', 'sms') | ||
417 | 105 | |||
418 | 106 | actual_driver = messaging._get_driver(user) | ||
419 | 107 | self.assertEquals(actual_driver, expected_driver) | ||
420 | 108 | |||
421 | 109 | def test_get_driver_fake(self): | ||
422 | 110 | user = utils.get_test_user(messaging_driver='fake') | ||
423 | 111 | expected_driver = drivers.get_driver('messaging', 'fake') | ||
424 | 112 | |||
425 | 113 | actual_driver = messaging._get_driver(user) | ||
426 | 114 | self.assertEquals(actual_driver, expected_driver) | ||
427 | 0 | 115 | ||
428 | === added file 'surveilr/tests/utils.py' | |||
429 | --- surveilr/tests/utils.py 1970-01-01 00:00:00 +0000 | |||
430 | +++ surveilr/tests/utils.py 2011-11-30 09:36:24 +0000 | |||
431 | @@ -0,0 +1,35 @@ | |||
432 | 1 | """ | ||
433 | 2 | Surveilr - Log aggregation, analysis and visualisation | ||
434 | 3 | |||
435 | 4 | Copyright (C) 2011 Linux2Go | ||
436 | 5 | |||
437 | 6 | This program is free software: you can redistribute it and/or | ||
438 | 7 | modify it under the terms of the GNU Affero General Public License | ||
439 | 8 | as published by the Free Software Foundation, either version 3 of | ||
440 | 9 | the License, or (at your option) any later version. | ||
441 | 10 | |||
442 | 11 | This program is distributed in the hope that it will be useful, | ||
443 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
444 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
445 | 14 | GNU Affero General Public License for more details. | ||
446 | 15 | |||
447 | 16 | You should have received a copy of the GNU Affero General Public | ||
448 | 17 | License along with this program. If not, see | ||
449 | 18 | <http://www.gnu.org/licenses/>. | ||
450 | 19 | |||
451 | 20 | Utilities for unit tests | ||
452 | 21 | """ | ||
453 | 22 | |||
454 | 23 | from surveilr import models | ||
455 | 24 | |||
456 | 25 | |||
457 | 26 | def get_test_user(messaging_driver='fake', messaging_address='somewhere'): | ||
458 | 27 | return models.User(**{'id': 'user_id', | ||
459 | 28 | 'messaging_driver': messaging_driver, | ||
460 | 29 | 'messaging_address': messaging_address}) | ||
461 | 30 | |||
462 | 31 | |||
463 | 32 | def get_test_notification_info(): | ||
464 | 33 | return {'service': 'service_id', | ||
465 | 34 | 'state': 'normal', | ||
466 | 35 | 'previous_state': 'unexpected high'} |