Source

py-pushnotify / pushnotify / tests / tests.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#!/usr/bin/env python
# vim: set fileencoding=utf-8

"""Unit tests.

copyright: Copyright (c) Jeffrey Goettsch and other contributors.
license: BSD, see LICENSE for details.

"""


import imp
import os
import unittest

from pushnotify import abstract
from pushnotify import get_client
from pushnotify import exceptions
from pushnotify import nma
from pushnotify import prowl
from pushnotify import pushover

try:
    imp.find_module('nmakeys', [os.path.dirname(__file__)])
except ImportError:
    NMA_API_KEYS = []
    NMA_DEVELOPER_KEY = ''
else:
    from pushnotify.tests.nmakeys import API_KEYS as NMA_API_KEYS
    from pushnotify.tests.nmakeys import DEVELOPER_KEY as NMA_DEVELOPER_KEY
try:
    imp.find_module('prowlkeys', [os.path.dirname(__file__)])
except ImportError:
    PROWL_API_KEYS = []
    PROWL_PROVIDER_KEY = ''
    PROWL_REG_TOKEN = ''
else:
    from pushnotify.tests.prowlkeys import API_KEYS as PROWL_API_KEYS
    from pushnotify.tests.prowlkeys import PROVIDER_KEY as PROWL_PROVIDER_KEY
    from pushnotify.tests.prowlkeys import REG_TOKEN as PROWL_REG_TOKEN
try:
    imp.find_module('pushoverkeys', [os.path.dirname(__file__)])
except ImportError:
    PUSHOVER_TOKEN = ''
    PUSHOVER_USER = ''
    PUSHOVER_DEVICE = ''
else:
    from pushnotify.tests.pushoverkeys import TOKEN as PUSHOVER_TOKEN
    from pushnotify.tests.pushoverkeys import USER as PUSHOVER_USER


class PushnotifyTest(unittest.TestCase):

    def setUp(self):

        pass

    def test_get_client_nma(self):
        """Test get_client for type='nma'.

        """

        client = get_client('nma', NMA_DEVELOPER_KEY, 'pushnotify unit tests')
        self.assertTrue(client._type == 'nma')
        self.assertTrue(isinstance(client, nma.Client))

    def test_get_client_prowl(self):
        """Test get_client for type='prowl'.

        """

        client = get_client('prowl', PROWL_PROVIDER_KEY,
                            'pushnotify unit tests')
        self.assertTrue(client._type == 'prowl')
        self.assertTrue(isinstance(client, prowl.Client))

    def test_get_client_pushover(self):
        """Test get_client for type='pushover'.

        """

        client = get_client('pushover', PUSHOVER_TOKEN,
                            'pushnotify unit tests')
        self.assertTrue(client._type == 'pushover')
        self.assertTrue(isinstance(client, pushover.Client))


class AbstractClientTest(unittest.TestCase):
    """Test the AbstractClient class.

    """

    def setUp(self):

        self.client = abstract.AbstractClient()

    def test_add_key_apikey(self):
        """Test the add_key method with an apikey.

        """

        apikey = 'foo'
        self.assertTrue(apikey not in self.client.apikeys.keys())

        self.client.add_key(apikey)
        self.assertTrue(apikey in self.client.apikeys.keys())

    def test_add_key_device_key(self):
        """Test the add_key method with a device_key.

        """

        apikey = 'foo'
        self.client.add_key('foo')

        device_key = 'bar'
        self.assertTrue(device_key not in self.client.apikeys[apikey])

        self.client.add_key(apikey, device_key)
        self.assertTrue(device_key in self.client.apikeys[apikey])

    def test_del_key_apikey(self):

        apikey = 'foo'
        self.client.add_key(apikey)
        self.assertTrue(apikey in self.client.apikeys.keys())

        self.client.del_key(apikey)
        self.assertTrue(apikey not in self.client.apikeys.keys())

    def test_del_key_device_key(self):

        apikey = 'foo'
        device_key = 'bar'
        self.client.add_key(apikey, device_key)
        self.assertTrue(device_key in self.client.apikeys[apikey])

        self.client.del_key(apikey, device_key)
        self.assertTrue(device_key not in self.client.apikeys[apikey])


