示例#1
0
 def set_up(self):
     super().set_up()
     self.user1 = ActiveUserFactory()
     self.chat = ChatFactory(ent1=self.user1)
     self.messages = []
     self.messages.append(Message.objects.send_message(from_entity=self.chat.ent1, chat=self.chat, text='text'))
     sleep(0.1)
     self.messages.append(Message.objects.send_message(from_entity=self.chat.ent2, chat=self.chat, text='text'))
     sleep(0.1)
     self.chat_url = '/messages/{}/'.format(self.chat.get_slug(current_user=self.user1))
     self.page_url = '/messages/{}/mark-read/'.format(self.chat.id)
示例#2
0
 def set_up(self):
     super().set_up()
     self.user1 = ActiveUserFactory()
     self.user2 = ActiveUserFactory()
     self.user3 = ActiveUserFactory()
     self.chat_1_2 = ChatFactory(ent1=self.user1, ent2=self.user2)
     self.chat_2_3 = ChatFactory(ent1=self.user2, ent2=self.user3)
     self.chat_3_1 = ChatFactory(ent1=self.user3, ent2=self.user1)
     self.chat_url = '/messages/{}/'.format(self.chat_1_2.get_slug(current_user=self.user1))
     self.page_url = '/messages/{}/send/'.format(self.chat_1_2.id)
     self.data = {
         'text': 'Hi Hi Hi',
     }
示例#3
0
 def test_get_slug_private_chat(self):
     user_1 = self.get_active_user_doron()
     user_2 = self.get_active_user_jennifer()
     chat = ChatFactory(ent1=user_1, ent2=user_2)
     self.assertEqual(first=chat.get_slug(current_user=user_1),
                      second=user_2.slug)
     self.assertEqual(first=chat.get_slug(current_user=user_2),
                      second=user_1.slug)
     self.assertNotEqual(first=chat.get_slug(current_user=user_1),
                         second=user_1.slug)
     self.assertNotEqual(first=chat.get_slug(current_user=user_2),
                         second=user_2.slug)
     self.assertEqual(first=chat.get_slug(current_user=user_1),
                      second="jennifer-connelly")
     self.assertEqual(first=chat.get_slug(current_user=user_2),
                      second="doron-matalon")
 def test_sending_message_to_existing_chat(self):
     user1 = ActiveUserFactory()
     chat = ChatFactory(ent1=user1)
     self.assertEqual(first=Chat.objects.count(), second=1)
     self.assertEqual(first=ReadMark.objects.count(), second=0)
     message = Message.objects.send_message(from_entity=user1, chat=chat, text='Hello2')
     self.assertEqual(first=Chat.objects.count(), second=1)
     self.assertEqual(first=message.chat, second=chat)
     self.assertEqual(first=message.sender_id, second=user1.id)
     self.assertEqual(first=message.text, second='Hello2')
     self.assertEqual(first=ReadMark.objects.count(), second=1)
     rmark = ReadMark.objects.latest()
     self.assertEqual(first=rmark.chat, second=chat)
     self.assertEqual(first=rmark.entity_id, second=user1.id)
示例#5
0
 def test_form_to_chat_save(self):
     data = {
         'text': 'Hi!',
     }
     user = ActiveUserFactory()
     chat = ChatFactory(ent1=user)
     form = MessageForm(from_entity=user, chat=chat, data=data)
     self.assertTrue(expr=form.is_valid())
     message = form.save()
     self.assertEqual(first=message.chat, second=chat)
     self.assertEqual(first=message.sender.user, second=user)
     self.assertEqual(first=message.text, second='Hi!')
     chat = message.chat
     self.assertEqual(first=chat.last_message, second=message)
