7
7
# -------------------------------------------------------------------------------
8
8
9
9
import os
10
+ import responses
10
11
11
12
import capycli .common .json_support
12
13
import capycli .common .script_base
13
14
from capycli .bom .findsources import FindSources
14
15
from capycli .common .capycli_bom_support import CaPyCliBom , CycloneDxSupport
15
16
from capycli .main .result_codes import ResultCode
16
17
from tests .test_base import AppArguments , TestBase
18
+ from unittest .mock import MagicMock , patch
17
19
18
20
19
21
class TestFindSources (TestBase ):
@@ -100,7 +102,7 @@ def test_find_sources(self) -> None:
100
102
self .assertTrue ("Using anonymous GitHub access" in out )
101
103
self .assertTrue ("8 components read from SBOM" in out )
102
104
self .assertTrue ("1 source files were already available" in out )
103
- self .assertTrue ("5 source file URLs were found" in out )
105
+ self .assertTrue ("6 source file URLs were found" in out )
104
106
105
107
sbom = CaPyCliBom .read_sbom (args .outputfile )
106
108
self .assertIsNotNone (sbom )
@@ -191,3 +193,69 @@ def test_normalize_version(self):
191
193
actual = sut .to_semver_string (version )
192
194
self .assertEqual (actual , expected )
193
195
self .assertTrue (actual == expected , 'version %s is %s' % (actual , expected ))
196
+
197
+ @responses .activate
198
+ def test_get_release_component_id (self ):
199
+ # Mock the sw360 client
200
+ mock_client = MagicMock ()
201
+ mock_client .get_release .return_value = {"_links" : {"sw360:component" : {"href" : self .MYURL + 'components/123' }}}
202
+
203
+ # Call the method and assert the result
204
+ find_sources = FindSources ()
205
+ find_sources .client = mock_client
206
+ component_id = find_sources .get_release_component_id ("some_release_id" )
207
+ self .assertEqual (component_id , "123" )
208
+
209
+ @responses .activate
210
+ def test_find_source_url_from_component (self ):
211
+ # Mock the client
212
+ mock_client = MagicMock ()
213
+ mock_client .get_component .return_value = {"_embedded" : {"sw360:releases" : [{"_links" : {"self" : {"href" : self .MYURL + 'releases/456' }}}]}}
214
+ mock_client .get_release .return_value = {"_links" : {"sw360:component" : {"href" : self .MYURL + 'components/123' }}, "sourceCodeDownloadurl" : "http://github.com/some/repo/0.0.0" }
215
+
216
+ # Call the method and assert the result
217
+ find_sources = FindSources ()
218
+ find_sources .client = mock_client # Inject the mocked client
219
+ source_url = find_sources .find_source_url_from_component (component_id = "some_component_id" )
220
+ self .assertEqual (source_url , "http://github.com/some/repo/0.0.0" )
221
+
222
+ @patch ('requests.get' )
223
+ @patch ('bs4.BeautifulSoup' )
224
+ def test_get_pkg_go_repo_url_success (self , mock_beautifulsoup , mock_requests_get ):
225
+ # Mocking successful response
226
+ mock_requests_get .return_value .text = '<div class="UnitMeta-repo"><a href="https://github.com/example/repo/1.0.0">Repo Link</a></div>'
227
+ mock_beautifulsoup .return_value .find .return_value = MagicMock (get = lambda x : 'https://github.com/example/repo/1.0.0' )
228
+ find_sources = FindSources ()
229
+ repo_url = find_sources .get_pkg_go_repo_url ('example/package' )
230
+ self .assertEqual (repo_url , 'https://github.com/example/repo/1.0.0' )
231
+
232
+ @patch ('requests.get' , side_effect = Exception ('Some error' ))
233
+ def test_get_pkg_go_repo_url_error (self , mock_requests_get ):
234
+ # Mocking an exception during the request
235
+ find_sources = FindSources ()
236
+ repo_url = find_sources .get_pkg_go_repo_url ('some/package' )
237
+ self .assertEqual (repo_url , 'https://pkg.go.dev/some/package' )
238
+
239
+ @patch ('capycli.bom.findsources.FindSources.get_github_info' )
240
+ @patch ('capycli.bom.findsources.FindSources.get_matching_tag' )
241
+ def test_find_golang_url_github (self , mock_get_github_info , mock_get_matching_tag ):
242
+ # Mocking a GitHub scenario
243
+ mock_get_github_info .return_value = 'https://pkg.go.dev/github.com/opencontainers/runc'
244
+ mock_get_matching_tag .return_value = 'https://github.com/opencontainers/runc/archive/refs/tags/v1.0.1.zip'
245
+ find_sources = FindSources ()
246
+ component = MagicMock ()
247
+ component .name = 'github.com/opencontainers/runc'
248
+ component .version = 'v1.0.1'
249
+ source_url = find_sources .find_golang_url (component )
250
+
251
+ self .assertEqual (source_url , 'https://pkg.go.dev/github.com/opencontainers/runc' )
252
+
253
+ def test_find_golang_url_non_github (self ):
254
+ # Mocking a non-GitHub scenario
255
+ find_sources = FindSources ()
256
+ component = MagicMock ()
257
+ component .name = 'example/package'
258
+ component .version = 'v1.0.0'
259
+ source_url = find_sources .find_golang_url (component )
260
+
261
+ self .assertEqual (source_url , '' )
0 commit comments