Exemplo n.º 1
0
    def test_group_application_stats(self):
        group = GroupFactory()

        [GroupApplicationFactory(group=group, user=UserFactory(), status='pending') for _ in range(3)]
        [GroupApplicationFactory(group=group, user=UserFactory(), status='accepted') for _ in range(4)]
        [GroupApplicationFactory(group=group, user=UserFactory(), status='declined') for _ in range(5)]
        [GroupApplicationFactory(group=group, user=UserFactory(), status='withdrawn') for _ in range(6)]

        points = stats.get_group_application_stats(group)

        self.assertEqual(
            points, [{
                'measurement': 'karrot.group.applications',
                'tags': {
                    'group': str(group.id),
                },
                'fields': {
                    'count_total': 18,
                    'count_status_pending': 3,
                    'count_status_accepted': 4,
                    'count_status_declined': 5,
                    'count_status_withdrawn': 6,
                },
            }]
        )
Exemplo n.º 2
0
class TestApplicationConversationModel(APITestCase):
    def setUp(self):
        self.applicant = VerifiedUserFactory()
        self.member = VerifiedUserFactory()
        self.group = GroupFactory(members=[self.member])
        self.application = GroupApplicationFactory(group=self.group,
                                                   user=self.applicant)
        self.conversation = Conversation.objects.get_for_target(
            self.application)

    def test_member_leaves_group(self):
        GroupMembership.objects.filter(user=self.member,
                                       group=self.group).delete()
        self.assertNotIn(
            self.member,
            self.conversation.participants.all(),
        )

    def test_user_erased(self):
        self.applicant.refresh_from_db()  # otherwise user.photo.name is None
        self.applicant.erase()

        self.application.refresh_from_db()
        self.assertEqual(self.application.status,
                         GroupApplicationStatus.WITHDRAWN.value)

    def test_deleting_application_deletes_conversation(self):
        GroupApplication.objects.filter(user=self.applicant,
                                        group=self.group).delete()
        self.assertIsNone(Conversation.objects.get_for_target(
            self.application))
Exemplo n.º 3
0
 def setUp(self):
     self.applicant = VerifiedUserFactory()
     self.member = VerifiedUserFactory()
     self.group = GroupFactory(members=[self.member])
     self.application = GroupApplicationFactory(group=self.group,
                                                user=self.applicant)
     self.conversation = Conversation.objects.get_for_target(
         self.application)
Exemplo n.º 4
0
    def test_applicant_receives_application_update(self):
        application = GroupApplicationFactory(user=self.user, group=self.group)

        self.client = self.connect_as(self.user)

        application.status = 'accepted'
        application.save()

        messages = self.application_update_messages()
        self.assertEqual(len(messages), 1)
        response = messages[0]
        self.assertEqual(response['payload']['id'], application.id)
