|
| 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 Any, Dict, Iterable, Tuple, Union |
| 19 | +from unittest import TestCase |
| 20 | + |
| 21 | +from cyclonedx.builder.this import this_component, this_tool |
| 22 | +from cyclonedx.model import ExternalReference, ExternalReferenceType |
| 23 | +from cyclonedx.model.component import ComponentType |
| 24 | +from cyclonedx.model.license import License, LicenseAcknowledgement |
| 25 | +from tests import load_pyproject |
| 26 | + |
| 27 | + |
| 28 | +class ExtRefsTestMixin: |
| 29 | + |
| 30 | + @staticmethod |
| 31 | + def __first_ers_uri(t: ExternalReferenceType, ers: Iterable[ExternalReference]) -> str: |
| 32 | + return next(filter(lambda r: r.type is t, ers)).url.uri |
| 33 | + |
| 34 | + def assertExtRefs( # noqa:N802 |
| 35 | + self: Union[TestCase, 'ExtRefsTestMixin'], |
| 36 | + p: Dict[str, Any], ers: Iterable[ExternalReference] |
| 37 | + ) -> None: |
| 38 | + self.assertEqual(p['tool']['poetry']['homepage'], self.__first_ers_uri( |
| 39 | + ExternalReferenceType.WEBSITE, ers)) |
| 40 | + self.assertEqual(p['tool']['poetry']['repository'], self.__first_ers_uri( |
| 41 | + ExternalReferenceType.VCS, ers)) |
| 42 | + self.assertEqual(p['tool']['poetry']['documentation'], self.__first_ers_uri( |
| 43 | + ExternalReferenceType.DOCUMENTATION, ers)) |
| 44 | + self.assertEqual(p['tool']['poetry']['urls']['Bug Tracker'], self.__first_ers_uri( |
| 45 | + ExternalReferenceType.ISSUE_TRACKER, ers)) |
| 46 | + |
| 47 | + |
| 48 | +class TestThisComponent(TestCase, ExtRefsTestMixin): |
| 49 | + def test_basics(self) -> None: |
| 50 | + p = load_pyproject() |
| 51 | + c = this_component() |
| 52 | + self.assertIs(ComponentType.LIBRARY, c.type) |
| 53 | + self.assertEqual('CycloneDX', c.group) |
| 54 | + self.assertEqual(p['tool']['poetry']['name'], c.name) |
| 55 | + self.assertEqual(p['tool']['poetry']['version'], c.version) |
| 56 | + self.assertEqual(p['tool']['poetry']['description'], c.description) |
| 57 | + |
| 58 | + def test_license(self) -> None: |
| 59 | + p = load_pyproject() |
| 60 | + ls: Tuple[License, ...] = tuple(this_component().licenses) |
| 61 | + self.assertEqual(1, len(ls)) |
| 62 | + l = ls[0] # noqa:E741 |
| 63 | + self.assertIs(LicenseAcknowledgement.DECLARED, l.acknowledgement) |
| 64 | + # this uses the fact that poetry expect license declarations as valid SPDX-license-id |
| 65 | + self.assertEqual(p['tool']['poetry']['license'], l.id) |
| 66 | + |
| 67 | + def test_extrefs(self) -> None: |
| 68 | + p = load_pyproject() |
| 69 | + ers: Tuple[ExternalReference, ...] = tuple(this_component().external_references) |
| 70 | + self.assertExtRefs(p, ers) |
| 71 | + |
| 72 | + |
| 73 | +class TestThisTool(TestCase, ExtRefsTestMixin): |
| 74 | + def test_basics(self) -> None: |
| 75 | + p = load_pyproject() |
| 76 | + t = this_tool() |
| 77 | + self.assertEqual('CycloneDX', t.vendor) |
| 78 | + self.assertEqual(p['tool']['poetry']['name'], t.name) |
| 79 | + self.assertEqual(p['tool']['poetry']['version'], t.version) |
| 80 | + |
| 81 | + def test_extrefs(self) -> None: |
| 82 | + p = load_pyproject() |
| 83 | + ers: Tuple[ExternalReference, ...] = tuple(this_tool().external_references) |
| 84 | + self.assertExtRefs(p, ers) |
0 commit comments