def test_sign_up(self): RoomFactory(is_default=False) room = RoomFactory(is_default=True) res = self.post( '/api/auth/sign-up', { 'display': 'display', 'email': '*****@*****.**', 'rank': '3k', 'password': '******', 'recaptcha': 'PASS' }) self.assertEqual(res.code, 200) user = session.query(User).one() self.assertFalse(user.is_active) self.assertEqual(user.display, 'display') self.assertEqual(user.email, '*****@*****.**') self.assertEqual(user.rating, min_rating('3k')) self.assertTrue(user.check_password('pw')) self.assertIsNotNone(user.rating_data) self.assertEqual(user.rating_data.rating, min_rating('3k')) self.assertEqual(len(user.rooms), 1) self.assertEqual(user.rooms[0].room_id, room.id)
def test_message_not_in_room(db, socket): room = RoomFactory() ru = RoomUserFactory() svc = RoomService(db, socket, ru.user) with pytest.raises(ServiceError): svc.execute('message', {'room_id': room.id, 'message': 'test'}) assert len(list(ru.room.recent_messages(db))) == 0 assert len(list(room.recent_messages(db))) == 0
def test_leave_room(db, socket): user = UserFactory() room = RoomFactory() svc = RoomService(db, socket, user) svc.join_room(room.id) svc.leave_room(room.id) assert db.query(RoomUser).count() == 0
def test_connection_data_rooms(db, socket): room = RoomFactory(type='main') ru = RoomUserFactory(room=room) socket.subscribe('connection_data') ConnectionService(db, socket, ru.user).connect() data = socket.sent_messages[0]['data'] assert 'rooms' in data assert len(data['rooms']) == 1
def test_room_recent_messages(db): room = RoomFactory() ru = RoomUserFactory(room=room) RoomMessageFactory(room=room, user=ru.user, message='new', created_at=datetime(2016, 5, 23)) RoomMessageFactory(room=room, user=ru.user, message='old', created_at=datetime(2016, 5, 22)) RoomMessageFactory(room=room, user=ru.user, message='even older', created_at=datetime(2016, 5, 21)) msg = list(room.recent_messages(db, 2)) assert msg[0].message == 'old' assert msg[1].message == 'new'
def test_room_users_offline(db, socket): room = RoomFactory() ru = RoomUserFactory(room=room, user__is_online=True) RoomUserFactory(room=room, user__is_online=False) ru2 = RoomUserFactory(room=room, user__is_online=True) svc = RoomService(db, socket, ru.user) data = svc.execute('users', {'room_id': ru.room_id}) users = data.get('users') user_ids = [u['user_id'] for u in users] assert users is not None assert len(users) == 2 assert ru.user_id in user_ids assert ru2.user_id in user_ids
def test_users_max(db, socket): room = RoomFactory() svc = RoomService(db, socket, UserFactory()) svc.join_room(room.id) assert room.users_max == 1 svc = RoomService(db, socket, UserFactory()) svc.join_room(room.id) assert room.users_max == 2 svc.leave_room(room.id) assert room.users_max == 2 svc = RoomService(db, socket, UserFactory(is_online=False)) svc.join_room(room.id) assert room.users_max == 2
def test_join_room(db, socket): user = UserFactory() room = RoomFactory() svc = RoomService(db, socket, user) socket.subscribe('room_user/' + str(room.id)) svc.join_room(room.id) assert db.query(RoomUser).count() == 1 ru = db.query(RoomUser).first() assert ru.room == room assert ru.user == user assert len(socket.sent_messages) == 1 assert socket.sent_messages[0]['method'] == 'room_user' assert socket.sent_messages[0]['data']['user_id'] == user.id assert socket.sent_messages[0]['data']['user_display'] == user.display assert socket.sent_messages[0]['data']['user_rating'] == user.rating