def test_creates_new_applicant_notification(self):
        member = UserFactory()
        group = GroupFactory(members=[member])
        self.assertEqual(
            Notification.objects.filter(
                user=member,
                type=NotificationType.NEW_APPLICANT.value).count(), 0)

        user = UserFactory()
        ApplicationFactory(user=user, group=group)

        self.assertEqual(
            Notification.objects.filter(
                user=member,
                type=NotificationType.NEW_APPLICANT.value).count(), 1)
    def test_creates_application_declined_notification(self):
        member = UserFactory()
        group = GroupFactory(members=[member])

        user = UserFactory()
        application = ApplicationFactory(user=user, group=group)
        application.decline(member)

        self.assertEqual(
            Notification.objects.filter(
                user=user,
                type=NotificationType.APPLICATION_DECLINED.value).count(), 1)
        self.assertEqual(
            Notification.objects.filter(
                type=NotificationType.NEW_APPLICANT.value).count(), 0)
    def test_creates_new_member_notification(self):
        member1 = UserFactory()
        member2 = UserFactory()
        group = GroupFactory(members=[member1, member2])
        Notification.objects.all().delete()

        user = UserFactory()
        group.add_member(user, added_by=member1)

        notifications = Notification.objects.filter(
            type=NotificationType.NEW_MEMBER.value)
        # member1 doesn't get a notification, as they added the user
        self.assertEqual(notifications.count(), 1, notifications)
        self.assertEqual(notifications[0].user, member2)
        self.assertEqual(notifications[0].context['user'], user.id)
Beispiel #4
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())
Beispiel #5
0
 def setUp(self):
     self.user = UserFactory()
     self.member = UserFactory()
     self.group = GroupFactory(members=[self.member], is_open=True)
     self.group_with_password = GroupFactory(password='******', is_open=True)
     self.join_password_url = '/api/groups/{}/join/'.format(self.group_with_password.id)
     self.url = '/api/groups/'
     self.group_data = {
         'name': faker.name(),
         'description': faker.text(),
         'address': faker.address(),
         'latitude': faker.latitude(),
         'longitude': faker.longitude(),
         'timezone': 'Europe/Berlin'
     }
Beispiel #6
0
    def test_removes_new_application_notification_when_decided(self):
        member = UserFactory()
        group = GroupFactory(members=[member])

        users = [UserFactory() for _ in range(3)]
        applications = [
            ApplicationFactory(user=user, group=group) for user in users
        ]
        applications[0].withdraw()
        applications[1].accept(member)
        applications[2].decline(member)

        self.assertEqual(
            Notification.objects.filter(
                type=NotificationType.NEW_APPLICANT.value).count(), 0)
Beispiel #7
0
    def setUp(self):
        self.member = UserFactory()
        self.non_editor_member = UserFactory()
        self.non_member = UserFactory()
        self.group = GroupFactory(
            members=[self.member, self.non_editor_member])
        self.place = PlaceFactory(group=self.group)
        self.activity_types = [
            ActivityTypeFactory(group=self.group) for _ in range(3)
        ]
        self.activity_type = self.activity_types[0]

        # remove all roles
        GroupMembership.objects.filter(
            group=self.group, user=self.non_editor_member).update(roles=[])
Beispiel #8
0
 def setUp(self):
     self.user = UserFactory()
     self.member = UserFactory()
     self.group = GroupFactory(members=[self.member,
                                        UserFactory()],
                               is_open=True)
     self.url = '/api/groups/'
     self.group_data = {
         'name': faker.name(),
         'description': faker.text(),
         'address': faker.address(),
         'latitude': faker.latitude(),
         'longitude': faker.longitude(),
         'timezone': 'Europe/Berlin'
     }
