Skip to content

Commit bd5b534

Browse files
committed
test: have more test coverage
1 parent 4b6ea3c commit bd5b534

6 files changed

Lines changed: 740 additions & 22 deletions

File tree

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* packages REST API calls implemented.
1111
* unit test `test_login_failed_invalid_url` disabled because it delays all tests.
1212
* have unit tests for packages.
13+
* have more test coverage.
1314

1415
## V1.5.1
1516

tests/test_sw360_base.py

Lines changed: 106 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -------------------------------------------------------------------------------
2-
# Copyright (c) 2019-2020 Siemens
2+
# Copyright (c) 2019-2024 Siemens
33
# All Rights Reserved.
44
# Author: thomas.graf@siemens.com
55
#
@@ -14,8 +14,11 @@
1414

1515
import responses
1616

17+
from sw360.base import BaseMixin
18+
1719
sys.path.insert(1, "..")
1820

21+
1922
from sw360 import SW360, SW360Error # noqa: E402
2023

2124

@@ -41,6 +44,23 @@ def test_constructor(self) -> None:
4144
lib = SW360("https://my.server.com", self.MYTOKEN, False)
4245
self.assertEqual(self.MYURL, lib.url)
4346

47+
def test_BaseMixin_constructor(self) -> None:
48+
lib = BaseMixin(self.MYURL, self.MYTOKEN, False)
49+
self.assertEqual(self.MYURL, lib.url)
50+
51+
expected_header = {"Authorization": "Token " + self.MYTOKEN}
52+
self.assertEqual(expected_header, lib.api_headers)
53+
54+
lib = BaseMixin(self.MYURL, self.MYTOKEN, True)
55+
self.assertEqual(self.MYURL, lib.url)
56+
57+
expected_header = {"Authorization": "Bearer " + self.MYTOKEN}
58+
self.assertEqual(expected_header, lib.api_headers)
59+
60+
# check for trailing slash
61+
lib = BaseMixin("https://my.server.com", self.MYTOKEN, False)
62+
self.assertEqual(self.MYURL, lib.url)
63+
4464
@responses.activate
4565
def test_login(self) -> None:
4666
lib = SW360(self.MYURL, self.MYTOKEN, False)
@@ -56,22 +76,22 @@ def test_login(self) -> None:
5676
actual = lib.login_api()
5777
self.assertTrue(actual)
5878

59-
def x_test_login_failed_invalid_url(self) -> None:
60-
lib = SW360(self.MYURL, self.MYTOKEN, False)
79+
# def test_login_failed_invalid_url(self) -> None:
80+
# lib = SW360(self.MYURL, self.MYTOKEN, False)
6181

62-
have_backup = False
63-
if "SW360ProductionToken" in os.environ:
64-
have_backup = True
65-
backup = os.environ["SW360ProductionToken"]
66-
os.environ["SW360ProductionToken"] = ""
82+
# have_backup = False
83+
# if "SW360ProductionToken" in os.environ:
84+
# have_backup = True
85+
# backup = os.environ["SW360ProductionToken"]
86+
# os.environ["SW360ProductionToken"] = ""
6787

68-
with self.assertRaises(SW360Error) as context:
69-
lib.login_api()
88+
# with self.assertRaises(SW360Error) as context:
89+
# lib.login_api()
7090

71-
self.assertTrue(context.exception.message.startswith(self.ERROR_MSG_NO_LOGIN))
91+
# self.assertTrue(context.exception.message.startswith(self.ERROR_MSG_NO_LOGIN))
7292

73-
if have_backup:
74-
os.environ["SW360ProductionToken"] = backup
93+
# if have_backup:
94+
# os.environ["SW360ProductionToken"] = backup
7595

7696
@responses.activate
7797
def test_login_failed_not_authorized(self) -> None:
@@ -156,24 +176,49 @@ def test_dump_rest_call_to_file(self) -> None:
156176

157177
lib.api_get_raw(self.MYURL + "resource/api/projects/123X")
158178

