Ejemplo n.º 1
0
    def test_send_staffing_request_priorities(self, mock_slack):

        worker2 = WorkerFactory()
        WorkerCertificationFactory(worker=worker2, staffing_priority=-1)

        CommunicationPreferenceFactory(
            worker=worker2,
            communication_type=(CommunicationPreference.CommunicationType.
                                NEW_TASK_AVAILABLE.value))

        worker3 = WorkerFactory()
        WorkerCertificationFactory(worker=worker3, staffing_priority=1)
        CommunicationPreferenceFactory(
            worker=worker3,
            communication_type=(CommunicationPreference.CommunicationType.
                                NEW_TASK_AVAILABLE.value))

        request = StaffBotRequestFactory()
        self.assertEqual(request.status,
                         StaffBotRequest.Status.PROCESSING.value)

        # Workers should be contacted in priority order.
        excluded = []
        for worker in (worker3, self.worker, worker2):
            send_staffing_requests(worker_batch_size=1,
                                   frequency=timedelta(minutes=0))
            inquiries = (StaffingRequestInquiry.objects.filter(
                request=request).exclude(
                    communication_preference__worker__in=excluded))
            for inquiry in inquiries:
                self.assertEqual(worker.id,
                                 inquiry.communication_preference.worker.id)
            excluded.append(worker)
Ejemplo n.º 2
0
    def test_send_staffing_requests(self, mock_slack):
        worker2 = WorkerFactory()
        CommunicationPreferenceFactory(
            worker=worker2,
            communication_type=(
                CommunicationPreference.CommunicationType
                .NEW_TASK_AVAILABLE.value))
        WorkerCertificationFactory(
            worker=worker2,
            certification=self.certification
        )

        request = StaffBotRequestFactory()
        request.task.step.required_certifications.add(self.certification)

        self.assertEqual(request.status,
                         StaffBotRequest.Status.SENDING_INQUIRIES.value)

        send_staffing_requests(worker_batch_size=1,
                               frequency=timedelta(minutes=0))
        mock_slack.assert_not_called()
        request.refresh_from_db()
        self.assertEqual(request.status,
                         StaffBotRequest.Status.SENDING_INQUIRIES.value)
        # Inquiries increase by two because we send a Slack and an
        # email notification.
        self.assertEqual(
            StaffingRequestInquiry.objects.filter(request=request).count(),
            2)

        send_staffing_requests(worker_batch_size=1,
                               frequency=timedelta(minutes=0))
        self.assertEqual(mock_slack.call_count, 1)
        mock_slack.reset()

        request.refresh_from_db()
        self.assertEqual(request.status,
                         StaffBotRequest.Status.SENDING_INQUIRIES.value)
        self.assertEqual(
            StaffingRequestInquiry.objects.filter(request=request).count(),
            4)

        # marked as closed and no new request inquiries sent.
        send_staffing_requests(worker_batch_size=1,
                               frequency=timedelta(minutes=0))
        self.assertTrue(mock_slack.called)
        request.refresh_from_db()
        self.assertEqual(request.status,
                         StaffBotRequest.Status.DONE_SENDING_INQUIRIES.value)
        self.assertEqual(
            StaffingRequestInquiry.objects.filter(request=request).count(),
            4)
Ejemplo n.º 3
0
    def test_send_staffing_requests(self, mock_slack):

        worker2 = WorkerFactory()
        CommunicationPreferenceFactory(
            worker=worker2,
            communication_type=(
                CommunicationPreference.CommunicationType
                .NEW_TASK_AVAILABLE.value))

        request = StaffBotRequestFactory()
        self.assertEquals(request.status,
                          StaffBotRequest.Status.PROCESSING.value)

        send_staffing_requests(worker_batch_size=1,
                               frequency=timedelta(minutes=0))
        mock_slack.assert_not_called()
        request.refresh_from_db()
        self.assertEquals(request.status,
                          StaffBotRequest.Status.PROCESSING.value)
        # Inquiries increase by two because we send a Slack and an
        # email notification.
        self.assertEquals(
            StaffingRequestInquiry.objects.filter(request=request).count(),
            2)

        send_staffing_requests(worker_batch_size=1,
                               frequency=timedelta(minutes=0))
        mock_slack.assert_not_called()
        request.refresh_from_db()
        self.assertEquals(request.status,
                          StaffBotRequest.Status.PROCESSING.value)
        self.assertEquals(
            StaffingRequestInquiry.objects.filter(request=request).count(),
            4)

        # marked as complete and no new request inquiries sent.
        send_staffing_requests(worker_batch_size=1,
                               frequency=timedelta(minutes=0))
        self.assertTrue(mock_slack.called)
        request.refresh_from_db()
        self.assertEquals(request.status,
                          StaffBotRequest.Status.COMPLETE.value)
        self.assertEquals(
            StaffingRequestInquiry.objects.filter(request=request).count(),
            4)
