Skip to content

Commit 1eb1bc3

Browse files
committed
Update tests including mocking head request #116
Reference: #116 Signed-off-by: John M. Horan <johnmhoran@gmail.com>
1 parent 82f2a1e commit 1eb1bc3

File tree

2 files changed

+298
-3
lines changed

2 files changed

+298
-3
lines changed

tests/data/cocoapods.json

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
[
2+
{
3+
"type": "cocoapods",
4+
"namespace": null,
5+
"name": "ASNetworking",
6+
"version": "0.1.5",
7+
"qualifiers": {},
8+
"subpath": null,
9+
"repository_homepage_url": "https://cocoapods.org/pods/ASNetworking",
10+
"repository_download_url": null,
11+
"api_data_url": null,
12+
"primary_language": "Swift",
13+
"description": null,
14+
"release_date": null,
15+
"parties": [],
16+
"keywords": [],
17+
"homepage_url": "https://github.com/Appspia/ASNetworking",
18+
"download_url": "https://github.com/Appspia/ASNetworking/archive/refs/tags/0.1.5.tar.gz",
19+
"api_url": "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/5/5/b/ASNetworking/0.1.5/ASNetworking.podspec.json",
20+
"size": null,
21+
"sha1": null,
22+
"md5": null,
23+
"sha256": null,
24+
"sha512": null,
25+
"bug_tracking_url": "https://github.com/Appspia/ASNetworking/issues",
26+
"code_view_url": "https://github.com/Appspia/ASNetworking",
27+
"vcs_url": "https://github.com/Appspia/ASNetworking.git",
28+
"copyright": null,
29+
"license_expression": null,
30+
"declared_license": {
31+
"type": "MIT",
32+
"file": "LICENSE"
33+
},
34+
"notice_text": null,
35+
"root_path": null,
36+
"dependencies": [],
37+
"contains_source_code": null,
38+
"source_packages": [],
39+
"purl": "pkg:cocoapods/ASNetworking@0.1.5"
40+
},
41+
{
42+
"type": "cocoapods",
43+
"namespace": null,
44+
"name": "ASNetworking",
45+
"version": "0.1.4",
46+
"qualifiers": {},
47+
"subpath": null,
48+
"repository_homepage_url": "https://cocoapods.org/pods/ASNetworking",
49+
"repository_download_url": null,
50+
"api_data_url": null,
51+
"primary_language": "Swift",
52+
"description": null,
53+
"release_date": null,
54+
"parties": [],
55+
"keywords": [],
56+
"homepage_url": "https://github.com/Appspia/ASNetworking",
57+
"download_url": "https://github.com/Appspia/ASNetworking/archive/refs/tags/0.1.4.tar.gz",
58+
"api_url": "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/5/5/b/ASNetworking/0.1.4/ASNetworking.podspec.json",
59+
"size": null,
60+
"sha1": null,
61+
"md5": null,
62+
"sha256": null,
63+
"sha512": null,
64+
"bug_tracking_url": "https://github.com/Appspia/ASNetworking/issues",
65+
"code_view_url": "https://github.com/Appspia/ASNetworking",
66+
"vcs_url": "https://github.com/Appspia/ASNetworking.git",
67+
"copyright": null,
68+
"license_expression": null,
69+
"declared_license": {
70+
"type": "MIT",
71+
"file": "LICENSE"
72+
},
73+
"notice_text": null,
74+
"root_path": null,
75+
"dependencies": [],
76+
"contains_source_code": null,
77+
"source_packages": [],
78+
"purl": "pkg:cocoapods/ASNetworking@0.1.4"
79+
},
80+
{
81+
"type": "cocoapods",
82+
"namespace": null,
83+
"name": "ASNetworking",
84+
"version": "0.1.3",
85+
"qualifiers": {},
86+
"subpath": null,
87+
"repository_homepage_url": "https://cocoapods.org/pods/ASNetworking",
88+
"repository_download_url": null,
89+
"api_data_url": null,
90+
"primary_language": "Swift",
91+
"description": null,
92+
"release_date": null,
93+
"parties": [],
94+
"keywords": [],
95+
"homepage_url": "https://github.com/Appspia/ASNetworking",
96+
"download_url": "https://github.com/Appspia/ASNetworking/archive/refs/tags/0.1.3.tar.gz",
97+
"api_url": "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/5/5/b/ASNetworking/0.1.3/ASNetworking.podspec.json",
98+
"size": null,
99+
"sha1": null,
100+
"md5": null,
101+
"sha256": null,
102+
"sha512": null,
103+
"bug_tracking_url": "https://github.com/Appspia/ASNetworking/issues",
104+
"code_view_url": "https://github.com/Appspia/ASNetworking",
105+
"vcs_url": "https://github.com/Appspia/ASNetworking.git",
106+
"copyright": null,
107+
"license_expression": null,
108+
"declared_license": {
109+
"type": "MIT",
110+
"file": "LICENSE"
111+
},
112+
"notice_text": null,
113+
"root_path": null,
114+
"dependencies": [],
115+
"contains_source_code": null,
116+
"source_packages": [],
117+
"purl": "pkg:cocoapods/ASNetworking@0.1.3"
118+
},
119+
{
120+
"type": "cocoapods",
121+
"namespace": null,
122+
"name": "ASNetworking",
123+
"version": "0.1.2",
124+
"qualifiers": {},
125+
"subpath": null,
126+
"repository_homepage_url": "https://cocoapods.org/pods/ASNetworking",
127+
"repository_download_url": null,
128+
"api_data_url": null,
129+
"primary_language": "Swift",
130+
"description": null,
131+
"release_date": null,
132+
"parties": [],
133+
"keywords": [],
134+
"homepage_url": "https://github.com/Appspia/ASNetworking",
135+
"download_url": "https://github.com/Appspia/ASNetworking/archive/refs/tags/0.1.2.tar.gz",
136+
"api_url": "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/5/5/b/ASNetworking/0.1.2/ASNetworking.podspec.json",
137+
"size": null,
138+
"sha1": null,
139+
"md5": null,
140+
"sha256": null,
141+
"sha512": null,
142+
"bug_tracking_url": "https://github.com/Appspia/ASNetworking/issues",
143+
"code_view_url": "https://github.com/Appspia/ASNetworking",
144+
"vcs_url": "https://github.com/Appspia/ASNetworking.git",
145+
"copyright": null,
146+
"license_expression": null,
147+
"declared_license": {
148+
"type": "MIT",
149+
"file": "LICENSE"
150+
},
151+
"notice_text": null,
152+
"root_path": null,
153+
"dependencies": [],
154+
"contains_source_code": null,
155+
"source_packages": [],
156+
"purl": "pkg:cocoapods/ASNetworking@0.1.2"
157+
},
158+
{
159+
"type": "cocoapods",
160+
"namespace": null,
161+
"name": "ASNetworking",
162+
"version": "0.1.1",
163+
"qualifiers": {},
164+
"subpath": null,
165+
"repository_homepage_url": "https://cocoapods.org/pods/ASNetworking",
166+
"repository_download_url": null,
167+
"api_data_url": null,
168+
"primary_language": "Swift",
169+
"description": null,
170+
"release_date": null,
171+
"parties": [],
172+
"keywords": [],
173+
"homepage_url": "https://github.com/Appspia/ASNetworking",
174+
"download_url": "https://github.com/Appspia/ASNetworking/archive/refs/tags/0.1.1.tar.gz",
175+
"api_url": "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/5/5/b/ASNetworking/0.1.1/ASNetworking.podspec.json",
176+
"size": null,
177+
"sha1": null,
178+
"md5": null,
179+
"sha256": null,
180+
"sha512": null,
181+
"bug_tracking_url": "https://github.com/Appspia/ASNetworking/issues",
182+
"code_view_url": "https://github.com/Appspia/ASNetworking",
183+
"vcs_url": "https://github.com/Appspia/ASNetworking.git",
184+
"copyright": null,
185+
"license_expression": null,
186+
"declared_license": {
187+
"type": "MIT",
188+
"file": "LICENSE"
189+
},
190+
"notice_text": null,
191+
"root_path": null,
192+
"dependencies": [],
193+
"contains_source_code": null,
194+
"source_packages": [],
195+
"purl": "pkg:cocoapods/ASNetworking@0.1.1"
196+
},
197+
{
198+
"type": "cocoapods",
199+
"namespace": null,
200+
"name": "ASNetworking",
201+
"version": "0.1.0",
202+
"qualifiers": {},
203+
"subpath": null,
204+
"repository_homepage_url": "https://cocoapods.org/pods/ASNetworking",
205+
"repository_download_url": null,
206+
"api_data_url": null,
207+
"primary_language": "Swift",
208+
"description": null,
209+
"release_date": null,
210+
"parties": [],
211+
"keywords": [],
212+
"homepage_url": "https://github.com/Appspia/ASNetworking",
213+
"download_url": "https://github.com/Appspia/ASNetworking/archive/refs/tags/0.1.0.tar.gz",
214+
"api_url": "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/5/5/b/ASNetworking/0.1.0/ASNetworking.podspec.json",
215+
"size": null,
216+
"sha1": null,
217+
"md5": null,
218+
"sha256": null,
219+
"sha512": null,
220+
"bug_tracking_url": "https://github.com/Appspia/ASNetworking/issues",
221+
"code_view_url": "https://github.com/Appspia/ASNetworking",
222+
"vcs_url": "https://github.com/Appspia/ASNetworking.git",
223+
"copyright": null,
224+
"license_expression": null,
225+
"declared_license": {
226+
"type": "MIT",
227+
"file": "LICENSE"
228+
},
229+
"notice_text": null,
230+
"root_path": null,
231+
"dependencies": [],
232+
"contains_source_code": null,
233+
"source_packages": [],
234+
"purl": "pkg:cocoapods/ASNetworking@0.1.0"
235+
}
236+
]

