Ejemplo n.º 1
0
    def test_chat_single(self):
        self.init_main_user()
        self.register_users(1)
        self.assertEqual(db.session.query(chats).count(), 0)
        self.assertEqual(db.session.query(Message).count(), 0)
        m1 = Message(text='bla', sender_id=1, receiver_id=2)
        m2 = Message(text='blabla', sender_id=2, receiver_id=1)
        db.session.add_all([m1, m2])
        db.session.commit()
        self.assertEqual(db.session.query(chats).count(), 1)
        self.assertEqual(db.session.query(Message).count(), 2)

        response = self.test_client.get('/api/chats/1', headers=self.bearer_auth_header)
        self.assertEqual(response.status_code, 200)
        data = response.json
        self.assertEqual(data['user_id'], 1)
        chat = data['data']
        self.assertTrue(chat['chat_id'] == 1 and chat['user1_id'] == 1 and chat['user2_id'] == 2)

        response = self.test_client.delete('/api/chats/1', headers=self.bearer_auth_header)
        self.assertEqual(response.status_code, 200)
        data = response.json
        self.assertTrue(data['user_id'] == 1 and data['chat_id'] == 1)
        self.assertEqual(db.session.query(chats).count(), 0)
        self.assertEqual(db.session.query(Message).count(), 0)
Ejemplo n.º 2
0
 def test_return_message_or_abort(self):
     users = init_users(2)
     m1 = Message(text='blabla', sender_id=2, receiver_id=1)
     m2 = Message(text='blabla2', sender_id=1, receiver_id=2)
     db.session.add_all(users + [m1, m2])
     db.session.commit()
     self.assertEqual(return_message_or_abort(1), m1)
     self.assertEqual(return_message_or_abort(2), m2)
     with self.assertRaises(NotFound):
         return_message_or_abort(3)
Ejemplo n.º 3
0
 def test_abort_if_not_from_a_chat(self):
     users = init_users(3)
     m1 = Message(text='blabla', sender_id=2, receiver_id=1)
     m2 = Message(text='blabla2', sender_id=1, receiver_id=3)
     db.session.add_all(users + [m1, m2])
     db.session.commit()
     with self.assertRaises(Forbidden):
         abort_if_not_from_a_chat(2, m1)
     try:
         abort_if_not_from_a_chat(2, m2)
     except Forbidden:
         self.fail(msg="403 error must not have been raised")
Ejemplo n.º 4
0
    def test_get_user_chats_and_last_messages(self):
        users = init_users(5)
        User.create_chat(1, 2)
        User.create_chat(1, 3)
        User.create_chat(1, 4)
        User.create_chat(2, 3)
        db.session.add_all(users)
        m1 = Message(text='m1', sender_id=1, receiver_id=2)
        m2 = Message(text='m2', sender_id=2, receiver_id=1)
        m3 = Message(text='m3', sender_id=1, receiver_id=2)

        m4 = Message(text='m4', sender_id=1, receiver_id=3)
        m5 = Message(text='m5', sender_id=3, receiver_id=1)

        m6 = Message(text='m6', sender_id=1, receiver_id=4)

        m7 = Message(text='m7', sender_id=2, receiver_id=3)
        m8 = Message(text='m8', sender_id=3, receiver_id=2)

        db.session.add_all([m1, m2, m3, m4, m5, m6, m7, m8])
        db.session.commit()

        chats1 = get_uc(1).all()
        self.assertEqual(len(chats1), 3)
        self.assertEqual(chats1[0][0], 'user4')
        self.assertEqual(chats1[1][0], 'user3')
        self.assertEqual(chats1[2][0], 'user2')
        self.assertEqual(chats1[0][2], 'm6')
        self.assertEqual(chats1[1][2], 'm5')
        self.assertEqual(chats1[2][2], 'm3')

        chats2 = get_uc(2).all()
        self.assertEqual(len(chats2), 2)
        self.assertEqual(chats2[0][0], 'user3')
        self.assertEqual(chats2[1][0], 'user1')
        self.assertEqual(chats2[0][2], 'm8')
        self.assertEqual(chats2[1][2], 'm3')

        chats3 = get_uc(3).all()
        self.assertEqual(len(chats3), 2)
        self.assertEqual(chats3[0][0], 'user2')
        self.assertEqual(chats3[1][0], 'user1')
        self.assertEqual(chats3[0][2], 'm8')
        self.assertEqual(chats3[1][2], 'm5')

        chats4 = get_uc(4).all()
        self.assertEqual(len(chats4), 1)
        self.assertEqual(chats4[0][0], 'user1')
        self.assertEqual(chats4[0][2], 'm6')

        chats5 = get_uc(5).all()
        self.assertEqual(len(chats5), 0)