Beispiel #9
0
    def test_no_message_notification_if_muted(self, notify):
        author = UserFactory()
        user = UserFactory()
        group = GroupFactory(members=[author, user])
        conversation = Conversation.objects.get_or_create_for_target(group)
        message = conversation.messages.create(author=author, content='bla')

        participant = ConversationParticipant.objects.get(user=user, conversation=conversation)
        participant.muted = True
        participant.save()
        PushSubscription.objects.create(user=user, token='', platform=PushSubscriptionPlatform.ANDROID.value)

        notify.reset_mock()
        notify_message_push_subscribers(message)
        notify.assert_not_called()
    def test_adds_participant_marks_existing_messages_as_read(self):
        existing_member = UserFactory()
        group = GroupFactory(members=[existing_member])

        group.conversation.messages.create(author=existing_member,
                                           content='foo')
        second_message = group.conversation.messages.create(
            author=existing_member, content='bar')

        new_member = UserFactory()
        GroupMembership.objects.create(group=group, user=new_member)

        new_participant = ConversationParticipant.objects.get(
            user=new_member, conversation=group.conversation)
        self.assertTrue(new_participant.seen_up_to == second_message)
Beispiel #11
0
    def test_place_statistics_as_average(self):
        user = UserFactory()
        self.client.force_login(user=user)
        group = GroupFactory(members=[user])
        place = PlaceFactory(group=group)

        response = self.client.get('/api/places/{}/statistics/'.format(
            place.id))
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, {
            'feedback_count': 0,
            'feedback_weight': 0,
            'activities_done': 0,
        })

        one_day_ago = to_range(timezone.now() - relativedelta(days=1))

        users = [UserFactory() for _ in range(9)]
        activities = [
            ActivityFactory(
                place=place,
                date=one_day_ago,
                participants=users,
                is_done=True,
            ) for _ in range(3)
        ]
        feedback = [
            FeedbackFactory(about=choice(activities), given_by=u)
            for u in users
        ]

        # calculate weight from feedback
        feedback.sort(key=attrgetter('about.id'))
        weight = 0
        for _, fs in groupby(feedback, key=attrgetter('about.id')):
            len_list = [f.weight for f in fs]
            weight += float(sum(len_list)) / len(len_list)
        weight = round(weight)

        response = self.client.get('/api/places/{}/statistics/'.format(
            place.id))
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(
            response.data, {
                'feedback_count': len(feedback),
                'feedback_weight': weight,
                'activities_done': len(activities),
            })
Beispiel #12
0
    def test_receive_reaction_deletion(self):
        self.maxDiff = None
        author, user, reaction_user = [UserFactory() for _ in range(3)]
        conversation = ConversationFactory(
            participants=[author, user, reaction_user])
        message = ConversationMessage.objects.create(
            conversation=conversation,
            content='yay',
            author=author,
        )
        reaction = ConversationMessageReaction.objects.create(
            message=message,
            user=reaction_user,
            name='carrot',
        )

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

        reaction.delete()

        # check if conversation update was received
        response = client.messages[0]
        parse_dates(response)
        self.assertEqual(response,
                         make_conversation_message_broadcast(message))
Beispiel #13
0
    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'])
Beispiel #14
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})
Beispiel #15
0
    def test_thread_marked_as_seen(self):
        author, op_author = [UserFactory() for _ in range(2)]
        conversation = ConversationFactory(participants=[author, op_author])
        thread = ConversationMessage.objects.create(conversation=conversation,
                                                    content='yay',
                                                    author=op_author)
        reply = ConversationMessage.objects.create(
            conversation=conversation,
            thread=thread,
            content='really yay?',
            author=author,
        )
        participant = thread.participants.get(user=op_author)
        client = self.connect_as(op_author)

        participant.seen_up_to = reply
        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': {},
            })