class NMATest(unittest.TestCase):
    """Test the Notify my Android client.

    """

    def setUp(self):

        self.client = get_client('nma', NMA_DEVELOPER_KEY,
                                 'pushnotify unit tests')

        for key in NMA_API_KEYS:
            self.client.add_key(key)

        self.event = 'unit test: test_notify'
        self.desc = 'valid notification test for pushnotify'

    def test_notify_valid(self):
        """Test nma.Client.notify with a valid notification.

        """

        html_desc = '<h1>{0}</h1><p>{1}<br>{2}</p>'.format(
            self.client.application, self.desc, self.event)
        priority = 0
        url = nma.NOTIFY_URL

        self.client.notify(html_desc, self.event, split=False,
                           kwargs={'priority': priority, 'url': url,
                                   'content-type': 'text/html'})

    def test_notify_valid_split(self):
        """Test nma.Client.notify with a valid notification, splitting
        up a long description.

        """

        long_desc = 'a' * 10101
        self.client.notify(long_desc, self.event, split=True)

    def test_notify_invalid_apikey(self):
        """Test nma.Client.notify with an invalid API key.

        """

        char = self.client.apikeys.keys()[0][0]
        apikey = self.client.apikeys.keys()[0].replace(char, '_')
        self.client.apikeys = {}
        self.client.add_key(apikey)
        self.client.developerkey = ''

        self.assertRaises(exceptions.ApiKeyError,
                          self.client.notify, self.desc, self.event)

    def test_notify_invalid_argument_lengths(self):
        """Test nma.Client.notify with invalid argument lengths.

        """

        long_desc = 'a' * 10001
        self.assertRaises(exceptions.FormatError,
                          self.client.notify, long_desc, self.event,
                          split=False)

    def test_verify_user_valid(self):
        """Test nma.Client.verify_user with a valid API key.

        """

        self.assertTrue(self.client.verify_user(self.client.apikeys.keys()[0]))

    def test_verify_user_invalid_apikey(self):
        """Test nma.Client.verify_user with an invalid API key.

        """

        char = self.client.apikeys.keys()[0][0]
        apikey = self.client.apikeys.keys()[0].replace(char, '_')

        self.assertFalse(self.client.verify_user(apikey))


class ProwlTest(unittest.TestCase):
    """Test the Prowl client.

    """

    def setUp(self):

        self.client = get_client('prowl', PROWL_PROVIDER_KEY,
                                 'pushnotify unit tests')

        for key in PROWL_API_KEYS:
            self.client.add_key(key)

        self.event = 'unit test: test_notify'
        self.desc = 'valid notification test for pushnotify'

    def test_notify_valid(self):
        """Test prowl.Client.notify with valid notifications.

        """

        self.client.notify(self.desc, self.event, split=False,
                           kwargs={'priority': 0, 'url': 'http://google.com/'})

    def test_notify_valid_split(self):
        """Test nma.Client.notify with a valid notification, splitting
        up a long description.

        """

        long_desc = 'a' * 10101
        self.client.notify(long_desc, self.event, split=True)

    def test_notify_invalid_apikey(self):
        """Test prowl.Client.notify with an invalid API key.

        """

        char = self.client.apikeys.keys()[0][0]
        apikey = self.client.apikeys.keys()[0].replace(char, '_')
        self.client.apikeys = {}
        self.client.add_key(apikey)
        self.client.developerkey = ''

        self.assertRaises(exceptions.ApiKeyError,
                          self.client.notify, self.desc, self.event)

    def test_notify_invalid_argument_lengths(self):
        """Test prowl.Client.notify with invalid argument lengths.

        """

        bad_desc = 'a' * 10001
        self.assertRaises(exceptions.FormatError,
                          self.client.notify, bad_desc, self.event, False)

    def test_retrieve_apikey_valid(self):
        """Test prowl.Client.retrieve_apikey with a valid token.

        """

        apikey = self.client.retrieve_apikey(PROWL_REG_TOKEN)
        self.assertTrue(apikey)
        self.assertIs(type(apikey), str)

    def test_retrieve_apikey_invalid_reg_token(self):
        """Test prowl.Client.retrieve_apikey with an invalid
        registration token.

        """

        self.assertRaises(exceptions.PermissionDenied,
                          self.client.retrieve_apikey, PROWL_REG_TOKEN[0:-1])

    def test_retrieve_apikey_invalid_developerkey(self):
        """Test prowl.Client.retrieve_apikey with an invalid developer
        key.

        """

        self.client.developerkey = self.client.developerkey[0:-1]
        self.assertRaises(exceptions.ProviderKeyError,
                          self.client.retrieve_apikey, PROWL_REG_TOKEN)

    def test_retrieve_token_valid(self):
        """Test prowl.Client.retrieve_token with a valid developer key.

        """

        token = self.client.retrieve_token()
        self.assertTrue(token)
        self.assertEqual(len(token), 2)
        self.assertIs(type(token[0]), str)
        self.assertIs(type(token[1]), str)

    def test_retrieve_token_invalid(self):
        """Test prowl.Client.retrieve_token with an invalid providerkey.

        """

        self.client.developerkey = self.client.developerkey[0:-1]
        self.assertRaises(exceptions.ProviderKeyError,
                          self.client.retrieve_token)

    def test_verify_user_valid(self):
        """Test prowl.Client.verify_user with a valid API key.

        """

        self.assertTrue(self.client.verify_user(self.client.apikeys.keys()[0]))

    def test_verify_user_invalid(self):
        """Test prowl.Client.verify_user with invalid API keys.

        """

        char = self.client.apikeys.keys()[0][0]
        apikey = self.client.apikeys.keys()[0].replace(char, '_')

        self.assertFalse(self.client.verify_user(apikey))


