Ejemplo n.º 1
0
    def test_last_message_when_have_messages_equals_to_last_message(self):
        application = ApplicationFactory.create(owner=self.resident)
        # The first message
        MessageFactory.create(owner=self.resident, application=application)
        # The second (last) message
        last_message = MessageFactory.create(owner=self.resident,
                                             application=application)

        self.assertEqual(application.last_message, last_message)
Ejemplo n.º 2
0
    def setUp(self):
        super(MessageViewSetTestCase, self).setUp()
        self.first_shift.owner = self.scheduler
        self.first_shift.save()

        self.first_application = ApplicationFactory.create(
            owner=self.approved_resident, shift=self.first_shift)
        self.second_application = ApplicationFactory.create(
            owner=self.approved_resident, shift=self.second_shift)
        self.third_application = ApplicationFactory.create(
            shift=self.second_shift)
        self.first_application_message = MessageFactory.create(
            application=self.first_application, owner=self.scheduler)
        self.second_application_message = MessageFactory.create(
            application=self.second_application, owner=self.approved_resident)
Ejemplo n.º 3
0
 def setUp(self):
     self.request = ExtendedRequestFactory()
     self.scheduler = SchedulerFactory.create()
     self.resident = ResidentFactory.create()
     self.shift = ShiftFactory.create(owner=self.scheduler)
     self.application = ApplicationFactory.create(shift=self.shift)
     self.message = MessageFactory.create(owner=self.resident,
                                          application=self.application,
                                          text='test text')
     self.message2 = MessageFactory.create(owner=self.resident,
                                           application=self.application,
                                           text='see attachment',
                                           attachment=SimpleUploadedFile(
                                               'text.txt',
                                               b'Some text in file'))
Ejemplo n.º 4
0
    def test_process_state_changing_without_comment(self, mock_create_message):
        mock_create_message.return_value = MessageFactory.create(
            application=self.application, owner=self.scheduler)

        process_state_changing(self.application, self.resident, 'Description')
        mock_create_message.assert_called_with(self.application, self.resident,
                                               'Description')
Ejemplo n.º 5
0
    def test_notify_message_created(self):
        message = MessageFactory.create(
            application=self.application, owner=self.resident)

        Group('user-{0}'.format(self.resident.pk)).add('ws-resident')
        Group('user-{0}'.format(self.scheduler.pk)).add('ws-scheduler')

        # Run notification inside the transaction, because
        # it will send notifications at the commit
        with transaction.atomic():
            notify_message_created(message)

        # Check message for a resident
        result = self.get_next_message('ws-resident')
        parsed_result = json.loads(result['text'])

        self.assertEqual(parsed_result['event'], 'message_created')
        self.assertEqual(parsed_result['payload']['message']['pk'], message.pk)

        # Check message for a scheduler
        result = self.get_next_message('ws-scheduler')
        parsed_result = json.loads(result['text'])

        self.assertEqual(parsed_result['event'], 'message_created')
        self.assertEqual(parsed_result['payload']['message']['pk'], message.pk)
Ejemplo n.º 6
0
    def test_process_message_creation_without_notification_notifies(
            self, mock_notify_message_created):
        message = MessageFactory.create(application=self.application,
                                        owner=self.resident)

        process_message_creation(message, notify=False)

        mock_notify_message_created.assert_not_called()
Ejemplo n.º 7
0
    def test_process_renewing_works_without_errors(self, mock_create_message):
        mock_create_message.return_value = MessageFactory.create(
            application=self.application, owner=self.scheduler)

        process_renewing(self.application)

        mock_create_message.assert_called_with(
            self.application, self.scheduler,
            'The shift became available and your application is renewed')
Ejemplo n.º 8
0
    def test_process_rejecting_works_without_errors(self, mock_create_message):
        mock_create_message.return_value = MessageFactory.create(
            application=self.application, owner=self.scheduler)

        process_rejecting(self.application, self.scheduler, 'Comment')

        mock_create_message.assert_called_with(
            self.application, self.scheduler,
            'Comment\nYour application is rejected. You may apply for '
            'other shifts at any time')
Ejemplo n.º 9
0
    def test_process_approving_works_without_errors(self, mock_create_message):
        mock_create_message.return_value = MessageFactory.create(
            application=self.application, owner=self.scheduler)

        process_approving(self.application, self.scheduler, 'Comment')

        mock_create_message.assert_called_with(
            self.application, self.scheduler,
            'Comment\nThe application is approved. Please, confirm that '
            'you will work on the shift')
