|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from __future__ import print_function, absolute_import, division |
| 4 | + |
| 5 | +import os |
| 6 | +import six |
| 7 | +import hmac |
| 8 | +import json |
| 9 | +import hashlib |
| 10 | +import unittest |
| 11 | +import time |
| 12 | +from decimal import Decimal |
| 13 | + |
| 14 | +from pusher.authentication_client import AuthenticationClient |
| 15 | +from pusher.signature import sign, verify |
| 16 | + |
| 17 | +try: |
| 18 | + import unittest.mock as mock |
| 19 | +except ImportError: |
| 20 | + import mock |
| 21 | + |
| 22 | + |
| 23 | +class TestAuthenticationClient(unittest.TestCase): |
| 24 | + def test_authenticate_for_private_channels(self): |
| 25 | + authenticationClient = AuthenticationClient( |
| 26 | + key=u'foo', secret=u'bar', host=u'host', app_id=u'4', ssl=True) |
| 27 | + |
| 28 | + expected = { |
| 29 | + u'auth': u"foo:89955e77e1b40e33df6d515a5ecbba86a01dc816a5b720da18a06fd26f7d92ff" |
| 30 | + } |
| 31 | + |
| 32 | + self.assertEqual(authenticationClient.authenticate(u'private-channel', u'345.23'), expected) |
| 33 | + |
| 34 | + |
| 35 | + def test_authenticate_types(self): |
| 36 | + authenticationClient = AuthenticationClient( |
| 37 | + key=u'foo', secret=u'bar', host=u'host', app_id=u'4', ssl=True) |
| 38 | + |
| 39 | + self.assertRaises(TypeError, lambda: authenticationClient.authenticate(2423, u'34554')) |
| 40 | + self.assertRaises(TypeError, lambda: authenticationClient.authenticate(u'plah', 234234)) |
| 41 | + self.assertRaises(ValueError, lambda: authenticationClient.authenticate(u'::', u'345345')) |
| 42 | + |
| 43 | + |
| 44 | + def test_authenticate_for_presence_channels(self): |
| 45 | + authenticationClient = AuthenticationClient( |
| 46 | + key=u'foo', secret=u'bar', host=u'host', app_id=u'4', ssl=True) |
| 47 | + |
| 48 | + custom_data = { |
| 49 | + u'user_id': u'fred', |
| 50 | + u'user_info': { |
| 51 | + u'key': u'value' |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + expected = { |
| 56 | + u'auth': u"foo:e80ba6439492c2113022c39297a87a948de14061cc67b5788e045645a68b8ccd", |
| 57 | + u'channel_data': u"{\"user_id\":\"fred\",\"user_info\":{\"key\":\"value\"}}" |
| 58 | + } |
| 59 | + |
| 60 | + with mock.patch('json.dumps', return_value=expected[u'channel_data']) as dumps_mock: |
| 61 | + actual = authenticationClient.authenticate(u'presence-channel', u'345.43245', custom_data) |
| 62 | + |
| 63 | + self.assertEqual(actual, expected) |
| 64 | + dumps_mock.assert_called_once_with(custom_data, cls=None) |
| 65 | + |
| 66 | + |
| 67 | + def test_validate_webhook_success_case(self): |
| 68 | + authenticationClient = AuthenticationClient( |
| 69 | + key=u'foo', secret=u'bar', host=u'host', app_id=u'4', ssl=True) |
| 70 | + |
| 71 | + body = u'{"time_ms": 1000000}' |
| 72 | + signature = six.text_type(hmac.new(authenticationClient.secret.encode('utf8'), body.encode('utf8'), hashlib.sha256).hexdigest()) |
| 73 | + |
| 74 | + with mock.patch('time.time', return_value=1200): |
| 75 | + self.assertEqual(authenticationClient.validate_webhook(authenticationClient.key, signature, body), {u'time_ms': 1000000}) |
| 76 | + |
| 77 | + |
| 78 | + def test_validate_webhook_bad_types(self): |
| 79 | + authenticationClient = AuthenticationClient( |
| 80 | + key=u'foo', secret=u'bar', host=u'host', app_id=u'4', ssl=True) |
| 81 | + |
| 82 | + authenticationClient.validate_webhook(u'key', u'signature', u'body') |
| 83 | + |
| 84 | + # These things are meant to be human readable, so enforcing being |
| 85 | + # text is sensible. |
| 86 | + |
| 87 | + with mock.patch('time.time') as time_mock: |
| 88 | + self.assertRaises(TypeError, lambda: authenticationClient.validate_webhook(4, u'signature', u'body')) |
| 89 | + self.assertRaises(TypeError, lambda: authenticationClient.validate_webhook(u'key', 4, u'body')) |
| 90 | + self.assertRaises(TypeError, lambda: authenticationClient.validate_webhook(u'key', u'signature', 4)) |
| 91 | + |
| 92 | + time_mock.assert_not_called() |
| 93 | + |
| 94 | + |
| 95 | + def test_validate_webhook_bad_key(self): |
| 96 | + authenticationClient = AuthenticationClient( |
| 97 | + key=u'foo', secret=u'bar', host=u'host', app_id=u'4', ssl=True) |
| 98 | + |
| 99 | + body = u'some body' |
| 100 | + signature = six.text_type(hmac.new(authenticationClient.secret.encode(u'utf8'), body.encode(u'utf8'), hashlib.sha256).hexdigest()) |
| 101 | + |
| 102 | + with mock.patch('time.time') as time_mock: |
| 103 | + self.assertEqual(authenticationClient.validate_webhook(u'badkey', signature, body), None) |
| 104 | + |
| 105 | + time_mock.assert_not_called() |
| 106 | + |
| 107 | + |
| 108 | + def test_validate_webhook_bad_signature(self): |
| 109 | + authenticationClient = AuthenticationClient( |
| 110 | + key=u'foo', secret=u'bar', host=u'host', app_id=u'4', ssl=True) |
| 111 | + |
| 112 | + body = u'some body' |
| 113 | + signature = u'some signature' |
| 114 | + |
| 115 | + with mock.patch('time.time') as time_mock: |
| 116 | + self.assertEqual(authenticationClient.validate_webhook(authenticationClient.key, signature, body), None) |
| 117 | + |
| 118 | + time_mock.assert_not_called() |
| 119 | + |
| 120 | + |
| 121 | + def test_validate_webhook_bad_time(self): |
| 122 | + authenticationClient = AuthenticationClient( |
| 123 | + key=u'foo', secret=u'bar', host=u'host', app_id=u'4', ssl=True) |
| 124 | + |
| 125 | + body = u'{"time_ms": 1000000}' |
| 126 | + signature = six.text_type(hmac.new(authenticationClient.secret.encode('utf8'), body.encode('utf8'), hashlib.sha256).hexdigest()) |
| 127 | + |
| 128 | + with mock.patch('time.time', return_value=1301): |
| 129 | + self.assertEqual(authenticationClient.validate_webhook(authenticationClient.key, signature, body), None) |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == '__main__': |
| 133 | + unittest.main() |
0 commit comments