179+
@responses.activate
180+
def test_api_get_no_content_no_session(self) -> None:
181+
lib = SW360(self.MYURL, self.MYTOKEN, False)
182+
lib.force_no_session = True
183+
184+
responses.add(
185+
responses.GET,
186+
url=self.MYURL + "resource/api/",
187+
body="{'status': 'ok'}",
188+
status=200,
189+
content_type="application/json",
190+
adding_headers={"Authorization": "Token " + self.MYTOKEN},
191+
)
192+
actual = lib.login_api()
193+
self.assertTrue(actual)
194+
195+
responses.add(
196+
responses.GET,
197+
url=self.MYURL + "resource/api/projects/123",
198+
# body must be empty!
199+
status=204,
200+
content_type="application/json",
201+
adding_headers={"Authorization": "Token " + self.MYTOKEN},
202+
)
203+
204+
rx = lib.get_project("123")
205+
self.assertIsNone(rx)
206+
159207
@responses.activate
160208
def test_api_get_no_content(self) -> None:
161209
lib = self.get_logged_in_lib()
162210

163211
responses.add(
164212
responses.GET,
165213
url=self.MYURL + "resource/api/projects/123X",
166-
body='{"name": "My Testproject"}',
214+
# body must be empty!
167215
status=204,
168216
content_type="application/json",
169217
adding_headers={"Authorization": "Token " + self.MYTOKEN},
170218
)
171219

172-
try:
173-
result = lib.api_get(self.MYURL + "resource/api/projects/123X")
174-
self.assertIsNone(result)
175-
except Exception:
176-
self.assertIsNone(None)
220+
result = lib.api_get(self.MYURL + "resource/api/projects/123X")
221+
self.assertIsNone(result)
177222

178223
@responses.activate
179224
def test_api_get_raw_not_logged_in(self) -> None:
@@ -317,6 +362,47 @@ def test_login_server_not_responding(self) -> None:
317362

318363
self.assertEqual(self.ERROR_MSG_NO_LOGIN, context.exception.message)
319364

365+
@responses.activate
366+
def test_api_post_not_logged_in(self) -> None:
367+
lib = SW360(self.MYURL, self.MYTOKEN, False, None)
368+
lib.force_no_session = False
369+
370+
with self.assertRaises(SW360Error) as context:
371+
lib.api_post(self.MYURL + "resource/api/projects/123X")
372+
373+
self.assertEqual("login_api needs to be called first", context.exception.message)
374+
375+
@responses.activate
376+
def test_api_post_multipart_not_logged_in(self) -> None:
377+
lib = SW360(self.MYURL, self.MYTOKEN, False, None)
378+
lib.force_no_session = False
379+
380+
with self.assertRaises(SW360Error) as context:
381+
lib.api_post_multipart(self.MYURL + "resource/api/projects/123X")
382+
383+
self.assertEqual("login_api needs to be called first", context.exception.message)
384+
385+
@responses.activate
386+
def test_api_patch_not_logged_in(self) -> None:
387+
lib = SW360(self.MYURL, self.MYTOKEN, False, None)
388+
lib.force_no_session = False
389+
390+
with self.assertRaises(SW360Error) as context:
391+
lib.api_patch(self.MYURL + "resource/api/projects/123X")
392+
393+
self.assertEqual("login_api needs to be called first", context.exception.message)
394+
395+
@responses.activate
396+
def test_api_delete_not_logged_in(self) -> None:
397+
lib = SW360(self.MYURL, self.MYTOKEN, False, None)
398+
lib.force_no_session = False
399+
400+
with self.assertRaises(SW360Error) as context:
401+
lib.api_delete(self.MYURL + "resource/api/projects/123X")
402+
403+
self.assertEqual("login_api needs to be called first", context.exception.message)
404+
320405

321406
if __name__ == "__main__":
322-
unittest.main()
407+
APP = Sw360Test()
408+
APP.test_api_post_multipart_not_logged_in()

tests/test_sw360_components.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,94 @@ def test_get_all_components_with_fields_and_paging(self) -> None:
141141
self.assertEqual("Tethys.Logging", components[0]["name"])
142142
self.assertEqual("DE", components[0]["ownerCountry"])
143143

