89d6960e31adf1ade87e37cc2342f100eeafeb77
[nit.git] / lib / github / tests / test_api.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 module test_api is test
16
17 intrude import api
18
19 # GithubAPI testing
20 #
21 # To avoid test flakyness we test the GithubAPI against a mock of the real one.
22 # For each api request we return a cache file of the real API response body.
23 #
24 # Cache files can be automatically created and updated by setting
25 # `update_responses_cache` to `true` then running `nitunit`.
26 class MockGithubAPI
27 super GithubAPI
28
29 # Mock so it returns the response from a file
30 #
31 # See `update_responses_cache`.
32 redef fun send(method, path, headers, body) do
33 print path # for debugging
34
35 assert has_response(path)
36
37 if update_responses_cache then
38 var file = response_file(path)
39 save_actual_response(path, file)
40 end
41
42 var response = response_string(path)
43 if response_is_error(path) then
44 last_error = new GithubAPIError(
45 response.parse_json.as(JsonObject)["message"].as(String),
46 response_code(path).to_i,
47 path
48 )
49 was_error = true
50 return null
51 end
52 return response
53 end
54
55 var test_responses: Map[String, String] do
56 var map = new HashMap[String, String]
57 map["/user"] = "user_Morriar"
58 map["/users/Morriar"] = "user_Morriar"
59 map["/repos/nitlang/nit"] = "repo_nit"
60 map["/repos/nitlang/nit/labels?page=1&per_page=3"] = "repo_labels_nit"
61 map["/repos/nitlang/nit/labels/ok_will_merge"] = "repo_labels_ok_will_merge"
62 map["/repos/nitlang/nit/milestones?page=1&per_page=3"] = "repo_milestones_nit"
63 map["/repos/nitlang/nit/milestones/4"] = "repo_milestones_4"
64 map["/repos/nitlang/nit/branches?page=1&per_page=2"] = "repo_branches_nit"
65 map["/repos/nitlang/nit/branches/master"] = "repo_branches_master"
66 map["/repos/nitlang/nit/issues?page=1&per_page=3"] = "repo_issues_nit"
67 map["/repos/nitlang/nit/issues/1000"] = "repo_issues_1000"
68 map["/repos/nitlang/nit/issues/1000/comments?page=1&per_page=3"] = "repo_issues_comments_nit"
69 map["/repos/nitlang/nit/issues/comments/6020149"] = "repo_issues_comments_6020149"
70 map["/repos/nitlang/nit/issues/1000/events?page=1&per_page=3"] = "repo_issues_events_nit"
71 map["/repos/nitlang/nit/issues/events/199674194"] = "repo_issues_events_199674194"
72 map["/repos/nitlang/nit/pulls?page=1&per_page=3"] = "repo_pulls_nit"
73 map["/repos/nitlang/nit/pulls/1000"] = "repo_pulls_1000"
74 map["/repos/nitlang/nit/commits/64ce1f"] = "repo_commits_64ce1f"
75 map["/repos/nitlang/nit/commits/4e3c688d/status"] = "repo_commits_4e3c68_status"
76 map["/repos/nitlang/nit/comments/8982707"] = "repo_comments_8982707"
77 map["/repos/nitlang/nit/pulls/comments/21010363"] = "repo_pulls_comment_21010363"
78 map["/search/issues?q=foo repo:nitlang/nit&page=1&per_page=3"] = "repo_search_issues_nit"
79 # errors
80 map["/users/not_found/not_found"] = "errors_404"
81 return map
82 end
83
84 # Does `self` have a mock response for Github `path`?
85 fun has_response(path: String): Bool do
86 return test_responses.has_key(path)
87 end
88
89 # Root responses cache directory
90 var responses_dir: String is lazy do
91 var path = "NIT_TESTING_PATH".environ.dirname / "mock"
92 path.mkdir
93 return path
94 end
95
96 # Returns the response file path for a Github `path`
97 fun response_file(path: String): String do
98 assert has_response(path)
99 return "{responses_dir / test_responses[path]}.res"
100 end
101
102 # Returns the response body string for a Github `path`
103 fun response_string(path: String): String do
104 var file = response_file(path)
105 assert file.file_exists
106 return file.to_path.read_all
107 end
108
109 # Is this response a simulated error?
110 fun response_is_error(path: String): Bool do
111 assert has_response(path)
112 return test_responses[path].has_prefix("errors_")
113 end
114
115 # Status code of a simulated error
116 #
117 # See `response_is_error`.
118 fun response_code(path: String): String do
119 assert response_is_error(path)
120 return test_responses[path].split("_").last
121 end
122
123 # Response caching
124
125 # Activate caching
126 #
127 # Change this value to `true` then run nitunit to cache the responses
128 # from the Github API.
129 #
130 # Default is `false`.
131 var update_responses_cache = false
132
133 # Save the actual Github API response body for `uri` to a `file`
134 private fun save_actual_response(uri, file: String) do
135 assert update_responses_cache
136
137 var request = new CurlHTTPRequest("{api_url}{sanitize_uri(uri)}")
138 request.user_agent = actual_api.user_agent
139 request.headers = actual_api.new_headers
140 var response = request.execute
141
142 if response isa CurlResponseSuccess then
143 response.body_str.write_to_file(file)
144 else if response isa CurlResponseFailed then
145 response.error_msg.write_to_file(file)
146 else abort
147
148 print "Response to `{uri}` saved at `{file}`"
149 end
150
151 # Actual GithubCurl instance used for caching
152 private var actual_api = new GithubAPI(get_github_oauth, "nitunit")
153 end
154
155 class TestGithubAPI
156 test
157
158 fun api: MockGithubAPI do return new MockGithubAPI("test", "test")
159
160 fun test_deserialize is test do
161 var response = api.response_string("/users/Morriar")
162 var obj = api.deserialize(response)
163 assert obj isa User
164 assert obj.login == "Morriar"
165 end
166
167 fun test_sanitize_url is test do
168 # TODO better tests
169 assert api.sanitize_uri("/repos/Nit with spaces/") == "/repos/Nit%20with%20spaces/"
170 end
171
172 fun test_get is test do
173 var api = self.api
174 var obj = api.get("/users/Morriar")
175 assert not api.was_error
176 assert api.last_error == null
177 assert obj isa User
178 assert obj.login == "Morriar"
179 end
180
181 fun test_get_404 is test do
182 var api = self.api
183 var res = api.get("/users/not_found/not_found")
184 assert res == null
185 assert api.was_error
186 var err = api.last_error
187 assert err isa GithubAPIError
188 assert err.status_code == 404
189 assert err.message == "Not Found"
190 end
191
192 # TODO test more error cases
193
194 fun test_get_auth_user is test do
195 var user = api.get_auth_user
196 assert user isa User
197 assert user.login == "Morriar"
198 assert user.avatar_url == "https://avatars2.githubusercontent.com/u/583144?v=4"
199 assert user.name == "Alexandre Terrasa"
200 assert user.email == "alexandre@moz-code.org"
201 assert user.blog == "moz-code.org"
202 end
203
204 fun test_get_user is test do
205 var user = api.get_user("Morriar")
206 assert user isa User
207 assert user.login == "Morriar"
208 assert user.avatar_url == "https://avatars2.githubusercontent.com/u/583144?v=4"
209 assert user.name == "Alexandre Terrasa"
210 assert user.email == "alexandre@moz-code.org"
211 assert user.blog == "moz-code.org"
212 end
213
214 fun test_get_repo is test do
215 var repo = api.get_repo("nitlang/nit")
216 assert repo isa Repo
217 assert repo.full_name == "nitlang/nit"
218 assert repo.name == "nit"
219 assert repo.owner.login == "nitlang"
220 assert repo.default_branch == "master"
221 end
222
223 fun test_get_branches is test do
224 var branches = api.get_repo_branches("nitlang/nit", 1, 2)
225 assert branches.length == 2
226 assert branches.first.name == "master"
227 assert branches.last.name == "next"
228 end
229
230 fun test_get_issues is test do
231 var issues = api.get_repo_issues("nitlang/nit", 1, 3)
232 assert issues.length == 3
233 assert issues.first.title == "nitrpg: Move `nitrpg` to its own repository"
234 assert issues.last.title == "Mock Github API tests"
235 end
236
237 fun test_search_issues is test do
238 var results = api.search_repo_issues("nitlang/nit", "foo", 1, 3)
239 assert results isa SearchResults
240 assert results.items.length == 3
241 assert results.items.first.as(Issue).title == "Introduction of contracts in Nit"
242 assert results.items.last.as(Issue).title == "Appel de méthodes abstraites non redéfinies"
243 end
244
245 fun test_get_labels is test do
246 var labels = api.get_repo_labels("nitlang/nit", 1, 3)
247 assert labels.length == 3
248 assert labels.first.name == "API"
249 assert labels.last.name == "NEP"
250 end
251
252 fun test_get_milestones is test do
253 var milestones = api.get_repo_milestones("nitlang/nit", 1, 3)
254 assert milestones.length == 3
255 assert milestones.first.title == "v1.0prealpha"
256 assert milestones.last.title == "nitdoc - Abstraction levels"
257 end
258
259 fun test_get_pulls is test do
260 var pulls = api.get_repo_pulls("nitlang/nit", 1, 3)
261 assert pulls.length == 3
262 assert pulls.first.title == "nitrpg: Move `nitrpg` to its own repository"
263 assert pulls.last.title == "Mock Github API tests"
264 end
265
266 # TODO contrib_stats
267
268 fun test_get_branch is test do
269 var branch = api.get_branch("nitlang/nit", "master")
270 assert branch isa Branch
271 assert branch.name == "master"
272 end
273
274 fun test_get_commit is test do
275 var commit = api.get_commit("nitlang/nit", "64ce1f")
276 assert commit isa Commit
277 assert commit.sha == "64ce1f587209024f5de46d06c70526a569ff537f"
278 # TODO other fields
279 end
280
281 fun test_get_commit_status is test do
282 var status = api.get_commit_status("nitlang/nit", "4e3c688d")
283 assert status isa CommitStatus
284 assert status.state == "failure"
285 assert status.sha == "4e3c688d2c4b875c00f206eb4c4b6f2c4f34c096"
286 assert status.total_count == 1
287
288 var sub = status.statuses.first
289 assert sub.state == "failure"
290 assert sub.description == "Merged pipeline on gitlab: failed"
291 assert sub.context == "gitlab-ci"
292
293 var repo = status.repository
294 assert repo isa Repo
295 assert repo.full_name == "nitlang/nit"
296 end
297
298 fun test_get_issue is test do
299 var issue = api.get_issue("nitlang/nit", 1000)
300 assert issue isa Issue
301 assert issue.number == 1000
302 assert issue.title == "Raise nitc from the dead"
303 assert issue.user.as(User).login == "privat"
304 assert issue.comments == 7
305 assert issue.created_at == "2014-12-11T02:55:09Z"
306 assert issue.closed_at == "2014-12-13T15:38:09Z"
307 assert issue.closed_by.as(User).login == "privat"
308 assert issue.body == "Raise dead on `nitc`.\nIt's super effective...\n"
309 assert issue.is_pull_request
310 end
311
312 fun test_get_issue_comments is test do
313 var comments = api.get_issue_comments("nitlang/nit", 1000, 1, 3)
314 assert comments.length == 3
315 assert comments.first.user.login == "R4PaSs"
316 assert comments.last.user.login == "xymus"
317 end
318
319 fun test_get_issue_events is test do
320 var events = api.get_issue_events("nitlang/nit", 1000, 1, 3)
321 assert events.length == 3
322 assert events.first.actor.login == "privat"
323 assert events.last.actor.login == "xymus"
324 end
325
326 fun test_get_pull is test do
327 var pull = api.get_pull("nitlang/nit", 1000)
328 assert pull isa PullRequest
329 assert pull.number == 1000
330 assert pull.title == "Raise nitc from the dead"
331 assert pull.user.as(User).login == "privat"
332 assert pull.comments == 7
333 assert pull.created_at == "2014-12-11T02:55:09Z"
334 assert pull.closed_at == "2014-12-13T15:38:09Z"
335 assert pull.merged_by.as(User).login == "privat"
336 assert pull.body == "Raise dead on `nitc`.\nIt's super effective...\n"
337 end
338
339 fun test_get_pull_comment is test do
340 var comment = api.get_pull_comment("nitlang/nit", 21010363)
341 assert comment isa ReviewComment
342 assert comment.path == "src/modelize/modelize_property.nit"
343 assert comment.original_position == 26
344 assert comment.pull_number == 945
345 # TODO other fields
346 end
347
348 fun test_get_label is test do
349 var labl = api.get_label("nitlang/nit", "ok_will_merge")
350 assert labl isa Label
351 assert labl.name == "ok_will_merge"
352 end
353
354 fun test_get_milestone is test do
355 var milestone = api.get_milestone("nitlang/nit", 4)
356 assert milestone isa Milestone
357 assert milestone.title == "v1.0prealpha"
358 # TODO other fields
359 end
360
361 fun test_get_issue_event is test do
362 var event = api.get_issue_event("nitlang/nit", 199674194)
363 assert event isa IssueEvent
364 assert event.actor.login == "privat"
365 assert event.event == "labeled"
366 assert event.labl.as(Label).name == "need_review"
367 end
368
369 fun test_get_issue_comment is test do
370 var comment = api.get_issue_comment("nitlang/nit", 6020149)
371 assert comment isa IssueComment
372 assert comment.user.login == "privat"
373 assert comment.created_at.to_s == "2012-05-30T20:16:54Z"
374 assert comment.issue_number == 10
375 end
376
377 fun test_get_comment is test do
378 var comment = api.get_commit_comment("nitlang/nit", 8982707)
379 assert comment isa CommitComment
380 assert comment.user.login == "Morriar"
381 assert comment.body == "For testing purposes...\n"
382 assert comment.commit_id == "7eacb86d1e24b7e72bc9ac869bf7182c0300ceca"
383 end
384 end