Skip to content

Commit

Permalink
Create tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Honny1 committed Feb 17, 2023
1 parent 4d777d0 commit d81413d
Show file tree
Hide file tree
Showing 6 changed files with 597 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build:
before:
- pip3 install -r requirements.txt
- npm i -g eslint
- pip3 install isort pylint flake8 coverage
- pip3 install isort pylint flake8 coverage jsonschema
environment:
python: 3.9.6
nodes:
Expand Down
2 changes: 2 additions & 0 deletions tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@
PATH_TO_XCCDF_WITHOUT_INFO = Path(__file__).parent / "test_data/xccdf-with-removed-info.xml"
PATH_TO_XCCDF_WITHOUT_SYSTEM_DATA = Path(__file__).parent / "test_data/xccdf_no_system_data.xml"
PATH_TO_XCCDF_WITH_MULTI_CHECK = Path(__file__).parent / "test_data/xccdf_multi_check.xml"

PATH_TO_JSON_SCHEMA = Path(__file__).parent / "json_schema_of_report.json"
30 changes: 30 additions & 0 deletions tests/integration_tests/test_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2022, Red Hat, Inc.
# SPDX-License-Identifier: LGPL-2.1-or-later

import json

import pytest
from jsonschema import validate

from openscap_report.debug_settings import DebugSetting
from openscap_report.report_generators.json import JSONReportGenerator
from tests.unit_tests.test_data_structure import get_parser, get_report

from ..constants import PATH_TO_ARF, PATH_TO_JSON_SCHEMA


@pytest.mark.integration_test
def test_json_structure_with_schema():
json_schema = None
with open(PATH_TO_JSON_SCHEMA, "r", encoding="utf-8") as schema_file:
json_schema = json.load(schema_file)
json_gen = JSONReportGenerator(get_parser(PATH_TO_ARF))
json_data = json_gen.generate_report(DebugSetting()).read().decode("utf-8")
validate(json.loads(json_data), json_schema)


@pytest.mark.integration_test
def test_json_count_of_rules():
report = get_report()
json_dict = report.as_dict_for_default_json()
assert len(json_dict["rules"]) == 714
311 changes: 311 additions & 0 deletions tests/json_schema_of_report.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,311 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"profile_info": {
"title": "Profile Info",
"type": "object",
"properties": {
"title": {
"title": "Title",
"type": "string"
},
"description": {
"title": "Description",
"type": "string"
},
"profile_id": {
"title": "Profile ID",
"type": "string"
},
"extends": {
"title": "Extends profile",
"type": "string"
}
},
"required": [
"profile_id"
]
},
"scan_results":{
"title": "Results of performed scan",
"type": "object",
"properties": {
"title": {
"title": "Title",
"type": "string"
},
"identity": {
"title": "Identity",
"type": "string"
},
"profile_id": {
"title": "Profile ID",
"type": "string"
},
"target": {
"title": "Target",
"type": "string"
},
"cpe_platforms": {
"title": "CPE Platforms",
"type": "array",
"items": {
"oneOf": [
{
"type": "string",
"pattern": "^\\#\\w+"
},
{
"type": "string",
"pattern": "[cC][pP][eE]:/[AHOaho]?(:[A-Za-z0-9\\._\\-~%]*){0,6}"
}
]
}
},
"scanner": {
"title": "Scanner",
"default": "",
"type": "string"
},
"scanner_version": {
"title": "Scanner Version",
"default": "",
"type": "string"
},
"benchmark_url": {
"title": "Benchmark URL",
"default": "",
"type": "string"
},
"benchmark_id": {
"title": "Benchmark ID",
"pattern": "xccdf_[^_]+_benchmark_.+",
"type": "string"
},
"benchmark_version": {
"title": "Benchmark Version",
"default": "",
"type": "string"
},
"start_time": {
"title": "Start Time",
"default": "",
"type": "string"
},
"end_time": {
"title": "End Time",
"default": "",
"type": "string"
},
"test_system": {
"title": "Test System",
"default": "",
"type": "string"
},
"score": {
"title": "Score",
"default": 0.0,
"type": "number"
},
"score_max": {
"title": "Score Max",
"default": 0.0,
"type": "number"
}
},
"required": [
"title",
"identity",
"profile_id",
"target",
"scanner",
"scanner_version",
"benchmark_url",
"benchmark_id",
"benchmark_version",
"start_time",
"end_time",
"test_system",
"score",
"score_max"
]
},
"rules": {
"title": "Rules",
"type": "object",
"patternProperties": {
"xccdf_[^_]+_rule_.+": {
"$ref": "#/definitions/Rule"
}
}
},
"references": {
"title": "References",
"type": "object",
"patternProperties": {
".": {
"type": "string"
}
}
},
"identifiers": {
"title": "Identifiers",
"type": "object",
"patternProperties": {
".": {
"type": "string"
}
}
}
},
"definitions": {
"Warning": {
"title": "Warning",
"type": "object",
"properties": {
"category": {
"title": "Category",
"type": "string"
},
"text": {
"title": "Text",
"type": "string"
}
}
},
"Remediation": {
"title": "Remediation",
"type": "object",
"properties": {
"remediation_id": {
"title": "Remediation ID",
"type": "string"
},
"system": {
"title": "System",
"type": "string"
},
"complexity": {
"title": "Complexity",
"type": "string"
},
"disruption": {
"title": "Disruption",
"type": "string"
},
"strategy": {
"title": "Strategy",
"type": "string"
},
"fix": {
"title": "Fix",
"type": "string"
}
},
"required": [
"remediation_id",
"fix"
]
},
"Rule": {
"title": "Rule",
"type": "object",
"properties": {
"rule_id": {
"title": "Rule ID",
"pattern": "xccdf_[^_]+_rule_.+",
"type": "string"
},
"title": {
"title": "Title",
"type": "string"
},
"result": {
"title": "Result",
"type": "string"
},
"time": {
"title": "Time",
"type": "string"
},
"severity": {
"title": "Severity",
"type": "string"
},
"identifiers": {
"title": "Identifiers",
"type": "array",
"items": {
"type": "string"
}
},
"references": {
"title": "References",
"type": "array",
"items": {
"type": "string"
}
},
"description": {
"title": "Description",
"type": "string"
},
"rationale": {
"title": "Rationale",
"type": "string"
},
"warnings": {
"title": "Warnings",
"type": "array",
"items": {
"$ref": "#/definitions/Warning"
}
},
"platforms": {
"title": "Platforms",
"type": "array",
"items": {
"oneOf": [
{
"type": "string",
"pattern": "^\\#\\w+"
},
{
"type": "string",
"pattern": "[cC][pP][eE]:/[AHOaho]?(:[A-Za-z0-9\\._\\-~%]*){0,6}"
}
]
}
},
"oval_definition_id": {
"title": "Oval Definition ID",
"pattern": "(oval:[A-Za-z0-9_\\-\\.]+:def:[1-9][0-9]*)|^$"
},
"messages": {
"title": "Messages",
"items": {
"type": "string"
}
},
"remediations": {
"title": "Remediations",
"type": "array",
"items": {
"$ref": "#/definitions/Remediation"
}
}
},
"required": [
"rule_id",
"result",
"time",
"severity"
]
}
},
"required": [
"scan_result",
"rules"
]
}
Loading

0 comments on commit d81413d

Please sign in to comment.