tests/test_package.py

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,62 @@ def test_tuby_package_with_invalid_url(mock_get):
109109
assert "Failed to fetch: https://rubygems.org/api/v1/gems/file.json" == e_info
110110

111111

112+
@mock.patch("fetchcode.package_util.utils.make_head_request")
113+
@mock.patch("fetchcode.package_util.utils.get_github_rest")
112114
@mock.patch("fetchcode.package_util.utils.get_response")
115+
@mock.patch("fetchcode.package.get_cocoapod_tags")
116+
@mock.patch("fetchcode.package.get_hashed_path")
117+
def test_cocoapods_packages(
118+
mock_get_hashed_path,
119+
mock_get_cocoapod_tags,
120+
mock_get_response,
121+
mock_get_github_rest,
122+
mock_make_head_request,
123+
):
124+
mock_get_hashed_path.return_value = "5/5/b"
125+
126+
mock_get_cocoapod_tags.return_value = [
127+
'0.1.5',
128+
'0.1.4',
129+
'0.1.3',
130+
'0.1.2',
131+
'0.1.1',
132+
'0.1.0',
133+
]
134+
135+
mock_get_response.side_effect = file_json("tests/data/cocoapods/mock_get_response_side_effect.json")
136+
137+
mock_get_github_rest.return_value = load_json("tests/data/cocoapods/mock_get_github_rest_return_value.json")
138+
139+
mock_response = mock.Mock()
140+
mock_response.status_code = 200
141+
mock_make_head_request.return_value = mock_response
142+
143+
purl = "pkg:cocoapods/ASNetworking"
144+
expected_data = "tests/data/cocoapods.json"
145+
packages = list(info(purl))
146+
check_packages(packages, expected_data)
147+
148+
149+
@mock.patch("fetchcode.package_util.utils.make_head_request")
113150
@mock.patch("fetchcode.package_util.utils.get_github_rest")
151+
@mock.patch("fetchcode.package_util.utils.get_response")
114152
@mock.patch("fetchcode.package.get_cocoapod_tags")
115153
@mock.patch("fetchcode.package.get_hashed_path")
116154
def test_get_cocoapods_data_from_purl(
117155
mock_get_hashed_path,
118156
mock_get_cocoapod_tags,
119-
mock_get_github_rest,
120157
mock_get_response,
158+
mock_get_github_rest,
159+
mock_make_head_request,
121160
):
161+
"""
162+
This already-existing test is structurally identical to the new
163+
test_cocoapods_packages() except this test checks the
164+
get_cocoapods_data_from_purl() function rather than list(info(purl)).
165+
"""
122166
mock_get_hashed_path.return_value = "5/5/b"
167+
123168
mock_get_cocoapod_tags.return_value = [
124169
'0.1.5',
125170
'0.1.4',
@@ -128,8 +173,15 @@ def test_get_cocoapods_data_from_purl(
128173
'0.1.1',
129174
'0.1.0',
130175
]
131-
mock_get_github_rest.return_value = load_json("tests/data/cocoapods/mock_get_github_rest_return_value.json")
176+
132177
mock_get_response.side_effect = file_json("tests/data/cocoapods/mock_get_response_side_effect.json")
178+
179+
mock_get_github_rest.return_value = load_json("tests/data/cocoapods/mock_get_github_rest_return_value.json")
180+
181+
mock_response = mock.Mock()
182+
mock_response.status_code = 200
183+
mock_make_head_request.return_value = mock_response
184+
133185
expected_result_to_dict = file_json("tests/data/cocoapods/expected_result_to_dict.json")
134186
purl = "pkg:cocoapods/ASNetworking"
135187
actual_result = get_cocoapods_data_from_purl(purl)
@@ -157,10 +209,17 @@ def test_get_cocoapod_tags(mock_get):
157209

158210

159211
@mock.patch("fetchcode.package_util.utils.get_response")
212+
@mock.patch("fetchcode.package_util.utils.make_head_request")
160213
@mock.patch("fetchcode.package_util.utils.get_github_rest")
161-
def test_construct_cocoapods_package(mock_get_github_rest, mock_get_response):
214+
def test_construct_cocoapods_package(mock_get_github_rest, mock_make_head_request, mock_get_response):
162215
mock_get_github_rest.return_value = "Failed to fetch: https://api.github.com/repos/KevalPatel94/KVLLibraries"
216+
217+
mock_response = mock.Mock()
218+
mock_response.status_code = 404
219+
mock_make_head_request.return_value = mock_response
220+
163221
mock_get_response.return_value = load_json("tests/data/cocoapods/get_response_kvllibraries.json")
222+
164223
expected_construct_cocoapods_package = load_json("tests/data/cocoapods/expected_construct_cocoapods_package.json")
165224

166225
purl = PackageURL.from_string("pkg:cocoapods/KVLLibraries")

0 commit comments

Comments
 (0)