Esempio n. 1
0
 def test_reaction_create(self):
     user = UserFactory()
     conversation = ConversationFactory()
     conversation.sync_users([user])
     message = conversation.messages.create(author=user, content='hello')
     message.reactions.create(message=message, user=user, name='tada')
     self.assertEqual(
         ConversationMessageReaction.objects.filter(message=message,
                                                    user=user).count(), 1)
Esempio n. 2
0
    def test_receives_messages(self):
        self.maxDiff = None
        user = UserFactory()
        author = UserFactory()

        # join a conversation
        conversation = ConversationFactory(participants=[user, author])

        # login and connect
        client = self.connect_as(user)
        author_client = self.connect_as(author)

        # add a message to the conversation
        message = ConversationMessage.objects.create(conversation=conversation,
                                                     content='yay',
                                                     author=author)

        # hopefully they receive it!
        self.assertEqual(len(client.messages), 2, client.messages)
        response = client.messages[0]
        parse_dates(response)
        self.assertEqual(response,
                         make_conversation_message_broadcast(message))

        # and they should get an updated conversation object
        response = client.messages[1]
        parse_dates(response)
        del response['payload']['participants']
        self.assertEqual(
            response,
            make_conversation_broadcast(
                conversation,
                unread_message_count=1,
                updated_at=response['payload']['updated_at'],  # TODO fix test
            ))

        # author should get message & updated conversations object too
        response = author_client.messages[0]
        parse_dates(response)
        self.assertEqual(
            response,
            make_conversation_message_broadcast(message, is_editable=True))

        # Author receives more recent `update_at` time,
        # because their `seen_up_to` status is set after sending the message.
        author_participant = conversation.conversationparticipant_set.get(
            user=author)
        response = author_client.messages[1]
        parse_dates(response)
        del response['payload']['participants']
        self.assertEqual(
            response,
            make_conversation_broadcast(
                conversation,
                seen_up_to=message.id,
                updated_at=author_participant.updated_at))
Esempio n. 3
0
 def test_reaction_remove(self):
     # setup
     user = UserFactory()
     conversation = ConversationFactory()
     conversation.sync_users([user])
     message = conversation.messages.create(author=user, content='hello')
     # creating reaction
     message.reactions.create(message=message, user=user, name='tada')
     instance = ConversationMessageReaction.objects.get(message=message, user=user, name='tada')
     # remove reaction
     instance.delete()
     self.assertEqual(ConversationMessageReaction.objects.filter(message=message, user=user).count(), 0)
Esempio n. 4
0
    def test_other_participants_receive_update_on_leave(self):
        user = UserFactory()
        leaving_user = UserFactory()

        # join a conversation
        conversation = ConversationFactory(participants=[user, leaving_user])

        # login and connect
        client = self.connect_as(user)

        conversation.leave(leaving_user)

        response = client.messages_by_topic['conversations:conversation'][0]
        self.assertEqual(response['payload']['participants'], [user.id])
Esempio n. 5
0
    def test_mark_seen_up_to_fails_for_message_in_other_conversation(self):
        conversation = ConversationFactory(participants=[self.user])

        message = conversation.messages.create(author=self.user, content='yay')
        self.client.force_login(user=self.user)

        data = {'seen_up_to': message.id}
        response = self.client.patch('/api/conversations/{}/'.format(
            self.conversation.id),
                                     data,
                                     format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data['seen_up_to'][0],
                         'Must refer to a message in the conversation')
Esempio n. 6
0
 def test_sync_users(self):
     user1 = UserFactory()
     user2 = UserFactory()
     user3 = UserFactory()
     user4 = UserFactory()
     users = [user1, user2, user3]
     conversation = ConversationFactory()
     conversation.join(user1)  # should not be added again
     conversation.join(user4)  # should be removed
     conversation.sync_users(users)
     self.assertEqual(conversation.participants.count(), 3)
     self.assertIn(user1, conversation.participants.all())
     self.assertIn(user2, conversation.participants.all())
     self.assertIn(user3, conversation.participants.all())
     self.assertNotIn(user4, conversation.participants.all())
Esempio n. 7
0
    def test_other_participants_receive_update_on_join(self):
        user = UserFactory()
        joining_user = UserFactory()

        # join a conversation
        conversation = ConversationFactory(participants=[user])
        # login and connect
        client = self.connect_as(user)

        conversation.join(joining_user)

        response = client.messages[0]

        self.assertEqual(response['topic'], 'conversations:conversation')
        self.assertEqual(set(response['payload']['participants']),
                         {user.id, joining_user.id})
