diff --git a/tests/unit/plugins/lookup/onepassword/__init__.py b/tests/unit/plugins/lookup/onepassword/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/tests/unit/plugins/lookup/onepassword/test_onepassword_cli_v1.py b/tests/unit/plugins/lookup/onepassword/test_onepassword_cli_v1.py deleted file mode 100644 index dc9b44af66..0000000000 --- a/tests/unit/plugins/lookup/onepassword/test_onepassword_cli_v1.py +++ /dev/null @@ -1,50 +0,0 @@ -# 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 - -from __future__ import (absolute_import, division, print_function) -__metaclass__ = type - -import pytest - - -from ansible_collections.community.general.plugins.lookup.onepassword import OnePassCLIv1 - - -@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(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(mocker): - mocker.patch.object(OnePassCLIv1, "_run", return_value=[0, "", ""]) - - op_cli = OnePassCLIv1( - subdomain="acme", - username="bob@acme.com", - secret_key="SECRET", - master_password="ONEKEYTORULETHEMALL", - ) - result = op_cli.full_signin() - - op_cli._run.assert_called_with([ - "signin", - "acme.1password.com", - b"bob@acme.com", - b"SECRET", - "--raw", - ], command_input=b"ONEKEYTORULETHEMALL") - assert result == [0, "", ""] diff --git a/tests/unit/plugins/lookup/onepassword/test_onepassword_cli_v2.py b/tests/unit/plugins/lookup/onepassword/test_onepassword_cli_v2.py deleted file mode 100644 index f9c3e48ab6..0000000000 --- a/tests/unit/plugins/lookup/onepassword/test_onepassword_cli_v2.py +++ /dev/null @@ -1,52 +0,0 @@ -# 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 - -from __future__ import (absolute_import, division, print_function) -__metaclass__ = type - -import pytest - - -from ansible_collections.community.general.plugins.lookup.onepassword import OnePassCLIv2 - - -@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(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(mocker): - mocker.patch.object(OnePassCLIv2, "_run", return_value=[0, "", ""]) - - op_cli = OnePassCLIv2( - subdomain="acme", - username="bob@acme.com", - 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"bob@acme.com", - "--signin", - ], - command_input=b"ONEKEYTORULETHEMALL", - environment_update={'OP_SECRET_KEY': 'SECRET'}, - ) - assert result == [0, "", ""] diff --git a/tests/unit/plugins/lookup/onepassword/common.py b/tests/unit/plugins/lookup/onepassword_common.py similarity index 96% rename from tests/unit/plugins/lookup/onepassword/common.py rename to tests/unit/plugins/lookup/onepassword_common.py index 141aea91a3..0929792251 100644 --- a/tests/unit/plugins/lookup/onepassword/common.py +++ b/tests/unit/plugins/lookup/onepassword_common.py @@ -15,7 +15,7 @@ from ansible_collections.community.general.plugins.lookup.onepassword import ( 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()) diff --git a/tests/unit/plugins/lookup/onepassword/conftest.py b/tests/unit/plugins/lookup/onepassword_conftest.py similarity index 100% rename from tests/unit/plugins/lookup/onepassword/conftest.py rename to tests/unit/plugins/lookup/onepassword_conftest.py diff --git a/tests/unit/plugins/lookup/onepassword/fixtures/v1_out_01.json b/tests/unit/plugins/lookup/onepassword_fixtures/v1_out_01.json similarity index 100% rename from tests/unit/plugins/lookup/onepassword/fixtures/v1_out_01.json rename to tests/unit/plugins/lookup/onepassword_fixtures/v1_out_01.json diff --git a/tests/unit/plugins/lookup/onepassword/fixtures/v1_out_01.json.license b/tests/unit/plugins/lookup/onepassword_fixtures/v1_out_01.json.license similarity index 100% rename from tests/unit/plugins/lookup/onepassword/fixtures/v1_out_01.json.license rename to tests/unit/plugins/lookup/onepassword_fixtures/v1_out_01.json.license diff --git a/tests/unit/plugins/lookup/onepassword/fixtures/v1_out_02.json b/tests/unit/plugins/lookup/onepassword_fixtures/v1_out_02.json similarity index 100% rename from tests/unit/plugins/lookup/onepassword/fixtures/v1_out_02.json rename to tests/unit/plugins/lookup/onepassword_fixtures/v1_out_02.json diff --git a/tests/unit/plugins/lookup/onepassword/fixtures/v1_out_02.json.license b/tests/unit/plugins/lookup/onepassword_fixtures/v1_out_02.json.license similarity index 100% rename from tests/unit/plugins/lookup/onepassword/fixtures/v1_out_02.json.license rename to tests/unit/plugins/lookup/onepassword_fixtures/v1_out_02.json.license diff --git a/tests/unit/plugins/lookup/onepassword/fixtures/v1_out_03.json b/tests/unit/plugins/lookup/onepassword_fixtures/v1_out_03.json similarity index 100% rename from tests/unit/plugins/lookup/onepassword/fixtures/v1_out_03.json rename to tests/unit/plugins/lookup/onepassword_fixtures/v1_out_03.json diff --git a/tests/unit/plugins/lookup/onepassword/fixtures/v1_out_03.json.license b/tests/unit/plugins/lookup/onepassword_fixtures/v1_out_03.json.license similarity index 100% rename from tests/unit/plugins/lookup/onepassword/fixtures/v1_out_03.json.license rename to tests/unit/plugins/lookup/onepassword_fixtures/v1_out_03.json.license diff --git a/tests/unit/plugins/lookup/onepassword/fixtures/v2_out_01.json b/tests/unit/plugins/lookup/onepassword_fixtures/v2_out_01.json similarity index 100% rename from tests/unit/plugins/lookup/onepassword/fixtures/v2_out_01.json rename to tests/unit/plugins/lookup/onepassword_fixtures/v2_out_01.json diff --git a/tests/unit/plugins/lookup/onepassword/fixtures/v2_out_01.json.license b/tests/unit/plugins/lookup/onepassword_fixtures/v2_out_01.json.license similarity index 100% rename from tests/unit/plugins/lookup/onepassword/fixtures/v2_out_01.json.license rename to tests/unit/plugins/lookup/onepassword_fixtures/v2_out_01.json.license diff --git a/tests/unit/plugins/lookup/onepassword/fixtures/v2_out_02.json b/tests/unit/plugins/lookup/onepassword_fixtures/v2_out_02.json similarity index 100% rename from tests/unit/plugins/lookup/onepassword/fixtures/v2_out_02.json rename to tests/unit/plugins/lookup/onepassword_fixtures/v2_out_02.json diff --git a/tests/unit/plugins/lookup/onepassword/fixtures/v2_out_02.json.license b/tests/unit/plugins/lookup/onepassword_fixtures/v2_out_02.json.license similarity index 100% rename from tests/unit/plugins/lookup/onepassword/fixtures/v2_out_02.json.license rename to tests/unit/plugins/lookup/onepassword_fixtures/v2_out_02.json.license diff --git a/tests/unit/plugins/lookup/onepassword/fixtures/v2_out_03.json b/tests/unit/plugins/lookup/onepassword_fixtures/v2_out_03.json similarity index 100% rename from tests/unit/plugins/lookup/onepassword/fixtures/v2_out_03.json rename to tests/unit/plugins/lookup/onepassword_fixtures/v2_out_03.json diff --git a/tests/unit/plugins/lookup/onepassword/fixtures/v2_out_03.json.license b/tests/unit/plugins/lookup/onepassword_fixtures/v2_out_03.json.license similarity index 100% rename from tests/unit/plugins/lookup/onepassword/fixtures/v2_out_03.json.license rename to tests/unit/plugins/lookup/onepassword_fixtures/v2_out_03.json.license diff --git a/tests/unit/plugins/lookup/onepassword/test_onepassword.py b/tests/unit/plugins/lookup/test_onepassword.py similarity index 68% rename from tests/unit/plugins/lookup/onepassword/test_onepassword.py rename to tests/unit/plugins/lookup/test_onepassword.py index e9f8f42c96..ab7f3def29 100644 --- a/tests/unit/plugins/lookup/onepassword/test_onepassword.py +++ b/tests/unit/plugins/lookup/test_onepassword.py @@ -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 @@ -10,8 +10,13 @@ import itertools 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 @@ -21,6 +26,86 @@ from ansible_collections.community.general.plugins.lookup.onepassword import ( ) +@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="bob@acme.com", + secret_key="SECRET", + master_password="ONEKEYTORULETHEMALL", + ) + result = op_cli.full_signin() + + op_cli._run.assert_called_with([ + "signin", + "acme.1password.com", + b"bob@acme.com", + 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="bob@acme.com", + 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"bob@acme.com", + "--signin", + ], + command_input=b"ONEKEYTORULETHEMALL", + environment_update={'OP_SECRET_KEY': 'SECRET'}, + ) + assert result == [0, "", ""] + + @pytest.mark.parametrize( ("version", "version_class"), (