Merge: fix ci nitunit some
[nit.git] / lib / github / tests / test_wallet.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_wallet is test
16
17 import wallet
18
19 redef class GithubAPI
20
21 # Tokens mocked as valid
22 #
23 # All other tokens will be considered as bad credentials.
24 var valid_tokens = ["t1", "t2"]
25
26 # Mock so it returns the response from a file
27 #
28 # See `update_responses_cache`.
29 redef fun get_auth_user do
30 if not valid_tokens.has(auth) then
31 was_error = true
32 last_error = new GithubAPIError("""{
33 "message":"Bad credentials",
34 "documentation_url":"https://developer.github.com/v3"
35 }""", 401, "/user")
36 return null
37 end
38
39 was_error = false
40 last_error = null
41 return new User("test")
42 end
43 end
44
45 class TestGithubWallet
46 test
47
48 fun test_get_next_token is test do
49 var wallet = new GithubWallet(["t1", "t2", "t3"])
50
51 for j in [1..3] do
52 for i in [1..3] do assert wallet.get_next_token == "t{i}"
53 end
54 end
55
56 fun test_get_api_only_bad is test do
57 var wallet = new GithubWallet(["bad1", "bad2"])
58 assert wallet.api.auth == "bad2"
59 assert wallet.api.auth == "bad2"
60 end
61
62 fun test_get_api is test do
63 var wallet = new GithubWallet(["bad1", "t1", "t2", "bad2"])
64 assert wallet.api.auth == "t1"
65 assert wallet.api.auth == "t2"
66 assert wallet.api.auth == "t1"
67 assert wallet.api.auth == "t2"
68 end
69 end