mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-07-08 11:39:02 +00:00
Merge d559bd73ec into 877f20f278
This commit is contained in:
commit
66a10c6ec8
5 changed files with 385 additions and 144 deletions
135
tests/unit/plugins/modules/test_onepassword_info.py
Normal file
135
tests/unit/plugins/modules/test_onepassword_info.py
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
# Copyright (c) 2026, Alexei Znamensky <russoz@gmail.com>
|
||||
# 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 annotations
|
||||
|
||||
import json
|
||||
from unittest.mock import PropertyMock
|
||||
|
||||
import pytest
|
||||
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import set_module_args
|
||||
|
||||
from ansible_collections.community.general.plugins.module_utils._onepassword import OnePasswordConfig
|
||||
from ansible_collections.community.general.plugins.modules import onepassword_info
|
||||
|
||||
from .uthelper import RunCommandMock, TestCaseMock, UTHelper
|
||||
|
||||
ITEM_JSON = json.dumps(
|
||||
{
|
||||
"fields": [
|
||||
{"id": "password", "label": "password", "value": "secret123"},
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class OnePasswordConfigMock(TestCaseMock):
|
||||
name = "onepassword_config"
|
||||
|
||||
def setup(self, mocker):
|
||||
mocker.patch.object(
|
||||
OnePasswordConfig,
|
||||
"config_file_path",
|
||||
new_callable=PropertyMock,
|
||||
return_value=self.mock_specs.get("config_file_path"),
|
||||
)
|
||||
|
||||
def check(self, test_case, results):
|
||||
pass
|
||||
|
||||
|
||||
UTHelper.from_module(onepassword_info, __name__, mocks=[RunCommandMock, OnePasswordConfigMock])
|
||||
|
||||
|
||||
def _patch_bin_path(mocker):
|
||||
mocker.patch(
|
||||
"ansible.module_utils.basic.AnsibleModule.get_bin_path",
|
||||
lambda self_, path, *args, **kwargs: f"/testbin/{path}",
|
||||
)
|
||||
|
||||
|
||||
def _patch_run_command(mocker, responses):
|
||||
mocker.patch(
|
||||
"ansible.module_utils.basic.AnsibleModule.run_command",
|
||||
side_effect=lambda cmd, **kwargs: next(responses),
|
||||
)
|
||||
|
||||
|
||||
def test_get_token_signin(mocker, capfd):
|
||||
master_password = "masterpass"
|
||||
with set_module_args(
|
||||
{
|
||||
"search_terms": [{"name": "My Item"}],
|
||||
"auto_login": {"subdomain": "mycompany", "master_password": master_password},
|
||||
}
|
||||
):
|
||||
_patch_bin_path(mocker)
|
||||
mocker.patch.object(
|
||||
OnePasswordConfig,
|
||||
"config_file_path",
|
||||
new_callable=PropertyMock,
|
||||
return_value="/home/user/.op/config",
|
||||
)
|
||||
mocker.patch(
|
||||
"ansible_collections.community.general.plugins.modules.onepassword_info.os.path.isfile",
|
||||
return_value=True,
|
||||
)
|
||||
_patch_run_command(
|
||||
mocker,
|
||||
iter(
|
||||
[
|
||||
(0, "", ""), # account list → out empty, not logged in
|
||||
(0, "mytoken\n", ""), # signin --raw --account mycompany
|
||||
(0, ITEM_JSON, ""), # item get --format json My Item --session=mytoken
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
onepassword_info.main()
|
||||
|
||||
out, dummy = capfd.readouterr()
|
||||
result = json.loads(out)
|
||||
assert not result.get("failed"), result.get("msg")
|
||||
assert result["onepassword"]["My Item"]["password"] == "secret123"
|
||||
|
||||
|
||||
def test_full_login(mocker, capfd):
|
||||
master_password = "masterpass"
|
||||
with set_module_args(
|
||||
{
|
||||
"search_terms": [{"name": "My Item"}],
|
||||
"auto_login": {
|
||||
"subdomain": "mycompany",
|
||||
"username": "user@example.com",
|
||||
"secret_key": "mysecretkey",
|
||||
"master_password": master_password,
|
||||
},
|
||||
}
|
||||
):
|
||||
_patch_bin_path(mocker)
|
||||
mocker.patch.object(
|
||||
OnePasswordConfig,
|
||||
"config_file_path",
|
||||
new_callable=PropertyMock,
|
||||
return_value=None,
|
||||
)
|
||||
_patch_run_command(
|
||||
mocker,
|
||||
iter(
|
||||
[
|
||||
(0, "", ""), # account list → not logged in
|
||||
(0, "mytoken\n", ""), # account add --raw --signin → token
|
||||
(0, ITEM_JSON, ""), # item get --format json My Item --session=mytoken
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
onepassword_info.main()
|
||||
|
||||
out, dummy = capfd.readouterr()
|
||||
result = json.loads(out)
|
||||
assert not result.get("failed"), result.get("msg")
|
||||
assert result["onepassword"]["My Item"]["password"] == "secret123"
|
||||
134
tests/unit/plugins/modules/test_onepassword_info.yaml
Normal file
134
tests/unit/plugins/modules/test_onepassword_info.yaml
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
# Copyright (c) 2026, Alexei Znamensky <russoz@gmail.com>
|
||||
# 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
|
||||
|
||||
---
|
||||
anchors:
|
||||
env: &env {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
item_json: &item_json '{"fields": [{"id": "password", "label": "password", "value": "secret123"}]}'
|
||||
account_list: &account_list
|
||||
command: [/testbin/op, account, list]
|
||||
environ: *env
|
||||
rc: 0
|
||||
out: "account info\n"
|
||||
err: ''
|
||||
account_get: &account_get
|
||||
command: [/testbin/op, account, get]
|
||||
environ: *env
|
||||
rc: 0
|
||||
out: ''
|
||||
err: ''
|
||||
|
||||
test_cases:
|
||||
- id: already_logged_in
|
||||
input:
|
||||
search_terms:
|
||||
- name: My Item
|
||||
output:
|
||||
changed: false
|
||||
onepassword:
|
||||
"My Item":
|
||||
password: secret123
|
||||
mocks:
|
||||
run_command:
|
||||
- *account_list
|
||||
- *account_get
|
||||
- command: [/testbin/op, item, get, --format, json, My Item]
|
||||
environ: *env
|
||||
rc: 0
|
||||
out: *item_json
|
||||
err: ''
|
||||
|
||||
- id: already_logged_in_with_vault
|
||||
input:
|
||||
search_terms:
|
||||
- name: My Item
|
||||
vault: Personal
|
||||
output:
|
||||
changed: false
|
||||
onepassword:
|
||||
"My Item":
|
||||
password: secret123
|
||||
mocks:
|
||||
run_command:
|
||||
- *account_list
|
||||
- *account_get
|
||||
- command: [/testbin/op, item, get, --format, json, My Item, --vault=Personal]
|
||||
environ: *env
|
||||
rc: 0
|
||||
out: *item_json
|
||||
err: ''
|
||||
|
||||
- id: already_logged_in_section_field
|
||||
input:
|
||||
search_terms:
|
||||
- name: My Item
|
||||
field: Custom Field
|
||||
section: My Section
|
||||
output:
|
||||
changed: false
|
||||
onepassword:
|
||||
"My Item":
|
||||
Custom Field: custom_val
|
||||
mocks:
|
||||
run_command:
|
||||
- *account_list
|
||||
- *account_get
|
||||
- command: [/testbin/op, item, get, --format, json, My Item]
|
||||
environ: *env
|
||||
rc: 0
|
||||
out: >-
|
||||
{"fields": [{"id": "cf_id", "label": "Custom Field", "value": "custom_val",
|
||||
"section": {"id": "sec_id", "label": "My Section"}}]}
|
||||
err: ''
|
||||
|
||||
- id: item_not_found
|
||||
input:
|
||||
search_terms:
|
||||
- name: Missing Item
|
||||
output:
|
||||
failed: true
|
||||
msg: "Unable to find an item in 1Password named 'Missing Item'."
|
||||
mocks:
|
||||
run_command:
|
||||
- *account_list
|
||||
- *account_get
|
||||
- command: [/testbin/op, item, get, --format, json, Missing Item]
|
||||
environ: *env
|
||||
rc: 1
|
||||
out: ''
|
||||
err: '"Missing Item" not found in vault'
|
||||
|
||||
- id: field_not_found
|
||||
input:
|
||||
search_terms:
|
||||
- name: My Item
|
||||
field: nonexistent
|
||||
output:
|
||||
failed: true
|
||||
msg: "Unable to find an item in 1Password named 'My Item' with the field 'nonexistent'."
|
||||
mocks:
|
||||
run_command:
|
||||
- *account_list
|
||||
- *account_get
|
||||
- command: [/testbin/op, item, get, --format, json, My Item]
|
||||
environ: *env
|
||||
rc: 0
|
||||
out: *item_json
|
||||
err: ''
|
||||
|
||||
- id: not_logged_in_no_auto_login
|
||||
input:
|
||||
search_terms:
|
||||
- name: My Item
|
||||
output:
|
||||
failed: true
|
||||
mocks:
|
||||
run_command:
|
||||
- command: [/testbin/op, account, list]
|
||||
environ: *env
|
||||
rc: 0
|
||||
out: ''
|
||||
err: ''
|
||||
onepassword_config:
|
||||
config_file_path: ~
|
||||
Loading…
Add table
Add a link
Reference in a new issue