Ejemplo n.º 4
0
    def test_send_staffing_requests(self, mock_slack):
        worker2 = WorkerFactory()

        StaffingRequestInquiryFactory(
            communication_preference__worker=worker2,
            communication_preference__communication_type=(
                CommunicationPreference.CommunicationType
                .NEW_TASK_AVAILABLE.value),
            request__task__step__is_human=True
        )

        request = StaffBotRequestFactory()
        self.assertEquals(request.status,
                          StaffBotRequest.Status.PROCESSING.value)

        send_staffing_requests(worker_batch_size=1)
        mock_slack.assert_not_called()
        request.refresh_from_db()
        self.assertEquals(request.status,
                          StaffBotRequest.Status.PROCESSING.value)

        self.assertEquals(
            StaffingRequestInquiry.objects.filter(request=request).count(),
            2)

        send_staffing_requests(worker_batch_size=1)
        mock_slack.assert_not_called()
        request.refresh_from_db()
        self.assertEquals(request.status,
                          StaffBotRequest.Status.PROCESSING.value)

        self.assertEquals(
            StaffingRequestInquiry.objects.filter(request=request).count(),
            4)

        # marked as complete and no new request inquiries sent.
        send_staffing_requests(worker_batch_size=1)
        self.assertTrue(mock_slack.called)
        request.refresh_from_db()
        self.assertEquals(request.status,
                          StaffBotRequest.Status.COMPLETE.value)

        self.assertEquals(
            StaffingRequestInquiry.objects.filter(request=request).count(),
            4)
Ejemplo n.º 5
0
    def test_address_staffing_requests_parameters(self, mock_slack):
        for idx in range(5):
            worker = WorkerFactory()
            CommunicationPreferenceFactory(
                worker=worker,
                communication_type=(CommunicationPreference.CommunicationType.
                                    NEW_TASK_AVAILABLE.value))
            WorkerCertificationFactory(worker=worker,
                                       certification=self.certification)
        # Make a new request and turn off the global request, as the
        # global one has an inquiry on it already.
        request = StaffBotRequestFactory(task__step__is_human=True)
        request.task.step.required_certifications.add(self.certification)

        self.staffing_request_inquiry.request.status = (
            StaffBotRequest.Status.DONE_SENDING_INQUIRIES)

        address_staffing_requests(worker_batch_size=1,
                                  frequency=timedelta(minutes=20))
        # Inquiries increase by two because we send a Slack and an
        # email notification.  `last_inquiry_sent` is None on new
        # tasks, so we send this batch regardless of frequency.
        self.assertEqual(
            StaffingRequestInquiry.objects.filter(request=request).count(), 2)

        address_staffing_requests(worker_batch_size=2,
                                  frequency=timedelta(minutes=20))
        # Don't send more inquiries, since it hasn't been 20 minutes.
        self.assertEqual(
            StaffingRequestInquiry.objects.filter(request=request).count(), 2)

        request.last_inquiry_sent = timezone.now() - timedelta(minutes=21)
        request.save()
        address_staffing_requests(worker_batch_size=2,
                                  frequency=timedelta(minutes=20))
        # Send two email and two slack inquiries since it's been 21 minutes.
        self.assertEqual(
            StaffingRequestInquiry.objects.filter(request=request).count(), 6)

        address_staffing_requests(worker_batch_size=10,
                                  frequency=timedelta(minutes=20))
        # Don't send more inquiries, since it hasn't been 20 minutes.
        self.assertEqual(
            StaffingRequestInquiry.objects.filter(request=request).count(), 6)

        request.last_inquiry_sent = timezone.now() - timedelta(minutes=21)
        request.save()
        address_staffing_requests(worker_batch_size=10,
                                  frequency=timedelta(minutes=20))
        # Send remaining inquiries, since enough time has passed.
        self.assertEqual(
            StaffingRequestInquiry.objects.filter(request=request).count(), 12)

        request.last_inquiry_sent = timezone.now() - timedelta(minutes=21)
        request.save()
        address_staffing_requests(worker_batch_size=10,
                                  frequency=timedelta(minutes=20))
        # We're all out of workers to whom we'd like to send inquiries.
        self.assertEqual(
            StaffingRequestInquiry.objects.filter(request=request).count(), 12)
