Skip to content

Commit

Permalink
Add fixes to user authentication and send a message to a specific one
Browse files Browse the repository at this point in the history
  • Loading branch information
andersonrocha0 committed Dec 27, 2024
1 parent 8f394fc commit 6242ea7
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 35 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,9 @@ To authenticate users on Pusher Channels on your application, you can use the au
|:-:|:-:|
|response `Dict` | A dictionary to send as a response to the authentication request.|

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

##### Example

Expand Down
8 changes: 5 additions & 3 deletions pusher/pusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pusher.util import (
ensure_text,
pusher_url_re,
doc_string)
doc_string, validate_user_id)

from pusher.pusher_client import PusherClient
from pusher.authentication_client import AuthenticationClient
Expand Down Expand Up @@ -146,9 +146,11 @@ def trigger(self, channels, event_name, data, socket_id=None):
return self._pusher_client.trigger(
channels, event_name, data, socket_id)

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

@doc_string(PusherClient.trigger_batch.__doc__)
def trigger_batch(self, batch=[], already_encoded=False):
Expand Down
7 changes: 0 additions & 7 deletions pusher/pusher_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,6 @@ def trigger(self, channels, event_name, data, socket_id=None):

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

def send_to_user(self, user_id, event_name, data):
"""Send an event to a specific user
"""
validate_user_id(user_id)
user_server_string = "#server-to-user-%s" % user_id
return self.trigger([user_server_string], event_name, data)

@request_method
def trigger_batch(self, batch=[], already_encoded=False):
"""Trigger multiple events with a single HTTP call.
Expand Down
1 change: 1 addition & 0 deletions pusher/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def validate_user_data(user_data: dict):
raise ValueError('user_data is null')
if user_data.get('id') is None:
raise ValueError('user_data has no id field')
validate_user_id(user_data.get('id'))


def join_attributes(attributes):
Expand Down
22 changes: 0 additions & 22 deletions pusher_tests/test_pusher_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,28 +360,6 @@ def test_terminate_user_connection_fail_case_invalid_user_id(self):
with self.assertRaises(ValueError):
self.pusher_client.terminate_user_connections("")

def test_send_to_user_success_case(self):
json_dumped = u'{"message": "hello worlds"}'
request_params = [u'#server-to-user-123'], u'some_event', {u'message': u'hello worlds'}

with mock.patch('json.dumps', return_value=json_dumped):
with mock.patch(
'pusher.pusher_client.PusherClient.trigger',
return_value=self.pusher_client.trigger.make_request(*request_params)
) as mock_trigger:
request = self.pusher_client.send_to_user(
u'123', u'some_event', {u'message': u'hello worlds'}
)

expected_params = {
u'channels': [u'#server-to-user-123'],
u'data': json_dumped,
u'name': u'some_event'
}

self.assertEqual(request.params, expected_params)
mock_trigger.assert_called_with(*request_params)


if __name__ == '__main__':
unittest.main()
8 changes: 6 additions & 2 deletions pusher_tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def test_validate_user_id(self):

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

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

Expand All @@ -31,10 +32,13 @@ def test_validate_channel(self):
def test_validate_server_to_user_channel(self):

valid_server_to_user_channel = "#server-to-user-123"

invalid_server_to_user_channel = "#server-to-useR-123"
valid_server_to_users = "#server-to-users"
valid_server_to_user1234 = "#server-to-user1234"

self.assertEqual(valid_server_to_user_channel, pusher.util.validate_channel(valid_server_to_user_channel))
self.assertEqual(valid_server_to_users, pusher.util.validate_channel(valid_server_to_users))
self.assertEqual(valid_server_to_user1234, pusher.util.validate_channel(valid_server_to_user1234))
with self.assertRaises(ValueError):
pusher.util.validate_channel(invalid_server_to_user_channel)

Expand Down

0 comments on commit 6242ea7

Please sign in to comment.