Package pushnotify :: Package tests :: Module tests
[hide private]
[frames] | no frames]

Source Code for Module pushnotify.tests.tests

  1  #!/usr/bin/env python 
  2  # vim: set fileencoding=utf-8 
  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 abstract 
 17  from pushnotify import get_client 
 18  from pushnotify import exceptions 
 19  from pushnotify import nma 
 20  from pushnotify import prowl 
 21  from pushnotify import pushover 
 22   
 23  try: 
 24      imp.find_module('nmakeys', [os.path.dirname(__file__)]) 
 25  except ImportError: 
 26      NMA_API_KEYS = [] 
 27      NMA_DEVELOPER_KEY = '' 
 28  else: 
 29      from pushnotify.tests.nmakeys import API_KEYS as NMA_API_KEYS 
 30      from pushnotify.tests.nmakeys import DEVELOPER_KEY as NMA_DEVELOPER_KEY 
 31  try: 
 32      imp.find_module('prowlkeys', [os.path.dirname(__file__)]) 
 33  except ImportError: 
 34      PROWL_API_KEYS = [] 
 35      PROWL_PROVIDER_KEY = '' 
 36      PROWL_REG_TOKEN = '' 
 37  else: 
 38      from pushnotify.tests.prowlkeys import API_KEYS as PROWL_API_KEYS 
 39      from pushnotify.tests.prowlkeys import PROVIDER_KEY as PROWL_PROVIDER_KEY 
 40      from pushnotify.tests.prowlkeys import REG_TOKEN as PROWL_REG_TOKEN 
 41  try: 
 42      imp.find_module('pushoverkeys', [os.path.dirname(__file__)]) 
 43  except ImportError: 
 44      PUSHOVER_TOKEN = '' 
 45      PUSHOVER_USER = '' 
 46      PUSHOVER_DEVICE = '' 
 47  else: 
 48      from pushnotify.tests.pushoverkeys import TOKEN as PUSHOVER_TOKEN 
 49      from pushnotify.tests.pushoverkeys import USER as PUSHOVER_USER 
 50   
 51   