144+
@responses.activate
145+
def test_get_all_components_with_all_details(self) -> None:
146+
lib = SW360(self.MYURL, self.MYTOKEN, False)
147+
lib.force_no_session = True
148+
self._add_login_response()
149+
actual = lib.login_api()
150+
self.assertTrue(actual)
151+
152+
responses.add(
153+
method=responses.GET,
154+
url=self.MYURL + "resource/api/components?allDetails=true", # noqa
155+
body='{"_embedded": {"sw360:components": [{"name": "Tethys.Logging", "ownerCountry": "DE", "componentType": "OSS", "externalIds": {"package-url": "pkg:nuget/Tethys.Logging"}}]}}', # noqa
156+
status=200,
157+
content_type="application/json",
158+
adding_headers={"Authorization": "Token " + self.MYTOKEN},
159+
)
160+
161+
components = lib.get_all_components(all_details=True)
162+
self.assertIsNotNone(components)
163+
self.assertTrue(len(components) > 0)
164+
self.assertEqual("Tethys.Logging", components[0]["name"])
165+
self.assertEqual("DE", components[0]["ownerCountry"])
166+
167+
@responses.activate
168+
def test_get_all_components_with_all_details_and_sorting(self) -> None:
169+
lib = SW360(self.MYURL, self.MYTOKEN, False)
170+
lib.force_no_session = True
171+
self._add_login_response()
172+
actual = lib.login_api()
173+
self.assertTrue(actual)
174+
175+
responses.add(
176+
method=responses.GET,
177+
url=self.MYURL + "resource/api/components?allDetails=true&sort=name%2Cdesc", # noqa
178+
body='{"_embedded": {"sw360:components": [{"name": "Tethys.Logging", "ownerCountry": "DE", "componentType": "OSS", "externalIds": {"package-url": "pkg:nuget/Tethys.Logging"}}]}}', # noqa
179+
status=200,
180+
content_type="application/json",
181+
adding_headers={"Authorization": "Token " + self.MYTOKEN},
182+
)
183+
184+
components = lib.get_all_components(all_details=True, sort="name,desc")
185+
self.assertIsNotNone(components)
186+
self.assertTrue(len(components) > 0)
187+
self.assertEqual("Tethys.Logging", components[0]["name"])
188+
self.assertEqual("DE", components[0]["ownerCountry"])
189+
190+
@responses.activate
191+
def test_get_all_components_invalid_reply(self) -> None:
192+
lib = SW360(self.MYURL, self.MYTOKEN, False)
193+
lib.force_no_session = True
194+
self._add_login_response()
195+
actual = lib.login_api()
196+
self.assertTrue(actual)
197+
198+
responses.add(
199+
method=responses.GET,
200+
url=self.MYURL + "resource/api/components", # noqa
201+
body='{"_xxembedded": {"sw360:components": [{"name": "Tethys.Logging", "ownerCountry": "DE", "componentType": "OSS", "externalIds": {"package-url": "pkg:nuget/Tethys.Logging"}}]}}', # noqa
202+
status=200,
203+
content_type="application/json",
204+
adding_headers={"Authorization": "Token " + self.MYTOKEN},
205+
)
206+
207+
components = lib.get_all_components(all_details=True, sort="name,desc")
208+
self.assertIsNotNone(components)
209+
self.assertTrue(len(components) == 0)
210+
211+
@responses.activate
212+
def test_get_all_components_invalid_reply2(self) -> None:
213+
lib = SW360(self.MYURL, self.MYTOKEN, False)
214+
lib.force_no_session = True
215+
self._add_login_response()
216+
actual = lib.login_api()
217+
self.assertTrue(actual)
218+
219+
responses.add(
220+
method=responses.GET,
221+
url=self.MYURL + "resource/api/components", # noqa
222+
body='{"_embedded": {"xxsw360:components": [{"name": "Tethys.Logging", "ownerCountry": "DE", "componentType": "OSS", "externalIds": {"package-url": "pkg:nuget/Tethys.Logging"}}]}}', # noqa
223+
status=200,
224+
content_type="application/json",
225+
adding_headers={"Authorization": "Token " + self.MYTOKEN},
226+
)
227+
228+
components = lib.get_all_components(all_details=True, sort="name,desc")
229+
self.assertIsNotNone(components)
230+
self.assertTrue(len(components) == 0)
231+
144232
@responses.activate
145233
def test_get_all_components_by_type(self) -> None:
146234
lib = SW360(self.MYURL, self.MYTOKEN, False)
@@ -184,6 +272,48 @@ def test_get_all_components_by_type_no_result(self) -> None:
184272
components = lib.get_components_by_type("OSS")
185273
self.assertEqual([], components)
186274