Exemplo n.º 5
0
    def test_creates_application_declined_notification(self):
        member = UserFactory()
        group = GroupFactory(members=[member])

        user = UserFactory()
        application = GroupApplicationFactory(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_member_receives_application_create(self):
        self.client = self.connect_as(self.member)

        application = GroupApplicationFactory(user=self.user, group=self.group)

        response = next(r for r in self.client.messages if r['topic'] == 'applications:update')
        self.assertEqual(response['payload']['id'], application.id)

        self.assertEqual(len(self.client.messages), 2)
Exemplo n.º 7
0
    def test_member_receives_application_create(self):
        self.client = self.connect_as(self.member)

        application = GroupApplicationFactory(user=self.user, group=self.group)

        messages = self.application_update_messages()
        self.assertEqual(len(messages), 1)
        response = messages[0]
        self.assertEqual(response['payload']['id'], application.id)
Exemplo n.º 8
0
 def test_list_own_applications(self):
     [
         GroupApplicationFactory(group=self.group, user=UserFactory())
         for _ in range(4)
     ]
     self.client.force_login(user=self.applicant)
     response = self.get_results('/api/group-applications/?user={}'.format(
         self.applicant.id))
     self.assertEqual(len(response.data), 1)
Exemplo n.º 9
0
    def test_group_application_status_update(self, write_points):
        two_hours_ago = timezone.now() - relativedelta(hours=2)
        application = GroupApplicationFactory(group=GroupFactory(), user=UserFactory(), created_at=two_hours_ago)
        application.status = 'accepted'
        application.save()

        write_points.reset_mock()
        stats.application_status_update(application)
        write_points.assert_called_with([{
            'measurement': 'karrot.events',
            'tags': {
                'group': str(application.group.id)
            },
            'fields': {
                'application_accepted': 1,
                'application_accepted_seconds': 60 * 60 * 2,
            },
        }])
Exemplo n.º 10
0
 def test_list_pending_applications(self):
     [
         GroupApplicationFactory(group=self.group,
                                 user=self.applicant,
                                 status='withdrawn') for _ in range(4)
     ]
     self.client.force_login(user=self.applicant)
     response = self.get_results(
         '/api/group-applications/?status={}'.format(self.applicant.id))
     self.assertEqual(len(response.data), 1)
Exemplo n.º 11
0
    def test_removes_new_application_notification_when_decided(self):
        member = UserFactory()
        group = GroupFactory(members=[member])

        users = [UserFactory() for _ in range(3)]
        applications = [GroupApplicationFactory(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)
Exemplo n.º 12
0
    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()
        GroupApplicationFactory(user=user, group=group)

        self.assertEqual(
            Notification.objects.filter(user=member, type=NotificationType.NEW_APPLICANT.value).count(), 1
        )
Exemplo n.º 13
0
    def test_cannot_have_two_pending_applications(self):
        GroupApplicationFactory(group=self.group, user=self.applicant)

        # create another application
        self.client.force_login(user=self.applicant)
        answers = faker.text()
        response = self.client.post(
            '/api/group-applications/',
            {
                'group': self.group.id,
                'answers': answers,
            },
        )
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Exemplo n.º 14
0
    def setUp(self):
        self.applicant = VerifiedUserFactory()
        self.member = VerifiedUserFactory()
        self.group = GroupFactory(members=[self.member])
        self.application = GroupApplicationFactory(group=self.group, user=self.applicant)
        self.conversation = Conversation.objects.get_for_target(self.application)

        def make_application():
            applicant = VerifiedUserFactory()
            group = GroupFactory(members=[self.member])
            GroupApplicationFactory(group=group, user=applicant)

        [make_application() for _ in range(5)]

        mail.outbox = []
Exemplo n.º 15
0
def get_or_create_application():
    application = GroupApplication.objects.order_by('?').first()

    if application is None:
        new_user = VerifiedUserFactory()
        group = random_group()
        if group.application_questions == '':
            group.application_questions = faker.text()
            group.save()
        application = GroupApplicationFactory(
            group=group,
            user=new_user,
        )

    return application
Exemplo n.º 16
0
    def test_can_apply_again(self):
        GroupApplicationFactory(
            group=self.group,
            user=self.applicant,
            status=GroupApplicationStatus.WITHDRAWN.value,
        )

        # create another application
        self.client.force_login(user=self.applicant)
        response = self.client.post(
            '/api/group-applications/',
            {
                'group': self.group.id,
                'answers': faker.text(),
            },
        )
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Exemplo n.º 17
0
    def test_application_message_title(self):
        author = UserFactory()
        group = GroupFactory(members=[author])
        applicant = UserFactory()
        application = GroupApplicationFactory(group=group, user=applicant)
        conversation = Conversation.objects.get_or_create_for_target(
            application)
        message = conversation.messages.create(author=author, content='bla')

        title = get_message_title(message, 'en')
        self.assertEqual(
            title, '❓ {} / {}'.format(applicant.display_name,
                                      author.display_name))

        application.accept(author)
        message.refresh_from_db()
        title = get_message_title(message, 'en')
        self.assertEqual(
            title, '✅ {} / {}'.format(applicant.display_name,
                                      author.display_name))

        application.decline(author)
        message.refresh_from_db()
        title = get_message_title(message, 'en')
        self.assertEqual(
            title, '❌ {} / {}'.format(applicant.display_name,
                                      author.display_name))

        application.withdraw()
        message.refresh_from_db()
        title = get_message_title(message, 'en')
        self.assertEqual(
            title, '🗑️ {} / {}'.format(applicant.display_name,
                                       author.display_name))

        message = conversation.messages.create(author=applicant, content='bla')
        message.refresh_from_db()
        title = get_message_title(message, 'en')
        self.assertEqual(title, '🗑️ {}'.format(applicant.display_name))
Exemplo n.º 18
0
    def test_list_conversations_with_related_data_efficiently(self):
        user = UserFactory()
        group = GroupFactory(members=[user])
        store = StoreFactory(group=group)
        pickup = PickupDateFactory(store=store)
        application = GroupApplicationFactory(user=UserFactory(), group=group)

        conversations = [
            Conversation.objects.get_or_create_for_target(t)
            for t in (group, pickup, application)
        ]
        [c.sync_users([user]) for c in conversations]
        [c.messages.create(content='hey', author=user) for c in conversations]

        self.client.force_login(user=user)
        with self.assertNumQueries(9):
            response = self.client.get('/api/conversations/',
                                       {'group': group.id},
                                       format='json')
        results = response.data['results']

        self.assertEqual(len(results['conversations']), len(conversations))
        self.assertEqual(results['pickups'][0]['id'], pickup.id)
        self.assertEqual(results['applications'][0]['id'], application.id)
Exemplo n.º 19
0
 def make_application():
     applicant = VerifiedUserFactory()
     group = GroupFactory(members=[self.member])
     GroupApplicationFactory(group=group, user=applicant)