def test_url_argument(self):
        """
        Test of presence and correct processing of the `--url` argument,
        inherited by all subparsers.
        """
        actions = (
            'audit FILE_HASH SEED',
            'download FILE_HASH',
            'upload {}'.format(os.path.abspath(__file__)),
            'files',
            'info',
        )
        # get `builtins` module name with respect to python version
        builtin_module_names = ('', '', '__builtin__', 'builtins')
        version_name = builtin_module_names[sys.version_info.major]

        with patch('{}.open'.format(version_name), mock_open(), create=False):
            for action in actions:
                # testing of parsing "url_base" value by default
                args_default = parse().parse_args('{}'.format(action).split())
                self.assertIsNone(args_default.url_base)

                # testing of parsing given "url_base" value
                args_passed_url = parse().parse_args(
                        '{} --url TEST_URL_STR'.format(action).split()
                )
                self.assertEqual(args_passed_url.url_base, 'TEST_URL_STR')
    def test_download_arguments(self):
        """
        Test for all features of the `download` subparser.
        """
        # testing of raising exception when required arguments are not given
        with patch('sys.stderr', new_callable=StringIO):
            with self.assertRaises(SystemExit):
                parse().parse_args('download'.split())

        # test on correctly parsing only "file_hash" argument.
        # Expect correct default values of optional arguments.
        args_list = 'download FILE_HASH'.split()
        expected_args_dict = {
            'decryption_key': None,
            'execute_case': core.download,
            'file_hash': args_list[1],
            'link': False,
            'rename_file': None,
            'url_base': None
        }
        parsed_args = parse().parse_args(args_list)
        real_parsed_args_dict = dict(parsed_args._get_kwargs())
        self.assertDictEqual(
            real_parsed_args_dict,
            expected_args_dict
        )

        # test on correctly parsing of full set of available arguments
        test_dec_key = binascii.hexlify(b'test 32 character long key......')
        args_list = 'download FILE_HASH ' \
                    '--decryption_key {} ' \
                    '--rename_file TEST_RENAME_FILE ' \
                    '--link'.format(test_dec_key.decode()).split()
        expected_args_dict = {
            'file_hash': args_list[1],
            'decryption_key': args_list[3],
            'rename_file': args_list[5],
            'link': True,
            'execute_case': core.download,
            'url_base': None
        }
        parsed_args = parse().parse_args(args_list)
        real_parsed_args_dict = dict(parsed_args._get_kwargs())
        self.assertDictEqual(
            real_parsed_args_dict,
            expected_args_dict
        )
    def test_audit_argument(self):
        # testing of raising exception when required arguments are not given
        with patch('sys.stderr', new_callable=StringIO):
            with self.assertRaises(SystemExit):
                parse().parse_args('audit'.split())

        # testing of correct parse of all given arguments
        args_list = 'audit FILE_HASH CHALLENGE_SEED'.split()
        expected_args_dict = {
            'execute_case': core.audit,
            'file_hash': args_list[1],
            'seed': args_list[2],
            'url_base': None
        }
        parsed_args = parse().parse_args(args_list)
        real_parsed_args_dict = dict(parsed_args._get_kwargs())
        self.assertDictEqual(
            real_parsed_args_dict,
            expected_args_dict
        )
    def test_subtest_through_all_help_info(self):
        """
        Set of tests for providing the help information for all of terminal
        commands.
        """
        # Tuple of tuples with subtest's variant of terminal's command. The
        # first list is tested `sys.argv` value and second list is arguments
        # for the `parse().parse_args()` used like the sample call result.
        tests_set = (
            (['', ], ['--help', ]),
            (['', '-h'], ['-h', ]),
            (['', '--help'], ['--help']),
            (['', 'info', '-h'], ['info', '-h']),
            (['', 'files', '-h'], ['files', '-h']),
            (['', 'download', '-h'], ['download', '-h']),
            (['', 'audit', '-h'], ['audit', '-h']),
            (['', 'upload', '-h'], ['upload', '-h']),
            (['', 'info', '--help'], ['info', '--help']),
            (['', 'files', '--help'], ['files', '--help']),
            (['', 'download', '--help'], ['download', '--help']),
            (['', 'audit', '--help'], ['audit', '--help']),
            (['', 'upload', '--help'], ['upload', '--help']),
        )

        for i, test_ in enumerate(tests_set, start=1):
            with patch('metatool.cli.sys.argv', test_[0]):
                main_print_info_help, manual_print_info_help = \
                    self.sys_stdout_help_run(
                        main,
                        parse().parse_args,
                        *test_[1:]
                    )
                self.assertEqual(
                    main_print_info_help,
                    manual_print_info_help,
                    'Unexpected result of the help printout with the '
                    'sys.argv={}'.format(test_[0])
                )
 def test_files_argument(self):
     # test of parsing appropriate default "core function"
     parsed_args = parse().parse_args('files'.split())
     self.assertEqual(parsed_args.execute_case, core.files)
 def test_info_argument(self):
     """
     Test of parsing appropriate default "core function".
     """
     parsed_args = parse().parse_args('info'.split())
     self.assertEqual(parsed_args.execute_case, core.info)
    def test_upload_arguments(self):
        """

        """
        # Test fixture
        full_file_path = os.path.join(os.path.dirname(__file__),
                                      'temporary_test_file')
        self.addCleanup(os.unlink, full_file_path)
        # Testing of raising exception when required arguments are not given
        with patch('sys.stderr', new_callable=StringIO):
            with self.assertRaises(SystemExit):
                parse().parse_args('upload'.split())

        # Test to the right opening file logic
        test_file_content = [b'test data 1', b'test data 2']
        with open(full_file_path, 'wb') as temp_file:
            parsed_args = parse().parse_args(['upload', full_file_path])
            temp_file.write(test_file_content[0])
            temp_file.flush()
            read_data = [parsed_args.file_.read(), ]
            temp_file.seek(0)
            temp_file.truncate()
            temp_file.write(test_file_content[1])
            temp_file.flush()
            parsed_args.file_.seek(0)
            read_data.append(parsed_args.file_.read())
            parsed_args.file_.close()
        self.assertListEqual(read_data, test_file_content)
        self.assertEqual(parsed_args.file_.mode, 'rb')

        # Test on correctly parsing only "file" argument.
        # Expect correct default values of optional arguments.
        args_list = 'upload {}'.format(full_file_path).split()
        parsed_args = parse().parse_args(args_list)
        real_parsed_args_dict = dict(parsed_args._get_kwargs())
        expected_args_dict = {
            'file_': parsed_args.file_,
            'file_role': '001',
            'url_base': None,
            'execute_case': core.upload,
            'encrypt': False,
        }
        self.assertDictEqual(
            real_parsed_args_dict,
            expected_args_dict,
        )
        parsed_args.file_.close()

        # test on correctly parsing of full set of available arguments
        args_list = 'upload {} ' \
                    '--url TEST_URL ' \
                    '--file_role TEST_FILE_ROLE'.format(full_file_path).split()
        parsed_args = parse().parse_args(args_list)
        real_parsed_args_dict = dict(parsed_args._get_kwargs())
        expected_args_dict = {
            'file_': parsed_args.file_,
            'file_role': args_list[5],
            'url_base': args_list[3],
            'execute_case': core.upload,
            'encrypt': False,
        }
        self.assertDictEqual(
            real_parsed_args_dict,
            expected_args_dict
        )
        parsed_args.file_.close()

        # test "-r" optional argument
        args_list = 'upload {} ' \
                    '-r TEST_FILE_ROLE'.format(full_file_path).split()
        parsed_args = parse().parse_args(args_list)
        self.assertEqual(parsed_args.file_role, args_list[3])
        parsed_args.file_.close()