Skip to content

Commit 6242ea7

Browse files
Add fixes to user authentication and send a message to a specific one
1 parent 8f394fc commit 6242ea7

File tree

6 files changed

+15
-35
lines changed

6 files changed

+15
-35
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,9 @@ To authenticate users on Pusher Channels on your application, you can use the au
329329
|:-:|:-:|
330330
|response `Dict` | A dictionary to send as a response to the authentication request.|
331331

332-
For more information see [authenticating users](https://pusher.com/docs/channels/server_api/authenticating-users/).
332+
For more information see:
333+
* [authenticating users](https://pusher.com/docs/channels/server_api/authenticating-users/)
334+
* [auth-signatures](https://pusher.com/docs/channels/library_auth_reference/auth-signatures/)
333335

334336
##### Example
335337

pusher/pusher.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from pusher.util import (
1717
ensure_text,
1818
pusher_url_re,
19-
doc_string)
19+
doc_string, validate_user_id)
2020

2121
from pusher.pusher_client import PusherClient
2222
from pusher.authentication_client import AuthenticationClient
@@ -146,9 +146,11 @@ def trigger(self, channels, event_name, data, socket_id=None):
146146
return self._pusher_client.trigger(
147147
channels, event_name, data, socket_id)
148148

149-
@doc_string(PusherClient.send_to_user.__doc__)
149+
@doc_string(PusherClient.trigger.__doc__)
150150
def send_to_user(self, user_id, event_name, data):
151-
return self._pusher_client.send_to_user(user_id, event_name, data)
151+
validate_user_id(user_id)
152+
user_server_string = "#server-to-user-%s" % user_id
153+
return self._pusher_client.trigger([user_server_string], event_name, data)
152154

153155
@doc_string(PusherClient.trigger_batch.__doc__)
154156
def trigger_batch(self, batch=[], already_encoded=False):

pusher/pusher_client.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,6 @@ def trigger(self, channels, event_name, data, socket_id=None):
114114

115115
return Request(self, POST, "/apps/%s/events" % self.app_id, params)
116116

117-
def send_to_user(self, user_id, event_name, data):
118-
"""Send an event to a specific user
119-
"""
120-
validate_user_id(user_id)
121-
user_server_string = "#server-to-user-%s" % user_id
122-
return self.trigger([user_server_string], event_name, data)
123-
124117
@request_method
125118
def trigger_batch(self, batch=[], already_encoded=False):
126119
"""Trigger multiple events with a single HTTP call.

pusher/util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def validate_user_data(user_data: dict):
116116
raise ValueError('user_data is null')
117117
if user_data.get('id') is None:
118118
raise ValueError('user_data has no id field')
119+
validate_user_id(user_data.get('id'))
119120

120121

121122
def join_attributes(attributes):

pusher_tests/test_pusher_client.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -360,28 +360,6 @@ def test_terminate_user_connection_fail_case_invalid_user_id(self):
360360
with self.assertRaises(ValueError):
361361
self.pusher_client.terminate_user_connections("")
362362

363-
def test_send_to_user_success_case(self):
364-
json_dumped = u'{"message": "hello worlds"}'
365-
request_params = [u'#server-to-user-123'], u'some_event', {u'message': u'hello worlds'}
366-
367-
with mock.patch('json.dumps', return_value=json_dumped):
368-
with mock.patch(
369-
'pusher.pusher_client.PusherClient.trigger',
370-
return_value=self.pusher_client.trigger.make_request(*request_params)
371-
) as mock_trigger:
372-
request = self.pusher_client.send_to_user(
373-
u'123', u'some_event', {u'message': u'hello worlds'}
374-
)
375-
376-
expected_params = {
377-
u'channels': [u'#server-to-user-123'],
378-
u'data': json_dumped,
379-
u'name': u'some_event'
380-
}
381-
382-
self.assertEqual(request.params, expected_params)
383-
mock_trigger.assert_called_with(*request_params)
384-
385363

386364
if __name__ == '__main__':
387365
unittest.main()

pusher_tests/test_util.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ def test_validate_user_id(self):
1717

1818
def test_validate_channel(self):
1919
valid_channels = ["123", "xyz", "xyz123", "xyz_123", "xyz-123", "Channel@123", "channel_xyz", "channel-xyz",
20-
"channel,456", "channel;asd", "[email protected],987;654"]
20+
"channel,456", "channel;asd", "[email protected],987;654", "#server-to-user1234",
21+
"#server-to-users"]
2122

2223
invalid_channels = ["#123", "x" * 201, "abc%&*"]
2324

@@ -31,10 +32,13 @@ def test_validate_channel(self):
3132
def test_validate_server_to_user_channel(self):
3233

3334
valid_server_to_user_channel = "#server-to-user-123"
34-
3535
invalid_server_to_user_channel = "#server-to-useR-123"
36+
valid_server_to_users = "#server-to-users"
37+
valid_server_to_user1234 = "#server-to-user1234"
3638

3739
self.assertEqual(valid_server_to_user_channel, pusher.util.validate_channel(valid_server_to_user_channel))
40+
self.assertEqual(valid_server_to_users, pusher.util.validate_channel(valid_server_to_users))
41+
self.assertEqual(valid_server_to_user1234, pusher.util.validate_channel(valid_server_to_user1234))
3842
with self.assertRaises(ValueError):
3943
pusher.util.validate_channel(invalid_server_to_user_channel)
4044

0 commit comments

Comments
 (0)