| 1 |
# Copyright (c) 2016 Andrew Zenk <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
# Make coding more python3-ish |
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import unittest |
| 9 |
from argparse import ArgumentParser |
| 10 |
from unittest.mock import patch |
| 11 |
|
| 12 |
from ansible.errors import AnsibleError |
| 13 |
from ansible.plugins.loader import lookup_loader |
| 14 |
|
| 15 |
from ansible_collections.community.general.plugins.lookup.lastpass import LPass, LPassException |
| 16 |
|
| 17 |
MOCK_ENTRIES = [ |
| 18 |
{ |
| 19 |
"username": "user", |
| 20 |
"name": "Mock Entry", |
| 21 |
"password": "t0pS3cret passphrase entry!", |
| 22 |
"url": "https://localhost/login", |
| 23 |
"notes": "Test\nnote with multiple lines.\n", |
| 24 |
"id": "0123456789", |
| 25 |
} |
| 26 |
] |
| 27 |
|
| 28 |
|
| 29 |
class MockLPass(LPass): |
| 30 |
_mock_logged_out = False |
| 31 |
_mock_disconnected = False |
| 32 |
|
| 33 |
def _lookup_mock_entry(self, key): |
| 34 |
for entry in MOCK_ENTRIES: |
| 35 |
if key == entry["id"] or key == entry["name"]: |
| 36 |
return entry |
| 37 |
|
| 38 |
def _run(self, args, stdin=None, expected_rc=0): |
| 39 |
# Mock behavior of lpass executable |
| 40 |
base_options = ArgumentParser(add_help=False) |
| 41 |
base_options.add_argument("--color", default="auto", choices=["auto", "always", "never"]) |
| 42 |
|
| 43 |
p = ArgumentParser() |
| 44 |
sp = p.add_subparsers(help="command", dest="subparser_name") |
| 45 |
|
| 46 |
sp.add_parser("logout", parents=[base_options], help="logout") |
| 47 |
show_p = sp.add_parser("show", parents=[base_options], help="show entry details") |
| 48 |
|
| 49 |
field_group = show_p.add_mutually_exclusive_group(required=True) |
| 50 |
for field in MOCK_ENTRIES[0].keys(): |
| 51 |
field_group.add_argument(f"--{field}", default=False, action="store_true") |
| 52 |
field_group.add_argument("--field", default=None) |
| 53 |
show_p.add_argument("selector", help="Unique Name or ID") |
| 54 |
|
| 55 |
args = p.parse_args(args) |
| 56 |
|
| 57 |
def mock_exit(output="", error="", rc=0): |
| 58 |
if rc != expected_rc: |
| 59 |
raise LPassException(error) |
| 60 |
return output, error |
| 61 |
|
| 62 |
if args.color != "never": |
| 63 |
return mock_exit(error="Error: Mock only supports --color=never", rc=1) |
| 64 |
|
| 65 |
if args.subparser_name == "logout": |
| 66 |
if self._mock_logged_out: |
| 67 |
return mock_exit(error="Error: Not currently logged in", rc=1) |
| 68 |
|
| 69 |
logged_in_error = "Are you sure you would like to log out? [Y/n]" |
| 70 |
if stdin and stdin.lower() == "n\n": |
| 71 |
return mock_exit(output="Log out: aborted.", error=logged_in_error, rc=1) |
| 72 |
elif stdin and stdin.lower() == "y\n": |
| 73 |
return mock_exit(output="Log out: complete.", error=logged_in_error, rc=0) |
| 74 |
else: |
| 75 |
return mock_exit(error="Error: aborted response", rc=1) |
| 76 |
|
| 77 |
if args.subparser_name == "show": |
| 78 |
if self._mock_logged_out: |
| 79 |
return mock_exit( |
| 80 |
error="Error: Could not find decryption key. Perhaps you need to login with `lpass login`.", rc=1 |
| 81 |
) |
| 82 |
|
| 83 |
if self._mock_disconnected: |
| 84 |
return mock_exit(error="Error: Couldn't resolve host name.", rc=1) |
| 85 |
|
| 86 |
mock_entry = self._lookup_mock_entry(args.selector) |
| 87 |
|
| 88 |
if args.field: |
| 89 |
return mock_exit(output=mock_entry.get(args.field, "")) |
| 90 |
elif args.password: |
| 91 |
return mock_exit(output=mock_entry.get("password", "")) |
| 92 |
elif args.username: |
| 93 |
return mock_exit(output=mock_entry.get("username", "")) |
| 94 |
elif args.url: |
| 95 |
return mock_exit(output=mock_entry.get("url", "")) |
| 96 |
elif args.name: |
| 97 |
return mock_exit(output=mock_entry.get("name", "")) |
| 98 |
elif args.id: |
| 99 |
return mock_exit(output=mock_entry.get("id", "")) |
| 100 |
elif args.notes: |
| 101 |
return mock_exit(output=mock_entry.get("notes", "")) |
| 102 |
|
| 103 |
raise LPassException("We should never get here") |
| 104 |
|
| 105 |
|
| 106 |
class DisconnectedMockLPass(MockLPass): |
| 107 |
_mock_disconnected = True |
| 108 |
|
| 109 |
|
| 110 |
class LoggedOutMockLPass(MockLPass): |
| 111 |
_mock_logged_out = True |
| 112 |
|
| 113 |
|
| 114 |
class TestLPass(unittest.TestCase): |
| 115 |
def setUp(self): |
| 116 |
self.lookup = lookup_loader.get("community.general.lastpass") |
| 117 |
|
| 118 |
def test_lastpass_cli_path(self): |
| 119 |
lp = MockLPass(path="/dev/null") |
| 120 |
self.assertEqual("/dev/null", lp.cli_path) |
| 121 |
|
| 122 |
def test_lastpass_build_args_logout(self): |
| 123 |
lp = MockLPass() |
| 124 |
self.assertEqual(["logout", "--color=never"], lp._build_args("logout")) |
| 125 |
|
| 126 |
def test_lastpass_logged_in_true(self): |
| 127 |
lp = MockLPass() |
| 128 |
self.assertTrue(lp.logged_in) |
| 129 |
|
| 130 |
def test_lastpass_logged_in_false(self): |
| 131 |
lp = LoggedOutMockLPass() |
| 132 |
self.assertFalse(lp.logged_in) |
| 133 |
|
| 134 |
def test_lastpass_show_disconnected(self): |
| 135 |
lp = DisconnectedMockLPass() |
| 136 |
|
| 137 |
with self.assertRaises(LPassException): |
| 138 |
lp.get_field("0123456789", "username") |
| 139 |
|
| 140 |
def test_lastpass_show(self): |
| 141 |
lp = MockLPass() |
| 142 |
for entry in MOCK_ENTRIES: |
| 143 |
entry_id = entry.get("id") |
| 144 |
for k, v in entry.items(): |
| 145 |
self.assertEqual(v.strip(), lp.get_field(entry_id, k)) |
| 146 |
|
| 147 |
|
| 148 |
class TestLastpassPlugin(unittest.TestCase): |
| 149 |
def setUp(self): |
| 150 |
self.lookup = lookup_loader.get("community.general.lastpass") |
| 151 |
|
| 152 |
@patch("ansible_collections.community.general.plugins.lookup.lastpass.LPass", new=MockLPass) |
| 153 |
def test_lastpass_plugin_normal(self): |
| 154 |
for entry in MOCK_ENTRIES: |
| 155 |
entry_id = entry.get("id") |
| 156 |
for k, v in entry.items(): |
| 157 |
self.assertEqual(v.strip(), self.lookup.run([entry_id], field=k)[0]) |
| 158 |
|
| 159 |
@patch("ansible_collections.community.general.plugins.lookup.lastpass.LPass", LoggedOutMockLPass) |
| 160 |
def test_lastpass_plugin_logged_out(self): |
| 161 |
entry = MOCK_ENTRIES[0] |
| 162 |
entry_id = entry.get("id") |
| 163 |
with self.assertRaises(AnsibleError): |
| 164 |
self.lookup.run([entry_id], field="password") |
| 165 |
|
| 166 |
@patch("ansible_collections.community.general.plugins.lookup.lastpass.LPass", DisconnectedMockLPass) |
| 167 |
def test_lastpass_plugin_disconnected(self): |
| 168 |
entry = MOCK_ENTRIES[0] |
| 169 |
entry_id = entry.get("id") |
| 170 |
with self.assertRaises(AnsibleError): |
| 171 |
self.lookup.run([entry_id], field="password") |