Ejemplo n.º 6
0
    def test_get_available_request(self, mock_slack):
        # Close all open requests so new worker doesn't receive them.
        address_staffing_requests(worker_batch_size=2,
                                  frequency=timedelta(minutes=0))

        self.assertEqual(len(get_available_requests(self.worker)), 1)

        worker2 = WorkerFactory()
        CommunicationPreferenceFactory(
            worker=worker2,
            communication_type=(CommunicationPreference.CommunicationType.
                                NEW_TASK_AVAILABLE.value))
        WorkerCertificationFactory(worker=worker2,
                                   certification=self.certification)

        request1 = StaffBotRequestFactory(task__step__is_human=True)
        request1.task.step.required_certifications.add(self.certification)
        request2 = StaffBotRequestFactory(task__step__is_human=True)
        request2.task.step.required_certifications.add(self.certification)

        address_staffing_requests(worker_batch_size=2,
                                  frequency=timedelta(minutes=0))
        inquiry1 = (StaffingRequestInquiry.objects.filter(
            communication_preference__worker=self.worker).filter(
                request=request1).first())
        inquiry2 = (StaffingRequestInquiry.objects.filter(
            communication_preference__worker=worker2).filter(
                request=request2).first())

        # `self.worker` now has three available tasks, whereas `worker2`
        # just has access to the two new tasks.
        available_requests = get_available_requests(self.worker)
        self.assertEqual(len(available_requests), 3)
        self.assertEqual(len(get_available_requests(worker2)), 2)

        # Tasks should be sorted by start_datetime in ascending order.

        first_available, second_available, third_available = (
            available_requests[0]['available_datetime'],
            available_requests[1]['available_datetime'],
            available_requests[2]['available_datetime'])
        self.assertLess(first_available, second_available)
        self.assertLess(second_available, third_available)

        # `self.worker` will lose an available task (they accept it),
        # whereas `worker2` is unchanged.
        handle_staffing_response(self.worker,
                                 self.staffing_request_inquiry.id,
                                 is_available=True)
        self.assertEqual(len(get_available_requests(self.worker)), 2)
        self.assertEqual(len(get_available_requests(worker2)), 2)

        # `self.worker` will lose an available task (they ignore it),
        # whereas `worker2` is unchanged.
        handle_staffing_response(self.worker, inquiry1.id, is_available=False)
        self.assertEqual(len(get_available_requests(self.worker)), 1)
        self.assertEqual(len(get_available_requests(worker2)), 2)

        # `worker2` takes a task.
        handle_staffing_response(worker2, inquiry2.id, is_available=True)
        self.assertEqual(len(get_available_requests(self.worker)), 0)
        self.assertEqual(len(get_available_requests(worker2)), 1)
