def test_take_action_good_pasphrase(self): """ Ensure the take_action method processes the arguments and good user input correctly: an rsa keypair is created, output files specified and the utils.save_keys method is called. """ with mock.patch('getpass.getpass', return_value='passphrase'): keygen = KeyGen(None, None) with mock.patch('drogulus.commands.keygen.save_keys') as sk: parsed_args = mock.MagicMock() parsed_args.size = 512 mock_pub = mock.MagicMock() mock_pub.save_pkcs1 = mock.MagicMock(return_value=b'pub') mock_priv = mock.MagicMock() mock_priv.save_pkcs1 = mock.MagicMock(return_value=b'priv') return_val = (mock_pub, mock_priv) with mock.patch('rsa.newkeys', return_value=return_val) as mock_newkeys: keygen.take_action(parsed_args) self.assertEqual(1, mock_newkeys.call_count) self.assertEqual(1, sk.call_count) output_pub = os.path.join(data_dir(), '{}.pub'.format(APPNAME)) output_priv = os.path.join(data_dir(), '{}.scrypt'.format(APPNAME)) self.assertEqual(sk.call_args[0][0], 'priv') self.assertEqual(sk.call_args[0][1], 'pub') self.assertEqual(sk.call_args[0][2], 'passphrase') self.assertEqual(sk.call_args[0][3], output_priv) self.assertEqual(sk.call_args[0][4], output_pub)
def test_data_dir(self): """ Ensure the function returns a string representation of a an existing directory on the filesystem. """ random_dir = str(uuid.uuid4()) tmp = os.path.join(tempfile.gettempdir(), random_dir) with mock.patch('drogulus.commands.utils.user_data_dir', return_value=tmp): result = data_dir() self.assertIsInstance(result, str) self.assertTrue(os.path.exists(result))
def test_take_action(self): """ Ensure that the take_action method interrogates the user in the expected way and produces an appropriate JSON file in the expected location containing the expected result. """ with mock.patch('builtins.input', return_value='y'): whoami = WhoAmI(None, None) with mock.patch('json.dump') as writer: whoami.take_action([]) self.assertEqual(1, writer.call_count) output_file = os.path.join(data_dir(), 'whoami.json') self.assertEqual(writer.call_args[0][1].name, output_file)
def test_get_whoami_default_path(self): """ Ensure that a dict is returned from reading a file in the default location. """ mock_fd = mock.MagicMock mock_fd.read = mock.MagicMock(return_value='{}') path = os.path.join(data_dir(), 'whoami.json') with mock.patch('builtins.open', return_value=mock_fd) as mock_open: result = get_whoami() self.assertIsInstance(result, dict) self.assertEqual({}, result) mock_open.assert_called_once_with(path, 'r')