Example #1
0
    def test_run_raises_type_error_if_attachments_callback_function_is_not_set(
        self,
        mock_config_list,
        mock_results,
        mock_jira_results,
        mock_jira_client,
        mock_open
    ):
        mock_jira_client.return_value = None
        mock_config_list.return_value = [self._path]
        Configuration.NAMESPACE = 'pyccata.core'
        report = ReportManager()
        report.add_callback('attachments', None)
        mock_jira_results.return_value = DataProviders._test_data_for_attachments()
        server = namedtuple('Server', 'server_address attachments')
        mock_results.return_value = server(server_address=None, attachments=None)

        self.assertIsInstance(Configuration().replacements, Replacements)

        Config = namedtuple('Config', 'query fields collate output_path')
        config = Config(
            query='project=test and attachments is not empty',
            fields=[
                'key',
                'attachments'
            ],
            collate='zip',
            output_path='/tmp/{FIX_VERSION}'
        )

        attachments = None
        with patch('os.path.exists') as mock_exists:
            mock_exists.return_value = False
            with patch('os.makedirs') as mock_os:
                attachments = Attachments(self._thread_manager, config)
                mock_os.assert_called_with('/tmp/' + Configuration().replacements.replace('{FIX_VERSION}'))

        self._thread_manager.append(attachments)

        with patch('pycurl.Curl') as mock_curl:
            with patch('pycurl.Curl.setopt') as mock_setopt:
                with patch('pycurl.Curl.perform') as mock_perform:
                    with patch('pycurl.Curl.close') as mock_close:
                        Curl = namedtuple('Curl', 'URL WRITEDATA setopt perform close getinfo')
                        mock_curl.return_value = Curl(
                            URL=None,
                            WRITEDATA=None,
                            setopt=mock_setopt,
                            perform=mock_perform,
                            close=mock_close,
                            getinfo=lambda x: 200
                        )
                        with self.assertRaises(InvalidCallbackError):
                            self._thread_manager.execute()
                        self.assertIsInstance(attachments.failure, InvalidCallbackError)
                        mock_setopt.assert_not_called()
                        mock_perform.assert_not_called()
                        mock_close.assert_not_called()
                        mock_open.assert_not_called()
Example #2
0
 def test_jira_attachments(self, mock_load, mock_search, mock_jira_client):
     data = DataProviders._test_data_for_attachments()
     mock_jira_client.return_value = None
     mock_search.return_value = data
     with patch('pyccata.core.configuration.Configuration.manager',
                new_callable=PropertyMock) as mock_manager:
         with patch(
                 'pyccata.core.configuration.Configuration._configuration',
                 new_callable=PropertyMock) as mock_config:
             mock_config.return_value = DataProviders._get_config_for_test()
             mock_manager.return_value = 'jira'
             manager = ProjectManager()
             self.assertIsInstance(manager.client.client, JIRA)
             attachments = manager.search_issues(
                 search_query='assignee = "bob123"',
                 max_results=2,
                 fields=['attachments'])
Example #3
0
    def test_setup_and_run(
        self,
        collation,
        result_count,
        result_filename,
        mock_config_list,
        mock_results,
        mock_jira_client,
        mock_open
    ):
        mock_jira_client.return_value = None
        mock_config_list.return_value = [self._path]
        Configuration.NAMESPACE = 'pyccata.core'
        report = ReportManager()
        mock_results.return_value = DataProviders._test_data_for_attachments()

        report.add_callback('attachments', getattr(DataProviders, 'test_callback'))
        self.assertIsInstance(Configuration().replacements, Replacements)

        Config = namedtuple('Config', 'query fields collate output_path')
        config = Config(
            query='project=test and attachments is not empty',
            fields=[
                'key',
                'attachments'
            ],
            collate=collation,
            output_path='/tmp/{FIX_VERSION}'
        )
        Replacements().find('FIX_VERSION').value = '28/Jul/2016'

        attachments = None
        with patch('os.makedirs') as mock_os:
            attachments = Attachments(self._thread_manager, config)
            mock_os.assert_called_with('/tmp/28/Jul/2016')

        self._thread_manager.append(attachments)

        with patch('pycurl.Curl') as mock_curl:
            with patch('pycurl.Curl.setopt') as mock_setopt:
                with patch('pycurl.Curl.perform') as mock_perform:
                    with patch('pycurl.Curl.close') as mock_close:
                        Curl = namedtuple('Curl', 'URL WRITEDATA setopt perform close getinfo')
                        mock_curl.return_value = Curl(
                            URL=None,
                            WRITEDATA=None,
                            setopt=mock_setopt,
                            perform=mock_perform,
                            close=mock_close,
                            getinfo=lambda x: 200
                        )
                        self._thread_manager.execute()

                        self.assertEquals(result_count, len(attachments._content))
                        self.assertEquals((3 * result_count), mock_setopt.call_count)
                        self.assertEquals((1 * result_count), mock_perform.call_count)
                        self.assertEquals((1 * result_count), mock_close.call_count)
                        self.assertEquals((1 * result_count), mock_open.call_count)
                        calls = []
                        if isinstance(result_filename, list):
                            for filename in result_filename:
                                calls.append(call(filename, 'wb'))
                        else:
                            calls.append(call(result_filename, 'wb'))
                        mock_open.assert_has_calls(calls, any_order=True)

        with patch('pyccata.core.managers.report.ReportManager.add_paragraph') as mock_paragraph:
            with patch('pyccata.core.managers.report.ReportManager.add_list') as mock_list:
                attachments.render(report)
                mock_paragraph.assert_called_with('The following file(s) have been attached to this document:')
                mock_list.assert_called_with('TestFile.zip')