Skip to content

Commit d6d7628

Browse files
author
Callum Oakley
committed
moved relevant tests in to test_authentication_client.py
1 parent 40f17c8 commit d6d7628

File tree

5 files changed

+157
-104
lines changed

5 files changed

+157
-104
lines changed

pusher/authentication_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
ensure_text,
1111
validate_channel,
1212
validate_socket_id,
13-
data_to_string)
13+
channel_name_re)
1414

1515
from pusher.client import Client
1616
from pusher.http import GET, POST, Request, request_method
17+
from pusher.signature import sign, verify
1718

1819
import collections
1920
import hashlib

pusher/pusher.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@
88

99
from pusher.util import (
1010
ensure_text,
11-
validate_channel,
12-
validate_socket_id,
1311
pusher_url_re,
14-
channel_name_re,
1512
doc_string)
1613

17-
from pusher.signature import sign, verify
1814
from pusher.pusher_client import PusherClient
1915
from pusher.notification_client import NotificationClient
2016
from pusher.authentication_client import AuthenticationClient
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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()

pusher_tests/test_client.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
try:
1818
import unittest.mock as mock
19+
1920
except ImportError:
2021
import mock
2122

@@ -24,25 +25,36 @@ class TestClient(unittest.TestCase):
2425
def test_should_be_constructable(self):
2526
Client(app_id=u'4', key=u'key', secret=u'secret', ssl=False)
2627

28+
2729
def test_app_id_should_be_text_if_present(self):
28-
self.assertRaises(TypeError, lambda: Client(app_id=4, key=u'key', secret=u'secret', ssl=False))
30+
self.assertRaises(TypeError, lambda: Client(
31+
app_id=4, key=u'key', secret=u'secret', ssl=False))
32+
2933

3034
def test_key_should_be_text_if_present(self):
31-
self.assertRaises(TypeError, lambda: Client(app_id=u'4', key=4, secret=u'secret', ssl=False))
35+
self.assertRaises(TypeError, lambda: Client(
36+
app_id=u'4', key=4, secret=u'secret', ssl=False))
37+
3238

3339
def test_secret_should_be_text_if_present(self):
34-
self.assertRaises(TypeError, lambda: Client(app_id=u'4', key=u'key', secret=4, ssl=False))
40+
self.assertRaises(TypeError, lambda: Client(
41+
app_id=u'4', key=u'key', secret=4, ssl=False))
42+
3543

3644
def test_ssl_should_be_boolean(self):
3745
Client(app_id=u'4', key=u'key', secret=u'secret', ssl=False)
3846
Client(app_id=u'4', key=u'key', secret=u'secret', ssl=True)
3947

40-
self.assertRaises(TypeError, lambda: Client(app_id=u'4', key=u'key', secret=u'secret', ssl=4))
48+
self.assertRaises(TypeError, lambda: Client(
49+
app_id=u'4', key=u'key', secret=u'secret', ssl=4))
50+
4151

4252
def test_port_should_be_number(self):
4353
Client(app_id=u'4', key=u'key', secret=u'secret', ssl=True, port=400)
4454

45-
self.assertRaises(TypeError, lambda: Client(app_id=u'4', key=u'key', secret=u'secret', ssl=True, port=u'400'))
55+
self.assertRaises(TypeError, lambda: Client(
56+
app_id=u'4', key=u'key', secret=u'secret', ssl=True, port=u'400'))
57+
4658

4759
def test_port_behaviour(self):
4860
conf = Client(app_id=u'4', key=u'key', secret=u'secret', ssl=True)
@@ -51,8 +63,11 @@ def test_port_behaviour(self):
5163
conf = Client(app_id=u'4', key=u'key', secret=u'secret', ssl=False)
5264
self.assertEqual(conf.port, 80, u'port should be 80 for non ssl')
5365

54-
conf = Client(app_id=u'4', key=u'key', secret=u'secret', ssl=False, port=4000)
55-
self.assertEqual(conf.port, 4000, u'the port setting override the default')
66+
conf = Client(
67+
app_id=u'4', key=u'key', secret=u'secret', ssl=False, port=4000)
68+
69+
self.assertEqual(
70+
conf.port, 4000, u'the port setting overrides the default')
5671

5772

5873
if __name__ == '__main__':

pusher_tests/test_pusher_client.py

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
from pusher.pusher_client import PusherClient
1515
from pusher.http import GET
16-
from pusher.signature import sign
1716

1817
try:
1918
import unittest.mock as mock
@@ -154,97 +153,6 @@ def test_user_info_success_case(self):
154153
self.assertEqual(request.path, u'/apps/4/channels/presence-channel/users')
155154
self.assertEqual(request.params, {})
156155

