From 3e82fedec774f2fb20d95f298e9c226780a766a2 Mon Sep 17 00:00:00 2001 From: Lev Gorodetskiy Date: Tue, 21 Apr 2020 19:07:10 +0300 Subject: [PATCH] More copypasted tests --- pynicotine/slskmessages.py | 15 +- tests/test_pynicotine/test_slskmessages.py | 179 +++++++++++++++++++++ 2 files changed, 187 insertions(+), 7 deletions(-) diff --git a/pynicotine/slskmessages.py b/pynicotine/slskmessages.py index 74f85585416a..c4a67d324c48 100644 --- a/pynicotine/slskmessages.py +++ b/pynicotine/slskmessages.py @@ -642,7 +642,7 @@ def __init__(self, room=None, msg=None): self.msg = _str(msg) def makeNetworkMessage(self): - return self.packObject(self.room) + self.packObject(self.msg) + return self.packObjects(self.room, self.msg) def parseNetworkMessage(self, message): pos, self.room = self.getObject(message, bytes) @@ -669,14 +669,14 @@ class JoinRoom(ServerMessage): with data on everyone.""" def __init__(self, room=None, private=None): self.room = _str(room) - self.private = private + self.private: int = private self.owner = None self.operators = [] def makeNetworkMessage(self): if self.private is not None: - return self.packObject(self.room) + self.packObject(self.private) - return self.packObject(self.room) + return self.packObjects(self.room, self.private) + return self.packObjects(self.room) def parseNetworkMessage(self, message): pos, self.room = self.getObject(message, bytes) @@ -684,10 +684,11 @@ def parseNetworkMessage(self, message): pos, self.users = self.getUsers(message[pos:]) pos = pos1 + pos - if len(message[pos:]) > 0: + if message[pos:]: + # FIXME: bool or int? self.private = True pos, self.owner = self.getObject(message, bytes, pos) - if len(message[pos:]) > 0 and self.private: + if message[pos:] and self.private: pos, numops = self.getObject(message, int, pos) for i in range(numops): pos, operator = self.getObject(message, bytes, pos) @@ -762,7 +763,7 @@ def __init__(self, room=None, user=None): self.user = _str(user) def makeNetworkMessage(self): - return self.packObject(self.room) + self.packObject(self.user) + return self.packObjects(self.room, self.user) def parseNetworkMessage(self, message): pos, self.room = self.getObject(message, bytes) diff --git a/tests/test_pynicotine/test_slskmessages.py b/tests/test_pynicotine/test_slskmessages.py index 7176c1619ed5..77aae8bb9fc6 100644 --- a/tests/test_pynicotine/test_slskmessages.py +++ b/tests/test_pynicotine/test_slskmessages.py @@ -1,16 +1,26 @@ import unittest +from pynicotine.slskmessages import AckNotifyPrivileges from pynicotine.slskmessages import AddUser from pynicotine.slskmessages import ChangePassword from pynicotine.slskmessages import GetPeerAddress from pynicotine.slskmessages import GetUserStatus +from pynicotine.slskmessages import JoinPublicRoom +from pynicotine.slskmessages import JoinRoom from pynicotine.slskmessages import JustDecoded +from pynicotine.slskmessages import LeavePublicRoom from pynicotine.slskmessages import Login from pynicotine.slskmessages import NetworkIntType from pynicotine.slskmessages import NetworkLongLongType from pynicotine.slskmessages import NetworkSignedIntType from pynicotine.slskmessages import NotifyPrivileges +from pynicotine.slskmessages import PrivateRoomAddUser +from pynicotine.slskmessages import PrivateRoomDismember +from pynicotine.slskmessages import PrivateRoomDisown +from pynicotine.slskmessages import PrivateRoomRemoveUser +from pynicotine.slskmessages import PrivateRoomSomething from pynicotine.slskmessages import RemoveUser +from pynicotine.slskmessages import SayChatroom from pynicotine.slskmessages import SetStatus from pynicotine.slskmessages import SetWaitPort from pynicotine.slskmessages import SlskMessage @@ -229,3 +239,172 @@ def test_makeNetworkMessage(self): self.assertEqual( b'@\xe2\x01\x00\x04\x00\x00\x00test', message) + + +class AckNotifyPrivilegesMessageTest(unittest.TestCase): + def test_makeNetworkMessage(self): + # Arrange + obj = AckNotifyPrivileges(token=123456) + + # Act + message = obj.makeNetworkMessage() + + # Assert + self.assertEqual( + b'@\xe2\x01\x00', + message) + + +class JoinPublicRoomMessageTest(unittest.TestCase): + def test_makeNetworkMessage(self): + # Arrange + obj = JoinPublicRoom(unknown=123) + + # Act + message = obj.makeNetworkMessage() + + # Assert + self.assertEqual( + b'{\x00\x00\x00', + message) + + +class LeavePublicRoomMessageTest(unittest.TestCase): + def test_makeNetworkMessage(self): + # Arrange + obj = LeavePublicRoom(unknown=123) + + # Act + message = obj.makeNetworkMessage() + + # Assert + self.assertEqual( + b'{\x00\x00\x00', + message) + + +class PublicRoomMessageMessageTest(unittest.TestCase): + ... + + +class SayChatroomMessageTest(unittest.TestCase): + def test_makeNetworkMessage(self): + # Arrange + obj = SayChatroom(room='nicotine', msg='Wassup?') + + # Act + message = obj.makeNetworkMessage() + + # Assert + self.assertEqual( + b'\x08\x00\x00\x00nicotine\x07\x00\x00\x00Wassup?', + message) + + +class JoinRoomMessageTest(unittest.TestCase): + def test_makeNetworkMessage(self): + # Arrange + obj = JoinRoom(room='nicotine', private=0) + obj_private = JoinRoom(room='nicotine', private=1) + + # Act + message = obj.makeNetworkMessage() + message_private = obj_private.makeNetworkMessage() + + # Assert + self.assertEqual( + b'\x08\x00\x00\x00nicotine\x00\x00\x00\x00', + message) + self.assertEqual( + b'\x08\x00\x00\x00nicotine\x01\x00\x00\x00', + message_private) + + +class PrivateRoomUsersMessageTest(unittest.TestCase): + ... + + +class PrivateRoomOwnedMessageTest(unittest.TestCase): + ... + + +class PrivateRoomAddUserMessageTest(unittest.TestCase): + def test_makeNetworkMessage(self): + # Arrange + obj = PrivateRoomAddUser(room='nicotine', user='admin') + + # Act + message = obj.makeNetworkMessage() + + # Assert + self.assertEqual( + b'\x08\x00\x00\x00nicotine\x05\x00\x00\x00admin', + message) + + +class PrivateRoomDismemberMessageTest(unittest.TestCase): + def test_makeNetworkMessage(self): + # Arrange + obj = PrivateRoomDismember(room='nicotine') + + # Act + message = obj.makeNetworkMessage() + + # Assert + self.assertEqual( + b'\x08\x00\x00\x00nicotine', + message) + + +class PrivateRoomDisownMessageTest(unittest.TestCase): + def test_makeNetworkMessage(self): + # Arrange + obj = PrivateRoomDisown(room='nicotine') + + # Act + message = obj.makeNetworkMessage() + + # Assert + self.assertEqual( + b'\x08\x00\x00\x00nicotine', + message) + + +class PrivateRoomSomethingMessageTest(unittest.TestCase): + def test_makeNetworkMessage(self): + # Arrange + obj = PrivateRoomSomething(room='nicotine') + + # Act + message = obj.makeNetworkMessage() + + # Assert + self.assertEqual( + b'\x08\x00\x00\x00nicotine', + message) + + +class PrivateRoomRemoveUserMessageTest(unittest.TestCase): + def test_makeNetworkMessage(self): + # Arrange + obj = PrivateRoomRemoveUser(room='nicotine', user='admin') + + # Act + message = obj.makeNetworkMessage() + + # Assert + self.assertEqual( + b'\x08\x00\x00\x00nicotine\x05\x00\x00\x00admin', + message) + + def test_parseNetworkMessage(self): + # Arrange + message = b'\x08\x00\x00\x00nicotine\x05\x00\x00\x00admin' + + # Act + obj = PrivateRoomRemoveUser() + obj.parseNetworkMessage(message) + + # Assert + self.assertEqual('nicotine', obj.room) + self.assertEqual('admin', obj.user)