Skip to content

Commit 2fcf070

Browse files
author
Callum Oakley
committed
all unit tests passing
1 parent d6d7628 commit 2fcf070

File tree

8 files changed

+84
-64
lines changed

8 files changed

+84
-64
lines changed

pusher/pusher.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import collections
1919
import hashlib
20-
import json
2120
import os
2221
import re
2322
import six

pusher/pusher_client.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import collections
2020
import hashlib
21-
import json
2221
import os
2322
import re
2423
import six

pusher_tests/test_authentication_client.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ def test_validate_webhook_bad_signature(self):
113113
signature = u'some signature'
114114

115115
with mock.patch('time.time') as time_mock:
116-
self.assertEqual(authenticationClient.validate_webhook(authenticationClient.key, signature, body), None)
116+
self.assertEqual(
117+
authenticationClient.validate_webhook(
118+
authenticationClient.key, signature, body), None)
117119

118120
time_mock.assert_not_called()
119121

@@ -123,10 +125,53 @@ def test_validate_webhook_bad_time(self):
123125
key=u'foo', secret=u'bar', host=u'host', app_id=u'4', ssl=True)
124126

125127
body = u'{"time_ms": 1000000}'
126-
signature = six.text_type(hmac.new(authenticationClient.secret.encode('utf8'), body.encode('utf8'), hashlib.sha256).hexdigest())
128+
signature = six.text_type(
129+
hmac.new(
130+
authenticationClient.secret.encode('utf8'),
131+
body.encode('utf8'), hashlib.sha256).hexdigest())
127132

128133
with mock.patch('time.time', return_value=1301):
129-
self.assertEqual(authenticationClient.validate_webhook(authenticationClient.key, signature, body), None)
134+
self.assertEqual(authenticationClient.validate_webhook(
135+
authenticationClient.key, signature, body), None)
136+
137+
138+
class TestJson(unittest.TestCase):
139+
def setUp(self):
140+
class JSONEncoder(json.JSONEncoder):
141+
def default(self, o):
142+
if isinstance(o, Decimal):
143+
return str(o)
144+
145+
return super(JSONEncoder, self).default(o)
146+
147+
constants = {"NaN": 99999}
148+
149+
class JSONDecoder(json.JSONDecoder):
150+
def __init__(self, **kwargs):
151+
super(JSONDecoder, self).__init__(
152+
parse_constant=constants.__getitem__)
153+
154+
self.authentication_client = AuthenticationClient(
155+
"4", "key", "secret", host="somehost", json_encoder=JSONEncoder,
156+
json_decoder=JSONDecoder)
157+
158+
159+
def test_custom_json_decoder(self):
160+
t = 1000 * time.time()
161+
body = u'{"nan": NaN, "time_ms": %f}' % t
162+
signature = sign(self.authentication_client.secret, body)
163+
data = self.authentication_client.validate_webhook(
164+
self.authentication_client.key, signature, body)
165+
self.assertEqual({u"nan": 99999, u"time_ms": t}, data)
166+
167+
168+
def test_custom_json_encoder(self):
169+
expected = {
170+
u'channel_data': '{"money": "1.32"}',
171+
u'auth': u'key:7f2ae5922800a20b9615543ce7c8e7d1c97115d108939410825ea690f308a05f'
172+
}
173+
data = self.authentication_client.authenticate("presence-c1", "1.1", {"money": Decimal("1.32")})
174+
self.assertEqual(expected, data)
130175

131176

132177
if __name__ == '__main__':

pusher_tests/test_gae_adapter.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
else:
1313
import unittest
1414

15-
skip_test = (sys.version_info[0:2] != (2,7)) or os.environ.get("TRAVIS_PYTHON_VERSION")
15+
skip_test = (sys.version_info[0:2] != (2,7)) or os.environ.get("TRAVIS_PYTHON_VERSION")
1616

1717
@unittest.skipIf(skip_test, "skip")
1818
class TestGAEBackend(unittest.TestCase):
@@ -21,9 +21,9 @@ def setUp(self):
2121
import pusher.gae
2222
from google.appengine.api import apiproxy_stub_map, urlfetch_stub
2323

24-
apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
25-
apiproxy_stub_map.apiproxy.RegisterStub('urlfetch',
26-
urlfetch_stub.URLFetchServiceStub())
24+
apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
25+
apiproxy_stub_map.apiproxy.RegisterStub('urlfetch',
26+
urlfetch_stub.URLFetchServiceStub())
2727

2828
self.p = pusher.Pusher.from_url(u'http://key:secret@api.pusherapp.com/apps/4',
2929
backend=pusher.gae.GAEBackend)
@@ -36,5 +36,6 @@ def test_trigger_gae_success(self):
3636
response = self.p.trigger(u'test_channel', u'test', {u'data': u'yolo'})
3737
self.assertEqual(response, {})
3838

39+
3940
if __name__ == '__main__':
40-
unittest.main()
41+
unittest.main()

pusher_tests/test_notification_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,7 @@ def test_notify_supplied_ssl_and_host(self):
5858

5959
def test_at_least_one_interest_sent_to(self):
6060
self.assertRaises(ValueError, lambda: self.client.notify([], self.success_fixture))
61+
62+
63+
if __name__ == '__main__':
64+
unittest.main()

pusher_tests/test_pusher.py

Lines changed: 23 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,62 +22,32 @@
2222

