Skip to content

Commit

Permalink
Consolidate onepassword unit tests so that ansible-test will find the…
Browse files Browse the repository at this point in the history
…m when the plugin is modified (#6075)

Consolidate onepassword unit tests so that ansible-test will find them when the plugin is modified.
  • Loading branch information
felixfontein authored Feb 25, 2023
1 parent b72b7d4 commit de1f0ff
Show file tree
Hide file tree
Showing 18 changed files with 89 additions and 106 deletions.
Empty file.
50 changes: 0 additions & 50 deletions tests/unit/plugins/lookup/onepassword/test_onepassword_cli_v1.py

This file was deleted.

52 changes: 0 additions & 52 deletions tests/unit/plugins/lookup/onepassword/test_onepassword_cli_v2.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


def load_file(file):
with open((os.path.join(os.path.dirname(__file__), "fixtures", file)), "r") as f:
with open((os.path.join(os.path.dirname(__file__), "onepassword_fixtures", file)), "r") as f:
return json.loads(f.read())


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020 Ansible Project
# Copyright (c) 2022 Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

Expand All @@ -10,8 +10,13 @@
import json
import pytest

from .conftest import OP_VERSION_FIXTURES
from .common import MOCK_ENTRIES
from .onepassword_conftest import ( # noqa: F401, pylint: disable=unused-import
OP_VERSION_FIXTURES,
fake_op,
opv1,
opv2,
)
from .onepassword_common import MOCK_ENTRIES

from ansible.errors import AnsibleLookupError
from ansible.plugins.loader import lookup_loader
Expand All @@ -21,6 +26,86 @@
)


@pytest.mark.parametrize(
("args", "rc", "expected_call_args", "expected_call_kwargs", "expected"),
(
([], 0, ["get", "account"], {"ignore_errors": True}, True,),
([], 1, ["get", "account"], {"ignore_errors": True}, False,),
(["acme"], 1, ["get", "account", "--account", "acme.1password.com"], {"ignore_errors": True}, False,),
)
)
def test_assert_logged_in_v1(mocker, args, rc, expected_call_args, expected_call_kwargs, expected):
mocker.patch.object(OnePassCLIv1, "_run", return_value=[rc, "", ""])

op_cli = OnePassCLIv1(*args)
result = op_cli.assert_logged_in()

op_cli._run.assert_called_with(expected_call_args, **expected_call_kwargs)
assert result == expected


def test_full_signin_v1(mocker):
mocker.patch.object(OnePassCLIv1, "_run", return_value=[0, "", ""])

op_cli = OnePassCLIv1(
subdomain="acme",
username="[email protected]",
secret_key="SECRET",
master_password="ONEKEYTORULETHEMALL",
)
result = op_cli.full_signin()

op_cli._run.assert_called_with([
"signin",
"acme.1password.com",
b"[email protected]",
b"SECRET",
"--raw",
], command_input=b"ONEKEYTORULETHEMALL")
assert result == [0, "", ""]


@pytest.mark.parametrize(
("args", "out", "expected_call_args", "expected_call_kwargs", "expected"),
(
([], "list of accounts", ["account", "get"], {"ignore_errors": True}, True,),
(["acme"], "list of accounts", ["account", "get", "--account", "acme.1password.com"], {"ignore_errors": True}, True,),
([], "", ["account", "list"], {}, False,),
)
)
def test_assert_logged_in_v2(mocker, args, out, expected_call_args, expected_call_kwargs, expected):
mocker.patch.object(OnePassCLIv2, "_run", return_value=[0, out, ""])
op_cli = OnePassCLIv2(*args)
result = op_cli.assert_logged_in()

op_cli._run.assert_called_with(expected_call_args, **expected_call_kwargs)
assert result == expected


def test_full_signin_v2(mocker):
mocker.patch.object(OnePassCLIv2, "_run", return_value=[0, "", ""])

op_cli = OnePassCLIv2(
subdomain="acme",
username="[email protected]",
secret_key="SECRET",
master_password="ONEKEYTORULETHEMALL",
)
result = op_cli.full_signin()

op_cli._run.assert_called_with(
[
"account", "add", "--raw",
"--address", "acme.1password.com",
"--email", b"[email protected]",
"--signin",
],
command_input=b"ONEKEYTORULETHEMALL",
environment_update={'OP_SECRET_KEY': 'SECRET'},
)
assert result == [0, "", ""]


@pytest.mark.parametrize(
("version", "version_class"),
(
Expand Down

0 comments on commit de1f0ff

Please sign in to comment.