|
| 1 | +# This file is part of CycloneDX Python Lib |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# SPDX-License-Identifier: Apache-2.0 |
| 16 | +# Copyright (c) OWASP Foundation. All Rights Reserved. |
| 17 | + |
| 18 | +from typing import Tuple, Iterable, Any, Dict |
| 19 | +from unittest import TestCase |
| 20 | + |
| 21 | +from cyclonedx.builder.this import this_tool, this_component |
| 22 | +from cyclonedx.model.component import ComponentType |
| 23 | +from cyclonedx.model.license import License, LicenseAcknowledgement |
| 24 | +from cyclonedx.model import ExternalReference, ExternalReferenceType |
| 25 | + |
| 26 | +from tests import load_pyproject |
| 27 | + |
| 28 | + |
| 29 | +class TestThisBase(TestCase): |
| 30 | + |
| 31 | + @staticmethod |
| 32 | + def __first_ers_uri(t: ExternalReferenceType, ers: Iterable[ExternalReference]): |
| 33 | + return next(filter(lambda r: r.type is t, ers)).url.uri |
| 34 | + |
| 35 | + def assertExtRefs(self, p : Dict[str, Any], ers: Iterable[ExternalReference]) -> None: |
| 36 | + self.assertEqual(p['tool']['poetry']['homepage'], self.__first_ers_uri( |
| 37 | + ExternalReferenceType.WEBSITE, ers)) |
| 38 | + self.assertEqual(p['tool']['poetry']['repository'], self.__first_ers_uri( |
| 39 | + ExternalReferenceType.VCS, ers)) |
| 40 | + self.assertEqual(p['tool']['poetry']['documentation'], self.__first_ers_uri( |
| 41 | + ExternalReferenceType.DOCUMENTATION, ers)) |
| 42 | + |
| 43 | + |
| 44 | +class TestThisComponent(TestThisBase): |
| 45 | + def test_basics(self) -> None: |
| 46 | + p = load_pyproject() |
| 47 | + c = this_component() |
| 48 | + self.assertIs(ComponentType.LIBRARY, c.type) |
| 49 | + self.assertEqual('CycloneDX', c.group) |
| 50 | + self.assertEqual(p['tool']['poetry']['name'], c.name) |
| 51 | + self.assertEqual(p['tool']['poetry']['version'], c.version) |
| 52 | + self.assertEqual(p['tool']['poetry']['description'], c.description) |
| 53 | + |
| 54 | + def test_license(self) -> None: |
| 55 | + p = load_pyproject() |
| 56 | + ls: Tuple[License, ...] = tuple(this_component().licenses) |
| 57 | + self.assertEqual(1, len(ls)) |
| 58 | + l = ls[0] |
| 59 | + self.assertIs(LicenseAcknowledgement.DECLARED, l.acknowledgement) |
| 60 | + # this uses the fact that poetry expect license declarations as valid SPDX-license-id |
| 61 | + self.assertEqual(p['tool']['poetry']['license'], l.id) |
| 62 | + |
| 63 | + def test_extrefs(self) -> None: |
| 64 | + p = load_pyproject() |
| 65 | + ers: Tuple[ExternalReference, ...] = tuple(this_component().external_references) |
| 66 | + self.assertExtRefs(p, ers) |
| 67 | + |
| 68 | + |
| 69 | +class TestThisTool(TestThisBase): |
| 70 | + def test_basics(self) -> None: |
| 71 | + p = load_pyproject() |
| 72 | + t = this_tool() |
| 73 | + self.assertEqual('CycloneDX', t.vendor) |
| 74 | + self.assertEqual(p['tool']['poetry']['name'], t.name) |
| 75 | + self.assertEqual(p['tool']['poetry']['version'], t.version) |
| 76 | + |
| 77 | + def test_extrefs(self) -> None: |
| 78 | + p = load_pyproject() |
| 79 | + ers: Tuple[ExternalReference, ...] = tuple(this_tool().external_references) |
| 80 | + self.assertExtRefs(p, ers) |
0 commit comments