Esempio n. 8
0
    def test_create_message(self):
        conversation = ConversationFactory(participants=[self.participant1])

        self.client.force_login(user=self.participant1)
        data = {'conversation': conversation.id, 'content': 'a nice message'}
        response = self.client.post('/api/messages/', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED,
                         response.data)
        self.assertEqual(response.data['content'], data['content'])
        self.assertEqual(conversation.messages.first().content,
                         data['content'])
        self.assertEqual(conversation.messages.first().created_at,
                         parse(response.data['created_at']), response.data)
        self.assertEqual(conversation.messages.first().id, response.data['id'])
        self.assertEqual(conversation.messages.first().author.id,
                         response.data['author'])
Esempio n. 9
0
    def setUp(self):
        self.user = UserFactory()
        self.author = UserFactory()

        self.token = faker.uuid4()
        self.content = faker.text()

        # join a conversation
        self.conversation = ConversationFactory(
            participants=[self.user, self.author])

        # add a push subscriber
        self.subscription = PushSubscription.objects.create(
            user=self.user,
            token=self.token,
            platform=PushSubscriptionPlatform.ANDROID.value,
        )
Esempio n. 10
0
    def tests_receive_message_on_leave(self):
        user = UserFactory()

        # join a conversation
        conversation = ConversationFactory(participants=[
            user,
        ])

        # login and connect
        client = self.connect_as(user)

        conversation.leave(user)

        self.assertEqual(client.messages[0], {
            'topic': 'conversations:leave',
            'payload': {
                'id': conversation.id
            }
        })
Esempio n. 11
0
    def test_conversations_list(self):
        self.conversation1.messages.create(author=self.participant1,
                                           content='yay')
        self.conversation1.messages.create(author=self.participant1,
                                           content='second!')
        conversation2 = ConversationFactory(
            participants=[self.participant1, self.participant2])
        conversation2.messages.create(author=self.participant1, content='yay')
        self.client.force_login(user=self.participant1)

        response = self.client.get('/api/conversations/', format='json')
        response_conversations = response.data['results']['conversations']
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        # is ordered by latest message first
        self.assertEqual(
            [conversation['id'] for conversation in response_conversations],
            [conversation2.id, self.conversation1.id, self.conversation2.id],
        )
        self.assertEqual(response.data['results']['meta'], {
            'marked_at': None,
        })
Esempio n. 12
0
    def test_conversation_marked_as_seen(self):
        user, author = [UserFactory() for _ in range(2)]
        conversation = ConversationFactory(participants=[user, author])
        message = ConversationMessage.objects.create(conversation=conversation,
                                                     content='yay',
                                                     author=author)
        participant = conversation.conversationparticipant_set.get(user=user)
        client = self.connect_as(user)

        participant.seen_up_to = message
        participant.save()

        messages = client.messages_by_topic
        self.assertEqual(len(messages['status']), 1, messages['status'])
        self.assertEqual(
            messages['status'][0]['payload'], {
                'unseen_thread_count': 0,
                'unseen_conversation_count': 0,
                'has_unread_conversations_or_threads': False,
                'groups': {},
                'places': {},
            })
Esempio n. 13
0
 def setUp(self):
     self.user = UserFactory()
     self.conversation = ConversationFactory()
     self.conversation.join(self.user)
Esempio n. 14
0
 def setUp(self):
     self.user = UserFactory()
     self.users = [UserFactory() for _ in range(10)] + [self.user]
     self.user_not_in_conversation = UserFactory()
     # filters user who share a conversation, so add all to one
     ConversationFactory(participants=self.users)
Esempio n. 15
0
 def setUp(self):
     self.user = UserFactory()
     self.conversation = ConversationFactory(participants=[self.user],
                                             is_closed=True)
Esempio n. 16
0
 def test_single_participant_per_conversation_and_user(self):
     user = UserFactory()
     conversation = ConversationFactory(participants=[user])
     with self.assertRaises(IntegrityError):
         ConversationParticipant.objects.create(conversation=conversation,
                                                user=user)
Esempio n. 17
0
 def test_message_create(self):
     user = UserFactory()
     conversation = ConversationFactory(participants=[user])
     conversation.messages.create(author=user, content='yay')
     self.assertEqual(
         ConversationMessage.objects.filter(author=user).count(), 1)
Esempio n. 18
0
 def test_message_create_requires_author(self):
     conversation = ConversationFactory()
     with self.assertRaises(IntegrityError):
         conversation.messages.create(content='ohno')
