#!/usr/bin/env python
"""This module tests the rename module"""
import builtins
import unittest
import argparse
import mock
import libpkpass.commands.cli as cli
import libpkpass.commands.rename as rename
from libpkpass.errors import DecryptionError, CliArgumentError
from .basetest.basetest import CONFIG, BADPIN

class RenameTests(unittest.TestCase):
    """This class tests the rename class"""

    @mock.patch('argparse.ArgumentParser.parse_args',
                return_value=argparse.Namespace(subparser_name='rename', identity='bleh',
                                                nopassphrase="true",
                                                all=None,
                                                pwname='test',
                                                rename='retest',
                                                config=CONFIG))
    def test_recipient_not_in_database(self, subparser_name):
        """test decryption functionality"""
        ret = False
        try:
            rename.Rename(cli.Cli())
        except CliArgumentError as error:
            if error.msg == "Error: Your user 'bleh' is not in the recipient database":
                ret = True
        self.assertTrue(ret)

    @mock.patch('argparse.ArgumentParser.parse_args',
                return_value=argparse.Namespace(subparser_name='rename', identity='r1',
                                                nopassphrase="true",
                                                all=True,
                                                pwname='test',
                                                rename='retest',
                                                overwrite="true",
                                                config=CONFIG))
    def test_rename_success(self, subparser_name):
        """test decryption functionality"""
        ret = True
        try:
            with mock.patch.object(builtins, 'input', lambda _: 'y'):
                rename.Rename(cli.Cli())
            with mock.patch('argparse.ArgumentParser.parse_args',
                            return_value=argparse.Namespace(
                                subparser_name='rename', identity='r1',
                                nopassphrase="true",
                                all=True,
                                pwname='retest',
                                rename='test',
                                overwrite="true",
                                config=CONFIG)):
                with mock.patch.object(builtins, 'input', lambda _: 'y'):
                    rename.Rename(cli.Cli())
        except DecryptionError as error:
            if error.msg == BADPIN:
                ret = False
        self.assertTrue(ret)

    @mock.patch('argparse.ArgumentParser.parse_args',
                return_value=argparse.Namespace(subparser_name='rename', identity='r1',
                                                nopassphrase="true",
                                                all=True,
                                                pwname='test',
                                                overwrite="true",
                                                config=CONFIG))
    def test_rename_cli_error(self, subparser_name):
        """test decryption functionality"""
        ret = False
        try:
            rename.Rename(cli.Cli())
        except CliArgumentError as error:
            if error.msg == "'rename' is a required argument":
                ret = True
        self.assertTrue(ret)

if __name__ == '__main__':
    unittest.main()