157-
def test_authenticate_for_private_channels(self):
158-
pusher = Pusher.from_url(u'http://foo:bar@host/apps/4')
159-
160-
expected = {
161-
u'auth': u"foo:89955e77e1b40e33df6d515a5ecbba86a01dc816a5b720da18a06fd26f7d92ff"
162-
}
163-
164-
self.assertEqual(pusher.authenticate(u'private-channel', u'345.23'), expected)
165-
166-
def test_authenticate_types(self):
167-
pusher = PusherClient.from_url(u'http://foo:bar@host/apps/4')
168-
169-
self.assertRaises(TypeError, lambda: pusher.authenticate(2423, u'34554'))
170-
self.assertRaises(TypeError, lambda: pusher.authenticate(u'plah', 234234))
171-
self.assertRaises(ValueError, lambda: pusher.authenticate(u'::', u'345345'))
172-
173-
def test_authenticate_for_presence_channels(self):
174-
pusher = PusherClient.from_url(u'http://foo:bar@host/apps/4')
175-
176-
custom_data = {
177-
u'user_id': u'fred',
178-
u'user_info': {
179-
u'key': u'value'
180-
}
181-
}
182-
183-
expected = {
184-
u'auth': u"foo:e80ba6439492c2113022c39297a87a948de14061cc67b5788e045645a68b8ccd",
185-
u'channel_data': u"{\"user_id\":\"fred\",\"user_info\":{\"key\":\"value\"}}"
186-
}
187-
188-
with mock.patch('json.dumps', return_value=expected[u'channel_data']) as dumps_mock:
189-
actual = pusher.authenticate(u'presence-channel', u'345.43245', custom_data)
190-
191-
self.assertEqual(actual, expected)
192-
dumps_mock.assert_called_once_with(custom_data, cls=None)
193-
194-
def test_validate_webhook_success_case(self):
195-
pusher = Pusher.from_url(u'http://foo:bar@host/apps/4')
196-
197-
body = u'{"time_ms": 1000000}'
198-
signature = six.text_type(hmac.new(pusher.secret.encode('utf8'), body.encode('utf8'), hashlib.sha256).hexdigest())
199-
200-
with mock.patch('time.time', return_value=1200):
201-
self.assertEqual(pusher.validate_webhook(pusher.key, signature, body), {u'time_ms': 1000000})
202-
203-
def test_validate_webhook_bad_types(self):
204-
pusher = PusherClient.from_url(u'http://foo:bar@host/apps/4')
205-
206-
pusher.validate_webhook(u'key', u'signature', u'body')
207-
208-
# These things are meant to be human readable, so enforcing being text is
209-
# sensible.
210-
211-
with mock.patch('time.time') as time_mock:
212-
self.assertRaises(TypeError, lambda: pusher.validate_webhook(4, u'signature', u'body'))
213-
self.assertRaises(TypeError, lambda: pusher.validate_webhook(u'key', 4, u'body'))
214-
self.assertRaises(TypeError, lambda: pusher.validate_webhook(u'key', u'signature', 4))
215-
216-
time_mock.assert_not_called()
217-
218-
def test_validate_webhook_bad_key(self):
219-
pusher = PusherClient.from_url(u'http://foo:bar@host/apps/4')
220-
221-
body = u'some body'
222-
signature = six.text_type(hmac.new(pusher.secret.encode(u'utf8'), body.encode(u'utf8'), hashlib.sha256).hexdigest())
223-
224-
with mock.patch('time.time') as time_mock:
225-
self.assertEqual(pusher.validate_webhook(u'badkey', signature, body), None)
226-
227-
time_mock.assert_not_called()
228-
229-
def test_validate_webhook_bad_signature(self):
230-
pusher = PusherClient.from_url(u'http://foo:bar@host/apps/4')
231-
232-
body = u'some body'
233-
signature = u'some signature'
234-
235-
with mock.patch('time.time') as time_mock:
236-
self.assertEqual(pusher.validate_webhook(pusher.key, signature, body), None)
237-
238-
time_mock.assert_not_called()
239-
240-
def test_validate_webhook_bad_time(self):
241-
pusher = PusherClient.from_url(u'http://foo:bar@host/apps/4')
242-
243-
body = u'{"time_ms": 1000000}'
244-
signature = six.text_type(hmac.new(pusher.secret.encode('utf8'), body.encode('utf8'), hashlib.sha256).hexdigest())
245-
246-
with mock.patch('time.time', return_value=1301):
247-
self.assertEqual(pusher.validate_webhook(pusher.key, signature, body), None)
248156

249157
if __name__ == '__main__':
250158
unittest.main()

0 commit comments

Comments
 (0)