示例#1
0
    def test_remove_trust_when_giver_leaves_group(self):
        editor = UserFactory()
        newcomer = UserFactory()
        group = GroupFactory(members=[editor], newcomers=[newcomer])
        membership = GroupMembership.objects.get(user=newcomer, group=group)
        Trust.objects.create(membership=membership, given_by=editor)

        group.remove_member(editor)

        self.assertEqual(0, Trust.objects.filter(membership=membership).count())
    def test_removes_notification_when_leaving_group(self):
        user = UserFactory()
        group = GroupFactory(members=[user])
        Notification.objects.all().delete()
        Notification.objects.create(user=user,
                                    type='foo',
                                    context={'group': group.id})

        group.remove_member(user)

        self.assertEqual(Notification.objects.count(), 0)
示例#3
0
    def test_do_not_remove_trust_in_other_groups(self):
        editor = UserFactory()
        newcomer = UserFactory()
        group = GroupFactory(members=[editor], newcomers=[newcomer])
        membership = GroupMembership.objects.get(user=newcomer, group=group)
        other_group = GroupFactory(members=[editor])
        Trust.objects.create(membership=membership, given_by=editor)

        other_group.remove_member(editor)

        self.assertEqual(1, Trust.objects.filter(membership=membership).count())
示例#4
0
class TestPlaceModel(TestCase):
    def setUp(self):
        self.group = GroupFactory()

    def test_create_fails_if_name_too_long(self):
        with self.assertRaises(DataError):
            Place.objects.create(name='a' * 81, group=self.group)

    def test_create_place_with_same_name_fails(self):
        Place.objects.create(name='abcdef', group=self.group)
        with self.assertRaises(IntegrityError):
            Place.objects.create(name='abcdef', group=self.group)

    def test_create_place_with_same_name_in_different_groups_works(self):
        Place.objects.create(name='abcdef', group=self.group)
        Place.objects.create(name='abcdef', group=GroupFactory())

    def test_get_active_status(self):
        s = Place.objects.create(name='my place', group=self.group)
        self.assertFalse(s.is_active())

        s.status = 'active'
        s.save()
        self.assertTrue(s.is_active())

        s.status = 'declined'
        s.save()
        self.assertFalse(s.is_active())

    def test_removes_subscription_when_leaving_group(self):
        place = Place.objects.create(name='abcdef', group=self.group)
        user = UserFactory()
        self.group.add_member(user)
        place.placesubscription_set.create(user=user)

        self.group.remove_member(user)

        self.assertFalse(place.placesubscription_set.filter(user=user).exists())
        conversation = place.conversation
        self.assertFalse(conversation.conversationparticipant_set.filter(user=user).exists())
示例#5
0
class GroupMembershipReceiverTests(WSTestCase):
    def setUp(self):
        super().setUp()
        self.member = UserFactory()
        self.user = UserFactory()
        self.group = GroupFactory(members=[self.member])

    def test_receive_group_join(self):
        member_client = self.connect_as(self.member)
        joining_client = self.connect_as(self.user)
        nonmember_client = self.connect_as(UserFactory())

        self.group.add_member(self.user)

        response = member_client.messages_by_topic.get(
            'groups:group_detail')[0]
        self.assertIn(self.user.id, response['payload']['members'])
        self.assertIn(self.user.id, response['payload']['memberships'].keys())

        response = member_client.messages_by_topic.get(
            'groups:group_preview')[0]
        self.assertIn(self.user.id, response['payload']['members'])
        self.assertNotIn('memberships', response['payload'])

        response = joining_client.messages_by_topic.get(
            'groups:group_detail')[0]
        self.assertIn(self.user.id, response['payload']['members'])

        response = joining_client.messages_by_topic.get(
            'groups:group_preview')[0]
        self.assertIn(self.user.id, response['payload']['members'])

        self.assertNotIn('groups:group_detail',
                         nonmember_client.messages_by_topic.keys())
        response = nonmember_client.messages_by_topic.get(
            'groups:group_preview')[0]
        self.assertIn(self.user.id, response['payload']['members'])
        self.assertNotIn('memberships', response['payload'])

    def test_receive_group_leave_as_leaving_user(self):
        # Clean up notifications from group setup, to prevent notification_deleted messages
        Notification.objects.all().delete()
        client = self.connect_as(self.member)

        self.group.remove_member(self.member)

        response = client.messages_by_topic.get('groups:group_preview')[0]
        self.assertNotIn(self.user.id, response['payload']['members'])
        self.assertNotIn('memberships', response['payload'])
        self.assertEqual([m['topic'] for m in client.messages], [
            'history:history',
            'conversations:leave',
            'conversations:conversation',
            'status',
            'groups:group_preview',
        ])

        status_messages = client.messages_by_topic['status']
        self.assertEqual(len(status_messages), 1, status_messages)
        self.assertEqual(
            status_messages[0]['payload'], {
                'unseen_conversation_count': 0,
                'unseen_thread_count': 0,
                'has_unread_conversations_or_threads': False,
                'groups': {
                    self.group.id: {
                        'unread_wall_message_count': 0
                    }
                },
                'places': {},
            })

    def test_receive_group_roles_update(self):
        membership = self.group.add_member(self.user)
        client = self.connect_as(self.member)

        membership.add_roles([roles.GROUP_EDITOR])
        membership.save()

        response = client.messages_by_topic.get('groups:group_detail')[0]
        self.assertIn(
            roles.GROUP_EDITOR,
            response['payload']['memberships'][self.user.id]['roles'])

        self.assertEqual([m['topic'] for m in client.messages], [
            'notifications:notification',
            'status',
            'groups:group_detail',
        ])
