Skip to content

Commit cfc8cfc

Browse files
authored
Merge pull request #13 from heliocastro/add_license
Create and Update licenses
2 parents 1c89a14 + 054737e commit cfc8cfc

2 files changed

Lines changed: 169 additions & 6 deletions

File tree

sw360/license.py

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,81 @@
99
# SPDX-License-Identifier: MIT
1010
# -------------------------------------------------------------------------------
1111

12+
from typing import Any
13+
1214
import requests
1315

16+
from .sw360error import SW360Error
17+
1418

1519
class LicenseMixin:
16-
def download_license_info(self, project_id, filename, generator="XhtmlGenerator",
17-
variant="DISCLOSURE"):
20+
def create_new_license(
21+
self,
22+
shortName: str,
23+
fullName: str,
24+
text: str,
25+
checked: False,
26+
license_details={},
27+
) -> Any:
28+
"""Create a new component
29+
30+
API endpoint: POST /licenses
31+
32+
:param shortName: short license name. i.e "MIT"
33+
:param fullName: descriptive license name
34+
:param text: license text
35+
:param checked: if license is checked
36+
:type shortname: string
37+
:type fullname: string
38+
:type text: string
39+
:type checked: bool
40+
:return: SW360 result
41+
:rtype: JSON SW360 result object
42+
:raises SW360Error: if there is a negative HTTP response
43+
"""
44+
45+
url = self.url + "resource/api/licenses"
46+
47+
license_details["shortName"] = shortName
48+
license_details["fullName"] = fullName
49+
license_details["text"] = text
50+
license_details["checked"] = checked
51+
52+
response = requests.post(url, json=license_details, headers=self.api_headers)
53+
if response.ok:
54+
return response.json()
55+
56+
raise SW360Error(response, url)
57+
58+
def delete_license(self, license_shortname):
59+
"""Delete an existing license
60+
61+
API endpoint: PATCH /licenses
62+
63+
:param license_shortname: license shortname as the id
64+
:type license_shortname: string
65+
:return: SW360 result
66+
:rtype: JSON SW360 result object
67+
:raises SW360Error: if there is a negative HTTP response
68+
"""
69+
70+
if not license_shortname:
71+
raise SW360Error(message="No license shortname provided!")
72+
73+
url = self.url + "resource/api/licenses/" + license_shortname
74+
print(url)
75+
response = requests.delete(
76+
url, headers=self.api_headers,
77+
)
78+
79+
if response.ok:
80+
return True
81+
82+
raise SW360Error(response, url)
83+
84+
def download_license_info(
85+
self, project_id, filename, generator="XhtmlGenerator", variant="DISCLOSURE"
86+
):
1887
"""Gets the license information, aka Readme_OSS for the project
1988
with the given id
2089
@@ -27,8 +96,15 @@ def download_license_info(self, project_id, filename, generator="XhtmlGenerator"
2796
"""
2897
hdr = self.api_headers.copy()
2998
hdr["Accept"] = "application/*"
30-
url = self.url + "resource/api/projects/" + project_id + \
31-
"/licenseinfo?generatorClassName=" + generator + "&variant=" + variant
99+
url = (
100+
self.url
101+
+ "resource/api/projects/"
102+
+ project_id
103+
+ "/licenseinfo?generatorClassName="
104+
+ generator
105+
+ "&variant="
106+
+ variant
107+
)
32108
req = requests.get(url, allow_redirects=True, headers=hdr)
33109
open(filename, "wb").write(req.content)
34110

tests/test_sw360_licenses.py

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
import responses
1515

16-
sys.path.insert(1, "..")
16+
from sw360 import SW360, SW360Error
1717

18-
from sw360 import SW360 # noqa: E402
18+
sys.path.insert(1, "..")
1919

2020

2121
class Sw360TestLicenses(unittest.TestCase):
@@ -123,6 +123,93 @@ def test_get_license(self):
123123
self.assertEqual("Apache-2.0", license["shortName"])
124124
self.assertEqual("Apache License 2.0", license["fullName"])
125125

126+
@responses.activate
127+
def test_create_new_license(self):
128+
lib = SW360(self.MYURL, self.MYTOKEN, False)
129+
self._add_login_response()
130+
actual = lib.login_api()
131+
self.assertTrue(actual)
132+
133+
responses.add(
134+
responses.POST,
135+
url=f"{self.MYURL}resource/api/licenses",
136+
status=201,
137+
json={
138+
# server returns full license, here we only mock a part of it
139+
'shortName': 'LGPL-2.0-only',
140+
'fullName': 'GNU Library General Public License v2 only',
141+
'checked': True,
142+
'_links': {
143+
'self': {
144+
'href': f"{self.MYURL}resource/api/licenses/LGPL-2.0-only"
145+
}
146+
}
147+
},
148+
match=[responses.json_params_matcher({
149+
"shortName": "LGPL-2.0-only",
150+
"fullName": "GNU Library General Public License v2 only",
151+
"checked": True,
152+
"text": ""})]
153+
)
154+
lib.create_new_license(
155+
shortName="LGPL-2.0-only",
156+
fullName="GNU Library General Public License v2 only",
157+
text="",
158+
checked=True,
159+
)
160+
161+
@responses.activate
162+
def test_create_new_license_fail(self):
163+
lib = SW360(self.MYURL, self.MYTOKEN, False)
164+
self._add_login_response()
165+
actual = lib.login_api()
166+
self.assertTrue(actual)
167+
168+
responses.add(
169+
responses.POST,
170+
url=f"{self.MYURL}resource/api/licenses",
171+
status=403,
172+
json={
173+
# server returns full license, here we only mock a part of it
174+
'shortName': 'LGPL-2.0-only',
175+
'fullName': 'GNU Library General Public License v2 only',
176+
'checked': True,
177+
'_links': {
178+
'self': {
179+
'href': f"{self.MYURL}resource/api/licenses/LGPL-2.0-only"
180+
}
181+
}
182+
},
183+
match=[responses.json_params_matcher({
184+
"shortName": "LGPL-2.0-only",
185+
"fullName": "GNU Library General Public License v2 only",
186+
"checked": True,
187+
"text": ""})]
188+
)
189+
190+
with self.assertRaises(SW360Error) as context:
191+
lib.create_new_license(
192+
shortName="LGPL-2.0-only",
193+
fullName="GNU Library General Public License v2 only",
194+
text="",
195+
checked=True,
196+
)
197+
self.assertEqual(403, context.exception.response.status_code)
198+
199+
@responses.activate
200+
def test_delete_license(self):
201+
lib = SW360(self.MYURL, self.MYTOKEN, False)
202+
self._add_login_response()
203+
actual = lib.login_api()
204+
self.assertTrue(actual)
205+
206+
responses.add(
207+
responses.DELETE,
208+
url=self.MYURL + "resource/api/licenses/LGPL-2.0-only",
209+
status=201)
210+
211+
lib.delete_license("LGPL-2.0-only")
212+
126213

127214
if __name__ == "__main__":
128215
unittest.main()

0 commit comments

Comments
 (0)