Skip to content
This repository has been archived by the owner on Jan 27, 2023. It is now read-only.

Add unit test for make_response_error #1311

Merged
merged 1 commit into from
Nov 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 161 additions & 0 deletions tests/unit/anchore_engine/common/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,164 @@ def test_valid_data(self):
assert len(extracted_content[key]["licenses"]) == 1
assert extracted_content[key]["licenses"][0] == "MIT"
assert len(extracted_content[key]["cpes"]) > 0


class TestMakeResponseError:
class TestException(Exception):
def __init__(self, msg, anchore_error_json=None):
super().__init__(msg)
if anchore_error_json is not None:
self.anchore_error_json = anchore_error_json

params = [
pytest.param(
{
"errmsg": "basic-test-case",
"in_httpcode": None,
"details": None,
"expected": {
"message": "basic-test-case",
"httpcode": 500,
"detail": {"error_codes": []},
},
},
id="basic",
),
pytest.param(
{
"errmsg": "basic-test-case",
"in_httpcode": 400,
"details": None,
"expected": {
"message": "basic-test-case",
"httpcode": 400,
"detail": {"error_codes": []},
},
},
id="basic-with-httpcode",
),
pytest.param(
{
"errmsg": "basic-test-case",
"in_httpcode": None,
"details": {"test": "value"},
"expected": {
"message": "basic-test-case",
"httpcode": 500,
"detail": {
"test": "value",
"error_codes": [],
},
},
},
id="basic-with-details",
),
pytest.param(
{
"errmsg": "basic-test-case",
"in_httpcode": None,
"details": {"error_codes": [500, 404]},
"expected": {
"message": "basic-test-case",
"httpcode": 500,
"detail": {"error_codes": [500, 404]},
},
},
id="basic-with-error-codes",
),
pytest.param(
{
"errmsg": Exception("thisisatest"),
"in_httpcode": None,
"details": None,
"expected": {
"message": "thisisatest",
"httpcode": 500,
"detail": {"error_codes": []},
},
},
id="basic-exception",
),
pytest.param(
{
"errmsg": TestException(
"testexception",
anchore_error_json={
"message": "test",
"httpcode": 500,
"detail": {"error_codes": [404]},
},
),
"in_httpcode": 400,
"details": None,
"expected": {
"message": "test",
"httpcode": 500,
"detail": {"error_codes": [404]},
},
},
id="basic-exception-with-anchore-error-json",
),
pytest.param(
{
"errmsg": TestException(
"testexception",
anchore_error_json={
"message": "test",
"httpcode": 500,
"detail": {"error_codes": [404]},
"error_code": 401,
},
),
"in_httpcode": 400,
"details": None,
"expected": {
"message": "test",
"httpcode": 500,
"detail": {"error_codes": [404, 401]},
},
},
id="basic-exception-with-anchore-error-json-and-error-code",
),
pytest.param(
{
"errmsg": TestException(
"testexception",
anchore_error_json='{"message": "test", "httpcode": 500, "detail": {"error_codes": [404]}}',
),
"in_httpcode": 400,
"details": None,
"expected": {
"message": "test",
"httpcode": 500,
"detail": {"error_codes": [404]},
},
},
id="basic-exception-with-json-string",
),
pytest.param(
{
"errmsg": TestException(
"testexception",
anchore_error_json='{"message" "test", "httpcode": 500, "detail": {"error_codes": [404]}}',
),
"in_httpcode": 400,
"details": None,
"expected": {
"message": "testexception",
"httpcode": 400,
"detail": {"error_codes": []},
},
},
id="basic-exception-with-bad-json-string",
),
]

@pytest.mark.parametrize("param", params)
def test_make_response_error(self, param):
actual = helpers.make_response_error(
param["errmsg"], param["in_httpcode"], param["details"]
)
assert actual["message"] == param["expected"]["message"]
assert actual["httpcode"] == param["expected"]["httpcode"]
assert actual["detail"] == param["expected"]["detail"]