diff --git a/galaxy/api/consts.py b/galaxy/api/consts.py index 4e595b2..a4ad274 100644 --- a/galaxy/api/consts.py +++ b/galaxy/api/consts.py @@ -24,6 +24,7 @@ class Feature(Enum): Chat = "Chat" ImportUsers = "ImportUsers" VerifyGame = "VerifyGame" + ImportFriends = "ImportFriends" class LicenseType(Enum): Unknown = "Unknown" diff --git a/galaxy/api/plugin.py b/galaxy/api/plugin.py index d6ee78d..3c51188 100644 --- a/galaxy/api/plugin.py +++ b/galaxy/api/plugin.py @@ -77,8 +77,8 @@ class Plugin(): self._register_method( "import_friends", self.get_friends, - result_name="user_info_list", - feature=Feature.ImportUsers + result_name="friend_info_list", + feature=Feature.ImportFriends ) self._register_method( "import_user_infos", @@ -227,17 +227,13 @@ class Plugin(): self._notification_client.notify("local_game_status_changed", params) def add_friend(self, user): - params = {"user_info" : user} + params = {"friend_info" : user} self._notification_client.notify("friend_added", params) def remove_friend(self, user_id): params = {"user_id" : user_id} self._notification_client.notify("friend_removed", params) - def update_friend(self, user): - params = {"user_info" : user} - self._notification_client.notify("friend_updated", params) - def update_room(self, room_id, unread_message_count=None, new_messages=None): params = {"room_id": room_id} if unread_message_count is not None: diff --git a/galaxy/api/types.py b/galaxy/api/types.py index 574d427..25dad4e 100644 --- a/galaxy/api/types.py +++ b/galaxy/api/types.py @@ -68,6 +68,11 @@ class UserInfo(): avatar_url: str presence: Presence +@dataclass +class FriendInfo(): + user_id: str + user_name: str + @dataclass class Room(): room_id: str diff --git a/tests/test_friends.py b/tests/test_friends.py new file mode 100644 index 0000000..eb90c14 --- /dev/null +++ b/tests/test_friends.py @@ -0,0 +1,90 @@ +import asyncio +import json + +from galaxy.api.types import FriendInfo +from galaxy.api.errors import UnknownError + + +def test_get_friends_success(plugin, readline, write): + request = { + "jsonrpc": "2.0", + "id": "3", + "method": "import_friends" + } + + readline.side_effect = [json.dumps(request), ""] + plugin.get_friends.return_value = [ + FriendInfo("3", "Jan"), + FriendInfo("5", "Ola") + ] + asyncio.run(plugin.run()) + plugin.get_friends.assert_called_with() + response = json.loads(write.call_args[0][0]) + + assert response == { + "jsonrpc": "2.0", + "id": "3", + "result": { + "friend_info_list": [ + {"user_id": "3", "user_name": "Jan"}, + {"user_id": "5", "user_name": "Ola"} + ] + } + } + + +def test_get_friends_failure(plugin, readline, write): + request = { + "jsonrpc": "2.0", + "id": "3", + "method": "import_friends" + } + + readline.side_effect = [json.dumps(request), ""] + plugin.get_friends.side_effect = UnknownError() + asyncio.run(plugin.run()) + plugin.get_friends.assert_called_with() + response = json.loads(write.call_args[0][0]) + + assert response == { + "jsonrpc": "2.0", + "id": "3", + "error": { + "code": 0, + "message": "Unknown error", + } + } + + +def test_add_friend(plugin, write): + friend = FriendInfo("7", "Kuba") + + async def couritine(): + plugin.add_friend(friend) + + asyncio.run(couritine()) + response = json.loads(write.call_args[0][0]) + + assert response == { + "jsonrpc": "2.0", + "method": "friend_added", + "params": { + "friend_info": {"user_id": "7", "user_name": "Kuba"} + } + } + + +def test_remove_friend(plugin, write): + async def couritine(): + plugin.remove_friend("5") + + asyncio.run(couritine()) + response = json.loads(write.call_args[0][0]) + + assert response == { + "jsonrpc": "2.0", + "method": "friend_removed", + "params": { + "user_id": "5" + } + } diff --git a/tests/test_users.py b/tests/test_users.py index f4bf775..1e82831 100644 --- a/tests/test_users.py +++ b/tests/test_users.py @@ -5,153 +5,6 @@ from galaxy.api.types import UserInfo, Presence from galaxy.api.errors import UnknownError from galaxy.api.consts import PresenceState -def test_get_friends_success(plugin, readline, write): - request = { - "jsonrpc": "2.0", - "id": "3", - "method": "import_friends" - } - - readline.side_effect = [json.dumps(request), ""] - plugin.get_friends.return_value = [ - UserInfo( - "3", - True, - "Jan", - "http://avatar1.png", - Presence( - PresenceState.Online, - "123", - "Main menu" - ) - ), - UserInfo( - "5", - True, - "Ola", - "http://avatar2.png", - Presence(PresenceState.Offline) - ) - ] - asyncio.run(plugin.run()) - plugin.get_friends.assert_called_with() - response = json.loads(write.call_args[0][0]) - - assert response == { - "jsonrpc": "2.0", - "id": "3", - "result": { - "user_info_list": [ - { - "user_id": "3", - "is_friend": True, - "user_name": "Jan", - "avatar_url": "http://avatar1.png", - "presence": { - "presence_state": "online", - "game_id": "123", - "presence_status": "Main menu" - } - }, - { - "user_id": "5", - "is_friend": True, - "user_name": "Ola", - "avatar_url": "http://avatar2.png", - "presence": { - "presence_state": "offline" - } - } - ] - } - } - -def test_get_friends_failure(plugin, readline, write): - request = { - "jsonrpc": "2.0", - "id": "3", - "method": "import_friends" - } - - readline.side_effect = [json.dumps(request), ""] - plugin.get_friends.side_effect = UnknownError() - asyncio.run(plugin.run()) - plugin.get_friends.assert_called_with() - response = json.loads(write.call_args[0][0]) - - assert response == { - "jsonrpc": "2.0", - "id": "3", - "error": { - "code": 0, - "message": "Unknown error", - } - } - -def test_add_friend(plugin, write): - friend = UserInfo("7", True, "Kuba", "http://avatar.png", Presence(PresenceState.Offline)) - - async def couritine(): - plugin.add_friend(friend) - - asyncio.run(couritine()) - response = json.loads(write.call_args[0][0]) - - assert response == { - "jsonrpc": "2.0", - "method": "friend_added", - "params": { - "user_info": { - "user_id": "7", - "is_friend": True, - "user_name": "Kuba", - "avatar_url": "http://avatar.png", - "presence": { - "presence_state": "offline" - } - } - } - } - -def test_remove_friend(plugin, write): - async def couritine(): - plugin.remove_friend("5") - - asyncio.run(couritine()) - response = json.loads(write.call_args[0][0]) - - assert response == { - "jsonrpc": "2.0", - "method": "friend_removed", - "params": { - "user_id": "5" - } - } - -def test_update_friend(plugin, write): - friend = UserInfo("9", True, "Anna", "http://avatar.png", Presence(PresenceState.Offline)) - - async def couritine(): - plugin.update_friend(friend) - - asyncio.run(couritine()) - response = json.loads(write.call_args[0][0]) - - assert response == { - "jsonrpc": "2.0", - "method": "friend_updated", - "params": { - "user_info": { - "user_id": "9", - "is_friend": True, - "user_name": "Anna", - "avatar_url": "http://avatar.png", - "presence": { - "presence_state": "offline" - } - } - } - } def test_get_users_success(plugin, readline, write): request = { @@ -189,6 +42,7 @@ def test_get_users_success(plugin, readline, write): } } + def test_get_users_failure(plugin, readline, write): request = { "jsonrpc": "2.0",