|
17 | 17 | # SPDX-License-Identifier: Apache-2.0 |
18 | 18 | # Copyright (c) OWASP Foundation. All Rights Reserved. |
19 | 19 |
|
20 | | -import itertools |
21 | 20 | import unittest |
22 | 21 | import unittest.mock |
23 | | -from typing import Optional |
24 | | - |
25 | | -from ddt import data, ddt, idata, unpack |
26 | 22 |
|
27 | 23 | from cyclonedx.exception.factory import InvalidLicenseExpressionException, InvalidSpdxLicenseException |
28 | 24 | from cyclonedx.factory.license import LicenseChoiceFactory, LicenseFactory |
29 | 25 | from cyclonedx.model import AttachedText, License, LicenseChoice, XsUri |
30 | 26 |
|
31 | 27 |
|
32 | | -@ddt |
33 | 28 | class TestFactoryLicense(unittest.TestCase): |
34 | 29 |
|
35 | | - @idata(itertools.product( |
36 | | - ['MIT'], |
37 | | - ['MIT', 'mit', 'MiT'], |
38 | | - [None, unittest.mock.NonCallableMock(spec=AttachedText)], |
39 | | - [None, unittest.mock.NonCallableMock(spec=XsUri)], |
40 | | - )) |
41 | | - @unpack |
42 | | - def test_make_from_string_with_id(self, expected_id: str, |
43 | | - id_: str, text: Optional[AttachedText], url: Optional[XsUri]) -> None: |
44 | | - expected = License(spdx_license_id=expected_id, license_text=text, license_url=url) |
45 | | - actual = LicenseFactory().make_from_string(id_, license_text=text, license_url=url) |
46 | | - self.assertEqual(expected, actual) |
| 30 | + def test_make_from_string_with_id(self) -> None: |
| 31 | + text = unittest.mock.NonCallableMock(spec=AttachedText) |
| 32 | + url = unittest.mock.NonCallableMock(spec=XsUri) |
| 33 | + expected = License(spdx_license_id='bar', license_text=text, license_url=url) |
| 34 | + factory = LicenseFactory() |
| 35 | + |
| 36 | + with unittest.mock.patch('cyclonedx.factory.license.spdx_fixup', return_value='bar'): |
| 37 | + actual = factory.make_from_string('foo', license_text=text, license_url=url) |
47 | 38 |
|
48 | | - @idata(itertools.product( |
49 | | - ['foo bar'], |
50 | | - [None, unittest.mock.NonCallableMock(spec=AttachedText)], |
51 | | - [None, unittest.mock.NonCallableMock(spec=XsUri)], |
52 | | - )) |
53 | | - @unpack |
54 | | - def test_make_from_string_with_name(self, name: str, text: Optional[AttachedText], url: Optional[XsUri]) -> None: |
55 | | - expected = License(license_name=name, license_text=text, license_url=url) |
56 | | - actual = LicenseFactory().make_from_string(name, license_text=text, license_url=url) |
57 | 39 | self.assertEqual(expected, actual) |
58 | 40 |
|
59 | | - @idata(itertools.product( |
60 | | - ['MIT'], |
61 | | - ['MIT', 'mit', 'MiT'], |
62 | | - [None, unittest.mock.NonCallableMock(spec=AttachedText)], |
63 | | - [None, unittest.mock.NonCallableMock(spec=XsUri)], |
64 | | - )) |
65 | | - @unpack |
66 | | - def test_make_with_id(self, expected_id: str, |
67 | | - id_: str, text: Optional[AttachedText], url: Optional[XsUri]) -> None: |
68 | | - expected = License(spdx_license_id=expected_id, license_text=text, license_url=url) |
69 | | - actual = LicenseFactory().make_with_id(id_, license_text=text, license_url=url) |
| 41 | + def test_make_from_string_with_name(self) -> None: |
| 42 | + text = unittest.mock.NonCallableMock(spec=AttachedText) |
| 43 | + url = unittest.mock.NonCallableMock(spec=XsUri) |
| 44 | + expected = License(license_name='foo', license_text=text, license_url=url) |
| 45 | + factory = LicenseFactory() |
| 46 | + |
| 47 | + with unittest.mock.patch('cyclonedx.factory.license.spdx_fixup', return_value=None): |
| 48 | + actual = factory.make_from_string('foo', license_text=text, license_url=url) |
| 49 | + |
70 | 50 | self.assertEqual(expected, actual) |
71 | 51 |
|
72 | | - @data( |
73 | | - 'FOO BaR', |
74 | | - ) |
75 | | - def test_make_with_id_raises(self, invalid_id: str) -> None: |
| 52 | + def test_make_with_id(self) -> None: |
| 53 | + text = unittest.mock.NonCallableMock(spec=AttachedText) |
| 54 | + url = unittest.mock.NonCallableMock(spec=XsUri) |
| 55 | + expected = License(spdx_license_id='bar', license_text=text, license_url=url) |
76 | 56 | factory = LicenseFactory() |
77 | | - with self.assertRaises(InvalidSpdxLicenseException, msg=invalid_id): |
78 | | - factory.make_with_id(invalid_id) |
79 | | - |
80 | | - @idata(itertools.product( |
81 | | - ['foo'], |
82 | | - [None, unittest.mock.NonCallableMock(spec=AttachedText)], |
83 | | - [None, unittest.mock.NonCallableMock(spec=XsUri)], |
84 | | - )) |
85 | | - @unpack |
86 | | - def test_make_with_name(self, name: str, text: Optional[AttachedText], url: Optional[XsUri]) -> None: |
87 | | - expected = License(license_name=name, license_text=text, license_url=url) |
88 | | - actual = LicenseFactory().make_with_name(name, license_text=text, license_url=url) |
| 57 | + |
| 58 | + with unittest.mock.patch('cyclonedx.factory.license.spdx_fixup', return_value='bar'): |
| 59 | + actual = factory.make_with_id('foo', license_text=text, license_url=url) |
| 60 | + |
89 | 61 | self.assertEqual(expected, actual) |
90 | 62 |
|
| 63 | + def test_make_with_id_raises(self) -> None: |
| 64 | + factory = LicenseFactory() |
| 65 | + with self.assertRaises(InvalidSpdxLicenseException, msg='foo'): |
| 66 | + with unittest.mock.patch('cyclonedx.factory.license.spdx_fixup', return_value=None): |
| 67 | + factory.make_with_id('foo') |
| 68 | + |
| 69 | + def test_make_with_name(self) -> None: |
| 70 | + text = unittest.mock.NonCallableMock(spec=AttachedText) |
| 71 | + url = unittest.mock.NonCallableMock(spec=XsUri) |
| 72 | + expected = License(license_name='foo', license_text=text, license_url=url) |
| 73 | + factory = LicenseFactory() |
| 74 | + |
| 75 | + actual = factory.make_with_name('foo', license_text=text, license_url=url) |
91 | 76 |
|
92 | | -VALID_EXPRESSIONS = { |
93 | | - # for valid test data see the spec: https://spdx.github.io/spdx-spec/v2.3/SPDX-license-expressions/ |
94 | | - '(MIT WITH Apache-2.0)', |
95 | | - '(BSD-2-Clause OR Apache-2.0)', |
96 | | -} |
| 77 | + self.assertEqual(expected, actual) |
97 | 78 |
|
98 | 79 |
|
99 | | -@ddt |
100 | 80 | class TestFactoryLicenseChoice(unittest.TestCase): |
101 | 81 |
|
102 | | - @idata(VALID_EXPRESSIONS) |
103 | | - def test_make_from_string_with_compound_expression(self, compound_expression: str) -> None: |
104 | | - expected = LicenseChoice(license_expression=compound_expression) |
105 | | - license_factory = unittest.mock.MagicMock(spec=LicenseFactory) |
106 | | - actual = LicenseChoiceFactory(license_factory=license_factory).make_from_string( |
107 | | - compound_expression) |
| 82 | + def test_make_from_string_with_compound_expression(self) -> None: |
| 83 | + expected = LicenseChoice(license_expression='foo') |
| 84 | + factory = LicenseChoiceFactory(license_factory=unittest.mock.MagicMock(spec=LicenseFactory)) |
| 85 | + |
| 86 | + with unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=True): |
| 87 | + actual = factory.make_from_string('foo') |
| 88 | + |
108 | 89 | self.assertEqual(expected, actual) |
109 | 90 |
|
110 | 91 | def test_make_from_string_with_license(self) -> None: |
111 | 92 | license_ = unittest.mock.NonCallableMock(spec=License) |
112 | 93 | expected = LicenseChoice(license_=license_) |
113 | 94 | license_factory = unittest.mock.MagicMock(spec=LicenseFactory) |
114 | 95 | license_factory.make_from_string.return_value = license_ |
115 | | - actual = LicenseChoiceFactory(license_factory=license_factory).make_from_string('foo') |
| 96 | + factory = LicenseChoiceFactory(license_factory=license_factory) |
| 97 | + |
| 98 | + with unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=False): |
| 99 | + actual = factory.make_from_string('foo') |
| 100 | + |
116 | 101 | self.assertEqual(expected, actual) |
117 | 102 | self.assertIs(license_, actual.license) |
118 | 103 | license_factory.make_from_string.assert_called_once_with('foo', license_text=None, license_url=None) |
119 | 104 |
|
120 | | - @idata(VALID_EXPRESSIONS) |
121 | | - def test_make_with_compound_expression(self, compound_expression: str) -> None: |
122 | | - expected = LicenseChoice(license_expression=compound_expression) |
123 | | - license_factory = unittest.mock.MagicMock(spec=LicenseFactory) |
124 | | - actual = LicenseChoiceFactory(license_factory=license_factory).make_with_compound_expression( |
125 | | - compound_expression) |
| 105 | + def test_make_with_compound_expression(self) -> None: |
| 106 | + expected = LicenseChoice(license_expression='foo') |
| 107 | + factory = LicenseChoiceFactory(license_factory=unittest.mock.MagicMock(spec=LicenseFactory)) |
| 108 | + |
| 109 | + with unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=True): |
| 110 | + actual = factory.make_with_compound_expression('foo') |
| 111 | + |
126 | 112 | self.assertEqual(expected, actual) |
127 | 113 |
|
128 | | - @data( |
129 | | - 'Foo Bar', |
130 | | - ) |
131 | | - def test_make_with_compound_expression_raises(self, invalid_expression: str) -> None: |
| 114 | + def test_make_with_compound_expression_raises(self) -> None: |
132 | 115 | factory = LicenseChoiceFactory(license_factory=unittest.mock.MagicMock(spec=LicenseFactory)) |
133 | | - with self.assertRaises(InvalidLicenseExpressionException, msg=invalid_expression): |
134 | | - factory.make_with_compound_expression(invalid_expression) |
135 | | - |
136 | | - @idata(itertools.product( |
137 | | - ['foo'], |
138 | | - [None, unittest.mock.NonCallableMock(spec=AttachedText)], |
139 | | - [None, unittest.mock.NonCallableMock(spec=XsUri)], |
140 | | - )) |
141 | | - @unpack |
142 | | - def test_make_with_license(self, name_or_spdx: str, text: Optional[AttachedText], url: Optional[XsUri]) -> None: |
| 116 | + with unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=False): |
| 117 | + with self.assertRaises(InvalidLicenseExpressionException, msg='foo'): |
| 118 | + factory.make_with_compound_expression('foo') |
| 119 | + |
| 120 | + def test_make_with_license(self) -> None: |
| 121 | + text = unittest.mock.NonCallableMock(spec=AttachedText) |
| 122 | + url = unittest.mock.NonCallableMock(spec=XsUri) |
143 | 123 | license_ = unittest.mock.NonCallableMock(spec=License) |
144 | 124 | expected = LicenseChoice(license_=license_) |
145 | 125 | license_factory = unittest.mock.MagicMock(spec=LicenseFactory) |
146 | 126 | license_factory.make_from_string.return_value = license_ |
147 | | - actual = LicenseChoiceFactory(license_factory=license_factory).make_with_license( |
148 | | - name_or_spdx, license_text=text, license_url=url) |
| 127 | + factory = LicenseChoiceFactory(license_factory=license_factory) |
| 128 | + |
| 129 | + with unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=False): |
| 130 | + actual = factory.make_with_license('foo', license_text=text, license_url=url) |
| 131 | + |
149 | 132 | self.assertEqual(expected, actual) |
150 | 133 | self.assertIs(license_, actual.license) |
151 | | - license_factory.make_from_string.assert_called_once_with(name_or_spdx, license_text=text, license_url=url) |
| 134 | + license_factory.make_from_string.assert_called_once_with('foo', license_text=text, license_url=url) |
0 commit comments