1
2
3
4 """Unit tests.
5
6 copyright: Copyright (c) Jeffrey Goettsch and other contributors.
7 license: BSD, see LICENSE for details.
8
9 """
10
11
12 import imp
13 import os
14 import unittest
15
16 from pushnotify import exceptions
17 from pushnotify import nma
18 from pushnotify import prowl
19 from pushnotify import pushover
20
21 try:
22 imp.find_module('nmakeys', [os.path.dirname(__file__)])
23 except ImportError:
24 NMA_API_KEYS = []
25 NMA_DEVELOPER_KEY = ''
26 else:
27 from pushnotify.tests.nmakeys import API_KEYS as NMA_API_KEYS
28 from pushnotify.tests.nmakeys import DEVELOPER_KEY as NMA_DEVELOPER_KEY
29 try:
30 imp.find_module('prowlkeys', [os.path.dirname(__file__)])
31 except ImportError:
32 PROWL_API_KEYS = []
33 PROWL_PROVIDER_KEY = ''
34 PROWL_REG_TOKEN = ''
35 else:
36 from pushnotify.tests.prowlkeys import API_KEYS as PROWL_API_KEYS
37 from pushnotify.tests.prowlkeys import PROVIDER_KEY as PROWL_PROVIDER_KEY
38 from pushnotify.tests.prowlkeys import REG_TOKEN as PROWL_REG_TOKEN
39 try:
40 imp.find_module('pushoverkeys', [os.path.dirname(__file__)])
41 except ImportError:
42 PUSHOVER_TOKEN = ''
43 PUSHOVER_USER = ''
44 PUSHOVER_DEVICE = ''
45 else:
46 from pushnotify.tests.pushoverkeys import TOKEN as PUSHOVER_TOKEN
47 from pushnotify.tests.pushoverkeys import USER as PUSHOVER_USER
48 from pushnotify.tests.pushoverkeys import DEVICE as PUSHOVER_DEVICE
49
50
52 """Test the Notify my Android client.
53
54 """
55
57
58 self.client = nma.Client(NMA_API_KEYS, NMA_DEVELOPER_KEY)
59
60 self.app = 'pushnotify unit tests'
61 self.event = 'unit test: test_notify'
62 self.desc = 'valid notification test for pushnotify'
63
65 """Test notify with valid notifications.
66
67 """
68
69
70
71 self.client.notify(self.app, self.event, self.desc)
72
73
74
75 html_desc = '<h1>{0}</h1><p>{1}<br>{2}</p>'.format(
76 self.app, self.event, self.desc)
77 priority = 0
78 url = nma.NOTIFY_URL
79
80 self.client.notify(self.app, self.event, html_desc,
81 kwargs={'priority': priority, 'url': url,
82 'content-type': 'text/html'})
83
85 """Test notify with invalid notifications.
86
87 """
88
89
90
91 char = self.client.apikeys[0][0]
92 apikey = self.client.apikeys[0].replace(char, '_')
93 self.client.apikeys = [apikey, ]
94 self.client.developerkey = ''
95
96 self.assertRaises(exceptions.ApiKeyError,
97 self.client.notify, self.app, self.event, self.desc)
98
99 self.client.apikeys = NMA_API_KEYS
100 self.client.developerkey = NMA_DEVELOPER_KEY
101
102
103
104 bad_app = 'a' * 257
105 self.assertRaises(exceptions.FormatError,
106 self.client.notify, bad_app, self.event, self.desc)
107
109 """Test verify with a valid API key.
110
111 """
112
113 self.assertTrue(self.client.verify(self.client.apikeys[0]))
114
116 """Test verify with invalid API keys.
117
118 """
119
120
121
122 apikey = u'{0}{1}'.format(self.client.apikeys[0], '1')
123
124 self.assertFalse(self.client.verify(apikey))
125
126
127
128 char = self.client.apikeys[0][0]
129 apikey = self.client.apikeys[0].replace(char, '_')
130
131 self.assertFalse(self.client.verify(apikey))
132
133
135 """Test the Prowl client.
136
137 """
138
140
141 self.client = prowl.Client(PROWL_API_KEYS, PROWL_PROVIDER_KEY)
142
143 self.app = 'pushnotify unit tests'
144 self.event = 'unit test: test_notify'
145 self.desc = 'valid notification test for pushnotify'
146
148 """Test notify with a valid notification.
149
150 """
151
152 self.client.notify(self.app, self.event, self.desc,
153 kwargs={'priority': 0, 'url': 'http://google.com/'})
154
156 """Test notify with invalid notifications.
157
158 """
159
160
161
162 char = self.client.apikeys[0][0]
163 apikey = self.client.apikeys[0].replace(char, '_')
164 self.client.apikeys = [apikey, ]
165 self.client.developerkey = ''
166
167 self.assertRaises(exceptions.ApiKeyError,
168 self.client.notify, self.app, self.event, self.desc)
169
170 self.client.apikeys = NMA_API_KEYS
171 self.client.developerkey = NMA_DEVELOPER_KEY
172
173
174
175 bad_app = 'a' * 257
176 self.assertRaises(exceptions.FormatError,
177 self.client.notify, bad_app, self.event, self.desc)
178
180 """Test retrieve_apikey with a valid token.
181
182 """
183
184 apikey = self.client.retrieve_apikey(PROWL_REG_TOKEN)
185 self.assertTrue(apikey)
186 self.assertIs(type(apikey), str)
187
203
205 """Test retrieve_token with a valid providerkey.
206
207 """
208
209 token = self.client.retrieve_token()
210 self.assertTrue(token)
211 self.assertEqual(len(token), 2)
212 self.assertIs(type(token[0]), str)
213 self.assertIs(type(token[1]), str)
214
216 """Test retrieve_token with an invalid providerkey.
217
218 """
219
220 self.client.providerkey = self.client.providerkey[0:-1]
221 self.assertRaises(exceptions.ProviderKeyError,
222 self.client.retrieve_token)
223
225 """Test verify_user with a valid API key.
226
227 """
228
229 self.assertTrue(self.client.verify_user(self.client.apikeys[0]))
230
232 """Test verify_user with invalid API keys.
233
234 """
235
236
237
238 apikey = u'{0}{1}'.format(self.client.apikeys[0], '1')
239
240 self.assertFalse(self.client.verify_user(apikey))
241
242
243
244 char = self.client.apikeys[0][0]
245 apikey = self.client.apikeys[0].replace(char, '_')
246
247 self.assertFalse(self.client.verify_user(apikey))
248
249
251 """Test the Pushover client.
252
253 """
254
256
257 self.client = pushover.Client(PUSHOVER_TOKEN,
258 [(PUSHOVER_USER, PUSHOVER_DEVICE)])
259
260 self.title = 'pushnotify unit tests'
261 self.message = 'valid notification test for pushnotify'
262
264 """Test notify with a valid notification.
265
266 """
267
268 self.client.notify(self.title, self.message,
269 kwargs={'priority': 1, 'url': 'http://google.com/',
270 'url_title': 'Google'})
271
273 """Test notify with an invalid token.
274
275 """
276
277 char = self.client.token[0]
278 bad_token = self.client.token.replace(char, '_')
279 self.client.token = bad_token
280
281 self.assertRaises(exceptions.ApiKeyError, self.client.notify,
282 self.title, self.message)
283
285 """Test notify with an invalid user.
286
287 """
288
289 char = self.client.users[0][0][0]
290 bad_users = (self.client.users[0][0].replace(char, '_'),
291 PUSHOVER_DEVICE)
292 self.client.users = bad_users
293
294 self.assertRaises(exceptions.ApiKeyError, self.client.notify,
295 self.title, self.message)
296
298 """Test notify with an invalid device.
299
300 """
301
302 char = self.client.users[0][1][0]
303 bad_users = (PUSHOVER_USER, self.client.users[0][1].replace(char, '_'))
304 self.client.users = bad_users
305
306 self.assertRaises(exceptions.ApiKeyError, self.client.notify,
307 self.title, self.message)
308
310 """Test notify with invalid argument lengths.
311
312 """
313
314
315
316
317
318 msg = 'a' * 513
319
320 try:
321 self.client.notify(self.title, msg)
322 except exceptions.FormatError:
323 pass
324
326 """Test veriy_user with a valid user token.
327
328 """
329
330 self.assertTrue(self.client.verify_user(PUSHOVER_USER))
331
333 """Test verify_user with an invalid user token.
334
335 """
336
337 self.assertFalse(self.client.verify_user('foo'))
338
340 """Test verify_device with a valid device string.
341
342 """
343
344 self.assertTrue(self.client.verify_device(PUSHOVER_USER,
345 PUSHOVER_DEVICE))
346
348 """Test verify_device with an invalid device string.
349
350 """
351
352 self.assertFalse(self.client.verify_device(PUSHOVER_USER, 'foo'))
353
355 """Test verify_device with an invalid user token.
356
357 """
358
359 self.assertRaises(exceptions.ApiKeyError, self.client.verify_device,
360 'foo', PUSHOVER_DEVICE)
361
362
363 if __name__ == '__main__':
364 pass
365