github: Add mock testing for GithubWallet
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 9 Jul 2019 00:44:28 +0000 (20:44 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Thu, 11 Jul 2019 02:07:01 +0000 (22:07 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

lib/github/tests/test_wallet.nit [new file with mode: 0644]
lib/github/wallet.nit

diff --git a/lib/github/tests/test_wallet.nit b/lib/github/tests/test_wallet.nit
new file mode 100644 (file)
index 0000000..d4d2c4d
--- /dev/null
@@ -0,0 +1,69 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+module test_wallet is test
+
+import wallet
+
+redef class GithubAPI
+
+       # Tokens mocked as valid
+       #
+       # All other tokens will be considered as bad credentials.
+       var valid_tokens = ["t1", "t2"]
+
+       # Mock so it returns the response from a file
+       #
+       # See `update_responses_cache`.
+       redef fun get_auth_user do
+               if not valid_tokens.has(auth) then
+                       was_error = true
+                       last_error = new GithubAPIError("""{
+                               "message":"Bad credentials",
+                               "documentation_url":"https://developer.github.com/v3"
+                       }""", 401, "/user")
+                       return null
+               end
+
+               was_error = false
+               last_error = null
+               return new User("test")
+       end
+end
+
+class TestGithubWallet
+       test
+
+       fun test_get_next_token is test do
+               var wallet = new GithubWallet.from_tokens(["t1", "t2", "t3"])
+
+               for j in [1..3] do
+                       for i in [1..3] do assert wallet.get_next_token == "t{i}"
+               end
+       end
+
+       fun test_get_api_only_bad is test do
+               var wallet = new GithubWallet(["bad1", "bad2"])
+               assert wallet.api.auth == "bad2"
+               assert wallet.api.auth == "bad2"
+       end
+
+       fun test_get_api is test do
+               var wallet = new GithubWallet.from_tokens(["bad1", "t1", "t2", "bad2"])
+               assert wallet.api.auth == "t1"
+               assert wallet.api.auth == "t2"
+               assert wallet.api.auth == "t1"
+               assert wallet.api.auth == "t2"
+       end
+end
index ecad4c2..68dc166 100644 (file)
@@ -153,7 +153,7 @@ class GithubWallet
        fun check_token(token: String): Bool do
                message "Try token {token}"
                var api = new GithubAPI(token)
-               api.get_repo("nitlang/nit")
+               api.get_auth_user
                return not api.was_error
        end