Exemple #1
0
 def test_get_task_new(self, create_task_instance_mock: MagicMock,
                       _get_new_task_mock: MagicMock):
     request = self.factory.get(self.fake_path)
     view = TaskView()
     view = setup_view(view, request)
     view._get_task(new=True)
     _get_new_task_mock.assert_any_call()
Exemple #2
0
 def test_post_no_form(
         self,
         get_success_url_mock: MagicMock,
         _send_task_mock: MagicMock,
         unpack_post_mock: MagicMock,
         get_form_mock: MagicMock,
         _get_form_class_data_mock: MagicMock,
         _get_task_mock: MagicMock
 ):
     get_form_mock.return_value = None
     get_success_url_mock.return_value = self.redirect_path
     request = self.factory.post(self.fake_path, self.post_data)
     view = TaskView()
     view = setup_view(view, request, **self.task_data)
     response = view.post(request)
     self.assertEqual(response.status_code, 302)
     get_success_url_mock.assert_any_call()
     # TODO: FIXME
     # _send_task_mock.assert_called_with(unpack_post_mock)
     # TODO: FIXME
     # unpack_post_mock.assert_called_once_with(self.post_data)
     get_form_mock.assert_any_call()
     _get_form_class_data_mock.assert_any_call()
     _get_task_mock.assert_called_with(
         new=False,
         project_id=str(self.post_data.get('_project_id')),
         task_id=str(self.post_data.get('_task_id'))
     )
Exemple #3
0
 def test_post_no_project_id(self):
     del self.post_data['_project_id']
     request = self.factory.post(self.fake_path, self.post_data)
     view = TaskView()
     view = setup_view(view, request, **self.task_data)
     response = view.post(request)
     self.assertEqual(response.status_code, 400)
Exemple #4
0
 def test_get_context_data(self, initialize_task_data_mock: MagicMock,
                           _get_task_mock: MagicMock):
     request = self.factory.get(self.fake_path)
     view = TaskView()
     view = setup_view(view, request)
     view.task = AbstractTask()
     context = view.get_context_data()
     self.assertIsInstance(context['task'], AbstractTask)
     self.assertEqual(context['task'], view.task)
Exemple #5
0
 def test_get_context_data_no_task(self, _get_task_mock: MagicMock):
     request = self.factory.get(self.fake_path)
     view = TaskView()
     view.error_message = 'Sample error message'
     view.error_template = 'Sample error template'
     view = setup_view(view, request)
     context = view.get_context_data()
     self.assertTrue(context['error'])
     self.assertEqual(context['message'], 'Sample error message')
     self.assertEqual(context['template'], 'Sample error template')
Exemple #6
0
 def test_get_context_data_presenter_typeerror(
         self, get_presenter_mock: MagicMock,
         initialize_task_data_mock: MagicMock, _get_task_mock: MagicMock):
     get_presenter_mock.side_effect = TypeError
     request = self.factory.get(self.fake_path)
     view = TaskView()
     view = setup_view(view, request)
     view.task = AbstractTask()
     with self.assertRaises(PresenterNotDefined):
         view.get_context_data()
Exemple #7
0
 def test_initialize_task_data(self, klass_from_name_mock: MagicMock):
     del self.task_data['info']['task_form']
     request = self.factory.get(self.fake_path)
     view = TaskView()
     view = setup_view(view, request)
     view.task = AbstractTask(**self.task_data)
     view.initialize_task_data()
     # TODO: divide to 2 test cases
     # self.assertEqual(view.template_name, self.task_data.get('info').get('template_name'))
     self.assertEqual(view.template_name,
                      self.task_data.get('info').get('template_name'))
Exemple #8
0
 def test_get_context_data(self, initialize_task_data_mock: MagicMock,
                           _get_task_mock: MagicMock):
     request = self.factory.get(self.fake_path)
     view = TaskView()
     view = setup_view(view, request)
     view.task = AbstractTask()
     context = view.get_context_data()
     self.assertIsInstance(context['task'], AbstractTask)
     self.assertEqual(context['task'], view.task)
     self.assertEqual(context['project_id'], PYBOSSA_PROJECT_ID)
     from moonsheep.settings import PYBOSSA_BASE_URL
     self.assertEqual(context['pybossa_url'], PYBOSSA_BASE_URL)
Exemple #9
0
 def test_get(self, get_context_data_mock: MagicMock,
              _get_form_class_data_mock: MagicMock,
              _get_task_mock: MagicMock):
     request = self.factory.get(self.fake_path)
     view = TaskView()
     view = setup_view(view, request, **self.task_data)
     view.template_name = self.template_name
     response = view.get(request)
     self.assertEqual(response.status_code, 200)
     _get_task_mock.assert_any_call()
     _get_form_class_data_mock.assert_any_call()
     get_context_data_mock.assert_any_call()