275+
@responses.activate
276+
def test_get_all_components_by_type_invalid_reply(self) -> None:
277+
lib = SW360(self.MYURL, self.MYTOKEN, False)
278+
lib.force_no_session = True
279+
self._add_login_response()
280+
actual = lib.login_api()
281+
self.assertTrue(actual)
282+
283+
responses.add(
284+
method=responses.GET,
285+
url=self.MYURL + "resource/api/components?type=OSS",
286+
body='{"_xxembedded": {"sw360:components": [{"name": "Tethys.Logging", "ownerCountry": "DE", "componentType": "OSS", "externalIds": {"package-url": "pkg:nuget/Tethys.Logging"}}]}}', # noqa
287+
status=200,
288+
content_type="application/json",
289+
adding_headers={"Authorization": "Token " + self.MYTOKEN},
290+
)
291+
292+
components = lib.get_components_by_type("OSS")
293+
self.assertIsNotNone(components)
294+
self.assertTrue(len(components) == 0)
295+
296+
@responses.activate
297+
def test_get_all_components_by_type_invalid_reply2(self) -> None:
298+
lib = SW360(self.MYURL, self.MYTOKEN, False)
299+
lib.force_no_session = True
300+
self._add_login_response()
301+
actual = lib.login_api()
302+
self.assertTrue(actual)
303+
304+
responses.add(
305+
method=responses.GET,
306+
url=self.MYURL + "resource/api/components?type=OSS",
307+
body='{"_embedded": {"xxsw360:components": [{"name": "Tethys.Logging", "ownerCountry": "DE", "componentType": "OSS", "externalIds": {"package-url": "pkg:nuget/Tethys.Logging"}}]}}', # noqa
308+
status=200,
309+
content_type="application/json",
310+
adding_headers={"Authorization": "Token " + self.MYTOKEN},
311+
)
312+
313+
components = lib.get_components_by_type("OSS")
314+
self.assertIsNotNone(components)
315+
self.assertTrue(len(components) == 0)
316+
187317
@responses.activate
188318
def test_get_component(self) -> None:
189319
lib = SW360(self.MYURL, self.MYTOKEN, False)
@@ -291,6 +421,27 @@ def test_get_components_by_external_id_full_answer(self) -> None:
291421
self.assertTrue(len(components) > 0)
292422
self.assertEqual("Tethys.Logging", components[0]["name"])
293423

424+
@responses.activate
425+
def test_get_components_by_external_id_invalid_answer(self) -> None:
426+
lib = SW360(self.MYURL, self.MYTOKEN, False)
427+
lib.force_no_session = True
428+
self._add_login_response()
429+
actual = lib.login_api()
430+
self.assertTrue(actual)
431+
432+
responses.add(
433+
method=responses.GET,
434+
url=self.MYURL + "resource/api/components/searchByExternalIds?package-url=pkg:nuget/Tethys.Logging", # noqa
435+
body='{"_xxembedded":{"sw360:components" :[{"name": "Tethys.Logging", "componentType": "OSS", "externalIds": {"package-url": "pkg:nuget/Tethys.Logging"}}]}}', # noqa
436+
status=200,
437+
content_type="application/json",
438+
adding_headers={"Authorization": "Token " + self.MYTOKEN},
439+
)
440+
441+
components = lib.get_components_by_external_id("package-url", "pkg:nuget/Tethys.Logging")
442+
self.assertIsNotNone(components)
443+
self.assertTrue(len(components) == 0)
444+
294445
@responses.activate
295446
def test_update_component_external_id_add_fresh_id(self) -> None:
296447
lib = SW360(self.MYURL, self.MYTOKEN, False)

0 commit comments

Comments
 (0)