Esempio n. 19
0
    def test_receives_messages(self):
        self.maxDiff = None
        user = UserFactory()
        author = UserFactory()

        conversation = ConversationFactory(participants=[user, author])
        thread = conversation.messages.create(author=user, content='yay')

        # login and connect
        client = self.connect_as(user)
        author_client = self.connect_as(author)

        reply = ConversationMessage.objects.create(
            conversation=conversation,
            thread=thread,
            content='really yay?',
            author=author,
        )

        # user receive message
        response = client.messages[0]
        parse_dates(response)
        self.assertEqual(
            response,
            make_conversation_message_broadcast(
                reply,
                thread=thread.id,
            ))

        # and they should get an updated thread object
        response = client.messages[1]
        parse_dates(response)
        self.assertEqual(
            response,
            make_conversation_message_broadcast(
                thread,
                thread_meta={
                    'is_participant': True,
                    'muted': False,
                    'participants': [user.id, author.id],
                    'reply_count': 1,
                    'seen_up_to': None,
                    'unread_reply_count': 1
                },
                thread=thread.id,
                is_editable=True,  # user is author of thread message
                updated_at=response['payload']['updated_at'],  # TODO fix test
            ))

        # reply author should get message too
        response = author_client.messages[0]
        parse_dates(response)
        self.assertEqual(
            response,
            make_conversation_message_broadcast(reply,
                                                is_editable=True,
                                                thread=thread.id))

        # Author receives more recent `update_at` time,
        # because their `seen_up_to` status is set after sending the message.
        response = author_client.messages[1]
        parse_dates(response)
        self.assertEqual(
            response,
            make_conversation_message_broadcast(
                thread,
                thread=thread.id,
                thread_meta={
                    'is_participant': True,
                    'muted': False,
                    'participants': [user.id, author.id],
                    'reply_count': 1,
                    'seen_up_to': reply.id,
                    'unread_reply_count': 0,
                },
                updated_at=response['payload']['updated_at'],  # TODO fix test
            ))
Esempio n. 20
0
 def test_join(self):
     user = UserFactory()
     conversation = ConversationFactory(participants=[user])
     self.assertIn(user, conversation.participants.all())
Esempio n. 21
0
 def test_tags_for_other_conversation(self):
     conversation = ConversationFactory()
     tags = conversation_tags(conversation)
     self.assertEqual(tags, {'type': 'unknown'})
Esempio n. 22
0
 def test_tags_for_private_conversation(self):
     conversation = ConversationFactory(is_private=True)
     tags = conversation_tags(conversation)
     self.assertEqual(tags, {'type': 'private'})
Esempio n. 23
0
    def test_receives_messages(self):
        self.maxDiff = None
        op_user = UserFactory()  # op: original post
        author = UserFactory()  # this user will reply to op

        conversation = ConversationFactory(participants=[op_user, author])
        thread = conversation.messages.create(author=op_user, content='yay')

        # login and connect
        op_client = self.connect_as(op_user)
        author_client = self.connect_as(author)

        reply = ConversationMessage.objects.create(
            conversation=conversation,
            thread=thread,
            content='really yay?',
            author=author,
        )

        op_messages = op_client.messages_by_topic

        # updated status
        self.assertEqual(len(op_messages['status']), 1, op_messages['status'])
        self.assertEqual(
            op_messages['status'][0]['payload'], {
                'unseen_thread_count': 1,
                'unseen_conversation_count': 0,
                'has_unread_conversations_or_threads': True,
                'groups': {},
                'places': {},
            })

        # user receive message
        response = op_messages['conversations:message'][0]
        parse_dates(response)
        self.assertEqual(
            response,
            make_conversation_message_broadcast(
                reply,
                thread=thread.id,
            ))

        # and they should get an updated thread object
        response = op_messages['conversations:message'][1]
        parse_dates(response)
        self.assertEqual(
            response,
            make_conversation_message_broadcast(
                thread,
                thread_meta={
                    'is_participant': True,
                    'muted': False,
                    'participants': [op_user.id, author.id],
                    'reply_count': 1,
                    'seen_up_to': None,
                    'unread_reply_count': 1
                },
                thread=thread.id,
                is_editable=True,  # user is author of thread message
                updated_at=response['payload']['updated_at'],  # TODO fix test
            ))

        # reply author should get message too
        response = author_client.messages[0]
        parse_dates(response)
        self.assertEqual(
            response,
            make_conversation_message_broadcast(reply,
                                                is_editable=True,
                                                thread=thread.id))

        # Author receives more recent `update_at` time,
        # because their `seen_up_to` status is set after sending the message.
        response = author_client.messages[1]
        parse_dates(response)
        self.assertEqual(
            response,
            make_conversation_message_broadcast(
                thread,
                thread=thread.id,
                thread_meta={
                    'is_participant': True,
                    'muted': False,
                    'participants': [op_user.id, author.id],
                    'reply_count': 1,
                    'seen_up_to': reply.id,
                    'unread_reply_count': 0,
                },
                updated_at=response['payload']['updated_at'],  # TODO fix test
            ))