-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathtest_validation_json.py
More file actions
111 lines (94 loc) · 4.45 KB
/
test_validation_json.py
File metadata and controls
111 lines (94 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# This file is part of CycloneDX Python Lib
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) OWASP Foundation. All Rights Reserved.
from glob import iglob
from os.path import join
from typing import Generator
from unittest import TestCase
from ddt import data, ddt, idata, unpack
from cyclonedx.exception import MissingOptionalDependencyException
from cyclonedx.schema import OutputFormat, SchemaVersion
from cyclonedx.validation.json import JsonStrictValidator, JsonValidator
from tests import SCHEMA_TESTDATA_DIRECTORY, UNDEFINED_SCHEMA_VERSIONS
_UNDEFINED_SCHEMA_VERSIONS = UNDEFINED_SCHEMA_VERSIONS[OutputFormat.JSON]
def _dp(prefix: str) -> Generator:
return (
(sv, tf) for sv in SchemaVersion if sv not in _UNDEFINED_SCHEMA_VERSIONS
for tf in iglob(join(SCHEMA_TESTDATA_DIRECTORY, sv.to_version(), f'{prefix}-*.json'))
)
@ddt
class TestJsonValidator(TestCase):
@idata(sv for sv in SchemaVersion if sv not in _UNDEFINED_SCHEMA_VERSIONS)
def test_validator_as_expected(self, schema_version: SchemaVersion) -> None:
validator = JsonValidator(schema_version)
self.assertIs(validator.schema_version, schema_version)
self.assertIs(validator.output_format, OutputFormat.JSON)
@idata(_UNDEFINED_SCHEMA_VERSIONS)
def test_throws_with_unsupported_schema_version(self, schema_version: SchemaVersion) -> None:
with self.assertRaisesRegex(ValueError, 'Unsupported schema_version'):
JsonValidator(schema_version)
@idata(_dp('valid'))
@unpack
def test_validate_no_none(self, schema_version: SchemaVersion, test_data_file: str) -> None:
validator = JsonValidator(schema_version)
with open(join(test_data_file), 'r') as tdfh:
test_data = tdfh.read()
try:
validation_error = validator.validate_str(test_data)
except MissingOptionalDependencyException:
self.skipTest('MissingOptionalDependencyException')
self.assertIsNone(validation_error)
@idata(_dp('invalid'))
@unpack
def test_validate_expected_error(self, schema_version: SchemaVersion, test_data_file: str) -> None:
validator = JsonValidator(schema_version)
with open(join(test_data_file), 'r') as tdfh:
test_data = tdfh.read()
try:
validation_error = validator.validate_str(test_data)
except MissingOptionalDependencyException:
self.skipTest('MissingOptionalDependencyException')
self.assertIsNotNone(validation_error)
self.assertIsNotNone(validation_error.data)
@ddt
class TestJsonStrictValidator(TestCase):
@data(*_UNDEFINED_SCHEMA_VERSIONS)
def test_throws_with_unsupported_schema_version(self, schema_version: SchemaVersion) -> None:
with self.assertRaisesRegex(ValueError, 'Unsupported schema_version'):
JsonStrictValidator(schema_version)
@idata(_dp('valid'))
@unpack
def test_validate_no_none(self, schema_version: SchemaVersion, test_data_file: str) -> None:
validator = JsonStrictValidator(schema_version)
with open(join(test_data_file), 'r') as tdfh:
test_data = tdfh.read()
try:
validation_error = validator.validate_str(test_data)
except MissingOptionalDependencyException:
self.skipTest('MissingOptionalDependencyException')
self.assertIsNone(validation_error)
@idata(_dp('invalid'))
@unpack
def test_validate_expected_error(self, schema_version: SchemaVersion, test_data_file: str) -> None:
validator = JsonStrictValidator(schema_version)
with open(join(test_data_file), 'r') as tdfh:
test_data = tdfh.read()
try:
validation_error = validator.validate_str(test_data)
except MissingOptionalDependencyException:
self.skipTest('MissingOptionalDependencyException')
self.assertIsNotNone(validation_error)
self.assertIsNotNone(validation_error.data)