class PushoverTest(unittest.TestCase):
    """Test the Pushover client.

    """

    def setUp(self):

        self.client = get_client('pushover', PUSHOVER_TOKEN, '')

        for key in PUSHOVER_USER.keys():
            self.client.add_key(key, PUSHOVER_USER[key][0])

        self.event = 'pushnotify unit tests'
        self.desc = 'valid notification test for pushnotify'

    def test_notify_valid(self):
        """Test pushover.Client.notify with a valid notification.

        """

        self.client.notify(self.desc, self.event, split=False,
                           kwargs={'priority': 1, 'url': 'http://google.com/',
                                   'url_title': 'Google'})

    def test_notify_valid_split(self):
        """Test pushover.Client.notify with a valid notification,
        splitting up a long description.

        """

        long_desc = 'a' * 513
        self.client.notify(long_desc, self.event, split=True)

    def test_notify_invalid_developerkey(self):
        """Test pushover.Client.notify with an invalid developer key.

        """

        self.client.developerkey = '_' + self.client.developerkey[1:]

        self.assertRaises(exceptions.ApiKeyError, self.client.notify,
                          self.desc, self.event)

    def test_notify_invalid_apikey(self):
        """Test pushover.Client.notify with an invalid API key.

        """

        apikey = self.client.apikeys.keys()[0]
        device_key = self.client.apikeys[apikey][0]

        apikey = '_' + apikey[1:]

        self.client.apikeys = {}
        self.client.add_key(apikey, device_key)

        self.assertRaises(exceptions.ApiKeyError, self.client.notify,
                          self.desc, self.event)

    def test_notify_invalid_device_key(self):
        """Test pushover.Client.notify with an invalid device key.

        """

        apikey = self.client.apikeys.keys()[0]

        self.client.apikeys = {}
        self.client.add_key(apikey, 'foo')

        self.assertRaises(exceptions.ApiKeyError, self.client.notify,
                          self.desc, self.event)

    def test_notify_invalid_argument_lengths(self):
        """Test pushover.Client.notify with invalid argument lengths.

        """

        # as of 2012-09-18, this is not returning a 4xx status code as
        # per the Pushover API docs, but instead chopping the delivered
        # messages off at 512 characters

        desc = 'a' * 513

        try:
            self.client.notify(desc, self.event, False)
        except exceptions.FormatError:
            pass

    def test_verify_user_valid(self):
        """Test pushover.Client.verify_user with a valid API key.

        """

        self.assertTrue(self.client.verify_user(self.client.apikeys.keys()[0]))

    def test_verify_user_invalid(self):
        """Test pushover.Client.verify_user with an invalid API key.

        """

        self.assertFalse(self.client.verify_user('foo'))

    def test_verify_device_valid(self):
        """Test pushover.Client.verify_device with a valid device key.

        """

        apikey = self.client.apikeys.keys()[0]
        device_key = self.client.apikeys[apikey][0]

        self.assertTrue(self.client.verify_device(apikey, device_key))

    def test_verify_device_invalid(self):
        """Test pushover.Client.verify_device with an invalid device
        key.

        """

        apikey = self.client.apikeys.keys()[0]

        self.assertFalse(self.client.verify_device(apikey, 'foo'))

    def test_verify_device_invalid_apikey(self):
        """Test pushover.Client.verify_device with an invalid API key.

        """

        apikey = self.client.apikeys.keys()[0]
        device_key = self.client.apikeys[apikey][0]

        self.assertRaises(exceptions.ApiKeyError, self.client.verify_device,
                          'foo', device_key)


if __name__ == '__main__':
    pass
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.