Exemple #10
0
 def test_post(self, form_valid_mock: MagicMock, get_form_mock: MagicMock,
               _get_form_class_data_mock: MagicMock,
               _get_task_mock: MagicMock):
     request = self.factory.post(self.fake_path, self.post_data)
     view = TaskView()
     view = setup_view(view, request, **self.task_data)
     view.post(request)
     _get_task_mock.assert_called_once_with(
         new=False,
         project_id=str(self.post_data.get('_project_id')),
         task_id=str(self.post_data.get('_task_id')))
     _get_form_class_data_mock.assert_any_call()
     get_form_mock.assert_any_call()
Exemple #11
0
 def test_get_context_data_no_task(self, _get_task_mock: MagicMock):
     request = self.factory.get(self.fake_path)
     view = TaskView()
     view.error_message = 'Sample error message'
     view.error_template = 'Sample error template'
     view = setup_view(view, request)
     context = view.get_context_data()
     self.assertTrue(context['error'])
     self.assertEqual(context['message'], 'Sample error message')
     self.assertEqual(context['template'], 'Sample error template')
     self.assertEqual(context['project_id'], PYBOSSA_PROJECT_ID)
     from moonsheep.settings import PYBOSSA_BASE_URL
     self.assertEqual(context['pybossa_url'], PYBOSSA_BASE_URL)
Exemple #12
0
 def test_get_random_mocked_task_data(self):
     request = self.factory.get(self.fake_path)
     base_task.register(AbstractTask)
     view = TaskView()
     view = setup_view(view, request)
     task = view.get_random_mocked_task_data()
     self.assertEqual(task, {
         'project_id': 'https://i.imgflip.com/hkimf.jpg',
         'info': {
             'url': 'https://nazk.gov.ua/sites/default/files/docs/2017/3/3_kv/2/Agrarna_partija/3%20%EA%E2%E0%F0%F2%E0%EB%202017%20%D6%C0%20%C0%CF%D3%20%97%20%E7%E0%F2%E5%F0%F2%E8%E9.pdf',
             'type': 'moonsheep.tasks.AbstractTask'
         },
         'id': 'moonsheep.tasks.AbstractTask'
     })
     base_task.clear()
Exemple #13
0
 def test_get_no_tasks(self, get_context_data_mock: MagicMock,
                       _get_form_class_data_mock: MagicMock,
                       _get_task_mock: MagicMock):
     _get_task_mock.side_effect = NoTasksLeft
     request = self.factory.get(self.fake_path)
     view = TaskView()
     view = setup_view(view, request, **self.task_data)
     view.task = AbstractTask()
     view.template_name = self.template_name
     response = view.get(request)
     self.assertEqual(view.task, None)
     self.assertEqual(view.error_message, 'Broker returned no tasks')
     self.assertEqual(view.error_template, 'error-messages/no-tasks.html')
     self.assertEqual(response.status_code, 200)
     _get_task_mock.assert_any_call()
     _get_form_class_data_mock.assert_not_called()
     get_context_data_mock.assert_any_call()
Exemple #14
0
 def test_get_improperly_configured(self, get_context_data_mock: MagicMock,
                                    _get_form_class_data_mock: MagicMock,
                                    _get_task_mock: MagicMock):
     _get_task_mock.side_effect = ImproperlyConfigured
     request = self.factory.get(self.fake_path)
     view = TaskView()
     view = setup_view(view, request, **self.task_data)
     view.task = AbstractTask()
     view.template_name = self.template_name
     response = view.get(request)
     self.assertEqual(view.task, None)
     self.assertEqual(view.error_message, 'Improperly configured PyBossa')
     self.assertEqual(view.error_template,
                      'error-messages/improperly-configured.html')
     self.assertEqual(response.status_code, 200)
     _get_task_mock.assert_any_call()
     _get_form_class_data_mock.assert_not_called()
     get_context_data_mock.assert_any_call()
Exemple #15
0
 def test_get_presenter_not_defined(self, get_context_data_mock: MagicMock,
                                    _get_form_class_data_mock: MagicMock,
                                    _get_task_mock: MagicMock):
     _get_task_mock.side_effect = PresenterNotDefined
     request = self.factory.get(self.fake_path)
     view = TaskView()
     view = setup_view(view, request, **self.task_data)
     view.task = AbstractTask()
     view.template_name = self.template_name
     response = view.get(request)
     self.assertEqual(view.task, None)
     self.assertEqual(view.error_message, 'Presenter not defined')
     self.assertEqual(view.error_template,
                      'error-messages/presenter-not-defined.html')
     self.assertEqual(response.status_code, 200)
     _get_task_mock.assert_any_call()
     _get_form_class_data_mock.assert_not_called()
     get_context_data_mock.assert_any_call()
Exemple #16
0
 def test_get_random_mocked_task_data_no_registry(self):
     request = self.factory.get(self.fake_path)
     view = TaskView()
     view = setup_view(view, request)
     with self.assertRaises(NotImplementedError):
         view.get_random_mocked_task_data()
Exemple #17
0
 def test_initialize_task_data_no_template_nor_form(self):
     request = self.factory.get(self.fake_path)
     view = TaskView()
     view = setup_view(view, request)
     self.assertRaises(TaskMustSetTemplate, view.initialize_task_data)