Skip to content

Commit

Permalink
Missing unit test (#137)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
AdrianRaFo authored and juanpedromoreno committed May 18, 2017
1 parent 02be2ca commit fa439da
Show file tree
Hide file tree
Showing 12 changed files with 467 additions and 37 deletions.
98 changes: 98 additions & 0 deletions github4s/shared/src/test/scala/github4s/unit/AuthSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2016-2017 47 Degrees, LLC. <http://www.47deg.com>
*
* 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 73 additions & 0 deletions github4s/shared/src/test/scala/github4s/unit/GHAuthSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2016-2017 47 Degrees, LLC. <http://www.47deg.com>
*
* 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)
}

}
49 changes: 49 additions & 0 deletions github4s/shared/src/test/scala/github4s/unit/GHGistSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2016-2017 47 Degrees, LLC. <http://www.47deg.com>
*
* 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)))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
70 changes: 70 additions & 0 deletions github4s/shared/src/test/scala/github4s/unit/GHUserSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2016-2017 47 Degrees, LLC. <http://www.47deg.com>
*
* 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)
}

}
64 changes: 64 additions & 0 deletions github4s/shared/src/test/scala/github4s/unit/GistSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2016-2017 47 Degrees, LLC. <http://www.47deg.com>
*
* 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
)
}

}
Loading

0 comments on commit fa439da

Please sign in to comment.