Ejemplo n.º 10
0
    def test_process_completing_works_without_errors(self,
                                                     mock_create_message):
        mock_create_message.return_value = MessageFactory.create(
            application=self.application, owner=self.scheduler)

        process_completing(self.application, self.scheduler, 'Comment')

        mock_create_message.assert_called_with(
            self.application, self.scheduler,
            'Comment\nThe application is completed')
Ejemplo n.º 11
0
    def test_process_postponing_works_without_errors(self,
                                                     mock_create_message):
        mock_create_message.return_value = MessageFactory.create(
            application=self.application, owner=self.scheduler)

        process_postponing(self.application)

        mock_create_message.assert_called_with(
            self.application, self.scheduler,
            'You application is postponed due to accepting an '
            'another application')
Ejemplo n.º 12
0
    def test_order_by_without_messages_first(self):
        def assert_applications_pks_equal_to(pks):
            applications = Application.objects \
                .order_by_without_messages_first() \
                .values_list('pk', flat=True)
            self.assertListEqual(list(applications), pks)

        # Create fourths applications without messages
        # They should be ordered by date
        first_application = ApplicationFactory.create()
        second_application = ApplicationFactory.create()
        third_application = ApplicationFactory.create()
        fourth_application = ApplicationFactory.create()

        assert_applications_pks_equal_to([
            fourth_application.pk,
            third_application.pk,
            second_application.pk,
            first_application.pk,
        ])

        # Create a message for third application. So, the third application
        # should be places after all application without messages
        MessageFactory.create(owner=self.resident,
                              application=third_application)

        assert_applications_pks_equal_to([
            fourth_application.pk,
            second_application.pk,
            first_application.pk,
            third_application.pk,
        ])

        # Create a message for first application. So, the first application
        # should be places after all application without messages and
        # ordered by date
        MessageFactory.create(owner=self.resident,
                              application=first_application)

        assert_applications_pks_equal_to([
            fourth_application.pk,
            second_application.pk,
            third_application.pk,
            first_application.pk,
        ])

        # Create a message for fourth application. So, the fourth application
        # should be places after all application without messages and
        # ordered by date
        MessageFactory.create(owner=self.resident,
                              application=fourth_application)

        assert_applications_pks_equal_to([
            second_application.pk,
            fourth_application.pk,
            third_application.pk,
            first_application.pk,
        ])
Ejemplo n.º 13
0
    def test_serialize_with_thumbnail(self):
        avatar_path = os.path.join(settings.BASE_DIR,
                                   'apps/accounts/tests/fixtures/avatar.jpg')
        message3 = MessageFactory.create(owner=self.resident,
                                         application=self.application,
                                         text='attach image',
                                         attachment=SimpleUploadedFile(
                                             'avatar.jpg',
                                             open(avatar_path, 'rb').read()))

        serializer = MessageSerializer(message3,
                                       context={'request': self.request})
        self.assertIsNotNone(serializer.data['attachment'])
        self.assertIsNotNone(serializer.data['thumbnail'])
Ejemplo n.º 14
0
    def test_notify_application_state_changed(self):
        message = MessageFactory.create(
            application=self.application, owner=self.resident)

        Group('user-{0}'.format(self.resident.pk)).add('ws-resident')
        Group('user-{0}'.format(self.scheduler.pk)).add('ws-scheduler')

        # Run notification inside the transaction, because
        # it will send notifications at the commit
        with transaction.atomic():
            notify_application_state_changed(self.application, message)

        # Check message for a resident
        result = self.get_next_message('ws-resident')
        parsed_result = json.loads(result['text'])

        self.assertEqual(parsed_result['event'], 'application_state_changed')
        payload = parsed_result['payload']
        self.assertEqual(payload['message']['pk'], message.pk)
        self.assertEqual(payload['application']['pk'], self.application.pk)
        # The available transitions depends on user
        self.assertEqual(
            payload['application']['available-transitions'],
            [])

        # Check message for a scheduler
        result = self.get_next_message('ws-scheduler')
        parsed_result = json.loads(result['text'])

        self.assertEqual(parsed_result['event'], 'application_state_changed')
        payload = parsed_result['payload']
        self.assertEqual(payload['message']['pk'], message.pk)
        self.assertEqual(payload['application']['pk'], self.application.pk)
        # The available transitions depends on user
        self.assertEqual(
            payload['application']['available-transitions'],
            ['approve', 'reject'])