Beispiel #16
0
 def setUp(self):
     super().setUp()
     self.member = UserFactory()
     self.group = GroupFactory(members=[self.member])
     self.place = PlaceFactory(group=self.group)
     self.activity = ActivityFactory(place=self.place,
                                     participants=[self.member])
 def test_unsubscribe_from_applications(self):
     application = ApplicationFactory(group=self.group, user=UserFactory())
     participant = application.conversation.conversationparticipant_set.filter(
         user=self.user)
     self.assertFalse(participant.get().muted)
     unsubscribe_from_all_conversations_in_group(self.user, self.group)
     self.assertTrue(participant.get().muted)
    def test_clicked(self, write_points):
        user = UserFactory()
        group = GroupFactory(members=[user])
        two_hours_ago = timezone.now() - relativedelta(hours=2)
        notification = Notification.objects.create(
            created_at=two_hours_ago,
            user=user,
            type=NotificationType.USER_BECAME_EDITOR.value,
            context={'group': group.id},
        )
        write_points.reset_mock()

        notification.clicked = True
        notification.save()

        write_points.assert_called_with([{
            'measurement': 'karrot.events',
            'tags': {
                'group': str(group.id),
                'group_status': group.status,
                'notification_type': NotificationType.USER_BECAME_EDITOR.value,
            },
            'fields': {
                'notification_clicked': 1,
                'notification_clicked_seconds': 60 * 60 * 2,
            },
        }])
Beispiel #19
0
    def test_reply_notification(self, notify):
        author = UserFactory()
        reply_author = UserFactory()
        group = GroupFactory(members=[author, reply_author])
        conversation = Conversation.objects.get_or_create_for_target(group)
        message = conversation.messages.create(author=author, content='bla')
        reply = ConversationMessage.objects.create(
            author=reply_author, conversation=conversation, thread=message, content='reply'
        )
        subscriptions = [
            PushSubscription.objects.create(user=author, token='', platform=PushSubscriptionPlatform.ANDROID.value)
        ]

        notify.reset_mock()
        notify_message_push_subscribers(reply)
        notify.assert_called_once_with(reply, subscriptions, 'en')
Beispiel #20
0
 def test_delete_as_newcomer_fails(self):
     newcomer = UserFactory()
     self.series.place.group.groupmembership_set.create(user=newcomer)
     self.client.force_login(user=newcomer)
     response = self.client.delete(self.series_url)
     self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN,
                      response.data)
Beispiel #21
0
 def setUp(self):
     self.now = timezone.now()
     self.member = UserFactory()
     self.group = GroupFactory(members=[self.member])
     self.place = PlaceFactory(group=self.group)
     self.series = PickupDateSeriesFactory(max_collectors=3,
                                           place=self.place)
Beispiel #22
0
    def setUpTestData(cls):
        cls.user = VerifiedUserFactory()
        cls.other_user = VerifiedUserFactory()
        cls.non_verified_user = UserFactory()
        cls.group = GroupFactory(
            members=[cls.user, cls.other_user, cls.non_verified_user])
        cls.place = PlaceFactory(
            group=cls.group,
            subscribers=[cls.user, cls.other_user, cls.non_verified_user])

        cls.declined_place = PlaceFactory(group=cls.group,
                                          status=PlaceStatus.DECLINED.value)

        # unsubscribe other_user from notifications
        GroupMembership.objects.filter(
            group=cls.group, user=cls.other_user).update(notification_types=[])

        # add some random inactive users, to make sure we don't send to them
        inactive_users = [
            VerifiedUserFactory(language='en')
            for _ in list(range(randint(2, 5)))
        ]
        for user in inactive_users:
            membership = cls.group.add_member(user)
            membership.inactive_at = timezone.now()
            membership.save()
Beispiel #23
0
 def setUp(self):
     self.maxDiff = None
     self.member = UserFactory()
     self.group = GroupFactory(members=[self.member])
     self.place = PlaceFactory(group=self.group)
     self.activity_type = ActivityTypeFactory(group=self.group)
     self.archived_activity_type = ActivityTypeFactory(group=self.group, status='archived')
 def setUp(self):
     super().setUp()
     self.member = UserFactory()
     self.group = GroupFactory(members=[self.member])
     self.place = PlaceFactory(group=self.group)
     self.pickup = PickupDateFactory(place=self.place,
                                     collectors=[self.member])
