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

Source Code for Module pushnotify.tests.tests

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