967b6310c96f37854d52eac5f79de8d3db34d9f3
1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 module test_api
is test
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.
24 # Cache files can be automatically created and updated by setting
25 # `update_responses_cache` to `true` then running `nitunit`.
29 # Mock so it returns the response from a file
31 # See `update_responses_cache`.
32 redef fun send
(method
, path
, headers
, body
) do
33 print path
# for debugging
35 assert has_response
(path
)
37 if update_responses_cache
then
38 var file
= response_file
(path
)
39 save_actual_response
(path
, file
)
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
,
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/pulls/945/comments?page=1&per_page=3"] = "repo_pulls_945_comments"
75 map
["/repos/nitlang/nit/pulls/comments/21010363"] = "repo_pulls_comment_21010363"
76 map
["/repos/nitlang/nit/commits/64ce1f"] = "repo_commits_64ce1f"
77 map
["/repos/nitlang/nit/commits/4e3c688d/status"] = "repo_commits_4e3c68_status"
78 map
["/repos/nitlang/nit/comments/8982707"] = "repo_comments_8982707"
79 map
["/search/issues?q=foo repo:nitlang/nit&page=1&per_page=3"] = "repo_search_issues_nit"
81 map
["/users/not_found/not_found"] = "errors_404"
85 # Does `self` have a mock response for Github `path`?
86 fun has_response
(path
: String): Bool do
87 return test_responses
.has_key
(path
)
90 # Root responses cache directory
91 var responses_dir
: String is lazy
do
92 var path
= "NIT_TESTING_PATH".environ
.dirname
/ "mock"
97 # Returns the response file path for a Github `path`
98 fun response_file
(path
: String): String do
99 assert has_response
(path
)
100 return "{responses_dir / test_responses[path]}.res"
103 # Returns the response body string for a Github `path`
104 fun response_string
(path
: String): String do
105 var file
= response_file
(path
)
106 assert file
.file_exists
107 return file
.to_path
.read_all
110 # Is this response a simulated error?
111 fun response_is_error
(path
: String): Bool do
112 assert has_response
(path
)
113 return test_responses
[path
].has_prefix
("errors_")
116 # Status code of a simulated error
118 # See `response_is_error`.
119 fun response_code
(path
: String): String do
120 assert response_is_error
(path
)
121 return test_responses
[path
].split
("_").last
128 # Change this value to `true` then run nitunit to cache the responses
129 # from the Github API.
131 # Default is `false`.
132 var update_responses_cache
= false
134 # Save the actual Github API response body for `uri` to a `file`
135 private fun save_actual_response
(uri
, file
: String) do
136 assert update_responses_cache
138 var request
= new CurlHTTPRequest("{api_url}{sanitize_uri(uri)}")
139 request
.user_agent
= actual_api
.user_agent
140 request
.headers
= actual_api
.new_headers
141 var response
= request
.execute
143 if response
isa CurlResponseSuccess then
144 response
.body_str
.write_to_file
(file
)
145 else if response
isa CurlResponseFailed then
146 response
.error_msg
.write_to_file
(file
)
149 print
"Response to `{uri}` saved at `{file}`"
152 # Actual GithubCurl instance used for caching
153 private var actual_api
= new GithubAPI(get_github_oauth
, "nitunit")
159 fun api
: MockGithubAPI do return new MockGithubAPI("test", "test")
161 fun test_deserialize
is test
do
162 var response
= api
.response_string
("/users/Morriar")
163 var obj
= api
.deserialize
(response
)
165 assert obj
.login
== "Morriar"
168 fun test_sanitize_url
is test
do
170 assert api
.sanitize_uri
("/repos/Nit with spaces/") == "/repos/Nit%20with%20spaces/"
173 fun test_get
is test
do
175 var obj
= api
.get
("/users/Morriar")
176 assert not api
.was_error
177 assert api
.last_error
== null
179 assert obj
.login
== "Morriar"
182 fun test_get_404
is test
do
184 var res
= api
.get
("/users/not_found/not_found")
187 var err
= api
.last_error
188 assert err
isa GithubAPIError
189 assert err
.status_code
== 404
190 assert err
.message
== "Not Found"
193 # TODO test more error cases
195 fun test_get_auth_user
is test
do
196 var user
= api
.get_auth_user
198 assert user
.login
== "Morriar"
199 assert user
.avatar_url
== "https://avatars2.githubusercontent.com/u/583144?v=4"
200 assert user
.name
== "Alexandre Terrasa"
201 assert user
.email
== "alexandre@moz-code.org"
202 assert user
.blog
== "moz-code.org"
205 fun test_get_user
is test
do
206 var user
= api
.get_user
("Morriar")
208 assert user
.login
== "Morriar"
209 assert user
.avatar_url
== "https://avatars2.githubusercontent.com/u/583144?v=4"
210 assert user
.name
== "Alexandre Terrasa"
211 assert user
.email
== "alexandre@moz-code.org"
212 assert user
.blog
== "moz-code.org"
215 fun test_get_repo
is test
do
216 var repo
= api
.get_repo
("nitlang/nit")
218 assert repo
.full_name
== "nitlang/nit"
219 assert repo
.name
== "nit"
220 assert repo
.owner
.login
== "nitlang"
221 assert repo
.default_branch
== "master"
224 fun test_get_branches
is test
do
225 var branches
= api
.get_repo_branches
("nitlang/nit", 1, 2)
226 assert branches
.length
== 2
227 assert branches
.first
.name
== "master"
228 assert branches
.last
.name
== "next"
231 fun test_get_issues
is test
do
232 var issues
= api
.get_repo_issues
("nitlang/nit", 1, 3)
233 assert issues
.length
== 3
234 assert issues
.first
.title
== "nitrpg: Move `nitrpg` to its own repository"
235 assert issues
.last
.title
== "Mock Github API tests"
238 fun test_search_issues
is test
do
239 var results
= api
.search_repo_issues
("nitlang/nit", "foo", 1, 3)
240 assert results
isa SearchResults
241 assert results
.items
.length
== 3
242 assert results
.items
.first
.as(Issue).title
== "Introduction of contracts in Nit"
243 assert results
.items
.last
.as(Issue).title
== "Appel de méthodes abstraites non redéfinies"
246 fun test_get_labels
is test
do
247 var labels
= api
.get_repo_labels
("nitlang/nit", 1, 3)
248 assert labels
.length
== 3
249 assert labels
.first
.name
== "API"
250 assert labels
.last
.name
== "NEP"
253 fun test_get_milestones
is test
do
254 var milestones
= api
.get_repo_milestones
("nitlang/nit", 1, 3)
255 assert milestones
.length
== 3
256 assert milestones
.first
.title
== "v1.0prealpha"
257 assert milestones
.last
.title
== "nitdoc - Abstraction levels"
260 fun test_get_pulls
is test
do
261 var pulls
= api
.get_repo_pulls
("nitlang/nit", 1, 3)
262 assert pulls
.length
== 3
263 assert pulls
.first
.title
== "nitrpg: Move `nitrpg` to its own repository"
264 assert pulls
.last
.title
== "Mock Github API tests"
269 fun test_get_branch
is test
do
270 var branch
= api
.get_branch
("nitlang/nit", "master")
271 assert branch
isa Branch
272 assert branch
.name
== "master"
275 fun test_get_commit
is test
do
276 var commit
= api
.get_commit
("nitlang/nit", "64ce1f")
277 assert commit
isa Commit
278 assert commit
.sha
== "64ce1f587209024f5de46d06c70526a569ff537f"
282 fun test_get_commit_status
is test
do
283 var status
= api
.get_commit_status
("nitlang/nit", "4e3c688d")
284 assert status
isa CommitStatus
285 assert status
.state
== "failure"
286 assert status
.sha
== "4e3c688d2c4b875c00f206eb4c4b6f2c4f34c096"
287 assert status
.total_count
== 1
289 var sub
= status
.statuses
.first
290 assert sub
.state
== "failure"
291 assert sub
.description
== "Merged pipeline on gitlab: failed"
292 assert sub
.context
== "gitlab-ci"
294 var repo
= status
.repository
296 assert repo
.full_name
== "nitlang/nit"
299 fun test_get_issue
is test
do
300 var issue
= api
.get_issue
("nitlang/nit", 1000)
301 assert issue
isa Issue
302 assert issue
.number
== 1000
303 assert issue
.title
== "Raise nitc from the dead"
304 assert issue
.user
.as(User).login
== "privat"
305 assert issue
.comments
== 7
306 assert issue
.created_at
== "2014-12-11T02:55:09Z"
307 assert issue
.closed_at
== "2014-12-13T15:38:09Z"
308 assert issue
.closed_by
.as(User).login
== "privat"
309 assert issue
.body
== "Raise dead on `nitc`.\nIt's super effective...\n"
310 assert issue
.is_pull_request
313 fun test_get_issue_comments
is test
do
314 var comments
= api
.get_issue_comments
("nitlang/nit", 1000, 1, 3)
315 assert comments
.length
== 3
316 assert comments
.first
.user
.login
== "R4PaSs"
317 assert comments
.last
.user
.login
== "xymus"
320 fun test_get_issue_events
is test
do
321 var events
= api
.get_issue_events
("nitlang/nit", 1000, 1, 3)
322 assert events
.length
== 3
323 assert events
.first
.actor
.login
== "privat"
324 assert events
.last
.actor
.login
== "xymus"
327 fun test_get_pull
is test
do
328 var pull
= api
.get_pull
("nitlang/nit", 1000)
329 assert pull
isa PullRequest
330 assert pull
.number
== 1000
331 assert pull
.title
== "Raise nitc from the dead"
332 assert pull
.user
.as(User).login
== "privat"
333 assert pull
.comments
== 7
334 assert pull
.created_at
== "2014-12-11T02:55:09Z"
335 assert pull
.closed_at
== "2014-12-13T15:38:09Z"
336 assert pull
.merged_by
.as(User).login
== "privat"
337 assert pull
.body
== "Raise dead on `nitc`.\nIt's super effective...\n"
340 fun test_get_pull_comments
is test
do
341 var comments
= api
.get_pull_comments
("nitlang/nit", 945, 1, 3)
342 assert comments
.length
== 2
343 assert comments
.first
.path
== "src/modelize/modelize_property.nit"
346 fun test_get_pull_comment
is test
do
347 var comment
= api
.get_pull_comment
("nitlang/nit", 21010363)
348 assert comment
isa PullComment
349 assert comment
.path
== "src/modelize/modelize_property.nit"
350 assert comment
.original_position
== 26
351 assert comment
.pull_number
== 945
355 fun test_get_label
is test
do
356 var labl
= api
.get_label
("nitlang/nit", "ok_will_merge")
357 assert labl
isa Label
358 assert labl
.name
== "ok_will_merge"
361 fun test_get_milestone
is test
do
362 var milestone
= api
.get_milestone
("nitlang/nit", 4)
363 assert milestone
isa Milestone
364 assert milestone
.title
== "v1.0prealpha"
368 fun test_get_issue_event
is test
do
369 var event
= api
.get_issue_event
("nitlang/nit", 199674194)
370 assert event
isa IssueEvent
371 assert event
.actor
.login
== "privat"
372 assert event
.event
== "labeled"
373 assert event
.labl
.as(Label).name
== "need_review"
376 fun test_get_issue_comment
is test
do
377 var comment
= api
.get_issue_comment
("nitlang/nit", 6020149)
378 assert comment
isa IssueComment
379 assert comment
.user
.login
== "privat"
380 assert comment
.created_at
.to_s
== "2012-05-30T20:16:54Z"
381 assert comment
.issue_number
== 10
384 fun test_get_comment
is test
do
385 var comment
= api
.get_commit_comment
("nitlang/nit", 8982707)
386 assert comment
isa CommitComment
387 assert comment
.user
.login
== "Morriar"
388 assert comment
.body
== "For testing purposes...\n"
389 assert comment
.commit_id
== "7eacb86d1e24b7e72bc9ac869bf7182c0300ceca"