示例#6
0
class SendMessageToChatViewTestCase(SiteTestCase):
    def set_up(self):
        super().set_up()
        self.user1 = ActiveUserFactory()
        self.user2 = ActiveUserFactory()
        self.user3 = ActiveUserFactory()
        self.chat_1_2 = ChatFactory(ent1=self.user1, ent2=self.user2)
        self.chat_2_3 = ChatFactory(ent1=self.user2, ent2=self.user3)
        self.chat_3_1 = ChatFactory(ent1=self.user3, ent2=self.user1)
        self.chat_url = '/messages/{}/'.format(
            self.chat_1_2.get_slug(current_user=self.user1))
        self.page_url = '/messages/{}/send/'.format(self.chat_1_2.id)
        self.data = {
            'text': 'Hi Hi Hi',
        }

    def test_visitor_has_no_access(self):
        self.client.logout()
        r = self.client.post(path=self.page_url, data=self.data)
        self.assertEqual(first=r.status_code, second=403)

    def test_get_redirects_to_chat_page(self):
        self.client.login(username=self.user1.slug,
                          password=tests_settings.USER_PASSWORD)
        r = self.client.get(path=self.page_url)
        self.assertRedirects(response=r, expected_url=self.chat_url)

    def test_user_can_write_to_a_chat_he_has_access_to(self):
        self.client.login(username=self.user1.slug,
                          password=tests_settings.USER_PASSWORD)
        r = self.client.post(path=self.page_url, data=self.data)
        self.assertRedirects(response=r, expected_url=self.chat_url)

    def test_cannot_write_to_a_blocker(self):
        self.client.login(username=self.user1.slug,
                          password=tests_settings.USER_PASSWORD)
        Block.objects.block(blocker=self.user2, blocked=self.user1)
        r = self.client.post(path=self.page_url, data=self.data)
        self.assertEqual(first=r.status_code, second=403)

    def test_cannot_write_to_a_blocked(self):
        self.client.login(username=self.user1.slug,
                          password=tests_settings.USER_PASSWORD)
        Block.objects.block(blocker=self.user1, blocked=self.user2)
        r = self.client.post(path=self.page_url, data=self.data)
        self.assertEqual(first=r.status_code, second=403)
示例#7
0
class SendMessageToChatViewTestCase(SiteTestCase):
    def set_up(self):
        super().set_up()
        self.user1 = ActiveUserFactory()
        self.user2 = ActiveUserFactory()
        self.user3 = ActiveUserFactory()
        self.chat_1_2 = ChatFactory(ent1=self.user1, ent2=self.user2)
        self.chat_2_3 = ChatFactory(ent1=self.user2, ent2=self.user3)
        self.chat_3_1 = ChatFactory(ent1=self.user3, ent2=self.user1)
        self.chat_url = '/messages/{}/'.format(self.chat_1_2.get_slug(current_user=self.user1))
        self.page_url = '/messages/{}/send/'.format(self.chat_1_2.id)
        self.data = {
            'text': 'Hi Hi Hi',
        }

    def test_visitor_has_no_access(self):
        self.client.logout()
        r = self.client.post(path=self.page_url, data=self.data)
        self.assertEqual(first=r.status_code, second=403)

    def test_get_redirects_to_chat_page(self):
        self.client.login(username=self.user1.slug, password=tests_settings.USER_PASSWORD)
        r = self.client.get(path=self.page_url)
        self.assertRedirects(response=r, expected_url=self.chat_url)

    def test_user_can_write_to_a_chat_he_has_access_to(self):
        self.client.login(username=self.user1.slug, password=tests_settings.USER_PASSWORD)
        r = self.client.post(path=self.page_url, data=self.data)
        self.assertRedirects(response=r, expected_url=self.chat_url)

    def test_cannot_write_to_a_blocker(self):
        self.client.login(username=self.user1.slug, password=tests_settings.USER_PASSWORD)
        Block.objects.block(blocker=self.user2, blocked=self.user1)
        r = self.client.post(path=self.page_url, data=self.data)
        self.assertEqual(first=r.status_code, second=403)

    def test_cannot_write_to_a_blocked(self):
        self.client.login(username=self.user1.slug, password=tests_settings.USER_PASSWORD)
        Block.objects.block(blocker=self.user1, blocked=self.user2)
        r = self.client.post(path=self.page_url, data=self.data)
        self.assertEqual(first=r.status_code, second=403)
    def test_tag(self):
        user1 = ActiveUserFactory()
        user2 = ActiveUserFactory()
        chat = ChatFactory(ent1=user1, ent2=user2)
        messages = [
            Message.objects.send_message(from_entity=user2, chat=chat, text='User 2 First Message'),
            Message.objects.send_message(from_entity=user1, chat=chat, text='User 1 Message'),
            Message.objects.send_message(from_entity=user2, chat=chat, text='User 2 Second Message'),
        ]
        self.assertEqual(first=ReadMark.objects.count(), second=2)

        output = im_tags.annotate_messages_with_read_marks(messages, user1)
        self.assertEqual(first=output, second='')
        self.assertFalse(expr=messages[0].is_unread)
        self.assertFalse(expr=messages[1].is_unread)
        self.assertTrue(expr=messages[2].is_unread)

        output = im_tags.annotate_messages_with_read_marks(messages, user2)
        self.assertEqual(first=output, second='')
        self.assertFalse(expr=messages[0].is_unread)
        self.assertFalse(expr=messages[1].is_unread)
        self.assertFalse(expr=messages[2].is_unread)