Ejemplo n.º 5
0
    def test_chat_messages_list_get(self):
        self.init_main_user()
        self.register_users(2)
        m1 = Message(text='bla1', sender_id=1, receiver_id=2)
        m2 = Message(text='bla2', sender_id=2, receiver_id=1)
        m3 = Message(text='bla3', sender_id=1, receiver_id=2)
        m4 = Message(text='bla4', sender_id=2, receiver_id=1)

        m5 = Message(text='bla5', sender_id=1, receiver_id=3)
        m6 = Message(text='bla6', sender_id=3, receiver_id=1)
        db.session.add_all([m1, m2, m3, m4, m5, m6])
        db.session.commit()

        response = self.test_client.get('/api/chats/1/messages', headers=self.bearer_auth_header)
        self.assertEqual(response.status_code, 200)
        data = response.json
        self.assertTrue(data['user_id'] == 1 and data['chat_id'] == 1)
        messages = data['data']
        self.assertEqual(len(messages), 4)
        self.assertTrue(
            messages[0]['text'] == 'bla1' and messages[1]['text'] == 'bla2' and messages[2]['text'] == 'bla3' and
            messages[3]['text'] == 'bla4')

        response = self.test_client.get('/api/chats/2/messages', headers=self.bearer_auth_header)
        self.assertEqual(response.status_code, 200)
        data = response.json
        self.assertTrue(data['user_id'] == 1 and data['chat_id'] == 2)
        messages = data['data']
        self.assertEqual(len(messages), 2)
        self.assertTrue(messages[0]['text'] == 'bla5' and messages[1]['text'] == 'bla6')
Ejemplo n.º 6
0
    def test_chat_message_single(self):
        self.init_main_user()
        self.register_users(1)
        m1 = Message(text='bla1', sender_id=1, receiver_id=2)
        m2 = Message(text='bla2', sender_id=2, receiver_id=1)
        m3 = Message(text='bla2', sender_id=1, receiver_id=2)
        db.session.add_all([m1, m2, m3])
        db.session.commit()
        response = self.test_client.get('/api/chats/1/messages/2', headers=self.bearer_auth_header)
        self.assertEqual(response.status_code, 200)
        data = response.json
        self.assertTrue(data['user_id'] == 1 and data['chat_id'] == 1)
        message = data['data']
        self.assertTrue(message['text'] == 'bla2' and message['sender_id'] == 2 and message['receiver_id'] == 1)

        response = self.test_client.get('/api/chats/1/messages/1', headers=self.bearer_auth_header)
        self.assertEqual(response.status_code, 200)
        data = response.json
        message = data['data']
        self.assertTrue(message['text'] == 'bla1' and message['sender_id'] == 1 and message['receiver_id'] == 2)

        self.assertEqual(db.session.query(Message).count(), 3)
        response = self.test_client.delete('/api/chats/1/messages/3', headers=self.bearer_auth_header)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json['message_id'], 3)

        self.assertEqual(db.session.query(Message).count(), 2)
        response = self.test_client.put('/api/chats/1/messages/1', headers=self.bearer_auth_header,
                                        json={'text': 'new text'})
        self.assertEqual(response.status_code, 200)
        data = response.json
        self.assertTrue(
            data['user_id'] == 1 and data['chat_id'] == 1 and data['message_id'] == 1 and data['text'] == 'new text')
        self.assertEqual(db.session.query(Message).count(), 2)
        message = Message.query.first()
        self.assertTrue(message.text == 'new text' and message.sender_id == 1 and message.receiver_id == 2)
Ejemplo n.º 7
0
 def post(self, chat_id: int) -> Tuple[dict, int]:
     """Allows to send one or more messages in given chat from the authenticated user"""
     current_user_id = g.user.user_id
     chat = return_chat_or_abort(chat_id)
     abort_if_not_a_participant(current_user_id, chat)
     parser = reqparse.RequestParser()
     parser.add_argument('texts', type=longer_than_zero, action='append', required=True)
     args = parser.parse_args()
     if args['texts']:
         user1_id, user2_id = chat.user1_id, chat.user2_id
         receiver_id = user2_id if user1_id == current_user_id else user1_id
         for text in args['texts']:
             message = Message(text=text, sender_id=current_user_id, receiver_id=receiver_id, chat_id=chat_id)
             db.session.add(message)
         db.session.commit()
         return {'user_id': current_user_id, 'chat_id': chat_id,
                 'message': f"Your message{'s were' if len(args['texts']) > 1 else ' was'} successfully sent"}, 201
Ejemplo n.º 8
0
    def test_user_chats_list(self):
        with self.test_client as client:
            client.post('/authentication/register',
                        data={
                            'email': '*****@*****.**',
                            'username': '******',
                            'name': 'Ann1',
                            'password1': 'Who am I',
                            'password2': 'Who am I'
                        },
                        follow_redirects=True)
            client.post('/authentication/register',
                        data={
                            'email': '*****@*****.**',
                            'username': '******',
                            'name': 'Ann2',
                            'password1': 'Who am I',
                            'password2': 'Who am I'
                        },
                        follow_redirects=True)

            client.post('/authentication/login',
                        data={
                            'email': '*****@*****.**',
                            'password': '******'
                        },
                        follow_redirects=True)

            User.create_chat(1, 2)
            db.session.add(
                Message(text='test_text', sender_id=1, receiver_id=2))
            db.session.commit()
            response = client.get('/chats/list')
            self.assertEqual(response.status_code, 200)
            response_data = response.data.decode()
            self.assertTrue('<title>Chats list</title>' in response_data)
            self.assertTrue('test_text' in response_data)
            self.assertTrue('Ann2' in response_data)
            self.assertTrue('test_user2' in response_data)