def test__handle_giveaway_non_owner(self): u = User() msg = SocketMessage(sender=u.user_id, recipient=self.room.room_id, message=SocketMessage.GIVEAWAY) with self.assertRaises(RoomException) as err: self.room._handle_giveaway(msg) self.assertIn("room owner", err.args[0])
def test_handle_message_giveaway(self): self.room.open() owner = User() self.room.add_owner(owner) msg = "{uid1}:{uid2}:GIVEAWAY".format(uid1=owner.user_id, uid2=self.room.room_id) resp = SocketMessage.from_text( self.room.handle_message(Mock("mock connection"), msg)) self.assertEqual(resp.info, owner.user_id) self.assertTrue(resp.message, "SUCCESS")
def test_handle_bad_recipient(self): u = User() self.room.open() for message in ("JOIN", "LEAVE", "GIVEAWAY"): with self.subTest(message=message): msg = "{uid1}:{uid2}:{m}".format(uid1=str(uuid.uuid1()), uid2=u.user_id, m=message) resp = SocketMessage.from_text( self.room.handle_message(Mock("mock connection"), msg)) self.assertEqual(resp.message, "FAILURE") self.assertIn("not intended for this room", resp.info)
def test_handle_message_giveaway_non_owner(self): self.room.open() owner = User() self.room.add_owner(owner) user = User() self.room.add_user(user) msg = "{uid1}:{uid2}:GIVEAWAY".format(uid1=user.user_id, uid2=self.room.room_id) resp = SocketMessage.from_text( self.room.handle_message(Mock("mock connection"), msg)) self.assertEqual(resp.message, "FAILURE") self.assertIn("initiated by the room owner", resp.info)
def test_handlers(self): for handler in ("giveaway", "join", "leave"): with self.subTest(handler=handler): fn = getattr(self.room, "_handle_{handler}".format(handler=handler)) u = User() self.room.open() self.room.add_owner(u) msg = SocketMessage(sender=u.user_id, recipient=self.room.room_id, message=handler.upper()) resp = fn(msg) self.assertEqual(resp.message, SocketMessage.SUCCESS)
def test_invalid_from_text_bad_message(self): with self.assertRaises(SocketMessageException) as err: msg = SocketMessage.from_text("{uid1}:{uid2}:{msg}".format(uid1=self.good_uid1, uid2=self.good_uid2, msg=self.bad_message)) self.assertIn("message", err.exception.args[0].lower())
def test_from_text_with_info(self): msg = SocketMessage.from_text("{uid1}:{uid2}:SUCCESS:{uid3}".format(uid1=self.good_uid1, uid2=self.good_uid2, uid3=self.good_uid2)) self.assertEqual(msg.sender, self.good_uid1) self.assertEqual(msg.recipient, self.good_uid2) self.assertEqual(msg.info, self.good_uid2)
def test_valid_from_text(self): msg = SocketMessage.from_text("{uid1}:{uid2}:{msg}".format(uid1=self.good_uid1, uid2=self.good_uid2, msg="JOIN")) self.assertEqual(msg.sender, self.good_uid1) self.assertEqual(msg.recipient, self.good_uid2) self.assertEqual(msg.message, "JOIN")
def test_invalid_message_bad_message(self): with self.assertRaises(SocketMessageException) as err: msg = SocketMessage(sender=self.good_uid1, recipient=self.good_uid2, message=self.bad_message) self.assertIn("message", err.exception.args[0].lower())
def test_valid_message(self): msg = SocketMessage(sender=self.good_uid1, recipient=self.good_uid2, message=SocketMessage.JOIN) self.assertEqual("{uid1}:{uid2}:JOIN".format(uid1=self.good_uid1, uid2=self.good_uid2), str(msg))
def add_user_to_room(user_id, room_id): msg = SocketMessage(sender=user_id, recipient=room_id, message=SocketMessage.JOIN)