Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
 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])
Ejemplo n.º 7
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")
Ejemplo n.º 8
0
 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)
Ejemplo n.º 9
0
 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)
Ejemplo n.º 10
0
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()
    })
Ejemplo n.º 11
0
 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)
Ejemplo n.º 12
0
 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"))
Ejemplo n.º 13
0
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)
Ejemplo n.º 14
0
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)
Ejemplo n.º 15
0
 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)
Ejemplo n.º 16
0
 def test_user_has_id(self):
     u = User()
     self.assertEqual(len(u.user_id), User.USER_ID_LEN)
Ejemplo n.º 17
0
 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)
Ejemplo n.º 18
0
 def test_user_from_id(self):
     test_id = "TEST_ID"
     u = User.from_user_id(test_id)
     self.assertEqual(test_id, u.user_id)
Ejemplo n.º 19
0
def create_new_user():
    u = User()
    return u.id
Ejemplo n.º 20
0
 def test_room_add_user(self):
     r = Room()
     r.open()
     u = User()
     r.add_user(u)
     self.assertEqual(len(r.users), 1)