Ejemplo n.º 7
0
    def test_get_staffing_request_messsage(self, mock_mail):
        def _task_factory(status, path):
            description_no_kwargs = {'path': path}
            return TaskFactory(
                status=status,
                step=StepFactory(
                    slug='stepslug',
                    description='the step',
                    detailed_description_function=description_no_kwargs),
                project__workflow_version__workflow__description=(
                    'the workflow'),
                project__short_description='the coolest project')

        # Test slack without review and with a detailed_description_function
        task = _task_factory(
            Task.Status.AWAITING_PROCESSING,
            'orchestra.tests.helpers.fixtures.get_detailed_description')
        staffing_request_inquiry = StaffingRequestInquiryFactory(
            communication_preference__worker__user__first_name='test-name',
            request__task=task)
        message = StaffBot()._get_staffing_request_message(
            staffing_request_inquiry,
            'communication/new_task_available_slack.txt')
        self.assertEqual(message, '''Hello test-name!

A new task is available for you to work on, if you'd like!  Here are the details:

Project: the workflow
Project description: the coolest project
Task: the step
Details: No text given stepslug

<http://127.0.0.1:8000/orchestra/communication/accept_staffing_request_inquiry/{}/|Accept the Task>
<http://127.0.0.1:8000/orchestra/communication/reject_staffing_request_inquiry/{}/|Ignore the Task>
<http://127.0.0.1:8000/orchestra/communication/available_staffing_requests/|View All Available Tasks>

'''.format(staffing_request_inquiry.id, staffing_request_inquiry.id))  # noqa

        # Test email with review and no detailed_description_function
        task = _task_factory(
            Task.Status.PENDING_REVIEW,
            'orchestra.bots.tests.test_staffbot._noop_details')
        staffing_request_inquiry = StaffingRequestInquiryFactory(
            communication_preference__worker__user__first_name='test-name2',
            request=StaffBotRequestFactory(task=task, required_role_counter=1))
        message = StaffBot()._get_staffing_request_message(
            staffing_request_inquiry,
            'communication/new_task_available_email.txt')
        self.assertEqual(message, '''Hello test-name2!

A new task is available for you to work on, if you'd like!  Here are the details:

Project: the workflow
Project description: the coolest project
Task: the step [Review]


<a href="http://127.0.0.1:8000/orchestra/communication/accept_staffing_request_inquiry/{}/">Accept the Task</a>
<a href="http://127.0.0.1:8000/orchestra/communication/reject_staffing_request_inquiry/{}/">Ignore the Task</a>
<a href="http://127.0.0.1:8000/orchestra/communication/available_staffing_requests/">View All Available Tasks</a>

'''.format(staffing_request_inquiry.id, staffing_request_inquiry.id))  # noqa

        # Test that we markdown things
        StaffBot()._send_staffing_request_by_mail('*****@*****.**', message)
        mock_mail.assert_called_once_with(
            'A new task is available for you',
            message,
            settings.ORCHESTRA_NOTIFICATIONS_FROM_EMAIL, ['*****@*****.**'],
            html_message=html_from_plaintext(message))
Ejemplo n.º 8
0
    def test_send_staffing_requests_parameters(self, mock_slack):
        for idx in range(5):
            CommunicationPreferenceFactory(
                worker=WorkerFactory(),
                communication_type=(
                    CommunicationPreference.CommunicationType
                    .NEW_TASK_AVAILABLE.value))
        # Make a new request and turn off the global request, as the
        # global one has an inquiry on it already.
        request = StaffBotRequestFactory(task__step__is_human=True)
        self.staffing_request_inquiry.request.status = (
            StaffBotRequest.Status.COMPLETE)

        send_staffing_requests(worker_batch_size=1,
                               frequency=timedelta(minutes=20))
        # Inquiries increase by two because we send a Slack and an
        # email notification.  `last_inquiry_sent` is None on new
        # tasks, so we send this batch regardless of frequency.
        self.assertEquals(
            StaffingRequestInquiry.objects.filter(request=request).count(),
            2)

        send_staffing_requests(worker_batch_size=2,
                               frequency=timedelta(minutes=20))
        # Don't send more inquiries, since it hasn't been 20 minutes.
        self.assertEquals(
            StaffingRequestInquiry.objects.filter(request=request).count(),
            2)

        request.last_inquiry_sent = timezone.now() - timedelta(minutes=21)
        request.save()
        send_staffing_requests(worker_batch_size=2,
                               frequency=timedelta(minutes=20))
        # Send two email and two slack inquiries since it's been 21 minutes.
        self.assertEquals(
            StaffingRequestInquiry.objects.filter(request=request).count(),
            6)

        send_staffing_requests(worker_batch_size=10,
                               frequency=timedelta(minutes=20))
        # Don't send more inquiries, since it hasn't been 20 minutes.
        self.assertEquals(
            StaffingRequestInquiry.objects.filter(request=request).count(),
            6)

        request.last_inquiry_sent = timezone.now() - timedelta(minutes=21)
        request.save()
        send_staffing_requests(worker_batch_size=10,
                               frequency=timedelta(minutes=20))
        # Send remaining inquiries, since enough time has passed.
        self.assertEquals(
            StaffingRequestInquiry.objects.filter(request=request).count(),
            12)

        request.last_inquiry_sent = timezone.now() - timedelta(minutes=21)
        request.save()
        send_staffing_requests(worker_batch_size=10,
                               frequency=timedelta(minutes=20))
        # We're all out of workers to whom we'd like to send inquiries.
        self.assertEquals(
            StaffingRequestInquiry.objects.filter(request=request).count(),
            12)