示例#6
0
class TestConversationNotificationTask(TestCase):
    def setUp(self):
        self.user = VerifiedUserFactory()
        self.author = VerifiedUserFactory()
        self.group = GroupFactory(members=[self.author, self.user])
        mail.outbox = []
        with suppressed_notifications():
            self.message = self.group.conversation.messages.create(
                author=self.author, content='initial message')

    def test_only_notifies_active_group_members(self):
        self.group.add_member(UserFactory())
        inactive_user = VerifiedUserFactory()
        self.group.add_member(inactive_user)
        self.group.groupmembership_set.filter(user=inactive_user).update(
            inactive_at=timezone.now())
        mail.outbox = []
        with execute_scheduled_tasks_immediately():
            self.group.conversation.messages.create(author=self.author,
                                                    content='foo')

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].to, [self.user.email])

    def test_notify_about_unseen_message(self):
        self.group.conversation.conversationparticipant_set.filter(
            user=self.user).update(seen_up_to=self.message)
        with execute_scheduled_tasks_immediately():
            self.group.conversation.messages.create(
                author=self.author, content='this should be sent')

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].to[0], self.user.email)
        self.assertIn('this should be sent', mail.outbox[0].body)
        self.assertNotIn('initial message', mail.outbox[0].body)

    def test_exclude_seen_message(self):
        with suppressed_notifications():
            another_message = self.group.conversation.messages.create(
                author=self.author, content='foo')
        self.group.conversation.conversationparticipant_set.filter(
            user=self.user).update(seen_up_to=another_message)

        tasks.notify_participants(another_message)
        self.assertEqual(len(mail.outbox), 0)

    def test_exclude_thread_replies_from_conversation_notification(self):
        with suppressed_notifications():
            self.group.conversation.messages.create(
                author=self.user,
                thread=self.message,
                content='first thread reply')
        with execute_scheduled_tasks_immediately():
            self.group.conversation.messages.create(author=self.author,
                                                    content='conversation')

        self.assertNotIn('first thread reply', mail.outbox[0].body)

    def test_does_notification_batching_in_threads(self):
        with suppressed_notifications():
            self.group.conversation.messages.create(
                author=self.user,
                thread=self.message,
                content='first thread reply')
        with execute_scheduled_tasks_immediately():
            recent_message = self.group.conversation.messages.create(
                author=self.user,
                thread=self.message,
                content='second thread reply')

        self.assertEqual(len(mail.outbox), 1)
        self.assertIn('first thread reply', mail.outbox[0].body)
        self.assertIn('second thread reply', mail.outbox[0].body)
        self.assertEqual(mail.outbox[0].to[0], self.author.email)
        participant = ConversationThreadParticipant.objects.get(
            thread=self.message, user=self.author)
        self.assertEqual(participant.notified_up_to.id, recent_message.id)

    def test_exclude_seen_message_in_thread(self):
        with suppressed_notifications():
            another_message = self.group.conversation.messages.create(
                author=self.user,
                thread=self.message,
                content='first thread reply')
        ConversationThreadParticipant.objects.filter(
            thread=self.message,
            user=self.author).update(seen_up_to=another_message)

        self.assertEqual(len(mail.outbox), 0)

    def test_exclude_already_notified_in_thread(self):
        with execute_scheduled_tasks_immediately():
            self.group.conversation.messages.create(
                author=self.user,
                thread=self.message,
                content='first thread reply')
            mail.outbox = []
            self.group.conversation.messages.create(
                author=self.user,
                thread=self.message,
                content='second thread reply')

        self.assertEqual(len(mail.outbox), 1)
        self.assertIn('second thread reply', mail.outbox[0].body)
        self.assertNotIn('first thread reply', mail.outbox[0].body)

    def test_exclude_participants_without_access(self):
        self.group.remove_member(self.author)
        with execute_scheduled_tasks_immediately():
            self.group.conversation.messages.create(
                author=self.user,
                thread=self.message,
                content='first thread reply')

        self.assertEqual(len(mail.outbox), 0)