|
| 1 | +# encoding: utf-8 |
| 2 | + |
| 3 | +# This file is part of CycloneDX Python Lib |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +# SPDX-License-Identifier: Apache-2.0 |
| 18 | +# Copyright (c) OWASP Foundation. All Rights Reserved. |
| 19 | + |
| 20 | +import unittest |
| 21 | +import unittest.mock |
| 22 | + |
| 23 | +from cyclonedx.exception.factory import InvalidLicenseExpressionException, InvalidSpdxLicenseException |
| 24 | +from cyclonedx.factory.license import LicenseChoiceFactory, LicenseFactory |
| 25 | +from cyclonedx.model import AttachedText, License, LicenseChoice, XsUri |
| 26 | + |
| 27 | + |
| 28 | +class TestFactoryLicense(unittest.TestCase): |
| 29 | + |
| 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) |
| 38 | + |
| 39 | + self.assertEqual(expected, actual) |
| 40 | + |
| 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 | + |
| 50 | + self.assertEqual(expected, actual) |
| 51 | + |
| 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) |
| 56 | + factory = LicenseFactory() |
| 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 | + |
| 61 | + self.assertEqual(expected, actual) |
| 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) |
| 76 | + |
| 77 | + self.assertEqual(expected, actual) |
| 78 | + |
| 79 | + |
| 80 | +class TestFactoryLicenseChoice(unittest.TestCase): |
| 81 | + |
| 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 | + |
| 89 | + self.assertEqual(expected, actual) |
| 90 | + |
| 91 | + def test_make_from_string_with_license(self) -> None: |
| 92 | + license_ = unittest.mock.NonCallableMock(spec=License) |
| 93 | + expected = LicenseChoice(license_=license_) |
| 94 | + license_factory = unittest.mock.MagicMock(spec=LicenseFactory) |
| 95 | + license_factory.make_from_string.return_value = license_ |
| 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 | + |
| 101 | + self.assertEqual(expected, actual) |
| 102 | + self.assertIs(license_, actual.license) |
| 103 | + license_factory.make_from_string.assert_called_once_with('foo', license_text=None, license_url=None) |
| 104 | + |
| 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 | + |
| 112 | + self.assertEqual(expected, actual) |
| 113 | + |
| 114 | + def test_make_with_compound_expression_raises(self) -> None: |
| 115 | + factory = LicenseChoiceFactory(license_factory=unittest.mock.MagicMock(spec=LicenseFactory)) |
| 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) |
| 123 | + license_ = unittest.mock.NonCallableMock(spec=License) |
| 124 | + expected = LicenseChoice(license_=license_) |
| 125 | + license_factory = unittest.mock.MagicMock(spec=LicenseFactory) |
| 126 | + license_factory.make_from_string.return_value = license_ |
| 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 | + |
| 132 | + self.assertEqual(expected, actual) |
| 133 | + self.assertIs(license_, actual.license) |
| 134 | + license_factory.make_from_string.assert_called_once_with('foo', license_text=text, license_url=url) |
0 commit comments