Skip to content

Commit

Permalink
More copypasted tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lev Gorodetskiy committed Apr 21, 2020
1 parent cf9edaa commit e9a2107
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 7 deletions.
15 changes: 8 additions & 7 deletions pynicotine/slskmessages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -669,25 +669,26 @@ 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)
pos1 = pos
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)
Expand Down Expand Up @@ -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)
Expand Down
179 changes: 179 additions & 0 deletions tests/test_pynicotine/test_slskmessages.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

0 comments on commit e9a2107

Please sign in to comment.