2022-07-21 05:27:26 +00:00
|
|
|
# Copyright (c) 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
|
|
|
|
|
2021-04-11 12:44:44 +00:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2022-09-04 20:21:31 +00:00
|
|
|
from ansible_collections.community.crypto.tests.unit.compat.mock import MagicMock
|
2021-04-11 12:44:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
from ansible_collections.community.crypto.plugins.module_utils.acme.errors import (
|
|
|
|
format_error_problem,
|
|
|
|
ACMEProtocolException,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
TEST_FORMAT_ERROR_PROBLEM = [
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'type': 'foo',
|
|
|
|
},
|
|
|
|
'',
|
|
|
|
'Error foo'
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'type': 'foo',
|
|
|
|
'title': 'bar'
|
|
|
|
},
|
|
|
|
'',
|
|
|
|
'Error "bar" (foo)'
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'type': 'foo',
|
|
|
|
'detail': 'bar baz'
|
|
|
|
},
|
|
|
|
'',
|
|
|
|
'Error foo: "bar baz"'
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'type': 'foo',
|
|
|
|
'subproblems': []
|
|
|
|
},
|
|
|
|
'',
|
|
|
|
'Error foo Subproblems:'
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'type': 'foo',
|
|
|
|
'subproblems': [
|
|
|
|
{
|
|
|
|
'type': 'bar',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
'',
|
|
|
|
'Error foo Subproblems:\n(0) Error bar'
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'type': 'foo',
|
|
|
|
'subproblems': [
|
|
|
|
{
|
|
|
|
'type': 'bar',
|
|
|
|
'subproblems': [
|
|
|
|
{
|
|
|
|
'type': 'baz',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
'',
|
|
|
|
'Error foo Subproblems:\n(0) Error bar Subproblems:\n(0.0) Error baz'
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'type': 'foo',
|
|
|
|
'title': 'Foo Error',
|
|
|
|
'detail': 'Foo went wrong',
|
|
|
|
'subproblems': [
|
|
|
|
{
|
|
|
|
'type': 'bar',
|
|
|
|
'detail': 'Bar went wrong',
|
|
|
|
'subproblems': [
|
|
|
|
{
|
|
|
|
'type': 'baz',
|
|
|
|
'title': 'Baz Error',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'type': 'bar2',
|
|
|
|
'title': 'Bar 2 Error',
|
|
|
|
'detail': 'Bar really went wrong'
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
'X.',
|
|
|
|
'Error "Foo Error" (foo): "Foo went wrong" Subproblems:\n'
|
|
|
|
'(X.0) Error bar: "Bar went wrong" Subproblems:\n'
|
|
|
|
'(X.0.0) Error "Baz Error" (baz)\n'
|
|
|
|
'(X.1) Error "Bar 2 Error" (bar2): "Bar really went wrong"'
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("problem, subproblem_prefix, result", TEST_FORMAT_ERROR_PROBLEM)
|
|
|
|
def test_format_error_problem(problem, subproblem_prefix, result):
|
|
|
|
res = format_error_problem(problem, subproblem_prefix)
|
|
|
|
assert res == result
|
|
|
|
|
|
|
|
|
|
|
|
def create_regular_response(response_text):
|
|
|
|
response = MagicMock()
|
|
|
|
response.read = MagicMock(return_value=response_text.encode('utf-8'))
|
2021-11-17 20:26:49 +00:00
|
|
|
response.closed = False
|
2021-04-11 12:44:44 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
def create_error_response():
|
|
|
|
response = MagicMock()
|
|
|
|
response.read = MagicMock(side_effect=AttributeError('read'))
|
2021-11-17 20:26:49 +00:00
|
|
|
response.closed = True
|
2021-04-11 12:44:44 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
def create_decode_error(msg):
|
|
|
|
def f(content):
|
|
|
|
raise Exception(msg)
|
|
|
|
|
|
|
|
return f
|
|
|
|
|
|
|
|
|
|
|
|
TEST_ACME_PROTOCOL_EXCEPTION = [
|
|
|
|
(
|
|
|
|
{},
|
|
|
|
None,
|
|
|
|
'ACME request failed.',
|
|
|
|
{
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'msg': 'Foo',
|
|
|
|
'extras': {
|
|
|
|
'foo': 'bar',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
None,
|
|
|
|
'Foo.',
|
|
|
|
{
|
|
|
|
'foo': 'bar',
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'info': {
|
|
|
|
'url': 'https://ca.example.com/foo',
|
|
|
|
'status': 201,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
None,
|
2022-10-31 20:33:27 +00:00
|
|
|
'ACME request failed for https://ca.example.com/foo with HTTP status 201 Created.',
|
2021-04-11 12:44:44 +00:00
|
|
|
{
|
|
|
|
'http_url': 'https://ca.example.com/foo',
|
|
|
|
'http_status': 201,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'info': {
|
|
|
|
'url': 'https://ca.example.com/foo',
|
|
|
|
'status': 201,
|
|
|
|
},
|
|
|
|
'response': create_regular_response('xxx'),
|
|
|
|
},
|
|
|
|
None,
|
2022-10-31 20:33:27 +00:00
|
|
|
'ACME request failed for https://ca.example.com/foo with HTTP status 201 Created. The raw error result: xxx',
|
2021-04-11 12:44:44 +00:00
|
|
|
{
|
|
|
|
'http_url': 'https://ca.example.com/foo',
|
|
|
|
'http_status': 201,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'info': {
|
|
|
|
'url': 'https://ca.example.com/foo',
|
|
|
|
'status': 201,
|
|
|
|
},
|
|
|
|
'response': create_regular_response('xxx'),
|
|
|
|
},
|
|
|
|
create_decode_error('yyy'),
|
2022-10-31 20:33:27 +00:00
|
|
|
'ACME request failed for https://ca.example.com/foo with HTTP status 201 Created. The raw error result: xxx',
|
2021-04-11 12:44:44 +00:00
|
|
|
{
|
|
|
|
'http_url': 'https://ca.example.com/foo',
|
|
|
|
'http_status': 201,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'info': {
|
|
|
|
'url': 'https://ca.example.com/foo',
|
|
|
|
'status': 201,
|
|
|
|
},
|
|
|
|
'response': create_regular_response('xxx'),
|
|
|
|
},
|
|
|
|
lambda content: dict(foo='bar'),
|
2022-10-31 20:33:27 +00:00
|
|
|
"ACME request failed for https://ca.example.com/foo with HTTP status 201 Created. The JSON error result: {'foo': 'bar'}",
|
2021-04-11 12:44:44 +00:00
|
|
|
{
|
|
|
|
'http_url': 'https://ca.example.com/foo',
|
|
|
|
'http_status': 201,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'info': {
|
|
|
|
'url': 'https://ca.example.com/foo',
|
|
|
|
'status': 201,
|
|
|
|
},
|
|
|
|
'response': create_error_response(),
|
|
|
|
},
|
|
|
|
None,
|
2022-10-31 20:33:27 +00:00
|
|
|
'ACME request failed for https://ca.example.com/foo with HTTP status 201 Created.',
|
2021-04-11 12:44:44 +00:00
|
|
|
{
|
|
|
|
'http_url': 'https://ca.example.com/foo',
|
|
|
|
'http_status': 201,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'info': {
|
|
|
|
'url': 'https://ca.example.com/foo',
|
|
|
|
'status': 201,
|
|
|
|
'body': 'xxx',
|
|
|
|
},
|
|
|
|
'response': create_error_response(),
|
|
|
|
},
|
|
|
|
lambda content: dict(foo='bar'),
|
2022-10-31 20:33:27 +00:00
|
|
|
"ACME request failed for https://ca.example.com/foo with HTTP status 201 Created. The JSON error result: {'foo': 'bar'}",
|
2021-04-11 12:44:44 +00:00
|
|
|
{
|
|
|
|
'http_url': 'https://ca.example.com/foo',
|
|
|
|
'http_status': 201,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'info': {
|
|
|
|
'url': 'https://ca.example.com/foo',
|
|
|
|
'status': 201,
|
|
|
|
},
|
|
|
|
'content': 'xxx',
|
|
|
|
},
|
|
|
|
None,
|
2022-10-31 20:33:27 +00:00
|
|
|
"ACME request failed for https://ca.example.com/foo with HTTP status 201 Created. The raw error result: xxx",
|
2021-04-11 12:44:44 +00:00
|
|
|
{
|
|
|
|
'http_url': 'https://ca.example.com/foo',
|
|
|
|
'http_status': 201,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'info': {
|
|
|
|
'url': 'https://ca.example.com/foo',
|
|
|
|
'status': 400,
|
|
|
|
},
|
|
|
|
'content_json': {
|
|
|
|
'foo': 'bar',
|
|
|
|
},
|
|
|
|
'extras': {
|
|
|
|
'bar': 'baz',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None,
|
2022-10-31 20:33:27 +00:00
|
|
|
"ACME request failed for https://ca.example.com/foo with HTTP status 400 Bad Request. The JSON error result: {'foo': 'bar'}",
|
2021-04-11 12:44:44 +00:00
|
|
|
{
|
|
|
|
'http_url': 'https://ca.example.com/foo',
|
|
|
|
'http_status': 400,
|
|
|
|
'bar': 'baz',
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'info': {
|
|
|
|
'url': 'https://ca.example.com/foo',
|
|
|
|
'status': 201,
|
|
|
|
},
|
|
|
|
'content_json': {
|
|
|
|
'type': 'foo',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
None,
|
2022-10-31 20:33:27 +00:00
|
|
|
"ACME request failed for https://ca.example.com/foo with HTTP status 201 Created. The JSON error result: {'type': 'foo'}",
|
2021-04-11 12:44:44 +00:00
|
|
|
{
|
|
|
|
'http_url': 'https://ca.example.com/foo',
|
|
|
|
'http_status': 201,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'info': {
|
|
|
|
'url': 'https://ca.example.com/foo',
|
|
|
|
'status': 400,
|
|
|
|
},
|
|
|
|
'content_json': {
|
|
|
|
'type': 'foo',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
None,
|
2022-10-31 20:33:27 +00:00
|
|
|
"ACME request failed for https://ca.example.com/foo with status 400 Bad Request. Error foo.",
|
2021-04-11 12:44:44 +00:00
|
|
|
{
|
|
|
|
'http_url': 'https://ca.example.com/foo',
|
|
|
|
'http_status': 400,
|
|
|
|
'problem': {
|
|
|
|
'type': 'foo',
|
|
|
|
},
|
|
|
|
'subproblems': [],
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'info': {
|
|
|
|
'url': 'https://ca.example.com/foo',
|
|
|
|
'status': 400,
|
|
|
|
},
|
|
|
|
'content_json': {
|
|
|
|
'type': 'foo',
|
|
|
|
'title': 'Foo Error',
|
|
|
|
'subproblems': [
|
|
|
|
{
|
|
|
|
'type': 'bar',
|
|
|
|
'detail': 'This is a bar error',
|
|
|
|
'details': 'Details.',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
None,
|
2022-10-31 20:33:27 +00:00
|
|
|
"ACME request failed for https://ca.example.com/foo with status 400 Bad Request. Error \"Foo Error\" (foo). Subproblems:\n"
|
2021-04-11 12:44:44 +00:00
|
|
|
"(0) Error bar: \"This is a bar error\".",
|
|
|
|
{
|
|
|
|
'http_url': 'https://ca.example.com/foo',
|
|
|
|
'http_status': 400,
|
|
|
|
'problem': {
|
|
|
|
'type': 'foo',
|
|
|
|
'title': 'Foo Error',
|
|
|
|
},
|
|
|
|
'subproblems': [
|
|
|
|
{
|
|
|
|
'type': 'bar',
|
|
|
|
'detail': 'This is a bar error',
|
|
|
|
'details': 'Details.',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("input, from_json, msg, args", TEST_ACME_PROTOCOL_EXCEPTION)
|
|
|
|
def test_acme_protocol_exception(input, from_json, msg, args):
|
|
|
|
if from_json is None:
|
|
|
|
module = None
|
|
|
|
else:
|
|
|
|
module = MagicMock()
|
|
|
|
module.from_json = from_json
|
|
|
|
with pytest.raises(ACMEProtocolException) as exc:
|
|
|
|
raise ACMEProtocolException(module, **input)
|
|
|
|
|
|
|
|
print(exc.value.msg)
|
|
|
|
print(exc.value.module_fail_args)
|
|
|
|
print(msg)
|
|
|
|
print(args)
|
|
|
|
assert exc.value.msg == msg
|
|
|
|
assert exc.value.module_fail_args == args
|