-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathtest_model_license.py
More file actions
135 lines (114 loc) · 5.03 KB
/
test_model_license.py
File metadata and controls
135 lines (114 loc) · 5.03 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# This file is part of CycloneDX Python Library
#
# 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 random import shuffle
from unittest import TestCase
from unittest.mock import MagicMock
from cyclonedx.exception.model import MutuallyExclusivePropertiesException
from cyclonedx.model import AttachedText, Property, XsUri
from cyclonedx.model.license import DisjunctiveLicense, LicenseExpression
from tests import reorder
class TestModelDisjunctiveLicense(TestCase):
def test_create_complete_id(self) -> None:
text = MagicMock(spec=AttachedText)
url = MagicMock(spec=XsUri)
license = DisjunctiveLicense(id='foo', text=text, url=url)
self.assertEqual('foo', license.id)
self.assertIsNone(license.name)
self.assertIs(text, license.text)
self.assertIs(url, license.url)
def test_update_id_name(self) -> None:
license = DisjunctiveLicense(id='foo')
self.assertEqual('foo', license.id)
self.assertIsNone(license.name)
license.name = 'bar'
self.assertIsNone(license.id)
self.assertEqual('bar', license.name)
def test_create_complete_named(self) -> None:
text = MagicMock(spec=AttachedText)
url = MagicMock(spec=XsUri)
license = DisjunctiveLicense(name='foo', text=text, url=url)
self.assertIsNone(license.id)
self.assertEqual('foo', license.name)
self.assertIs(text, license.text)
self.assertIs(url, license.url)
def test_update_name_id(self) -> None:
license = DisjunctiveLicense(name='foo')
self.assertEqual('foo', license.name)
self.assertIsNone(license.id)
license.id = 'bar'
self.assertIsNone(license.name)
self.assertEqual('bar', license.id)
def test_throws_when_no_id_nor_name(self) -> None:
with self.assertRaises(MutuallyExclusivePropertiesException):
DisjunctiveLicense(id=None, name=None)
def test_prefers_id_over_name(self) -> None:
with self.assertWarnsRegex(
RuntimeWarning,
'Both `id` and `name` have been supplied - `name` will be ignored!'):
license = DisjunctiveLicense(id='foo', name='bar')
self.assertEqual('foo', license.id)
self.assertEqual(None, license.name)
def test_equal(self) -> None:
a = DisjunctiveLicense(id='foo', name='bar')
b = DisjunctiveLicense(id='foo', name='bar')
c = DisjunctiveLicense(id='bar', name='foo')
self.assertEqual(a, b)
self.assertNotEqual(a, c)
self.assertNotEqual(a, 'foo')
def test_create_with_properties(self) -> None:
properties = [Property(name='key1', value='value1')]
license = DisjunctiveLicense(id='MIT', properties=properties)
self.assertEqual(1, len(license.properties))
def test_set_properties(self) -> None:
license = DisjunctiveLicense(id='MIT')
self.assertEqual(0, len(license.properties))
license.properties = [Property(name='key1', value='value1')]
self.assertEqual(1, len(license.properties))
def test_equal_with_properties(self) -> None:
a = DisjunctiveLicense(id='MIT', properties=[Property(name='key1', value='value1')])
b = DisjunctiveLicense(id='MIT', properties=[Property(name='key1', value='value1')])
c = DisjunctiveLicense(id='MIT')
self.assertEqual(a, b)
self.assertNotEqual(a, c)
class TestModelLicenseExpression(TestCase):
def test_create(self) -> None:
license = LicenseExpression('foo')
self.assertEqual('foo', license.value)
def test_update(self) -> None:
license = LicenseExpression('foo')
self.assertEqual('foo', license.value)
license.value = 'bar'
self.assertEqual('bar', license.value)
def test_equal(self) -> None:
a = LicenseExpression('foo')
b = LicenseExpression('foo')
c = LicenseExpression('bar')
self.assertEqual(a, b)
self.assertNotEqual(a, c)
self.assertNotEqual(a, 'foo')
class TestModelLicense(TestCase):
def test_sort_mixed(self) -> None:
expected_order = [1, 2, 0]
licenses = [
DisjunctiveLicense(name='my license'),
LicenseExpression(value='MIT or Apache-2.0'),
DisjunctiveLicense(id='MIT'),
]
expected_licenses = reorder(licenses, expected_order)
shuffle(licenses)
sorted_licenses = sorted(licenses)
self.assertListEqual(sorted_licenses, expected_licenses)