From fa439da7c59423963c3acfaffe2aee7691e4940c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Ram=C3=ADrez=20Fornell?= Date: Thu, 18 May 2017 23:15:33 +0200 Subject: [PATCH] Missing unit test (#137) * finish docs set default pagination * Fixes in docs delete default pagination * minor fixes * wip create unit test * create auth unit test * create GHAuth unit test minor fixes * ready to do user test * create user and gist unit test * delete repeat test * minor fix in microsite * minor change --- .../test/scala/github4s/unit/AuthSpec.scala | 98 +++++++++++++++++++ .../github4s/unit/GHActivitiesSpec.scala | 1 - .../test/scala/github4s/unit/GHAuthSpec.scala | 73 ++++++++++++++ .../test/scala/github4s/unit/GHGistSpec.scala | 49 ++++++++++ .../scala/github4s/unit/GHReposSpec.scala | 1 - .../test/scala/github4s/unit/GHUserSpec.scala | 70 +++++++++++++ .../test/scala/github4s/unit/GistSpec.scala | 64 ++++++++++++ .../test/scala/github4s/unit/ReposSpec.scala | 32 ------ .../test/scala/github4s/unit/UserSpec.scala | 79 +++++++++++++++ .../test/scala/github4s/utils/BaseSpec.scala | 28 ++++++ .../test/scala/github4s/utils/TestData.scala | 5 +- project/ProjectPlugin.scala | 4 +- 12 files changed, 467 insertions(+), 37 deletions(-) create mode 100644 github4s/shared/src/test/scala/github4s/unit/AuthSpec.scala create mode 100644 github4s/shared/src/test/scala/github4s/unit/GHAuthSpec.scala create mode 100644 github4s/shared/src/test/scala/github4s/unit/GHGistSpec.scala create mode 100644 github4s/shared/src/test/scala/github4s/unit/GHUserSpec.scala create mode 100644 github4s/shared/src/test/scala/github4s/unit/GistSpec.scala create mode 100644 github4s/shared/src/test/scala/github4s/unit/UserSpec.scala diff --git a/github4s/shared/src/test/scala/github4s/unit/AuthSpec.scala b/github4s/shared/src/test/scala/github4s/unit/AuthSpec.scala new file mode 100644 index 000000000..95ab14c7a --- /dev/null +++ b/github4s/shared/src/test/scala/github4s/unit/AuthSpec.scala @@ -0,0 +1,98 @@ +/* + * Copyright 2016-2017 47 Degrees, LLC. + * + * 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. + */ + +package github4s.unit + +import cats.Id +import github4s.GithubResponses.{GHResponse, GHResult} +import github4s.HttpClient +import github4s.api.Auth +import github4s.free.domain._ +import github4s.utils.BaseSpec +import com.github.marklister.base64.Base64.Encoder + +class AuthSpec extends BaseSpec { + + "Auth.newAuth" should "call to httpClient.postAuth with the right parameters" in { + + val response: GHResponse[Authorization] = + Right(GHResult(authorization, okStatusCode, Map.empty)) + + val request = + """ + |{ + |"scopes": ["public_repo"], + |"note": "New access token", + |"client_id": "e8e39175648c9db8c280", + |"client_secret": "1234567890" + |}""".stripMargin + + val httpClientMock = httpClientMockPostAuth[Authorization]( + url = "authorizations", + headers = + Map("Authorization" → s"Basic ${s"rafaparadela:invalidPassword".getBytes.toBase64}"), + json = request, + response = response + ) + + val auth = new Auth[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + + auth.newAuth( + validUsername, + invalidPassword, + validScopes, + validNote, + validClientId, + invalidClientSecret, + headerUserAgent) + } + + "Auth.getAccessToken" should "call to httpClient.postOAuth with the right parameters" in { + + val response: GHResponse[OAuthToken] = + Right(GHResult(oAuthToken, okStatusCode, Map.empty)) + + val request = + s""" + |{ + |"client_id": "e8e39175648c9db8c280", + |"client_secret": "1234567890", + |"code": "code", + |"redirect_uri": "http://localhost:9000/_oauth-callback", + |"state": "$validAuthState" + |}""".stripMargin + + val httpClientMock = httpClientMockPostOAuth[OAuthToken]( + url = "http://127.0.0.1:9999/login/oauth/access_token", + json = request, + response = response + ) + + val auth = new Auth[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + + auth.getAccessToken( + validClientId, + invalidClientSecret, + validCode, + validRedirectUri, + validAuthState, + headerUserAgent) + } +} diff --git a/github4s/shared/src/test/scala/github4s/unit/GHActivitiesSpec.scala b/github4s/shared/src/test/scala/github4s/unit/GHActivitiesSpec.scala index 4ff524224..8517c540d 100644 --- a/github4s/shared/src/test/scala/github4s/unit/GHActivitiesSpec.scala +++ b/github4s/shared/src/test/scala/github4s/unit/GHActivitiesSpec.scala @@ -19,7 +19,6 @@ package github4s.unit import cats.free.Free import github4s.GithubResponses.{GHResponse, GHResult} import github4s.{GHActivities, HttpClient} -import github4s.api.Activities import github4s.app.GitHub4s import github4s.free.domain._ import github4s.utils.BaseSpec diff --git a/github4s/shared/src/test/scala/github4s/unit/GHAuthSpec.scala b/github4s/shared/src/test/scala/github4s/unit/GHAuthSpec.scala new file mode 100644 index 000000000..d377015bd --- /dev/null +++ b/github4s/shared/src/test/scala/github4s/unit/GHAuthSpec.scala @@ -0,0 +1,73 @@ +/* + * Copyright 2016-2017 47 Degrees, LLC. + * + * 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. + */ + +package github4s.unit + +import cats.free.Free +import github4s.GithubResponses.{GHResponse, GHResult} +import github4s.{GHAuth, HttpClient} +import github4s.app.GitHub4s +import github4s.free.domain._ +import github4s.utils.BaseSpec + +class GHAuthSpec extends BaseSpec { + + "Auth.newAuth" should "call to AuthOps with the right parameters" in { + + val response: Free[GitHub4s, GHResponse[Authorization]] = + Free.pure(Right(GHResult(authorization, okStatusCode, Map.empty))) + + val authOps = mock[AuthOpsTest] + (authOps.newAuth _) + .expects( + validUsername, + invalidPassword, + validScopes, + validNote, + validClientId, + invalidClientSecret) + .returns(response) + + val ghAuth = new GHAuth(None)(authOps) + ghAuth.newAuth( + validUsername, + invalidPassword, + validScopes, + validNote, + validClientId, + invalidClientSecret) + } + + "Auth.getAccessToken" should "call to AuthOps with the right parameters" in { + + val response: Free[GitHub4s, GHResponse[OAuthToken]] = + Free.pure(Right(GHResult(oAuthToken, okStatusCode, Map.empty))) + + val authOps = mock[AuthOpsTest] + (authOps.getAccessToken _) + .expects(validClientId, invalidClientSecret, validCode, validRedirectUri, validAuthState) + .returns(response) + + val ghAuth = new GHAuth(None)(authOps) + ghAuth.getAccessToken( + validClientId, + invalidClientSecret, + validCode, + validRedirectUri, + validAuthState) + } + +} diff --git a/github4s/shared/src/test/scala/github4s/unit/GHGistSpec.scala b/github4s/shared/src/test/scala/github4s/unit/GHGistSpec.scala new file mode 100644 index 000000000..21e5e244d --- /dev/null +++ b/github4s/shared/src/test/scala/github4s/unit/GHGistSpec.scala @@ -0,0 +1,49 @@ +/* + * Copyright 2016-2017 47 Degrees, LLC. + * + * 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. + */ + +package github4s.unit + +import cats.free.Free +import github4s.GithubResponses.{GHResponse, GHResult} +import github4s.{GHGists, HttpClient} +import github4s.app.GitHub4s +import github4s.free.domain._ +import github4s.utils.BaseSpec + +class GHGistSpec extends BaseSpec { + + "Gist.newGist" should "call to GistOps with the right parameters" in { + + val response: Free[GitHub4s, GHResponse[Gist]] = + Free.pure(Right(GHResult(gist, okStatusCode, Map.empty))) + + val gistOps = mock[GistOpsTest] + (gistOps.newGist _) + .expects( + validGistDescription, + validGistPublic, + Map(validGistFilename → GistFile(validGistFileContent)), + sampleToken) + .returns(response) + + val ghGists = new GHGists(sampleToken)(gistOps) + ghGists.newGist( + validGistDescription, + validGistPublic, + Map(validGistFilename → GistFile(validGistFileContent))) + } + +} diff --git a/github4s/shared/src/test/scala/github4s/unit/GHReposSpec.scala b/github4s/shared/src/test/scala/github4s/unit/GHReposSpec.scala index 61b7bc1f3..3aed7f4f2 100644 --- a/github4s/shared/src/test/scala/github4s/unit/GHReposSpec.scala +++ b/github4s/shared/src/test/scala/github4s/unit/GHReposSpec.scala @@ -21,7 +21,6 @@ import cats.free.Free import github4s.GHRepos import github4s.GithubResponses.{GHResponse, GHResult} import github4s.app.GitHub4s -import github4s.free.algebra.RepositoryOps import github4s.free.domain._ import github4s.utils.BaseSpec diff --git a/github4s/shared/src/test/scala/github4s/unit/GHUserSpec.scala b/github4s/shared/src/test/scala/github4s/unit/GHUserSpec.scala new file mode 100644 index 000000000..9eb644fa9 --- /dev/null +++ b/github4s/shared/src/test/scala/github4s/unit/GHUserSpec.scala @@ -0,0 +1,70 @@ +/* + * Copyright 2016-2017 47 Degrees, LLC. + * + * 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. + */ + +package github4s.unit + +import cats.free.Free +import github4s.GithubResponses.{GHResponse, GHResult} +import github4s.{GHUsers, HttpClient} +import github4s.app.GitHub4s +import github4s.free.domain._ +import github4s.utils.BaseSpec + +class GHUserSpec extends BaseSpec { + + "User.getUser" should "call to UserOps with the right parameters" in { + + val response: Free[GitHub4s, GHResponse[User]] = + Free.pure(Right(GHResult(user, okStatusCode, Map.empty))) + + val userOps = mock[UserOpsTest] + (userOps.getUser _) + .expects(validUsername, sampleToken) + .returns(response) + + val ghUsers = new GHUsers(sampleToken)(userOps) + ghUsers.get(validUsername) + } + + "User.getAuthUser" should "call to UserOps with the right parameters" in { + + val response: Free[GitHub4s, GHResponse[User]] = + Free.pure(Right(GHResult(user, okStatusCode, Map.empty))) + + val userOps = mock[UserOpsTest] + (userOps.getAuthUser _) + .expects(sampleToken) + .returns(response) + + val ghUsers = new GHUsers(sampleToken)(userOps) + ghUsers.getAuth + } + + "User.getUsers" should "call to UserOps with the right parameters" in { + + val response: Free[GitHub4s, GHResponse[List[User]]] = + Free.pure(Right(GHResult(List(user), okStatusCode, Map.empty))) + + val userOps = mock[UserOpsTest] + (userOps.getUsers _) + .expects(1, None, sampleToken) + .returns(response) + + val ghUsers = new GHUsers(sampleToken)(userOps) + ghUsers.getUsers(1) + } + +} diff --git a/github4s/shared/src/test/scala/github4s/unit/GistSpec.scala b/github4s/shared/src/test/scala/github4s/unit/GistSpec.scala new file mode 100644 index 000000000..61d27bf5c --- /dev/null +++ b/github4s/shared/src/test/scala/github4s/unit/GistSpec.scala @@ -0,0 +1,64 @@ +/* + * Copyright 2016-2017 47 Degrees, LLC. + * + * 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. + */ + +package github4s.unit + +import cats.Id +import github4s.GithubResponses.{GHResponse, GHResult} +import github4s.HttpClient +import github4s.api.Gists +import github4s.free.domain._ +import github4s.utils.BaseSpec + +class GistSpec extends BaseSpec { + + "Gist.newGist" should "call to httpClient.post with the right parameters" in { + + val response: GHResponse[Gist] = + Right(GHResult(gist, okStatusCode, Map.empty)) + + val request = + """ + |{ + | "description": "A Gist", + | "public": true, + | "files": { + | "test.scala": { + | "content": "val meaningOfLife = 42" + | } + | } + |}""".stripMargin + + val httpClientMock = httpClientMockPost[Gist]( + url = "gists", + json = request, + response = response + ) + + val gists = new Gists[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + + gists.newGist( + validGistDescription, + validGistPublic, + Map(validGistFilename → GistFile(validGistFileContent)), + headerUserAgent, + sampleToken + ) + } + +} diff --git a/github4s/shared/src/test/scala/github4s/unit/ReposSpec.scala b/github4s/shared/src/test/scala/github4s/unit/ReposSpec.scala index d266a646b..b26aaa9ac 100644 --- a/github4s/shared/src/test/scala/github4s/unit/ReposSpec.scala +++ b/github4s/shared/src/test/scala/github4s/unit/ReposSpec.scala @@ -152,38 +152,6 @@ class ReposSpec extends BaseSpec { ) } - "Repos.createRelease" should "call httpClient.post with the right parameters" in { - val response: GHResponse[Release] = Right(GHResult(release, createdStatusCode, Map.empty)) - - val request = - s""" - |{ - | "tag_name": "$validTagTitle", - | "name": "$validTagTitle", - | "target_commitish": "master", - | "body": "$validNote" - |}""".stripMargin - - val httpClientMock = httpClientMockPost[Release]( - url = s"repos/$validRepoOwner/$validRepoName/releases", - json = request, - response = response - ) - - val repos = new Repos[String, Id] { - override val httpClient: HttpClient[String, Id] = httpClientMock - } - repos.createRelease( - sampleToken, - headerUserAgent, - validRepoOwner, - validRepoName, - validTagTitle, - validTagTitle, - validNote, - Some("master")) - } - "Repos.getStatus" should "call httpClient.get with the right parameters" in { val response: GHResponse[CombinedStatus] = Right(GHResult(combinedStatus, okStatusCode, Map.empty)) diff --git a/github4s/shared/src/test/scala/github4s/unit/UserSpec.scala b/github4s/shared/src/test/scala/github4s/unit/UserSpec.scala new file mode 100644 index 000000000..01f7e54be --- /dev/null +++ b/github4s/shared/src/test/scala/github4s/unit/UserSpec.scala @@ -0,0 +1,79 @@ +/* + * Copyright 2016-2017 47 Degrees, LLC. + * + * 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. + */ + +package github4s.unit + +import cats.Id +import github4s.GithubResponses.{GHResponse, GHResult} +import github4s.HttpClient +import github4s.api.Users +import github4s.free.domain._ +import github4s.utils.BaseSpec + +class UserSpec extends BaseSpec { + + "User.get" should "call to httpClient.get with the right parameters" in { + + val response: GHResponse[User] = + Right(GHResult(user, okStatusCode, Map.empty)) + + val httpClientMock = httpClientMockGet[User]( + url = s"users/$validUsername", + response = response + ) + + val users = new Users[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + users.get(sampleToken, headerUserAgent, validUsername) + } + + "User.getAuth" should "call to httpClient.get with the right parameters" in { + + val response: GHResponse[User] = + Right(GHResult(user, okStatusCode, Map.empty)) + + val httpClientMock = httpClientMockGet[User]( + url = "user", + response = response + ) + + val users = new Users[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + users.getAuth(sampleToken, headerUserAgent) + } + + "User.getUsers" should "call to httpClient.get with the right parameters" in { + + val response: GHResponse[List[User]] = + Right(GHResult(List(user), okStatusCode, Map.empty)) + + val request = Map("since" → 1.toString) + + val httpClientMock = httpClientMockGet[List[User]]( + url = "users", + params = request, + response = response + ) + + val users = new Users[String, Id] { + override val httpClient: HttpClient[String, Id] = httpClientMock + } + users.getUsers(sampleToken, headerUserAgent, 1) + } + +} diff --git a/github4s/shared/src/test/scala/github4s/utils/BaseSpec.scala b/github4s/shared/src/test/scala/github4s/utils/BaseSpec.scala index 440335d90..9657756c3 100644 --- a/github4s/shared/src/test/scala/github4s/utils/BaseSpec.scala +++ b/github4s/shared/src/test/scala/github4s/utils/BaseSpec.scala @@ -76,6 +76,31 @@ trait BaseSpec extends FlatSpec with Matchers with TestData with IdInstances wit httpClientMock } + def httpClientMockPostAuth[T]( + url: String, + headers: Map[String, String], + json: String, + response: GHResponse[T]): HttpClient[String, Id] = { + val httpClientMock = mock[HttpClientTest] + (httpClientMock + .postAuth[T](_: String, _: Map[String, String], _: String)(_: Decoder[T])) + .expects(url, headers ++ headerUserAgent, JsonMockParameter(json), *) + .returns(response) + httpClientMock + } + + def httpClientMockPostOAuth[T]( + url: String, + json: String, + response: GHResponse[T]): HttpClient[String, Id] = { + val httpClientMock = mock[HttpClientTest] + (httpClientMock + .postOAuth[T](_: String, _: Map[String, String], _: String)(_: Decoder[T])) + .expects(url, headerUserAgent, JsonMockParameter(json), *) + .returns(response) + httpClientMock + } + def httpClientMockPatch[T]( url: String, json: String, @@ -114,5 +139,8 @@ trait BaseSpec extends FlatSpec with Matchers with TestData with IdInstances wit class RepositoryOpsTest extends RepositoryOps[GitHub4s] class IssueOpsTest extends IssueOps[GitHub4s] class ActivityOpsTest extends ActivityOps[GitHub4s] + class AuthOpsTest extends AuthOps[GitHub4s] + class UserOpsTest extends UserOps[GitHub4s] + class GistOpsTest extends GistOps[GitHub4s] } diff --git a/github4s/shared/src/test/scala/github4s/utils/TestData.scala b/github4s/shared/src/test/scala/github4s/utils/TestData.scala index 38c177180..690409009 100644 --- a/github4s/shared/src/test/scala/github4s/utils/TestData.scala +++ b/github4s/shared/src/test/scala/github4s/utils/TestData.scala @@ -353,5 +353,8 @@ trait TestData extends DummyGithubUrls { val authorization = Authorization(1, validRedirectUri, "token") val authorize = Authorize(validRedirectUri, validAuthState) - val oAuthToken = OAuthToken("token", validTokenType, "public_repo") + val oAuthToken = OAuthToken("token", validTokenType, "public_repo") + val validGistUrl = "https://api.github.com/gists/aa5a315d61ae9438b18d" + val validGistId = "aa5a315d61ae9438b18d" + val gist = Gist(validGistUrl, validGistId, validGistDescription, validGistPublic) } diff --git a/project/ProjectPlugin.scala b/project/ProjectPlugin.scala index 6b2dc02cf..965f108ed 100644 --- a/project/ProjectPlugin.scala +++ b/project/ProjectPlugin.scala @@ -21,7 +21,7 @@ object ProjectPlugin extends AutoPlugin { object autoImport { lazy val micrositeSettings = Seq( - micrositeName := "github4s", + micrositeName := "Github4s", micrositeDescription := "Github API wrapper written in Scala", micrositeBaseUrl := "github4s", micrositeDocumentationUrl := "/github4s/docs.html", @@ -42,7 +42,7 @@ object ProjectPlugin extends AutoPlugin { %%("circe-parser"), %%("base64"), %%("scalamockScalatest") % "test", - %%("scalatest") % "test" + %%("scalatest") % "test" ) lazy val standardCommonDeps = Seq(