示例#9
0
class MarkChatAsReadViewTestCase(SiteTestCase):
    def set_up(self):
        super().set_up()
        self.user1 = ActiveUserFactory()
        self.chat = ChatFactory(ent1=self.user1)
        self.messages = []
        self.messages.append(
            Message.objects.send_message(from_entity=self.chat.ent1,
                                         chat=self.chat,
                                         text='text'))
        sleep(0.1)
        self.messages.append(
            Message.objects.send_message(from_entity=self.chat.ent2,
                                         chat=self.chat,
                                         text='text'))
        sleep(0.1)
        self.chat_url = '/messages/{}/'.format(
            self.chat.get_slug(current_user=self.user1))
        self.page_url = '/messages/{}/mark-read/'.format(self.chat.id)

    def test_visitor_has_no_access(self):
        self.client.logout()
        r = self.client.post(path=self.page_url)
        self.assertRedirects(response=r,
                             expected_url='/login/?next={}'.format(
                                 self.page_url))

    def test_user_can_mark_chat_as_read(self):
        self.client.login(username=self.user1.slug,
                          password=tests_settings.USER_PASSWORD)
        self.assertLess(
            a=ReadMark.objects.get(entity_id=self.user1.id).date_updated,
            b=self.messages[1].date_created)
        r = self.client.post(path=self.page_url)
        self.assertRedirects(response=r, expected_url=self.chat_url)
        self.assertGreater(
            a=ReadMark.objects.get(entity_id=self.user1.id).date_updated,
            b=self.messages[1].date_created)
示例#10
0
class MarkChatAsReadViewTestCase(SiteTestCase):
    def set_up(self):
        super().set_up()
        self.user1 = ActiveUserFactory()
        self.chat = ChatFactory(ent1=self.user1)
        self.messages = []
        self.messages.append(Message.objects.send_message(from_entity=self.chat.ent1, chat=self.chat, text='text'))
        sleep(0.1)
        self.messages.append(Message.objects.send_message(from_entity=self.chat.ent2, chat=self.chat, text='text'))
        sleep(0.1)
        self.chat_url = '/messages/{}/'.format(self.chat.get_slug(current_user=self.user1))
        self.page_url = '/messages/{}/mark-read/'.format(self.chat.id)

    def test_visitor_has_no_access(self):
        self.client.logout()
        r = self.client.post(path=self.page_url)
        self.assertRedirects(response=r, expected_url='/login/?next={}'.format(self.page_url))

    def test_user_can_mark_chat_as_read(self):
        self.client.login(username=self.user1.slug, password=tests_settings.USER_PASSWORD)
        self.assertLess(a=ReadMark.objects.get(entity_id=self.user1.id).date_updated, b=self.messages[1].date_created)
        r = self.client.post(path=self.page_url)
        self.assertRedirects(response=r, expected_url=self.chat_url)
        self.assertGreater(a=ReadMark.objects.get(entity_id=self.user1.id).date_updated, b=self.messages[1].date_created)
 def test_tag(self):
     user1 = ActiveUserFactory()
     user2 = ActiveUserFactory()
     chat = ChatFactory(ent1=user1, ent2=user2)
     self.assertEqual(first=im_tags.get_other_participant(chat, user1).id, second=user2.id)
     self.assertEqual(first=im_tags.get_other_participant(chat, user2).id, second=user1.id)
示例#12
0
 def test_id_length(self):
     chat = ChatFactory()
     self.assertEqual(first=len(chat.id), second=20)