2323
class TestPusher(unittest.TestCase):
2424
def test_initialize_from_url(self):
25-
self.assertRaises(TypeError, lambda: Client.from_url(4))
26-
self.assertRaises(Exception, lambda: Client.from_url(u'httpsahsutaeh'))
27-
28-
conf = Client.from_url(u'http://foo:bar@host/apps/4')
29-
self.assertEqual(conf.ssl, False)
30-
self.assertEqual(conf.key, u'foo')
31-
self.assertEqual(conf.secret, u'bar')
32-
self.assertEqual(conf.host, u'host')
33-
self.assertEqual(conf.app_id, u'4')
34-
35-
conf = Client.from_url(u'https://foo:bar@host/apps/4')
36-
self.assertEqual(conf.ssl, True)
37-
self.assertEqual(conf.key, u'foo')
38-
self.assertEqual(conf.secret, u'bar')
39-
self.assertEqual(conf.host, u'host')
40-
self.assertEqual(conf.app_id, u'4')
25+
self.assertRaises(TypeError, lambda: Pusher.from_url(4))
26+
self.assertRaises(Exception, lambda: Pusher.from_url(u'httpsahsutaeh'))
27+
28+
pusher = Pusher.from_url(u'http://foo:bar@host/apps/4')
29+
self.assertEqual(pusher._pusher_client.ssl, False)
30+
self.assertEqual(pusher._pusher_client.key, u'foo')
31+
self.assertEqual(pusher._pusher_client.secret, u'bar')
32+
self.assertEqual(pusher._pusher_client.host, u'host')
33+
self.assertEqual(pusher._pusher_client.app_id, u'4')
34+
35+
pusher = Pusher.from_url(u'https://foo:bar@host/apps/4')
36+
self.assertEqual(pusher._pusher_client.ssl, True)
37+
self.assertEqual(pusher._pusher_client.key, u'foo')
38+
self.assertEqual(pusher._pusher_client.secret, u'bar')
39+
self.assertEqual(pusher._pusher_client.host, u'host')
40+
self.assertEqual(pusher._pusher_client.app_id, u'4')
4141

4242
def test_initialize_from_env(self):
4343
with mock.patch.object(os, 'environ', new={'PUSHER_URL':'https://plah:bob@somehost/apps/42'}):
4444
pusher = Pusher.from_env()
45-
self.assertEqual(pusher.ssl, True)
46-
self.assertEqual(pusher.key, u'plah')
47-
self.assertEqual(pusher.secret, u'bob')
48-
self.assertEqual(pusher.host, u'somehost')
49-
self.assertEqual(pusher.app_id, u'42')
50-
51-
52-
class TestJson(unittest.TestCase):
53-
def setUp(self):
54-
class JSONEncoder(json.JSONEncoder):
55-
def default(self, o):
56-
if isinstance(o, Decimal):
57-
return str(o)
58-
return super(JSONEncoder, self).default(o)
59-
60-
constants = {"NaN": 99999}
61-
62-
class JSONDecoder(json.JSONDecoder):
63-
def __init__(self, **kwargs):
64-
super(JSONDecoder, self).__init__(parse_constant=constants.__getitem__)
65-
66-
self.pusher_client = PusherClient.from_url(u'http://key:secret@somehost/apps/4',
67-
json_encoder=JSONEncoder,
68-
json_decoder=JSONDecoder)
45+
self.assertEqual(pusher._pusher_client.ssl, True)
46+
self.assertEqual(pusher._pusher_client.key, u'plah')
47+
self.assertEqual(pusher._pusher_client.secret, u'bob')
48+
self.assertEqual(pusher._pusher_client.host, u'somehost')
49+
self.assertEqual(pusher._pusher_client.app_id, u'42')
6950

70-
def test_custom_json_decoder(self):
71-
t = 1000 * time.time()
72-
body = u'{"nan": NaN, "time_ms": %f}' % t
73-
signature = sign(self.pusher_client.secret, body)
74-
data = self.pusher_client.validate_webhook(self.pusher_client.key, signature, body)
75-
self.assertEqual({u"nan": 99999, u"time_ms": t}, data)
7651

77-
def test_custom_json_encoder(self):
78-
expected = {
79-
u'channel_data': '{"money": "1.32"}',
80-
u'auth': u'key:7f2ae5922800a20b9615543ce7c8e7d1c97115d108939410825ea690f308a05f'
81-
}
82-
data = self.pusher_client.authenticate("presence-c1", "1.1", {"money": Decimal("1.32")})
83-
self.assertEqual(expected, data)
52+
if __name__ == '__main__':
53+
unittest.main()

pusher_tests/test_request.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,6 @@ def test_x_pusher_library_header(self):
7171
pusherLib = req.headers['X-Pusher-Library']
7272
self.assertRegexpMatches(pusherLib, r'^pusher-http-python \d+(\.\d+)+(rc\d+)?$')
7373

74+
7475
if __name__ == '__main__':
7576
unittest.main()

pusher_tests/test_requests_adapter.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ def test_trigger_requests_success(self):
1919
response = self.pusher.trigger(u'test_channel', u'test', {u'data': u'yolo'})
2020
self.assertEqual(response, {})
2121

22+
2223
if __name__ == '__main__':
23-
unittest.main()
24+
unittest.main()

0 commit comments

Comments
 (0)