def test_handle_message_leave(self): self.room.open() u = User() self.room.users = [u, User(), User(), User()] msg = "{uid1}:{uid2}:LEAVE".format(uid1=u.user_id, uid2=self.room.room_id) resp = self.room.handle_message(Mock("mock connection"), msg) self.assertTrue(resp.endswith("SUCCESS")) self.assertNotIn(u, self.room.users)
def test_remove_user(self): r = Room() r.open() u1 = User() u2 = User() r.add_user(u1) r.add_user(u2) self.assertIn(u1, r.users) r.remove_user(u1) self.assertNotIn(u1, r.users) self.assertIn(u2, r.users)
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_close_room(self): r = Room() r.open() u1 = User() u2 = User() o = User() r.add_user(u1) r.add_user(u2) r.add_owner(o) r.close() self.assertFalse(r.is_open()) self.assertEqual(len(r.users), 0) self.assertEqual(r.owner, o)
def test_room_add_owner(self): r = Room() r.open() o = User() r.add_owner(o) self.assertEqual(len(r.users), 1) self.assertEqual(o, r.owner)
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 _handle_giveaway(self, msg): sender = User.from_user_id(msg.sender) if sender != self.owner: logger.warning("Giveaway attempted in room {r} by non-owner {u}".format(r=self.room_id, u=sender.user_id)) raise RoomException("Giveaways can only be initiated by the room owner") else: g = Giveaway(users=self.users) winner = g.draw(1)[-1] return self.MessageClass(sender=self.room_id, recipient=sender.user_id, message=SocketMessage.SUCCESS, info=winner.user_id)
def api_room_add_user(): h = House.get_instance() data = request.get_json() user_id = data.get("user_id") room_id = data.get("room_id") room = h.get_room(room_id) room.add_user(User.from_user_id(user_id)) return jsonify({ "success": True, "room_id": room_id, "user_count": room.user_count() })
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_endpoint_room_remove_user(self): user_id = "TEST REMOVE USER" room_id = self.room.room_id self.room.open() self.room.add_user(User.from_user_id(user_id)) json_resp = self.json_response(path="/api/room/remove_user", method="POST", params={ "user_id": user_id, "room_id": self.room.room_id }) self.assertTrue(json_resp.get("success")) self.assertEqual(0, json_resp.get("user_count")) self.assertEqual(self.room.room_id, json_resp.get("room_id"))
def api_room_open(): h = House.get_instance() data = request.get_json() room_id = data.get("room_id") room = h.get_room(room_id) room.open() room.listen() owner_id = data.get("owner_id") resp = { "success": True, "address": room.address[0], "port": room.address[1] } if owner_id: room.add_owner(User.from_user_id(owner_id)) resp["owner_id"] = owner_id return jsonify(resp)
def api_room_remove_user(): h = House.get_instance() data = request.get_json() user_id = data.get("user_id") room_id = data.get("room_id") room = h.get_room(room_id) user = User.from_user_id(user_id) if room.has_user(user): room.remove_user(user) resp = { "success": True, "user_count": room.user_count(), "room_id": room.room_id } else: resp = { "success": True, "user_count": room.user_count(), "message": "User not in room" } return jsonify(resp)
def _handle_leave(self, msg): user = User.from_user_id(msg.sender) self.remove_user(user) return self.MessageClass(recipient=msg.sender, sender=self.room_id, message=SocketMessage.SUCCESS)
def test_user_has_id(self): u = User() self.assertEqual(len(u.user_id), User.USER_ID_LEN)
def test_add_users_to_unopened_room(self): r = Room() u = User() self.assertFalse(r.is_open()) with self.assertRaises(RoomException): r.add_user(u)
def test_user_from_id(self): test_id = "TEST_ID" u = User.from_user_id(test_id) self.assertEqual(test_id, u.user_id)
def create_new_user(): u = User() return u.id
def test_room_add_user(self): r = Room() r.open() u = User() r.add_user(u) self.assertEqual(len(r.users), 1)