Beispiel #25
0
    def test_leave(self):
        user = UserFactory()
        conversation = ConversationFactory(participants=[user])
        self.assertIn(user, conversation.participants.all())

        conversation.leave(user)
        self.assertNotIn(user, conversation.participants.all())
Beispiel #26
0
 def create_group_with_members(self, member_count):
     self.members = [UserFactory() for _ in range(member_count)]
     self.group = GroupFactory(members=self.members)
     # trust threshold calculation ignores recently joined users, so we need to create users before that
     two_days_ago = timezone.now() - relativedelta(days=2)
     GroupMembership.objects.filter(group=self.group).update(
         created_at=two_days_ago)
Beispiel #27
0
    def setUp(self):
        self.active_user = UserFactory()
        self.inactive_user = UserFactory()
        self.group = GroupFactory(
            members=[self.active_user, self.inactive_user])

        self.active_membership = GroupMembership.objects.get(
            group=self.group, user=self.active_user)
        set_lastseen_at(self.group, self.inactive_user, days=99999)
        set_inactive_at(self.group, self.inactive_user, days=99999)
        self.inactive_membership = set_removal_notification_at(
            self.group,
            self.inactive_user,
            days=settings.
            NUMBER_OF_DAYS_AFTER_REMOVAL_NOTIFICATION_WE_ACTUALLY_REMOVE_THEM)
        mail.outbox = []
Beispiel #28
0
 def test_ignores_recently_joined_users(self):
     self.create_group_with_members(1)
     [self.group.add_member(UserFactory()) for _ in range(5)]
     self.assertEqual(
         self.group.trust_threshold_for_newcomer(),
         1,
     )
Beispiel #29
0
    def test_create_activity_upcoming_notifications(self):
        users = [UserFactory() for _ in range(3)]
        group = GroupFactory(members=users)
        place = PlaceFactory(group=group)
        in_one_hour = to_range(timezone.now() + relativedelta(hours=1))
        activity1 = ActivityFactory(place=place, date=in_one_hour, participants=users)
        in_two_hours = to_range(timezone.now() + relativedelta(hours=1))
        ActivityFactory(place=place, date=in_two_hours, participants=users)
        Notification.objects.all().delete()

        create_activity_upcoming_notifications.call_local()
        notifications = Notification.objects.filter(type=NotificationType.ACTIVITY_UPCOMING.value)
        self.assertEqual(notifications.count(), 6)
        self.assertEqual(set(n.user.id for n in notifications), set(user.id for user in users))
        activity1_user1_participant = ActivityParticipant.objects.get(user=users[0], activity=activity1)
        activity1_user1_notification = next(
            n for n in notifications if n.context['activity_participant'] == activity1_user1_participant.id
        )
        self.assertEqual(
            activity1_user1_notification.context, {
                'group': group.id,
                'place': place.id,
                'activity': activity1.id,
                'activity_participant': activity1_user1_participant.id,
            }
        )
        self.assertEqual(activity1_user1_notification.expires_at, activity1.date.start)
Beispiel #30
0
    def test_do_not_send_notification_again(self):
        editor = UserFactory()
        editor2 = UserFactory()
        group = GroupFactory(members=[editor, editor2])
        two_days_ago = timezone.now() - relativedelta(days=2)
        GroupMembership.objects.filter(group=group).update(
            created_at=two_days_ago)
        mail.outbox = []

        membership = GroupMembership.objects.get(user=editor, group=group)
        Trust.objects.create(membership=membership, given_by=editor2)

        self.assertEqual(len(mail.outbox), 0)
        self.assertEqual(
            History.objects.filter(
                typus=HistoryTypus.MEMBER_BECAME_EDITOR).count(), 0)