52 -class PushnotifyTest(unittest.TestCase):
53
54 - def setUp(self):
55 56 pass
57
58 - def test_get_client_nma(self):
59 """Test get_client for type='nma'. 60 61 """ 62 63 client = get_client('nma', NMA_DEVELOPER_KEY, 'pushnotify unit tests') 64 self.assertTrue(client._type == 'nma') 65 self.assertTrue(isinstance(client, nma.Client))
66
67 - def test_get_client_prowl(self):
68 """Test get_client for type='prowl'. 69 70 """ 71 72 client = get_client('prowl', PROWL_PROVIDER_KEY, 73 'pushnotify unit tests') 74 self.assertTrue(client._type == 'prowl') 75 self.assertTrue(isinstance(client, prowl.Client))
76
77 - def test_get_client_pushover(self):
78 """Test get_client for type='pushover'. 79 80 """ 81 82 client = get_client('pushover', PUSHOVER_TOKEN, 83 'pushnotify unit tests') 84 self.assertTrue(client._type == 'pushover') 85 self.assertTrue(isinstance(client, pushover.Client))
86 87
88 -class AbstractClientTest(unittest.TestCase):
89 """Test the AbstractClient class. 90 91 """ 92
93 - def setUp(self):
94 95 self.client = abstract.AbstractClient()
96
97 - def test_add_key_apikey(self):
98 """Test the add_key method with an apikey. 99 100 """ 101 102 apikey = 'foo' 103 self.assertTrue(apikey not in self.client.apikeys.keys()) 104 105 self.client.add_key(apikey) 106 self.assertTrue(apikey in self.client.apikeys.keys())
107
108 - def test_add_key_device_key(self):
109 """Test the add_key method with a device_key. 110 111 """ 112 113 apikey = 'foo' 114 self.client.add_key('foo') 115 116 device_key = 'bar' 117 self.assertTrue(device_key not in self.client.apikeys[apikey]) 118 119 self.client.add_key(apikey, device_key) 120 self.assertTrue(device_key in self.client.apikeys[apikey])
121
122 - def test_del_key_apikey(self):
123 124 apikey = 'foo' 125 self.client.add_key(apikey) 126 self.assertTrue(apikey in self.client.apikeys.keys()) 127 128 self.client.del_key(apikey) 129 self.assertTrue(apikey not in self.client.apikeys.keys())
130
131 - def test_del_key_device_key(self):
132 133 apikey = 'foo' 134 device_key = 'bar' 135 self.client.add_key(apikey, device_key) 136 self.assertTrue(device_key in self.client.apikeys[apikey]) 137 138 self.client.del_key(apikey, device_key) 139 self.assertTrue(device_key not in self.client.apikeys[apikey])
140 141
142 -class NMATest(unittest.TestCase):
143 """Test the Notify my Android client. 144 145 """ 146
147 - def setUp(self):
148 149 self.client = get_client('nma', NMA_DEVELOPER_KEY, 150 'pushnotify unit tests') 151 152 for key in NMA_API_KEYS: 153 self.client.add_key(key) 154 155 self.event = 'unit test: test_notify' 156 self.desc = 'valid notification test for pushnotify'
157
158 - def test_notify_valid(self):
159 """Test nma.Client.notify with a valid notification. 160 161 """ 162 163 html_desc = '<h1>{0}</h1><p>{1}<br>{2}</p>'.format( 164 self.client.application, self.desc, self.event) 165 priority = 0 166 url = nma.NOTIFY_URL 167 168 self.client.notify(html_desc, self.event, split=False, 169 kwargs={'priority': priority, 'url': url, 170 'content-type': 'text/html'})
171
172 - def test_notify_valid_split(self):
173 """Test nma.Client.notify with a valid notification, splitting 174 up a long description. 175 176 """ 177 178 long_desc = 'a' * 10101 179 self.client.notify(long_desc, self.event, split=True)
180
182 """Test nma.Client.notify with an invalid API key. 183 184 """ 185 186 char = self.client.apikeys.keys()[0][0] 187 apikey = self.client.apikeys.keys()[0].replace(char, '_') 188 self.client.apikeys = {} 189 self.client.add_key(apikey) 190 self.client.developerkey = '' 191 192 self.assertRaises(exceptions.ApiKeyError, 193 self.client.notify, self.desc, self.event)
194
196 """Test nma.Client.notify with invalid argument lengths. 197 198 """ 199 200 long_desc = 'a' * 10001 201 self.assertRaises(exceptions.FormatError, 202 self.client.notify, long_desc, self.event, 203 split=False)
204
205 - def test_verify_user_valid(self):
206 """Test nma.Client.verify_user with a valid API key. 207 208 """ 209 210 self.assertTrue(self.client.verify_user(self.client.apikeys.keys()[0]))
211
213 """Test nma.Client.verify_user with an invalid API key. 214 215 """ 216 217 char = self.client.apikeys.keys()[0][0] 218 apikey = self.client.apikeys.keys()[0].replace(char, '_') 219 220 self.assertFalse(self.client.verify_user(apikey))
221 222
223 -class ProwlTest(unittest.TestCase):
224 """Test the Prowl client. 225 226 """ 227
228 - def setUp(self):
229 230 self.client = get_client('prowl', PROWL_PROVIDER_KEY, 231 'pushnotify unit tests') 232 233 for key in PROWL_API_KEYS: 234 self.client.add_key(key) 235 236 self.event = 'unit test: test_notify' 237 self.desc = 'valid notification test for pushnotify'
238
239 - def test_notify_valid(self):
240 """Test prowl.Client.notify with valid notifications. 241 242 """ 243 244 self.client.notify(self.desc, self.event, split=False, 245 kwargs={'priority': 0, 'url': 'http://google.com/'})
246
247 - def test_notify_valid_split(self):
248 """Test nma.Client.notify with a valid notification, splitting 249 up a long description. 250 251 """ 252 253 long_desc = 'a' * 10101 254 self.client.notify(long_desc, self.event, split=True)
255
257 """Test prowl.Client.notify with an invalid API key. 258 259 """ 260 261 char = self.client.apikeys.keys()[0][0] 262 apikey = self.client.apikeys.keys()[0].replace(char, '_') 263 self.client.apikeys = {} 264 self.client.add_key(apikey) 265 self.client.developerkey = '' 266 267 self.assertRaises(exceptions.ApiKeyError, 268 self.client.notify, self.desc, self.event)
269
271 """Test prowl.Client.notify with invalid argument lengths. 272 273 """ 274 275 bad_desc = 'a' * 10001 276 self.assertRaises(exceptions.FormatError, 277 self.client.notify, bad_desc, self.event, False)
278
280 """Test prowl.Client.retrieve_apikey with a valid token. 281 282 """ 283 284 apikey = self.client.retrieve_apikey(PROWL_REG_TOKEN) 285 self.assertTrue(apikey) 286 self.assertIs(type(apikey), str)
287
289 """Test prowl.Client.retrieve_apikey with an invalid 290 registration token. 291 292 """ 293 294 self.assertRaises(exceptions.PermissionDenied, 295 self.client.retrieve_apikey, PROWL_REG_TOKEN[0:-1])
296
298 """Test prowl.Client.retrieve_apikey with an invalid developer 299 key. 300 301 """ 302 303 self.client.developerkey = self.client.developerkey[0:-1] 304 self.assertRaises(exceptions.ProviderKeyError, 305 self.client.retrieve_apikey, PROWL_REG_TOKEN)
306
308 """Test prowl.Client.retrieve_token with a valid developer key. 309 310 """ 311 312 token = self.client.retrieve_token() 313 self.assertTrue(token) 314 self.assertEqual(len(token), 2) 315 self.assertIs(type(token[0]), str) 316 self.assertIs(type(token[1]), str)
317
319 """Test prowl.Client.retrieve_token with an invalid providerkey. 320 321 """ 322 323 self.client.developerkey = self.client.developerkey[0:-1] 324 self.assertRaises(exceptions.ProviderKeyError, 325 self.client.retrieve_token)
326
327 - def test_verify_user_valid(self):
328 """Test prowl.Client.verify_user with a valid API key. 329 330 """ 331 332 self.assertTrue(self.client.verify_user(self.client.apikeys.keys()[0]))
333
334 - def test_verify_user_invalid(self):
335 """Test prowl.Client.verify_user with invalid API keys. 336 337 """ 338 339 char = self.client.apikeys.keys()[0][0] 340 apikey = self.client.apikeys.keys()[0].replace(char, '_') 341 342 self.assertFalse(self.client.verify_user(apikey))
343 344
345 -class PushoverTest(unittest.TestCase):
346 """Test the Pushover client. 347 348 """ 349
350 - def setUp(self):
351 352 self.client = get_client('pushover', PUSHOVER_TOKEN, '') 353 354 for key in PUSHOVER_USER.keys(): 355 self.client.add_key(key, PUSHOVER_USER[key][0]) 356 357 self.event = 'pushnotify unit tests' 358 self.desc = 'valid notification test for pushnotify'
359
360 - def test_notify_valid(self):
361 """Test pushover.Client.notify with a valid notification. 362 363 """ 364 365 self.client.notify(self.desc, self.event, split=False, 366 kwargs={'priority': 1, 'url': 'http://google.com/', 367 'url_title': 'Google'})
368
369 - def test_notify_valid_split(self):
370 """Test pushover.Client.notify with a valid notification, 371 splitting up a long description. 372 373 """ 374 375 long_desc = 'a' * 513 376 self.client.notify(long_desc, self.event, split=True)
377
379 """Test pushover.Client.notify with an invalid developer key. 380 381 """ 382 383 self.client.developerkey = '_' + self.client.developerkey[1:] 384 385 self.assertRaises(exceptions.ApiKeyError, self.client.notify, 386 self.desc, self.event)
387
389 """Test pushover.Client.notify with an invalid API key. 390 391 """ 392 393 apikey = self.client.apikeys.keys()[0] 394 device_key = self.client.apikeys[apikey][0] 395 396 apikey = '_' + apikey[1:] 397 398 self.client.apikeys = {} 399 self.client.add_key(apikey, device_key) 400 401 self.assertRaises(exceptions.ApiKeyError, self.client.notify, 402 self.desc, self.event)
403
405 """Test pushover.Client.notify with an invalid device key. 406 407 """ 408 409 apikey = self.client.apikeys.keys()[0] 410 411 self.client.apikeys = {} 412 self.client.add_key(apikey, 'foo') 413 414 self.assertRaises(exceptions.ApiKeyError, self.client.notify, 415 self.desc, self.event)
416
418 """Test pushover.Client.notify with invalid argument lengths. 419 420 """ 421 422 # as of 2012-09-18, this is not returning a 4xx status code as 423 # per the Pushover API docs, but instead chopping the delivered 424 # messages off at 512 characters 425 426 desc = 'a' * 513 427 428 try: 429 self.client.notify(desc, self.event, False) 430 except exceptions.FormatError: 431 pass
432
433 - def test_verify_user_valid(self):
434 """Test pushover.Client.verify_user with a valid API key. 435 436 """ 437 438 self.assertTrue(self.client.verify_user(self.client.apikeys.keys()[0]))
439
440 - def test_verify_user_invalid(self):
441 """Test pushover.Client.verify_user with an invalid API key. 442 443 """ 444 445 self.assertFalse(self.client.verify_user('foo'))
446
447 - def test_verify_device_valid(self):
448 """Test pushover.Client.verify_device with a valid device key. 449 450 """ 451 452 apikey = self.client.apikeys.keys()[0] 453 device_key = self.client.apikeys[apikey][0] 454 455 self.assertTrue(self.client.verify_device(apikey, device_key))
456
458 """Test pushover.Client.verify_device with an invalid device 459 key. 460 461 """ 462 463 apikey = self.client.apikeys.keys()[0] 464 465 self.assertFalse(self.client.verify_device(apikey, 'foo'))
466
468 """Test pushover.Client.verify_device with an invalid API key. 469 470 """ 471 472 apikey = self.client.apikeys.keys()[0] 473 device_key = self.client.apikeys[apikey][0] 474 475 self.assertRaises(exceptions.ApiKeyError, self.client.verify_device, 476 'foo', device_key)
477 478 479 if __name__ == '__main__': 480 pass 481