diff --git a/tests/data/cocoapods.json b/tests/data/cocoapods.json new file mode 100644 index 0000000..a17f7e5 --- /dev/null +++ b/tests/data/cocoapods.json @@ -0,0 +1,236 @@ +[ + { + "type": "cocoapods", + "namespace": null, + "name": "ASNetworking", + "version": "0.1.5", + "qualifiers": {}, + "subpath": null, + "repository_homepage_url": "https://cocoapods.org/pods/ASNetworking", + "repository_download_url": null, + "api_data_url": null, + "primary_language": "Swift", + "description": null, + "release_date": null, + "parties": [], + "keywords": [], + "homepage_url": "https://github.com/Appspia/ASNetworking", + "download_url": "https://github.com/Appspia/ASNetworking/archive/refs/tags/0.1.5.tar.gz", + "api_url": "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/5/5/b/ASNetworking/0.1.5/ASNetworking.podspec.json", + "size": null, + "sha1": null, + "md5": null, + "sha256": null, + "sha512": null, + "bug_tracking_url": "https://github.com/Appspia/ASNetworking/issues", + "code_view_url": "https://github.com/Appspia/ASNetworking", + "vcs_url": "https://github.com/Appspia/ASNetworking.git", + "copyright": null, + "license_expression": null, + "declared_license": { + "type": "MIT", + "file": "LICENSE" + }, + "notice_text": null, + "root_path": null, + "dependencies": [], + "contains_source_code": null, + "source_packages": [], + "purl": "pkg:cocoapods/ASNetworking@0.1.5" + }, + { + "type": "cocoapods", + "namespace": null, + "name": "ASNetworking", + "version": "0.1.4", + "qualifiers": {}, + "subpath": null, + "repository_homepage_url": "https://cocoapods.org/pods/ASNetworking", + "repository_download_url": null, + "api_data_url": null, + "primary_language": "Swift", + "description": null, + "release_date": null, + "parties": [], + "keywords": [], + "homepage_url": "https://github.com/Appspia/ASNetworking", + "download_url": "https://github.com/Appspia/ASNetworking/archive/refs/tags/0.1.4.tar.gz", + "api_url": "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/5/5/b/ASNetworking/0.1.4/ASNetworking.podspec.json", + "size": null, + "sha1": null, + "md5": null, + "sha256": null, + "sha512": null, + "bug_tracking_url": "https://github.com/Appspia/ASNetworking/issues", + "code_view_url": "https://github.com/Appspia/ASNetworking", + "vcs_url": "https://github.com/Appspia/ASNetworking.git", + "copyright": null, + "license_expression": null, + "declared_license": { + "type": "MIT", + "file": "LICENSE" + }, + "notice_text": null, + "root_path": null, + "dependencies": [], + "contains_source_code": null, + "source_packages": [], + "purl": "pkg:cocoapods/ASNetworking@0.1.4" + }, + { + "type": "cocoapods", + "namespace": null, + "name": "ASNetworking", + "version": "0.1.3", + "qualifiers": {}, + "subpath": null, + "repository_homepage_url": "https://cocoapods.org/pods/ASNetworking", + "repository_download_url": null, + "api_data_url": null, + "primary_language": "Swift", + "description": null, + "release_date": null, + "parties": [], + "keywords": [], + "homepage_url": "https://github.com/Appspia/ASNetworking", + "download_url": "https://github.com/Appspia/ASNetworking/archive/refs/tags/0.1.3.tar.gz", + "api_url": "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/5/5/b/ASNetworking/0.1.3/ASNetworking.podspec.json", + "size": null, + "sha1": null, + "md5": null, + "sha256": null, + "sha512": null, + "bug_tracking_url": "https://github.com/Appspia/ASNetworking/issues", + "code_view_url": "https://github.com/Appspia/ASNetworking", + "vcs_url": "https://github.com/Appspia/ASNetworking.git", + "copyright": null, + "license_expression": null, + "declared_license": { + "type": "MIT", + "file": "LICENSE" + }, + "notice_text": null, + "root_path": null, + "dependencies": [], + "contains_source_code": null, + "source_packages": [], + "purl": "pkg:cocoapods/ASNetworking@0.1.3" + }, + { + "type": "cocoapods", + "namespace": null, + "name": "ASNetworking", + "version": "0.1.2", + "qualifiers": {}, + "subpath": null, + "repository_homepage_url": "https://cocoapods.org/pods/ASNetworking", + "repository_download_url": null, + "api_data_url": null, + "primary_language": "Swift", + "description": null, + "release_date": null, + "parties": [], + "keywords": [], + "homepage_url": "https://github.com/Appspia/ASNetworking", + "download_url": "https://github.com/Appspia/ASNetworking/archive/refs/tags/0.1.2.tar.gz", + "api_url": "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/5/5/b/ASNetworking/0.1.2/ASNetworking.podspec.json", + "size": null, + "sha1": null, + "md5": null, + "sha256": null, + "sha512": null, + "bug_tracking_url": "https://github.com/Appspia/ASNetworking/issues", + "code_view_url": "https://github.com/Appspia/ASNetworking", + "vcs_url": "https://github.com/Appspia/ASNetworking.git", + "copyright": null, + "license_expression": null, + "declared_license": { + "type": "MIT", + "file": "LICENSE" + }, + "notice_text": null, + "root_path": null, + "dependencies": [], + "contains_source_code": null, + "source_packages": [], + "purl": "pkg:cocoapods/ASNetworking@0.1.2" + }, + { + "type": "cocoapods", + "namespace": null, + "name": "ASNetworking", + "version": "0.1.1", + "qualifiers": {}, + "subpath": null, + "repository_homepage_url": "https://cocoapods.org/pods/ASNetworking", + "repository_download_url": null, + "api_data_url": null, + "primary_language": "Swift", + "description": null, + "release_date": null, + "parties": [], + "keywords": [], + "homepage_url": "https://github.com/Appspia/ASNetworking", + "download_url": "https://github.com/Appspia/ASNetworking/archive/refs/tags/0.1.1.tar.gz", + "api_url": "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/5/5/b/ASNetworking/0.1.1/ASNetworking.podspec.json", + "size": null, + "sha1": null, + "md5": null, + "sha256": null, + "sha512": null, + "bug_tracking_url": "https://github.com/Appspia/ASNetworking/issues", + "code_view_url": "https://github.com/Appspia/ASNetworking", + "vcs_url": "https://github.com/Appspia/ASNetworking.git", + "copyright": null, + "license_expression": null, + "declared_license": { + "type": "MIT", + "file": "LICENSE" + }, + "notice_text": null, + "root_path": null, + "dependencies": [], + "contains_source_code": null, + "source_packages": [], + "purl": "pkg:cocoapods/ASNetworking@0.1.1" + }, + { + "type": "cocoapods", + "namespace": null, + "name": "ASNetworking", + "version": "0.1.0", + "qualifiers": {}, + "subpath": null, + "repository_homepage_url": "https://cocoapods.org/pods/ASNetworking", + "repository_download_url": null, + "api_data_url": null, + "primary_language": "Swift", + "description": null, + "release_date": null, + "parties": [], + "keywords": [], + "homepage_url": "https://github.com/Appspia/ASNetworking", + "download_url": "https://github.com/Appspia/ASNetworking/archive/refs/tags/0.1.0.tar.gz", + "api_url": "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/5/5/b/ASNetworking/0.1.0/ASNetworking.podspec.json", + "size": null, + "sha1": null, + "md5": null, + "sha256": null, + "sha512": null, + "bug_tracking_url": "https://github.com/Appspia/ASNetworking/issues", + "code_view_url": "https://github.com/Appspia/ASNetworking", + "vcs_url": "https://github.com/Appspia/ASNetworking.git", + "copyright": null, + "license_expression": null, + "declared_license": { + "type": "MIT", + "file": "LICENSE" + }, + "notice_text": null, + "root_path": null, + "dependencies": [], + "contains_source_code": null, + "source_packages": [], + "purl": "pkg:cocoapods/ASNetworking@0.1.0" + } +] diff --git a/tests/test_package.py b/tests/test_package.py index 468917f..46dd9bd 100644 --- a/tests/test_package.py +++ b/tests/test_package.py @@ -109,17 +109,62 @@ def test_tuby_package_with_invalid_url(mock_get): assert "Failed to fetch: https://rubygems.org/api/v1/gems/file.json" == e_info +@mock.patch("fetchcode.package_util.utils.make_head_request") +@mock.patch("fetchcode.package_util.utils.get_github_rest") @mock.patch("fetchcode.package_util.utils.get_response") +@mock.patch("fetchcode.package.get_cocoapod_tags") +@mock.patch("fetchcode.package.get_hashed_path") +def test_cocoapods_packages( + mock_get_hashed_path, + mock_get_cocoapod_tags, + mock_get_response, + mock_get_github_rest, + mock_make_head_request, +): + mock_get_hashed_path.return_value = "5/5/b" + + mock_get_cocoapod_tags.return_value = [ + '0.1.5', + '0.1.4', + '0.1.3', + '0.1.2', + '0.1.1', + '0.1.0', + ] + + mock_get_response.side_effect = file_json("tests/data/cocoapods/mock_get_response_side_effect.json") + + mock_get_github_rest.return_value = load_json("tests/data/cocoapods/mock_get_github_rest_return_value.json") + + mock_response = mock.Mock() + mock_response.status_code = 200 + mock_make_head_request.return_value = mock_response + + purl = "pkg:cocoapods/ASNetworking" + expected_data = "tests/data/cocoapods.json" + packages = list(info(purl)) + check_packages(packages, expected_data) + + +@mock.patch("fetchcode.package_util.utils.make_head_request") @mock.patch("fetchcode.package_util.utils.get_github_rest") +@mock.patch("fetchcode.package_util.utils.get_response") @mock.patch("fetchcode.package.get_cocoapod_tags") @mock.patch("fetchcode.package.get_hashed_path") def test_get_cocoapods_data_from_purl( mock_get_hashed_path, mock_get_cocoapod_tags, - mock_get_github_rest, mock_get_response, + mock_get_github_rest, + mock_make_head_request, ): + """ + This already-existing test is structurally identical to the new + test_cocoapods_packages() except this test checks the + get_cocoapods_data_from_purl() function rather than list(info(purl)). + """ mock_get_hashed_path.return_value = "5/5/b" + mock_get_cocoapod_tags.return_value = [ '0.1.5', '0.1.4', @@ -128,8 +173,15 @@ def test_get_cocoapods_data_from_purl( '0.1.1', '0.1.0', ] - mock_get_github_rest.return_value = load_json("tests/data/cocoapods/mock_get_github_rest_return_value.json") + mock_get_response.side_effect = file_json("tests/data/cocoapods/mock_get_response_side_effect.json") + + mock_get_github_rest.return_value = load_json("tests/data/cocoapods/mock_get_github_rest_return_value.json") + + mock_response = mock.Mock() + mock_response.status_code = 200 + mock_make_head_request.return_value = mock_response + expected_result_to_dict = file_json("tests/data/cocoapods/expected_result_to_dict.json") purl = "pkg:cocoapods/ASNetworking" actual_result = get_cocoapods_data_from_purl(purl) @@ -157,10 +209,17 @@ def test_get_cocoapod_tags(mock_get): @mock.patch("fetchcode.package_util.utils.get_response") +@mock.patch("fetchcode.package_util.utils.make_head_request") @mock.patch("fetchcode.package_util.utils.get_github_rest") -def test_construct_cocoapods_package(mock_get_github_rest, mock_get_response): +def test_construct_cocoapods_package(mock_get_github_rest, mock_make_head_request, mock_get_response): mock_get_github_rest.return_value = "Failed to fetch: https://api.github.com/repos/KevalPatel94/KVLLibraries" + + mock_response = mock.Mock() + mock_response.status_code = 404 + mock_make_head_request.return_value = mock_response + mock_get_response.return_value = load_json("tests/data/cocoapods/get_response_kvllibraries.json") + expected_construct_cocoapods_package = load_json("tests/data/cocoapods/expected_construct_cocoapods_package.json") purl = PackageURL.from_string("pkg:cocoapods/KVLLibraries")