def test_url_parser_providing(self, mock_requests_get):
     """
     Test on priority of using the `--url` provided in the terminal.
     :param mock_requests_get: auto-provided by the `@patch()` mocked arg
     """
     with patch('os.getenv', Mock(return_value='http://spam.eggs')):
         with patch('sys.argv', ['', 'info', '--url', 'http://test.url']):
             try:
                 main()
             except SystemExit:
                 pass
             self.assertEqual(
                 mock_requests_get.call_args_list,
                 [call('http://test.url/api/nodes/me/')],
                 "Request must be sent to the http://test.url/api/nodes/me/"
             )
    def walk_through_addresses(exit_point_result, mock_show_data, mock_parse):
        """
        This is the static method for the testing the metatool.cli.main()
        function to walking through the list of addresses.
        It forges the process of walking through default addresses until
        the acceptable result will be returned from the core function call, or
        all addresses will be used.
        It returns the list of calls of a core function, expected call-list,
        and show_data call-list in the course of executing the main() function.

        :param exit_point_result: return-value of the `core_func()` that should
            stop the walking
        :type exit_point_result: requests.models.Response object
        :type exit_point_result: string object

        :param mock_show_data: auto-provided by the `@patch()` mocked arg
        :param mock_parse: auto-provided by the `@patch()` mocked arg

        :returns: tuple of lists with calls of mocked objects to make assertion
        :rtype: tuple of lists
        """
        bad_response_result = Mock(__class__=Response, status_code=500)
        # Set up the order of the core function call's results.
        core_func = Mock(
            side_effect=[bad_response_result, bad_response_result,
                         exit_point_result, bad_response_result]
        )
        # mocking the parsing logic
        parser = mock_parse.return_value
        parser.parse_args.return_value = argparse.Namespace(
            execute_case=core_func, url_base=None)
        # Mocking the list of default URL-addresses.
        # Walking should retrieve an acceptable result on the third address.
        url_list = ['first.fail.url', 'second.fail.url',
                    'third.success.stop.url', 'forth.unreached.url']
        with patch('os.getenv', Mock(return_value=None)):
            with patch('metatool.cli.CORE_NODES_URL', url_list):
                with patch('sys.argv', ['', '']):
                    main()
        # Preparing the return data
        tested_core_func_calls = core_func.call_args_list
        expected_core_func_calls = [call(url_base=url) for url in url_list[:3]]
        show_data_calls = mock_show_data.call_args_list

        return (tested_core_func_calls, expected_core_func_calls,
                show_data_calls)
 def test_url_env_providing(self, mock_requests_get):
     """
     Test to getting the `url_base` argument from the environment variable
     `MEATADISKSERVER` if it's set.
     :param mock_requests_get: mocked argument provided by the `@patch()`
     """
     with patch('os.getenv', Mock(return_value='http://env.var.com')):
         with patch('sys.argv', ['', 'info']):
             try:
                 main()
             except SystemExit:
                 pass
             self.assertEqual(
                 mock_requests_get.call_args_list,
                 [call('http://env.var.com/api/nodes/me/')],
                 "Request should use the http://env.var.com address"
             )
    def test_prepare_btctx_api_and_send_key_for_download_when_link_true(
            self, mock_btctxstore):
        """
        Test, that when the ``main`` function passes ``link=False``
        argument to the ``download`` API function, the ``sender_key`` and
        ``btctx_api`` arguments will be prepared and passed to the
        ``download()`` call.
        """
        # Make a list of arguments for the parser.
        mock_sys_argv = '__file__ download FILE_HASH'.split()
        # Get argument's list from the "download" function before as mock it.
        download_available_args = get_all_func_args(core.download)
        # mock the Response object for the mocking the download's return
        trick_response = Response()
        trick_response.status_code = 200

        with patch('sys.argv', mock_sys_argv):
            with patch('metatool.cli.get_all_func_args',
                       return_value=get_all_func_args(core.download)):
                with patch('metatool.core.download',
                           return_value=trick_response) as mock_download:
                    main()
        passed_args = mock_download.call_args[1]
        omitted_args = set(download_available_args) - set(passed_args.keys())
        self.assertSetEqual(
            omitted_args, set(),
            'Full set of arguments should be passed '
            'to the download() call, when the "--link" is not occured.'
        )
        expected_BtcTxStore_call = call(testnet=True, dryrun=True).create_key()
        self.assertEqual(
            mock_btctxstore.mock_calls,
            expected_BtcTxStore_call.call_list(),
            "The btctxstore.BtcTxStore should be properly called to create "
            "credentials, as expected when the --link argument was parsed!"

        )
        self.assertEqual(
            mock_download.call_count,
            1,
            '"download" should be called only once!'
        )
 def test_url_default_providing(self, mock_requests_get):
     """
     Test on default using the `url_base` argument from the CORE_NODES_URL
     list.
     :param mock_requests_get: auto-provided by the `@patch()` mocked arg
     """
     mock_response = mock_requests_get.return_value
     mock_response.status_code = 500
     expected_calls = [
         call('{}api/nodes/me/'.format(url)) for url in CORE_NODES_URL
         ]
     with patch('os.getenv', Mock(return_value=None)):
         with patch('metatool.cli.show_data'):
             with patch('sys.argv', ['', 'info']):
                 main()
     self.assertEqual(
         mock_requests_get.call_args_list,
         expected_calls,
         "Must be called in the order of `CORE_NODES_URL` list items"
     )
    def test_omit_btctx_api_and_send_key_for_download_when_link_true(
            self, mock_btctxstore):
        """
        Test, that when the ``main`` function passes ``link=True`` argument
         to the ``download`` API function, the ``sender_key`` and
        ``btctx_api`` arguments will not be prepared and passed to the
        ``download()`` call.
        """
        # Make a list of arguments for the parser.
        mock_sys_argv = '__file__ download FILE_HASH --link'.split()

        # Get argument's list from the "download" function before as mock it.
        download_available_args = get_all_func_args(core.download)

        # mock the Response object for the mocking the download's return
        trick_response = Response()
        trick_response.status_code = 200

        with patch('sys.argv', mock_sys_argv):
            with patch('metatool.cli.get_all_func_args',
                       return_value=get_all_func_args(core.download)):
                with patch('metatool.core.download',
                           return_value=trick_response) as mock_download:
                    main()
        passed_args = mock_download.call_args[1]
        omitted_args = set(download_available_args) - set(passed_args.keys())
        self.assertFalse(
            mock_btctxstore.called,
            "The btctxstore.BtcTxStore module should not be called when "
            "the --link argument wasn't parsed!"
        )
        self.assertSetEqual(
            omitted_args, {'btctx_api', 'sender_key'},
            "Only btctx_api and sender_key arguments should not be passed "
            "to the download() call, when the `--link` argument is occurred."
        )
        self.assertEqual(
            mock_download.call_count,
            1,
            '"download" should be called only once!'
        )
Beispiel #7
0
"""The MetaTool package runner file."""

from metatool.cli import main


if __name__ == '__main__':
    main()