mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 22:33:25 +00:00
Reformat everything.
This commit is contained in:
@@ -19,19 +19,34 @@ from ansible_collections.community.general.plugins.lookup.onepassword import (
|
||||
)
|
||||
|
||||
|
||||
OP_VERSION_FIXTURES = [
|
||||
"opv1",
|
||||
"opv2"
|
||||
]
|
||||
OP_VERSION_FIXTURES = ["opv1", "opv2"]
|
||||
|
||||
|
||||
@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,),
|
||||
)
|
||||
(
|
||||
[],
|
||||
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, "", ""])
|
||||
@@ -54,23 +69,44 @@ def test_full_signin_v1(mocker):
|
||||
)
|
||||
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")
|
||||
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,),
|
||||
)
|
||||
(
|
||||
[],
|
||||
"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, ""])
|
||||
@@ -100,13 +136,17 @@ def test_full_signin_v2(mocker):
|
||||
|
||||
op_cli._run.assert_called_with(
|
||||
[
|
||||
"account", "add", "--raw",
|
||||
"--address", "acme.1password.com",
|
||||
"--email", b"bob@acme.com",
|
||||
"account",
|
||||
"add",
|
||||
"--raw",
|
||||
"--address",
|
||||
"acme.1password.com",
|
||||
"--email",
|
||||
b"bob@acme.com",
|
||||
"--signin",
|
||||
],
|
||||
command_input=b"ONEKEYTORULETHEMALL",
|
||||
environment_update={'OP_SECRET_KEY': 'SECRET'},
|
||||
environment_update={"OP_SECRET_KEY": "SECRET"},
|
||||
)
|
||||
assert result == [0, "", ""]
|
||||
|
||||
@@ -116,7 +156,7 @@ def test_full_signin_v2(mocker):
|
||||
(
|
||||
("1.17.2", OnePassCLIv1),
|
||||
("2.27.4", OnePassCLIv2),
|
||||
)
|
||||
),
|
||||
)
|
||||
def test_op_correct_cli_class(fake_op, version, version_class):
|
||||
op = fake_op(version)
|
||||
@@ -146,12 +186,11 @@ def test_op_set_token_with_config(op_fixture, mocker, request):
|
||||
[
|
||||
(op, value)
|
||||
for op in OP_VERSION_FIXTURES
|
||||
for value in
|
||||
(
|
||||
for value in (
|
||||
"Missing required parameters",
|
||||
"The operation is unauthorized",
|
||||
)
|
||||
]
|
||||
],
|
||||
)
|
||||
def test_op_set_token_with_config_missing_args(op_fixture, message, request, mocker):
|
||||
op = request.getfixturevalue(op_fixture)
|
||||
@@ -169,7 +208,9 @@ def test_op_set_token_with_config_missing_args(op_fixture, message, request, moc
|
||||
def test_op_set_token_with_config_full_signin(op_fixture, request, mocker):
|
||||
op = request.getfixturevalue(op_fixture)
|
||||
mocker.patch("os.path.isfile", return_value=True)
|
||||
mocker.patch.object(op._cli, "signin", return_value=(99, "", ""), side_effect=AnsibleLookupError("Raised intentionally"))
|
||||
mocker.patch.object(
|
||||
op._cli, "signin", return_value=(99, "", ""), side_effect=AnsibleLookupError("Raised intentionally")
|
||||
)
|
||||
mocker.patch.object(op._cli, "full_signin", return_value=(0, "", ""))
|
||||
|
||||
op.set_token()
|
||||
@@ -192,8 +233,7 @@ def test_op_set_token_without_config(op_fixture, request, mocker):
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("op_fixture", "login_status"),
|
||||
[(op, value) for op in OP_VERSION_FIXTURES for value in [False, True]]
|
||||
("op_fixture", "login_status"), [(op, value) for op in OP_VERSION_FIXTURES for value in [False, True]]
|
||||
)
|
||||
def test_op_assert_logged_in(mocker, login_status, op_fixture, request):
|
||||
op = request.getfixturevalue(op_fixture)
|
||||
@@ -230,7 +270,7 @@ def test_op_get_raw_v1(mocker, op_fixture, request):
|
||||
(None, ""),
|
||||
("", ""),
|
||||
]
|
||||
)
|
||||
),
|
||||
)
|
||||
def test_op_get_field(mocker, op_fixture, output, expected, request):
|
||||
op = request.getfixturevalue(op_fixture)
|
||||
@@ -251,12 +291,17 @@ def test_op_get_field(mocker, op_fixture, output, expected, request):
|
||||
(_cli_class, item["vault_name"], item["queries"], item.get("kwargs", {}), item["output"], item["expected"])
|
||||
for _cli_class in sorted(MOCK_ENTRIES, key=operator.attrgetter("__name__"))
|
||||
for item in MOCK_ENTRIES[_cli_class]
|
||||
)
|
||||
),
|
||||
)
|
||||
def test_op_lookup(mocker, cli_class, vault, queries, kwargs, output, expected):
|
||||
mocker.patch("ansible_collections.community.general.plugins.lookup.onepassword.OnePass._get_cli_class", cli_class)
|
||||
mocker.patch("ansible_collections.community.general.plugins.lookup.onepassword.OnePass.assert_logged_in", return_value=True)
|
||||
mocker.patch("ansible_collections.community.general.plugins.lookup.onepassword.OnePassCLIBase._run", return_value=(0, json.dumps(output), ""))
|
||||
mocker.patch(
|
||||
"ansible_collections.community.general.plugins.lookup.onepassword.OnePass.assert_logged_in", return_value=True
|
||||
)
|
||||
mocker.patch(
|
||||
"ansible_collections.community.general.plugins.lookup.onepassword.OnePassCLIBase._run",
|
||||
return_value=(0, json.dumps(output), ""),
|
||||
)
|
||||
|
||||
op_lookup = lookup_loader.get("community.general.onepassword")
|
||||
result = op_lookup.run(queries, vault=vault, **kwargs)
|
||||
@@ -269,14 +314,19 @@ def test_signin(op_fixture, request):
|
||||
op = request.getfixturevalue(op_fixture)
|
||||
op._cli.master_password = "master_pass"
|
||||
op._cli.signin()
|
||||
op._cli._run.assert_called_once_with(['signin', '--raw'], command_input=b"master_pass")
|
||||
op._cli._run.assert_called_once_with(["signin", "--raw"], command_input=b"master_pass")
|
||||
|
||||
|
||||
def test_op_doc(mocker):
|
||||
document_contents = "Document Contents\n"
|
||||
|
||||
mocker.patch("ansible_collections.community.general.plugins.lookup.onepassword.OnePass.assert_logged_in", return_value=True)
|
||||
mocker.patch("ansible_collections.community.general.plugins.lookup.onepassword.OnePassCLIBase._run", return_value=(0, document_contents, ""))
|
||||
mocker.patch(
|
||||
"ansible_collections.community.general.plugins.lookup.onepassword.OnePass.assert_logged_in", return_value=True
|
||||
)
|
||||
mocker.patch(
|
||||
"ansible_collections.community.general.plugins.lookup.onepassword.OnePassCLIBase._run",
|
||||
return_value=(0, document_contents, ""),
|
||||
)
|
||||
|
||||
op_lookup = lookup_loader.get("community.general.onepassword_doc")
|
||||
result = op_lookup.run(["Private key doc"])
|
||||
@@ -289,17 +339,18 @@ def test_op_doc(mocker):
|
||||
[
|
||||
(plugin, connect_host, connect_token)
|
||||
for plugin in ("community.general.onepassword", "community.general.onepassword_raw")
|
||||
for (connect_host, connect_token) in
|
||||
(
|
||||
for (connect_host, connect_token) in (
|
||||
("http://localhost", None),
|
||||
(None, "foobar"),
|
||||
)
|
||||
]
|
||||
],
|
||||
)
|
||||
def test_op_connect_partial_args(plugin, connect_host, connect_token, mocker):
|
||||
op_lookup = lookup_loader.get(plugin)
|
||||
|
||||
mocker.patch("ansible_collections.community.general.plugins.lookup.onepassword.OnePass._get_cli_class", OnePassCLIv2)
|
||||
mocker.patch(
|
||||
"ansible_collections.community.general.plugins.lookup.onepassword.OnePass._get_cli_class", OnePassCLIv2
|
||||
)
|
||||
|
||||
with pytest.raises(AnsibleOptionsError):
|
||||
op_lookup.run("login", vault_name="test vault", connect_host=connect_host, connect_token=connect_token)
|
||||
@@ -310,7 +361,7 @@ def test_op_connect_partial_args(plugin, connect_host, connect_token, mocker):
|
||||
(
|
||||
{"connect_host": "http://localhost", "connect_token": "foobar"},
|
||||
{"service_account_token": "foobar"},
|
||||
)
|
||||
),
|
||||
)
|
||||
def test_opv1_unsupported_features(kwargs):
|
||||
op_cli = OnePassCLIv1(**kwargs)
|
||||
|
||||
Reference in New Issue
Block a user