diff --git a/pyproject.toml b/pyproject.toml index a32e3d9..bff1957 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -47,3 +47,22 @@ mypy_path = "stubs" exclude = [ '/tests', ] +show_error_codes = "True" +pretty = "True" + +warn_unreachable = "True" +allow_redefinition = "False" + +### Strict mode ### +warn_unused_configs = "True" +disallow_subclassing_any = "True" +disallow_any_generics = "True" +disallow_untyped_calls = "True" +disallow_untyped_defs = "True" +disallow_incomplete_defs = "True" +check_untyped_defs = "True" +disallow_untyped_decorators = "True" +no_implicit_optional = "True" +warn_redundant_casts = "True" +warn_unused_ignores = "True" +no_implicit_reexport = "True" diff --git a/stubs/responses.pyi b/stubs/responses.pyi index 0c63cd8..8979d06 100644 --- a/stubs/responses.pyi +++ b/stubs/responses.pyi @@ -6,7 +6,7 @@ from io import BufferedReader, BytesIO from re import Pattern from typing import (TYPE_CHECKING, Any, Callable, Dict, Iterable, Iterator, List, Mapping, NamedTuple, Optional, Sequence, Sized, - Tuple, Type, Union) + Tuple, Type, TypeVar, Union) from requests.adapters import HTTPAdapter # from urllib3.response import HTTPHeaderDict @@ -59,6 +59,13 @@ _real_send = HTTPAdapter.send _UNSET = object() +F = TypeVar('F', bound=Callable[..., Any]) + + +def activate(func: F) -> F: + ... + + class FalseBool: def __bool__(self) -> bool: ... diff --git a/sw360/attachments.py b/sw360/attachments.py index b3a61d5..0d08d3a 100644 --- a/sw360/attachments.py +++ b/sw360/attachments.py @@ -25,7 +25,7 @@ class AttachmentsMixin(BaseMixin): - def get_attachment_infos_by_hash(self, hashvalue) -> Optional[Dict[str, Any]]: + def get_attachment_infos_by_hash(self, hashvalue: str) -> Optional[Dict[str, Any]]: """Get information about attachments with a given sha1 hash value. This usually returns zero or one result, but if the same binary file @@ -202,7 +202,7 @@ def _upload_resource_attachment(self, resource_type: str, resource_id: str, uplo if resource_type not in ("releases", "components", "projects"): raise SW360Error(message="Invalid resource type provided!") - if type(resource_id) is not str: + if (type(resource_id) is not str) or (resource_id == ""): raise SW360Error(message="Invalid resource id provided!") filename = os.path.basename(upload_file) diff --git a/sw360/components.py b/sw360/components.py index f92da0d..db73022 100644 --- a/sw360/components.py +++ b/sw360/components.py @@ -20,7 +20,7 @@ class ComponentsMixin(BaseMixin): # return type List[Dict[str, Any]] | Optional[Dict[str, Any]] for Python 3.11 is good, # Union[List[Dict[str, Any]], Optional[Dict[str, Any]]] for lower Python versions is not good - def get_all_components(self, fields=None, page=-1, page_size=-1, + def get_all_components(self, fields: str = "", page: int = -1, page_size: int = -1, all_details: bool = False, sort: str = "") -> Any: """Get information of about all components diff --git a/sw360/license.py b/sw360/license.py index d865687..fd24d38 100644 --- a/sw360/license.py +++ b/sw360/license.py @@ -24,7 +24,7 @@ def create_new_license( fullName: str, text: str, checked: bool = False, - license_details={}, + license_details: Dict[str, Any] = {}, ) -> Any: """Create a new license diff --git a/sw360/project.py b/sw360/project.py index 3cdb31b..7565949 100644 --- a/sw360/project.py +++ b/sw360/project.py @@ -272,7 +272,7 @@ def get_project_vulnerabilities(self, project_id: str) -> Optional[Dict[str, Any return resp - def create_new_project(self, name: str, project_type: str, visibility, + def create_new_project(self, name: str, project_type: str, visibility: Any, description: str = "", version: str = "", project_details: Dict[str, Any] = {}) -> Optional[Dict[str, Any]]: """Create a new project. @@ -352,7 +352,7 @@ def update_project(self, project: Dict[str, Any], project_id: str, raise SW360Error(response, url) - def update_project_releases(self, releases, project_id: str, add: bool = False) -> bool: + def update_project_releases(self, releases: List[Dict[str, Any]], project_id: str, add: bool = False) -> bool: """Update the releases of an existing project. If `add` is True, given `releases` are added to the project, otherwise, the existing releases will be replaced. diff --git a/tests/test_sw360_attachments.py b/tests/test_sw360_attachments.py index dcce867..01dc162 100644 --- a/tests/test_sw360_attachments.py +++ b/tests/test_sw360_attachments.py @@ -10,6 +10,7 @@ import os import sys import tempfile +from typing import Any import unittest import warnings from unittest.mock import MagicMock, patch @@ -26,10 +27,10 @@ class Sw360TestAttachments(unittest.TestCase): MYURL = "https://my.server.com/" ERROR_MSG_NO_LOGIN = "Unable to login" - def setUp(self): + def setUp(self) -> None: warnings.simplefilter("ignore", ResourceWarning) - def _add_login_response(self): + def _add_login_response(self) -> None: """ Add the response for a successfull login. """ @@ -42,18 +43,18 @@ def _add_login_response(self): adding_headers={"Authorization": "Token " + self.MYTOKEN}, ) - def _my_matcher(self): + def _my_matcher(self) -> Any: """ Helper method to display the JSON parameters of a REST call. """ - def display_json_params(request_body): + def display_json_params(request_body: Any) -> bool: print("MyMatcher:'" + request_body + "'") return True return display_json_params @responses.activate - def test_get_attachment_infos_by_hash(self): + def test_get_attachment_infos_by_hash(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -71,14 +72,15 @@ def test_get_attachment_infos_by_hash(self): data = lib.get_attachment_infos_by_hash("5f392efeb0934339fb6b0f3e021076db19fad164") self.assertIsNotNone(data) - self.assertTrue("_embedded" in data) - self.assertTrue("sw360:attachments" in data["_embedded"]) - att_info = data["_embedded"]["sw360:attachments"] - self.assertTrue(len(att_info) > 0) - self.assertEqual("5f392efeb0934339fb6b0f3e021076db19fad164", att_info[0]["sha1"]) + if data: # only for mypy + self.assertTrue("_embedded" in data) + self.assertTrue("sw360:attachments" in data["_embedded"]) + att_info = data["_embedded"]["sw360:attachments"] + self.assertTrue(len(att_info) > 0) + self.assertEqual("5f392efeb0934339fb6b0f3e021076db19fad164", att_info[0]["sha1"]) @responses.activate - def test_get_attachment_infos_for_release(self): + def test_get_attachment_infos_for_release(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -100,7 +102,7 @@ def test_get_attachment_infos_for_release(self): self.assertEqual("ABCD", attachments[0]["sha1"]) @responses.activate - def test_get_attachment_infos_for_component(self): + def test_get_attachment_infos_for_component(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -121,7 +123,7 @@ def test_get_attachment_infos_for_component(self): self.assertTrue(len(attachments) > 0) @responses.activate - def test_get_attachment_infos_for_project(self): + def test_get_attachment_infos_for_project(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -142,7 +144,7 @@ def test_get_attachment_infos_for_project(self): self.assertTrue(len(attachments) > 0) @responses.activate - def test_get_attachment_by_url(self): + def test_get_attachment_by_url(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -160,10 +162,11 @@ def test_get_attachment_by_url(self): attachments = lib.get_attachment_by_url(self.MYURL + "resource/api/releases/1234/attachments/5678") # noqa self.assertIsNotNone(attachments) - self.assertTrue(len(attachments) > 0) + if attachments: # only for mypy + self.assertTrue(len(attachments) > 0) @responses.activate - def test_download_release_attachment_no_resource_id(self): + def test_download_release_attachment_no_resource_id(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -171,12 +174,12 @@ def test_download_release_attachment_no_resource_id(self): self.assertTrue(actual) with self.assertRaises(SW360Error) as context: - lib.download_release_attachment("myfile.txt", None, "5678") + lib.download_release_attachment("myfile.txt", "", "5678") self.assertEqual("No resource id provided!", context.exception.message) @responses.activate - def test_download_release_attachment_no_attachment_id(self): + def test_download_release_attachment_no_attachment_id(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -184,12 +187,12 @@ def test_download_release_attachment_no_attachment_id(self): self.assertTrue(actual) with self.assertRaises(SW360Error) as context: - lib.download_release_attachment("myfile.txt", "1234", None) + lib.download_release_attachment("myfile.txt", "1234", "") self.assertEqual("No attachment id provided!", context.exception.message) @responses.activate - def test_download_release_attachment(self): + def test_download_release_attachment(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -217,7 +220,7 @@ def test_download_release_attachment(self): os.removedirs(tmpdir) @responses.activate - def test_download_release_attachment_404(self): + def test_download_release_attachment_404(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -243,12 +246,20 @@ def test_download_release_attachment_404(self): with self.assertRaises(SW360Error) as context: lib.download_release_attachment(filename, "1234", "5678") self.assertFalse(os.path.exists(filename)) + + if not context.exception: + self.assertTrue(False, "no exception") + self.assertEqual(context.exception.url, url) - self.assertEqual(context.exception.response.status_code, 404) + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(context.exception.response.status_code, 404) + os.removedirs(tmpdir) @responses.activate - def test_download_project_attachment(self): + def test_download_project_attachment(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -276,7 +287,7 @@ def test_download_project_attachment(self): os.removedirs(tmpdir) @responses.activate - def test_download_component_attachment(self): + def test_download_component_attachment(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -304,7 +315,7 @@ def test_download_component_attachment(self): os.removedirs(tmpdir) @responses.activate - def test_get_attachment(self): + def test_get_attachment(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -322,10 +333,11 @@ def test_get_attachment(self): attachments = lib.get_attachment("1234") self.assertIsNotNone(attachments) - self.assertTrue(len(attachments) > 0) + if attachments: # only for mypy + self.assertTrue(len(attachments) > 0) @responses.activate - def test_upload_resource_attachment_no_resource_type(self): + def test_upload_resource_attachment_no_resource_type(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -335,7 +347,7 @@ def test_upload_resource_attachment_no_resource_type(self): _, filename = tempfile.mkstemp() self.assertTrue(os.path.exists(filename)) with self.assertRaises(SW360Error) as context: - lib._upload_resource_attachment(None, "123", filename) + lib._upload_resource_attachment("", "123", filename) self.assertTrue(context.exception.message.startswith("Invalid resource type provided!")) try: @@ -345,7 +357,7 @@ def test_upload_resource_attachment_no_resource_type(self): pass @responses.activate - def test_upload_attachment_file_does_not_exist(self): + def test_upload_attachment_file_does_not_exist(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -360,7 +372,7 @@ def test_upload_attachment_file_does_not_exist(self): self.assertTrue(context.exception.message.startswith("ERROR: file not found:")) @responses.activate - def test_upload_release_attachment_no_release_id(self): + def test_upload_release_attachment_no_release_id(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -370,7 +382,7 @@ def test_upload_release_attachment_no_release_id(self): _, filename = tempfile.mkstemp() self.assertTrue(os.path.exists(filename)) with self.assertRaises(SW360Error) as context: - lib.upload_release_attachment(None, filename) + lib.upload_release_attachment("", filename) self.assertTrue(context.exception.message.startswith("Invalid resource id provided!")) try: @@ -380,7 +392,7 @@ def test_upload_release_attachment_no_release_id(self): pass @responses.activate - def test_upload_release_attachment(self): + def test_upload_release_attachment(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -406,7 +418,7 @@ def test_upload_release_attachment(self): pass @responses.activate - def test_upload_release_attachment_failed(self): + def test_upload_release_attachment_failed(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -434,13 +446,20 @@ def test_upload_release_attachment_failed(self): # ignore pass - self.assertEqual(500, context.exception.response.status_code) - self.assertEqual("500", context.exception.details["status"]) - self.assertEqual("Internal Server Error", context.exception.details["error"]) - self.assertEqual("forbidded", context.exception.details["message"]) + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(500, context.exception.response.status_code) + if context.exception.details: + self.assertEqual("500", context.exception.details["status"]) + self.assertEqual("Internal Server Error", context.exception.details["error"]) + self.assertEqual("forbidded", context.exception.details["message"]) @responses.activate - def test_upload_release_attachment_returns_202(self): + def test_upload_release_attachment_returns_202(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -476,7 +495,7 @@ def test_upload_release_attachment_returns_202(self): pass @responses.activate - def test_upload_component_attachment(self): + def test_upload_component_attachment(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -502,7 +521,7 @@ def test_upload_component_attachment(self): pass @responses.activate - def test_upload_project_attachment(self): + def test_upload_project_attachment(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() diff --git a/tests/test_sw360_base.py b/tests/test_sw360_base.py index 8d9b39b..7514b4e 100644 --- a/tests/test_sw360_base.py +++ b/tests/test_sw360_base.py @@ -24,7 +24,7 @@ class Sw360Test(unittest.TestCase): MYURL = "https://my.server.com/" ERROR_MSG_NO_LOGIN = "Unable to login" - def test_constructor(self): + def test_constructor(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self.assertEqual(self.MYURL, lib.url) @@ -42,7 +42,7 @@ def test_constructor(self): self.assertEqual(self.MYURL, lib.url) @responses.activate - def test_login(self): + def test_login(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) responses.add( @@ -56,7 +56,7 @@ def test_login(self): actual = lib.login_api() self.assertTrue(actual) - def test_login_failed_invalid_url(self): + def test_login_failed_invalid_url(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) have_backup = False @@ -74,7 +74,7 @@ def test_login_failed_invalid_url(self): os.environ["SW360ProductionToken"] = backup @responses.activate - def test_login_failed_not_authorized(self): + def test_login_failed_not_authorized(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) responses.add( @@ -92,7 +92,7 @@ def test_login_failed_not_authorized(self): self.assertEqual(self.ERROR_MSG_NO_LOGIN, context.exception.message) @responses.activate - def test_login_failed_forbidden(self): + def test_login_failed_forbidden(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) responses.add( @@ -108,7 +108,7 @@ def test_login_failed_forbidden(self): self.assertEqual(self.ERROR_MSG_NO_LOGIN, context.exception.message) - def get_logged_in_lib(self): + def get_logged_in_lib(self) -> SW360: lib = SW360(self.MYURL, self.MYTOKEN, False) # lib.force_no_session = True @@ -126,7 +126,7 @@ def get_logged_in_lib(self): return lib @responses.activate - def test_dump_rest_call_to_file(self): + def test_dump_rest_call_to_file(self) -> None: FILENAME = "delete_me.txt" if os.path.isfile(FILENAME): os.remove(FILENAME) @@ -157,7 +157,7 @@ def test_dump_rest_call_to_file(self): lib.api_get_raw(self.MYURL + "resource/api/projects/123X") @responses.activate - def test_api_get_no_content(self): + def test_api_get_no_content(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -176,7 +176,7 @@ def test_api_get_no_content(self): self.assertIsNone(None) @responses.activate - def test_api_get_raw_not_logged_in(self): + def test_api_get_raw_not_logged_in(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = False @@ -195,7 +195,7 @@ def test_api_get_raw_not_logged_in(self): self.assertEqual("login_api needs to be called first", context.exception.message) @responses.activate - def test_api_get_raw(self): + def test_api_get_raw(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True @@ -224,7 +224,7 @@ def test_api_get_raw(self): self.assertEqual("My Testproject", p["name"]) @responses.activate - def test_api_get_raw_error(self): + def test_api_get_raw_error(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True @@ -251,12 +251,19 @@ def test_api_get_raw_error(self): with self.assertRaises(SW360Error) as context: lib.api_get_raw(self.MYURL + "resource/api/projects/123456X") - self.assertEqual(404, context.exception.response.status_code) - self.assertEqual("Not Found", context.exception.details["error"]) - self.assertEqual("Requested Project Not Found", context.exception.details["message"]) + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(404, context.exception.response.status_code) + if context.exception.details: + self.assertEqual("Not Found", context.exception.details["error"]) + self.assertEqual("Requested Project Not Found", context.exception.details["message"]) @responses.activate - def test_api_get_raw_error_string(self): + def test_api_get_raw_error_string(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True @@ -283,8 +290,14 @@ def test_api_get_raw_error_string(self): with self.assertRaises(SW360Error) as context: lib.api_get_raw(self.MYURL + "resource/api/projects/123456X") - self.assertEqual(404, context.exception.response.status_code) - self.assertEqual("Error-String", context.exception.response.text) + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(404, context.exception.response.status_code) + self.assertEqual("Error-String", context.exception.response.text) if __name__ == "__main__": diff --git a/tests/test_sw360_clearingrequests.py b/tests/test_sw360_clearingrequests.py index 3a53373..159ba72 100644 --- a/tests/test_sw360_clearingrequests.py +++ b/tests/test_sw360_clearingrequests.py @@ -23,12 +23,12 @@ class Sw360TestClearingRequests(unittest.TestCase): MYURL = "https://my.server.com/" ERROR_MSG_NO_LOGIN = "Unable to login" - def setUp(self): + def setUp(self) -> None: warnings.filterwarnings( "ignore", category=ResourceWarning, message="unclosed.*") - def _add_login_response(self): + def _add_login_response(self) -> None: """ Add the response for a successfull login. """ @@ -42,7 +42,7 @@ def _add_login_response(self): ) @responses.activate - def test_get_clearing_request(self): + def test_get_clearing_request(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -62,10 +62,11 @@ def test_get_clearing_request(self): vendor = lib.get_clearing_request("12345") self.assertIsNotNone(vendor) - self.assertEqual("2021-09-04", vendor["requestedClearingDate"]) + if vendor: # only for mypy + self.assertEqual("2021-09-04", vendor["requestedClearingDate"]) @responses.activate - def test_get_clearing_request_for_project(self): + def test_get_clearing_request_for_project(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -85,7 +86,8 @@ def test_get_clearing_request_for_project(self): vendor = lib.get_clearing_request_for_project("12345") self.assertIsNotNone(vendor) - self.assertEqual("2021-09-04", vendor["requestedClearingDate"]) + if vendor: # only for mypy + self.assertEqual("2021-09-04", vendor["requestedClearingDate"]) if __name__ == "__main__": diff --git a/tests/test_sw360_components.py b/tests/test_sw360_components.py index 7927bc3..9dd8f36 100644 --- a/tests/test_sw360_components.py +++ b/tests/test_sw360_components.py @@ -9,6 +9,7 @@ import os import sys +from typing import Any import unittest import warnings @@ -24,12 +25,12 @@ class Sw360TestComponents(unittest.TestCase): MYURL = "https://my.server.com/" ERROR_MSG_NO_LOGIN = "Unable to login" - def setUp(self): + def setUp(self) -> None: warnings.filterwarnings( "ignore", category=ResourceWarning, message="unclosed.*") - def _add_login_response(self): + def _add_login_response(self) -> None: """ Add the response for a successfull login. """ @@ -42,18 +43,18 @@ def _add_login_response(self): adding_headers={"Authorization": "Token " + self.MYTOKEN}, ) - def _my_matcher(self): + def _my_matcher(self) -> Any: """ Helper method to display the JSON parameters of a REST call. """ - def display_json_params(request_body): + def display_json_params(request_body: Any) -> bool: print("MyMatcher:'" + request_body + "'") return True return display_json_params @responses.activate - def test_get_all_components(self): + def test_get_all_components(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -75,7 +76,7 @@ def test_get_all_components(self): self.assertEqual("Tethys.Logging", components[0]["name"]) @responses.activate - def test_get_all_components_no_result(self): + def test_get_all_components_no_result(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -95,7 +96,7 @@ def test_get_all_components_no_result(self): self.assertEqual([], components) @responses.activate - def test_get_all_components_with_fields(self): + def test_get_all_components_with_fields(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -118,7 +119,7 @@ def test_get_all_components_with_fields(self): self.assertEqual("DE", components[0]["ownerCountry"]) @responses.activate - def test_get_all_components_with_fields_and_paging(self): + def test_get_all_components_with_fields_and_paging(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -141,7 +142,7 @@ def test_get_all_components_with_fields_and_paging(self): self.assertEqual("DE", components[0]["ownerCountry"]) @responses.activate - def test_get_all_components_by_type(self): + def test_get_all_components_by_type(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -164,7 +165,7 @@ def test_get_all_components_by_type(self): self.assertEqual("OSS", components[0]["componentType"]) @responses.activate - def test_get_all_components_by_type_no_result(self): + def test_get_all_components_by_type_no_result(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -184,7 +185,7 @@ def test_get_all_components_by_type_no_result(self): self.assertEqual([], components) @responses.activate - def test_get_component(self): + def test_get_component(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -201,10 +202,11 @@ def test_get_component(self): ) comp = lib.get_component("123") - self.assertEqual("Tethys.Logging", comp["name"]) + if comp: # only for mypy + self.assertEqual("Tethys.Logging", comp["name"]) @responses.activate - def test_get_component_by_url(self): + def test_get_component_by_url(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -221,10 +223,11 @@ def test_get_component_by_url(self): ) comp = lib.get_component_by_url(self.MYURL + "resource/api/components/123") - self.assertEqual("Tethys.Logging1", comp["name"]) + if comp: # only for mypy + self.assertEqual("Tethys.Logging1", comp["name"]) @responses.activate - def test_get_component_by_name(self): + def test_get_component_by_name(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -241,10 +244,11 @@ def test_get_component_by_name(self): ) comp = lib.get_component_by_name("MyComponent") - self.assertEqual("MyComponent", comp["name"]) + if comp: # only for mypy + self.assertEqual("MyComponent", comp["name"]) @responses.activate - def test_get_components_by_external_id(self): + def test_get_components_by_external_id(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -266,7 +270,7 @@ def test_get_components_by_external_id(self): self.assertEqual("Tethys.Logging", components[0]["name"]) @responses.activate - def test_get_components_by_external_id_full_answer(self): + def test_get_components_by_external_id_full_answer(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -288,7 +292,7 @@ def test_get_components_by_external_id_full_answer(self): self.assertEqual("Tethys.Logging", components[0]["name"]) @responses.activate - def test_update_component_external_id_add_fresh_id(self): + def test_update_component_external_id_add_fresh_id(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -320,7 +324,7 @@ def test_update_component_external_id_add_fresh_id(self): "bc75c910ca9866886cb4d7b3a301061f") @responses.activate - def test_update_component_external_id_no_overwrite(self): + def test_update_component_external_id_no_overwrite(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -343,7 +347,7 @@ def test_update_component_external_id_no_overwrite(self): "bc75c910ca9866886cb4d7b3a301061f") @responses.activate - def test_update_component_external_id_overwrite(self): + def test_update_component_external_id_overwrite(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -376,7 +380,7 @@ def test_update_component_external_id_overwrite(self): update_mode="overwrite") @responses.activate - def test_update_component_external_id_delete(self): + def test_update_component_external_id_delete(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -409,7 +413,7 @@ def test_update_component_external_id_delete(self): update_mode="delete") @responses.activate - def test_update_component_external_id_no_exist(self): + def test_update_component_external_id_no_exist(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -446,7 +450,7 @@ def test_update_component_external_id_no_exist(self): update_mode="delete") @responses.activate - def test_update_component_external_id_no_extids_yet(self): + def test_update_component_external_id_no_extids_yet(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -478,7 +482,7 @@ def test_update_component_external_id_no_extids_yet(self): "bc75c910ca9866886cb4d7b3a301061f") @responses.activate - def test_create_new_component(self): + def test_create_new_component(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -514,7 +518,7 @@ def test_create_new_component(self): ) @responses.activate - def test_create_new_component_fail(self): + def test_create_new_component_fail(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -547,10 +551,17 @@ def test_create_new_component_fail(self): description="Illustrative example component", homepage="https://www.github.com/NewComponent" ) - self.assertEqual(409, context.exception.response.status_code) + + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(409, context.exception.response.status_code) @responses.activate - def test_update_component_no_id(self): + def test_update_component_no_id(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -561,12 +572,12 @@ def test_update_component_no_id(self): comp["name"] = "NewComponent" with self.assertRaises(SW360Error) as context: - lib.update_component(comp, None) + lib.update_component(comp, "") self.assertEqual("No component id provided!", context.exception.message) @responses.activate - def test_update_component_failed(self): + def test_update_component_failed(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -586,10 +597,16 @@ def test_update_component_failed(self): with self.assertRaises(SW360Error) as context: lib.update_component(comp, "123") - self.assertEqual(403, context.exception.response.status_code) + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(403, context.exception.response.status_code) @responses.activate - def test_delete_component(self): + def test_delete_component(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -606,7 +623,7 @@ def test_delete_component(self): lib.delete_component("123") @responses.activate - def test_delete_component_no_id(self): + def test_delete_component_no_id(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -614,12 +631,12 @@ def test_delete_component_no_id(self): self.assertTrue(actual) with self.assertRaises(SW360Error) as context: - lib.delete_component(None) + lib.delete_component("") self.assertEqual("No component id provided!", context.exception.message) @responses.activate - def test_delete_component_failed(self): + def test_delete_component_failed(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -636,10 +653,16 @@ def test_delete_component_failed(self): with self.assertRaises(SW360Error) as context: lib.delete_component("123") - self.assertEqual(404, context.exception.response.status_code) + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(404, context.exception.response.status_code) @responses.activate - def test_get_users_of_component(self): + def test_get_users_of_component(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -657,7 +680,7 @@ def test_get_users_of_component(self): lib.get_users_of_component("123") - def xtest_get_component_real(self): + def xtest_get_component_real(self) -> None: """ Only for debugging... """ diff --git a/tests/test_sw360_health.py b/tests/test_sw360_health.py index a891712..8625c38 100644 --- a/tests/test_sw360_health.py +++ b/tests/test_sw360_health.py @@ -1,5 +1,5 @@ # ------------------------------------------------------------------------------- -# Copyright (c) 2020-2022 Siemens +# Copyright (c) 2020-2023 Siemens # All Rights Reserved. # Author: thomas.graf@siemens.com # @@ -23,12 +23,12 @@ class Sw360TestHealth(unittest.TestCase): MYURL = "https://my.server.com/" ERROR_MSG_NO_LOGIN = "Unable to login" - def setUp(self): + def setUp(self) -> None: warnings.filterwarnings( "ignore", category=ResourceWarning, message="unclosed.*") - def _add_login_response(self): + def _add_login_response(self) -> None: """ Add the response for a successfull login. """ @@ -42,7 +42,7 @@ def _add_login_response(self): ) @responses.activate - def test_get_health_status(self): + def test_get_health_status(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -59,7 +59,8 @@ def test_get_health_status(self): status = lib.get_health_status() self.assertIsNotNone(status) - self.assertTrue("status" in status) + if status: # only for mypy + self.assertTrue("status" in status) if __name__ == "__main__": diff --git a/tests/test_sw360_licenses.py b/tests/test_sw360_licenses.py index e76baff..81a6b98 100644 --- a/tests/test_sw360_licenses.py +++ b/tests/test_sw360_licenses.py @@ -23,12 +23,12 @@ class Sw360TestLicenses(unittest.TestCase): MYURL = "https://my.server.com/" ERROR_MSG_NO_LOGIN = "Unable to login" - def setUp(self): + def setUp(self) -> None: warnings.filterwarnings( "ignore", category=ResourceWarning, message="unclosed.*") - def _add_login_response(self): + def _add_login_response(self) -> None: """ Add the response for a successfull login. """ @@ -42,7 +42,7 @@ def _add_login_response(self): ) @responses.activate - def test_get_all_licenses(self): + def test_get_all_licenses(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -77,7 +77,7 @@ def test_get_all_licenses(self): self.assertEqual("BSD Zero Clause License", licenses[0]["fullName"]) @responses.activate - def test_get_all_licenses_none(self): + def test_get_all_licenses_none(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -96,7 +96,7 @@ def test_get_all_licenses_none(self): self.assertEqual([], licenses) @responses.activate - def test_get_license(self): + def test_get_license(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -119,12 +119,13 @@ def test_get_license(self): license = lib.get_license("Apache-2.0") self.assertIsNotNone(license) - self.assertEqual("True", license["checked"]) - self.assertEqual("Apache-2.0", license["shortName"]) - self.assertEqual("Apache License 2.0", license["fullName"]) + if license: # only for mypy + self.assertEqual("True", license["checked"]) + self.assertEqual("Apache-2.0", license["shortName"]) + self.assertEqual("Apache License 2.0", license["fullName"]) @responses.activate - def test_create_new_license(self): + def test_create_new_license(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -159,7 +160,7 @@ def test_create_new_license(self): ) @responses.activate - def test_create_new_license_fail(self): + def test_create_new_license_fail(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -194,10 +195,17 @@ def test_create_new_license_fail(self): text="", checked=True, ) - self.assertEqual(403, context.exception.response.status_code) + + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(403, context.exception.response.status_code) @responses.activate - def test_delete_license(self): + def test_delete_license(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() diff --git a/tests/test_sw360_projects.py b/tests/test_sw360_projects.py index eb8fa45..b5c3a39 100644 --- a/tests/test_sw360_projects.py +++ b/tests/test_sw360_projects.py @@ -12,6 +12,7 @@ import tempfile import unittest import warnings +from typing import Any, Dict, List import responses @@ -25,10 +26,10 @@ class Sw360TestProjects(unittest.TestCase): MYURL = "https://my.server.com/" ERROR_MSG_NO_LOGIN = "Unable to login" - def setUp(self): + def setUp(self) -> None: warnings.simplefilter("ignore", ResourceWarning) - def get_logged_in_lib(self): + def get_logged_in_lib(self) -> SW360: lib = SW360(self.MYURL, self.MYTOKEN, False) responses.add( @@ -45,7 +46,7 @@ def get_logged_in_lib(self): return lib @responses.activate - def test_get_project(self): + def test_get_project(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) responses.add( @@ -69,14 +70,15 @@ def test_get_project(self): ) p = lib.get_project("123") - self.assertEqual("My Testproject", p["name"]) + if p: # only for mypy + self.assertEqual("My Testproject", p["name"]) self.assertIsNotNone(lib.session) lib.close_api() self.assertIsNone(lib.session) @responses.activate - def test_get_project_not_logged_in(self): + def test_get_project_not_logged_in(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) responses.add( @@ -94,7 +96,7 @@ def test_get_project_not_logged_in(self): self.assertTrue(context.exception.message.startswith("login_api needs to be called first")) @responses.activate - def test_get_project_not_found(self): + def test_get_project_not_found(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) # lib.force_no_session = True @@ -121,12 +123,19 @@ def test_get_project_not_found(self): with self.assertRaises(SW360Error) as context: lib.get_project("123456") - self.assertEqual(404, context.exception.response.status_code) - self.assertEqual("Not Found", context.exception.details["error"]) - self.assertEqual("Requested Project Not Found", context.exception.details["message"]) + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(404, context.exception.response.status_code) + if context.exception.details: + self.assertEqual("Not Found", context.exception.details["error"]) + self.assertEqual("Requested Project Not Found", context.exception.details["message"]) @responses.activate - def test_get_project_releases(self): + def test_get_project_releases(self) -> None: lib = self.get_logged_in_lib() responses.add( responses.GET, @@ -153,7 +162,7 @@ def test_get_project_releases(self): self.assertIsNotNone(releases) @responses.activate - def test_get_project_by_url(self): + def test_get_project_by_url(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -166,10 +175,11 @@ def test_get_project_by_url(self): ) p = lib.get_project_by_url(self.MYURL + "resource/api/projects/123") - self.assertEqual("My Testproject", p["name"]) + if p: # only for mypy + self.assertEqual("My Testproject", p["name"]) @responses.activate - def test_get_projects(self): + def test_get_projects(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -183,12 +193,13 @@ def test_get_projects(self): projects = lib.get_projects() self.assertIsNotNone(projects) - self.assertTrue("_embedded" in projects) - self.assertTrue("sw360:projects" in projects["_embedded"]) - self.assertEqual("My Testproject", projects["_embedded"]["sw360:projects"][0]["name"]) + if projects: # only for mypy + self.assertTrue("_embedded" in projects) + self.assertTrue("sw360:projects" in projects["_embedded"]) + self.assertEqual("My Testproject", projects["_embedded"]["sw360:projects"][0]["name"]) @responses.activate - def test_get_projects_with_details(self): + def test_get_projects_with_details(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -202,12 +213,13 @@ def test_get_projects_with_details(self): projects = lib.get_projects(all_details=True) self.assertIsNotNone(projects) - self.assertTrue("_embedded" in projects) - self.assertTrue("sw360:projects" in projects["_embedded"]) - self.assertEqual("My Testproject", projects["_embedded"]["sw360:projects"][0]["name"]) + if projects: # only for mypy + self.assertTrue("_embedded" in projects) + self.assertTrue("sw360:projects" in projects["_embedded"]) + self.assertEqual("My Testproject", projects["_embedded"]["sw360:projects"][0]["name"]) @responses.activate - def test_get_projects_with_paging(self): + def test_get_projects_with_paging(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -221,12 +233,13 @@ def test_get_projects_with_paging(self): projects = lib.get_projects(page=1, page_size=2) self.assertIsNotNone(projects) - self.assertTrue("_embedded" in projects) - self.assertTrue("sw360:projects" in projects["_embedded"]) - self.assertEqual("My Testproject", projects["_embedded"]["sw360:projects"][0]["name"]) + if projects: # only for mypy + self.assertTrue("_embedded" in projects) + self.assertTrue("sw360:projects" in projects["_embedded"]) + self.assertEqual("My Testproject", projects["_embedded"]["sw360:projects"][0]["name"]) @responses.activate - def test_get_projects_with_paging_and_details(self): + def test_get_projects_with_paging_and_details(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -240,12 +253,13 @@ def test_get_projects_with_paging_and_details(self): projects = lib.get_projects(all_details=True, page=3, page_size=4, sort="name,desc") self.assertIsNotNone(projects) - self.assertTrue("_embedded" in projects) - self.assertTrue("sw360:projects" in projects["_embedded"]) - self.assertEqual("My Testproject", projects["_embedded"]["sw360:projects"][0]["name"]) + if projects: # only for mypy + self.assertTrue("_embedded" in projects) + self.assertTrue("sw360:projects" in projects["_embedded"]) + self.assertEqual("My Testproject", projects["_embedded"]["sw360:projects"][0]["name"]) @responses.activate - def test_get_projects_by_type(self): + def test_get_projects_by_type(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -264,7 +278,7 @@ def test_get_projects_by_type(self): self.assertEqual("SERVICE", projects[0]["projectType"]) @responses.activate - def test_get_project_names(self): + def test_get_project_names(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -282,7 +296,7 @@ def test_get_project_names(self): self.assertEqual("My Testproject, 1.0.0", project_names[0]) @responses.activate - def test_get_projects_by_name(self): + def test_get_projects_by_name(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -298,7 +312,7 @@ def test_get_projects_by_name(self): self.assertEqual([], projects) @responses.activate - def test_get_projects_by_name_no_result(self): + def test_get_projects_by_name_no_result(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -316,7 +330,7 @@ def test_get_projects_by_name_no_result(self): self.assertEqual("My Testproject", projects[0]["name"]) @responses.activate - def test_get_projects_by_external_id(self): + def test_get_projects_by_external_id(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -336,7 +350,7 @@ def test_get_projects_by_external_id(self): self.assertEqual("9999", projects[0]["externalIds"]["myid"]) @responses.activate - def test_get_projects_by_external_id_no_result(self): + def test_get_projects_by_external_id_no_result(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -352,7 +366,7 @@ def test_get_projects_by_external_id_no_result(self): self.assertEqual([], projects) @responses.activate - def test_get_projects_by_group(self): + def test_get_projects_by_group(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -370,7 +384,7 @@ def test_get_projects_by_group(self): self.assertEqual("My Testproject", projects[0]["name"]) @responses.activate - def test_get_projects_by_group_with_details(self): + def test_get_projects_by_group_with_details(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -388,7 +402,7 @@ def test_get_projects_by_group_with_details(self): self.assertEqual("My Testproject", projects[0]["name"]) @responses.activate - def test_get_projects_by_group_no_result(self): + def test_get_projects_by_group_no_result(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -404,7 +418,7 @@ def test_get_projects_by_group_no_result(self): self.assertEqual([], projects) @responses.activate - def test_get_projects_by_tag(self): + def test_get_projects_by_tag(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -422,7 +436,7 @@ def test_get_projects_by_tag(self): self.assertEqual("My Testproject", projects[0]["name"]) @responses.activate - def test_get_projects_by_tag_no_result(self): + def test_get_projects_by_tag_no_result(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -438,7 +452,7 @@ def test_get_projects_by_tag_no_result(self): self.assertEqual([], projects) @responses.activate - def test_download_license_info(self): + def test_download_license_info(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -462,7 +476,7 @@ def test_download_license_info(self): os.removedirs(tmpdir) @responses.activate - def test_get_project_vulnerabilities(self): + def test_get_project_vulnerabilities(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -476,15 +490,16 @@ def test_get_project_vulnerabilities(self): data = lib.get_project_vulnerabilities("123") self.assertIsNotNone(data) - self.assertTrue("_embedded" in data) - self.assertTrue("sw360:vulnerabilityDToes" in data["_embedded"]) - vulnerabilities = data["_embedded"]["sw360:vulnerabilityDToes"] - self.assertIsNotNone(vulnerabilities) - self.assertTrue(len(vulnerabilities) > 0) - self.assertEqual("2 - major", vulnerabilities[0]["priority"]) + if data: # only for mypy + self.assertTrue("_embedded" in data) + self.assertTrue("sw360:vulnerabilityDToes" in data["_embedded"]) + vulnerabilities = data["_embedded"]["sw360:vulnerabilityDToes"] + self.assertIsNotNone(vulnerabilities) + self.assertTrue(len(vulnerabilities) > 0) + self.assertEqual("2 - major", vulnerabilities[0]["priority"]) @responses.activate - def test_get_project_vulnerabilities_no_result(self): + def test_get_project_vulnerabilities_no_result(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -500,7 +515,7 @@ def test_get_project_vulnerabilities_no_result(self): self.assertIsNone(data) @responses.activate - def test_create_new_project(self): + def test_create_new_project(self) -> None: lib = self.get_logged_in_lib() responses.add( responses.POST, @@ -529,7 +544,7 @@ def test_create_new_project(self): ) @responses.activate - def test_create_new_project_already_exists(self): + def test_create_new_project_already_exists(self) -> None: lib = self.get_logged_in_lib() responses.add( responses.POST, @@ -554,10 +569,17 @@ def test_create_new_project_already_exists(self): description="Example Product", project_type="PRODUCT", visibility="EVERYONE", ) - self.assertEqual(409, context.exception.response.status_code) + + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(409, context.exception.response.status_code) @responses.activate - def test_update_project(self): + def test_update_project(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -577,12 +599,12 @@ def test_update_project(self): sub_proj["projectRelationship"] = "CONTAINED" sub_projects["12345"] = sub_proj - project["linkedProjects"] = sub_projects + project["linkedProjects"] = sub_projects # type: ignore lib.update_project(project, "123", False) @responses.activate - def test_update_project_sub_projects_no_add(self): + def test_update_project_sub_projects_no_add(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -621,12 +643,12 @@ def test_update_project_sub_projects_no_add(self): sub_proj["projectRelationship"] = "CONTAINED" sub_projects["12345"] = sub_proj - project["linkedProjects"] = sub_projects + project["linkedProjects"] = sub_projects # type: ignore lib.update_project(project, "123", True) @responses.activate - def test_update_project_sub_projects_with_add(self): + def test_update_project_sub_projects_with_add(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -669,12 +691,12 @@ def test_update_project_sub_projects_with_add(self): sub_proj["projectRelationship"] = "CONTAINED" sub_projects["12345"] = sub_proj - project["linkedProjects"] = sub_projects + project["linkedProjects"] = sub_projects # type: ignore lib.update_project(project, "123", True) @responses.activate - def test_update_project_no_id(self): + def test_update_project_no_id(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -690,12 +712,12 @@ def test_update_project_no_id(self): project["projectType"] = "PRODUCT" with self.assertRaises(SW360Error) as context: - lib.update_project(project, None) + lib.update_project(project, "") self.assertEqual("No project id provided!", context.exception.message) @responses.activate - def test_update_project_failed(self): + def test_update_project_failed(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -713,10 +735,16 @@ def test_update_project_failed(self): with self.assertRaises(SW360Error) as context: lib.update_project(project, "123") - self.assertEqual(404, context.exception.response.status_code) + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(404, context.exception.response.status_code) @responses.activate - def test_update_project_releases_no_id(self): + def test_update_project_releases_no_id(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -727,12 +755,12 @@ def test_update_project_releases_no_id(self): ) with self.assertRaises(SW360Error) as context: - lib.update_project_releases(None, None) + lib.update_project_releases([], "") self.assertEqual("No project id provided!", context.exception.message) @responses.activate - def test_update_project_releases_fresh_prj(self): + def test_update_project_releases_fresh_prj(self) -> None: lib = self.get_logged_in_lib() responses.add( responses.GET, @@ -744,7 +772,7 @@ def test_update_project_releases_fresh_prj(self): url=self.MYURL + "resource/api/projects/123/releases", status=202, ) - lib.update_project_releases({}, "123", add=True) + lib.update_project_releases([], "123", add=True) responses.add( responses.GET, @@ -756,10 +784,10 @@ def test_update_project_releases_fresh_prj(self): url=self.MYURL + "resource/api/projects/124/releases", status=202, ) - lib.update_project_releases({}, "124", add=True) + lib.update_project_releases([], "124", add=True) @responses.activate - def test_update_project_releases(self): + def test_update_project_releases(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -778,11 +806,11 @@ def test_update_project_releases(self): status=202, ) - releases = {} + releases: List[Dict[str, Any]] = [] lib.update_project_releases(releases, "123", add=True) @responses.activate - def test_update_project_releases_failed(self): + def test_update_project_releases_failed(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -801,14 +829,20 @@ def test_update_project_releases_failed(self): status=404, ) - releases = {} + releases: List[Dict[str, Any]] = [] with self.assertRaises(SW360Error) as context: lib.update_project_releases(releases, "123", add=True) - self.assertEqual(404, context.exception.response.status_code) + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(404, context.exception.response.status_code) @responses.activate - def test_update_project_external_id_add_fresh_id(self): + def test_update_project_external_id_add_fresh_id(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -836,7 +870,7 @@ def test_update_project_external_id_add_fresh_id(self): "123") @responses.activate - def test_delete_project(self): + def test_delete_project(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -849,16 +883,16 @@ def test_delete_project(self): lib.delete_project("123") @responses.activate - def test_delete_project_no_id(self): + def test_delete_project_no_id(self) -> None: lib = self.get_logged_in_lib() with self.assertRaises(SW360Error) as context: - lib.delete_project(None) + lib.delete_project("") self.assertEqual("No project id provided!", context.exception.message) @responses.activate - def test_delete_project_failed(self): + def test_delete_project_failed(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -871,10 +905,16 @@ def test_delete_project_failed(self): with self.assertRaises(SW360Error) as context: lib.delete_project("123") - self.assertEqual(404, context.exception.response.status_code) + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(404, context.exception.response.status_code) @responses.activate - def test_get_users_of_project(self): + def test_get_users_of_project(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -889,7 +929,7 @@ def test_get_users_of_project(self): lib.get_users_of_project("123") @responses.activate - def test_duplicate_project(self): + def test_duplicate_project(self) -> None: lib = self.get_logged_in_lib() responses.add( responses.POST, @@ -914,22 +954,26 @@ def test_duplicate_project(self): ) result = lib.duplicate_project("007", "42") self.assertIsNotNone(result) - self.assertTrue("clearingState" in result) - self.assertEqual("OPEN", result["clearingState"]) - self.assertTrue("version" in result) - self.assertEqual("42", result["version"]) + if result: # only for mypy + self.assertTrue("clearingState" in result) + self.assertEqual("OPEN", result["clearingState"]) + self.assertTrue("version" in result) + self.assertEqual("42", result["version"]) @responses.activate - def test_duplicate_project_no_id(self): + def test_duplicate_project_no_id(self) -> None: lib = self.get_logged_in_lib() with self.assertRaises(SW360Error) as context: - lib.duplicate_project(None, "42") + lib.duplicate_project("", "42") - self.assertEqual("No project id provided!", context.exception.message) + if not context.exception: + self.assertTrue(False, "no exception") + else: + self.assertEqual("No project id provided!", context.exception.message) @responses.activate - def test_duplicate_project_failed(self): + def test_duplicate_project_failed(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -943,10 +987,17 @@ def test_duplicate_project_failed(self): lib.duplicate_project("123", "42") print(context.exception) - self.assertEqual(404, context.exception.response.status_code) + + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(404, context.exception.response.status_code) @responses.activate - def test_update_project_release_relationship_no_project_id(self): + def test_update_project_release_relationship_no_project_id(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -962,12 +1013,15 @@ def test_update_project_release_relationship_no_project_id(self): project["projectType"] = "PRODUCT" with self.assertRaises(SW360Error) as context: - lib.update_project_release_relationship(None, "22", "state", "rel", "cmt") + lib.update_project_release_relationship("", "22", "state", "rel", "cmt") - self.assertEqual("No project id provided!", context.exception.message) + if not context.exception: + self.assertTrue(False, "no exception") + else: + self.assertEqual("No project id provided!", context.exception.message) @responses.activate - def test_update_project_release_relationship_no_release_id(self): + def test_update_project_release_relationship_no_release_id(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -983,12 +1037,12 @@ def test_update_project_release_relationship_no_release_id(self): project["projectType"] = "PRODUCT" with self.assertRaises(SW360Error) as context: - lib.update_project_release_relationship("123", None, "state", "rel", "cmt") + lib.update_project_release_relationship("123", "", "state", "rel", "cmt") self.assertEqual("No release id provided!", context.exception.message) @responses.activate - def test_update_project_release_relationship_failed(self): + def test_update_project_release_relationship_failed(self) -> None: lib = self.get_logged_in_lib() responses.add( @@ -1006,10 +1060,16 @@ def test_update_project_release_relationship_failed(self): with self.assertRaises(SW360Error) as context: lib.update_project_release_relationship("123", "9988", "state", "rel", "cmt") - self.assertEqual(404, context.exception.response.status_code) + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(404, context.exception.response.status_code) @responses.activate - def test_update_project_release_relationship(self): + def test_update_project_release_relationship(self) -> None: lib = self.get_logged_in_lib() responses.add( diff --git a/tests/test_sw360_releases.py b/tests/test_sw360_releases.py index 1dd9390..5b09716 100644 --- a/tests/test_sw360_releases.py +++ b/tests/test_sw360_releases.py @@ -8,6 +8,7 @@ # ------------------------------------------------------------------------------- import sys +from typing import Any import unittest import warnings @@ -23,12 +24,12 @@ class Sw360TestReleases(unittest.TestCase): MYURL = "https://my.server.com/" ERROR_MSG_NO_LOGIN = "Unable to login" - def setUp(self): + def setUp(self) -> None: warnings.filterwarnings( "ignore", category=ResourceWarning, message="unclosed.*") - def _add_login_response(self): + def _add_login_response(self) -> None: """ Add the response for a successfull login. """ @@ -41,18 +42,18 @@ def _add_login_response(self): adding_headers={"Authorization": "Token " + self.MYTOKEN}, ) - def _my_matcher(self): + def _my_matcher(self) -> Any: """ Helper method to display the JSON parameters of a REST call. """ - def display_json_params(request_body): + def display_json_params(request_body: Any) -> bool: print("MyMatcher:'" + request_body + "'") return True return display_json_params @responses.activate - def test_get_get_release(self): + def test_get_get_release(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -69,11 +70,12 @@ def test_get_get_release(self): release = lib.get_release("123") self.assertIsNotNone(release) - self.assertEqual("Tethys.Logging", release["name"]) - self.assertEqual("1.4.0", release["version"]) + if release: # only for mypy + self.assertEqual("Tethys.Logging", release["name"]) + self.assertEqual("1.4.0", release["version"]) @responses.activate - def test_get_get_release_internal_server_error(self): + def test_get_get_release_internal_server_error(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -91,12 +93,19 @@ def test_get_get_release_internal_server_error(self): with self.assertRaises(SW360Error) as context: lib.get_release("123") - self.assertEqual(500, context.exception.response.status_code) - self.assertEqual("500", context.exception.details["status"]) - self.assertEqual("Internal Server Error", context.exception.details["error"]) + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(500, context.exception.response.status_code) + if context.exception.details: + self.assertEqual("500", context.exception.details["status"]) + self.assertEqual("Internal Server Error", context.exception.details["error"]) @responses.activate - def test_get_release_by_url(self): + def test_get_release_by_url(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -113,11 +122,12 @@ def test_get_release_by_url(self): release = lib.get_release_by_url(self.MYURL + "resource/api/releases/124") self.assertIsNotNone(release) - self.assertEqual("Tethys.Logging", release["name"]) - self.assertEqual("1.3.0", release["version"]) + if release: # only for mypy + self.assertEqual("Tethys.Logging", release["name"]) + self.assertEqual("1.3.0", release["version"]) @responses.activate - def test_get_all_releases(self): + def test_get_all_releases(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -134,12 +144,13 @@ def test_get_all_releases(self): releases = lib.get_all_releases() self.assertIsNotNone(releases) - self.assertTrue(len(releases) > 0) - self.assertEqual("Tethys.Logging", releases[0]["name"]) - self.assertEqual("1.3.0", releases[0]["version"]) + if releases: # only for mypy + self.assertTrue(len(releases) > 0) + self.assertEqual("Tethys.Logging", releases[0]["name"]) + self.assertEqual("1.3.0", releases[0]["version"]) @responses.activate - def test_get_all_releases_all_details(self): + def test_get_all_releases_all_details(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -154,14 +165,14 @@ def test_get_all_releases_all_details(self): adding_headers={"Authorization": "Token " + self.MYTOKEN}, ) - releases = lib.get_all_releases(None, True) + releases = lib.get_all_releases("", True) self.assertIsNotNone(releases) self.assertTrue(len(releases) > 0) self.assertEqual("Tethys.Logging", releases[0]["name"]) self.assertEqual("1.3.0", releases[0]["version"]) @responses.activate - def test_get_all_releases_with_fields(self): + def test_get_all_releases_with_fields(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -183,7 +194,7 @@ def test_get_all_releases_with_fields(self): self.assertEqual("1.3.0", releases[0]["version"]) @responses.activate - def test_get_all_releases_with_fields_and_all_details(self): + def test_get_all_releases_with_fields_and_all_details(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -205,7 +216,7 @@ def test_get_all_releases_with_fields_and_all_details(self): self.assertEqual("1.3.0", releases[0]["version"]) @responses.activate - def test_get_releases_by_external_id(self): + def test_get_releases_by_external_id(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -227,7 +238,7 @@ def test_get_releases_by_external_id(self): self.assertEqual("1.3.0", releases[0]["version"]) @responses.activate - def test_get_releases_by_name(self): + def test_get_releases_by_name(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -249,7 +260,7 @@ def test_get_releases_by_name(self): self.assertEqual("2.2.2", releases[0]["version"]) @responses.activate - def test_create_new_release(self): + def test_create_new_release(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -277,7 +288,7 @@ def test_create_new_release(self): lib.create_new_release("NewComponent", "1.0.0", "9876") @responses.activate - def test_create_new_release_already_exists(self): + def test_create_new_release_already_exists(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -303,12 +314,19 @@ def test_create_new_release_already_exists(self): with self.assertRaises(SW360Error) as context: lib.create_new_release("NewComponent", "1.0.0", "9876") - self.assertEqual(409, context.exception.response.status_code) - self.assertEqual(409, context.exception.details["status"]) - self.assertEqual("Conflict", context.exception.details["error"]) + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(409, context.exception.response.status_code) + if context.exception.details: + self.assertEqual(409, context.exception.details["status"]) + self.assertEqual("Conflict", context.exception.details["error"]) @responses.activate - def test_update_release(self): + def test_update_release(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -329,7 +347,7 @@ def test_update_release(self): lib.update_release(release, "123") @responses.activate - def test_update_release_no_id(self): + def test_update_release_no_id(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -347,12 +365,12 @@ def test_update_release_no_id(self): release["name"] = "NewComponent" with self.assertRaises(SW360Error) as context: - lib.update_release(release, None) + lib.update_release(release, "") self.assertEqual("No release id provided!", context.exception.message) @responses.activate - def test_update_release_failed(self): + def test_update_release_failed(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -372,10 +390,16 @@ def test_update_release_failed(self): with self.assertRaises(SW360Error) as context: lib.update_release(release, "123") - self.assertEqual(404, context.exception.response.status_code) + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(404, context.exception.response.status_code) @responses.activate - def test_update_release_external_id_add_fresh_id(self): + def test_update_release_external_id_add_fresh_id(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -406,7 +430,7 @@ def test_update_release_external_id_add_fresh_id(self): "123") @responses.activate - def test_delete_release(self): + def test_delete_release(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -423,19 +447,19 @@ def test_delete_release(self): lib.delete_release("123") @responses.activate - def test_delete_release_no_id(self): + def test_delete_release_no_id(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() self.assertTrue(actual) with self.assertRaises(SW360Error) as context: - lib.delete_release(None) + lib.delete_release("") self.assertEqual("No release id provided!", context.exception.message) @responses.activate - def test_delete_release_failed(self): + def test_delete_release_failed(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -451,10 +475,16 @@ def test_delete_release_failed(self): with self.assertRaises(SW360Error) as context: lib.delete_release("123") - self.assertEqual(404, context.exception.response.status_code) + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(404, context.exception.response.status_code) @responses.activate - def test_get_users_of_release(self): + def test_get_users_of_release(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() diff --git a/tests/test_sw360_support.py b/tests/test_sw360_support.py index 8ee2780..ba0e985 100644 --- a/tests/test_sw360_support.py +++ b/tests/test_sw360_support.py @@ -20,7 +20,7 @@ class Sw360TestSupportMethods(unittest.TestCase): MYURL = "https://my.server.com/" ERROR_MSG_NO_LOGIN = "Unable to login" - def test_get_id_from_href(self): + def test_get_id_from_href(self) -> None: URL = "https://sw360.siemens.com/resource/api/releases/00dc0db789f9372ed6bcfd55f100e3ce" lib = SW360(self.MYURL, self.MYTOKEN, False) diff --git a/tests/test_sw360_vendors.py b/tests/test_sw360_vendors.py index 6bfd0e4..f1d40ea 100644 --- a/tests/test_sw360_vendors.py +++ b/tests/test_sw360_vendors.py @@ -1,5 +1,5 @@ # ------------------------------------------------------------------------------- -# Copyright (c) 2020 Siemens +# Copyright (c) 2020-2023 Siemens # All Rights Reserved. # Author: thomas.graf@siemens.com # @@ -23,12 +23,12 @@ class Sw360TestVendors(unittest.TestCase): MYURL = "https://my.server.com/" ERROR_MSG_NO_LOGIN = "Unable to login" - def setUp(self): + def setUp(self) -> None: warnings.filterwarnings( "ignore", category=ResourceWarning, message="unclosed.*") - def _add_login_response(self): + def _add_login_response(self) -> None: """ Add the response for a successfull login. """ @@ -42,7 +42,7 @@ def _add_login_response(self): ) @responses.activate - def test_get_all_vendors_not_yet_implemented(self): + def test_get_all_vendors_not_yet_implemented(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -73,15 +73,16 @@ def test_get_all_vendors_not_yet_implemented(self): data = lib.api_get(self.MYURL + "resource/api/vendors") self.assertIsNotNone(data) - self.assertTrue("_embedded" in data) - self.assertTrue("sw360:vendors" in data["_embedded"]) - vlist = data["_embedded"]["sw360:vendors"] - self.assertEqual(2, len(vlist)) - self.assertEqual("Triangle, Inc.", vlist[0]["shortName"]) - self.assertEqual("Me", vlist[1]["shortName"]) + if data: # only for mypy + self.assertTrue("_embedded" in data) + self.assertTrue("sw360:vendors" in data["_embedded"]) + vlist = data["_embedded"]["sw360:vendors"] + self.assertEqual(2, len(vlist)) + self.assertEqual("Triangle, Inc.", vlist[0]["shortName"]) + self.assertEqual("Me", vlist[1]["shortName"]) @responses.activate - def test_get_vendor(self): + def test_get_vendor(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -102,10 +103,11 @@ def test_get_vendor(self): vendor = lib.get_vendor("12345") self.assertIsNotNone(vendor) - self.assertEqual("Triangle, Inc.", vendor["shortName"]) + if vendor: # only for mypy + self.assertEqual("Triangle, Inc.", vendor["shortName"]) @responses.activate - def test_get_all_vendors(self): + def test_get_all_vendors(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -130,7 +132,7 @@ def test_get_all_vendors(self): self.assertEqual("Premium Software", vendors[0]["fullName"]) @responses.activate - def test_get_all_vendors_no_result(self): + def test_get_all_vendors_no_result(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -149,7 +151,7 @@ def test_get_all_vendors_no_result(self): self.assertEqual([], vendors) @responses.activate - def test_create_new_vendor(self): + def test_create_new_vendor(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -179,7 +181,7 @@ def test_create_new_vendor(self): lib.create_new_vendor(vendor) @responses.activate - def test_create_new_vendor_fail(self): + def test_create_new_vendor_fail(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -209,10 +211,16 @@ def test_create_new_vendor_fail(self): with self.assertRaises(SW360Error) as context: lib.create_new_vendor(vendor) - self.assertEqual(403, context.exception.response.status_code) + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(403, context.exception.response.status_code) @responses.activate - def test_update_vendor_no_id(self): + def test_update_vendor_no_id(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -223,12 +231,12 @@ def test_update_vendor_no_id(self): vendor["shortName"] = "xxx" with self.assertRaises(SW360Error) as context: - lib.update_vendor(vendor, None) + lib.update_vendor(vendor, "") self.assertEqual("No vendor id provided!", context.exception.message) @responses.activate - def test_update_vendor(self): + def test_update_vendor(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -252,7 +260,7 @@ def test_update_vendor(self): lib.update_vendor(vendor, "112233") @responses.activate - def test_update_vendor_fail(self): + def test_update_vendor_fail(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -272,10 +280,16 @@ def test_update_vendor_fail(self): with self.assertRaises(SW360Error) as context: lib.update_vendor(vendor, "112233") - self.assertEqual(403, context.exception.response.status_code) + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(403, context.exception.response.status_code) @responses.activate - def test_delete_vendor_no_id(self): + def test_delete_vendor_no_id(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -283,12 +297,12 @@ def test_delete_vendor_no_id(self): self.assertTrue(actual) with self.assertRaises(SW360Error) as context: - lib.delete_vendor(None) + lib.delete_vendor("") self.assertEqual("No vendor id provided!", context.exception.message) @responses.activate - def test_delete_vendor(self): + def test_delete_vendor(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -305,7 +319,7 @@ def test_delete_vendor(self): lib.delete_vendor("123") @responses.activate - def test_delete_vendor_fail(self): + def test_delete_vendor_fail(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) lib.force_no_session = True self._add_login_response() @@ -322,8 +336,15 @@ def test_delete_vendor_fail(self): with self.assertRaises(SW360Error) as context: lib.delete_vendor("123") - self.assertEqual(405, context.exception.response.status_code) + if not context.exception: + self.assertTrue(False, "no exception") + + if context.exception.response is None: + self.assertTrue(False, "no response") + else: + self.assertEqual(405, context.exception.response.status_code) if __name__ == "__main__": - unittest.main() + sut = Sw360TestVendors() + sut.test_delete_vendor_fail() diff --git a/tests/test_sw360_vulnerabilities.py b/tests/test_sw360_vulnerabilities.py index 515c436..d072558 100644 --- a/tests/test_sw360_vulnerabilities.py +++ b/tests/test_sw360_vulnerabilities.py @@ -1,5 +1,5 @@ # ------------------------------------------------------------------------------- -# Copyright (c) 2020 Siemens +# Copyright (c) 2020-2023 Siemens # All Rights Reserved. # Author: thomas.graf@siemens.com # @@ -23,12 +23,12 @@ class Sw360TestVulnerabilities(unittest.TestCase): MYURL = "https://my.server.com/" ERROR_MSG_NO_LOGIN = "Unable to login" - def setUp(self): + def setUp(self) -> None: warnings.filterwarnings( "ignore", category=ResourceWarning, message="unclosed.*") - def _add_login_response(self): + def _add_login_response(self) -> None: """ Add the response for a successfull login. """ @@ -42,7 +42,7 @@ def _add_login_response(self): ) @responses.activate - def test_get_all_vulnerabilities(self): + def test_get_all_vulnerabilities(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -66,14 +66,15 @@ def test_get_all_vulnerabilities(self): data = lib.get_all_vulnerabilities() self.assertIsNotNone(data) - self.assertTrue("_embedded" in data) - self.assertTrue("sw360:vulnerabilities" in data["_embedded"]) - vlist = data["_embedded"]["sw360:vulnerabilities"] - self.assertEqual(2, len(vlist)) - self.assertEqual("Title of vulnerability 12345", vlist[0]["title"]) + if data: # only for mypy + self.assertTrue("_embedded" in data) + self.assertTrue("sw360:vulnerabilities" in data["_embedded"]) + vlist = data["_embedded"]["sw360:vulnerabilities"] + self.assertEqual(2, len(vlist)) + self.assertEqual("Title of vulnerability 12345", vlist[0]["title"]) @responses.activate - def test_get_vulnerability(self): + def test_get_vulnerability(self) -> None: lib = SW360(self.MYURL, self.MYTOKEN, False) self._add_login_response() actual = lib.login_api() @@ -94,7 +95,8 @@ def test_get_vulnerability(self): v = lib.get_vulnerability("47936") self.assertIsNotNone(v) - self.assertEqual("47936", v["externalId"]) + if v: # only for mypy + self.assertEqual("47936", v["externalId"]) if __name__ == "__main__":