-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from 47deg/rafa-15-unit-test
Rafa 15 unit test
- Loading branch information
Showing
17 changed files
with
634 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
github.baseUrl = "https://api.github.com/" | ||
github.authorizeUrl = "https://github.com/login/oauth/authorize?client_id=%s&redirect_uri=%s&scope=%s&state=%s" | ||
github.accessTokenUrl = "https://github.com/login/oauth/access_token" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package github4s | ||
|
||
import com.typesafe.config.ConfigFactory | ||
|
||
class GithubApiConfig(hocon: Option[String] = None) { | ||
|
||
val config = hocon.fold(ConfigFactory.load)(ConfigFactory.parseString) | ||
|
||
def getString(key: String) = sys.props.getOrElse(key, config.getString(key)) | ||
} | ||
|
||
object GithubApiConfig { | ||
|
||
implicit val defaultConfig: GithubApiConfig = new GithubApiConfig | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package github4s.unit | ||
|
||
import cats.scalatest.{ XorMatchers, XorValues } | ||
import github4s.api.{ Users, Repos, Auth } | ||
import github4s.free.domain.Pagination | ||
import github4s.utils.{ DummyGithubConfig, MockGithubApiServer, TestUtils } | ||
import org.scalatest._ | ||
|
||
class ApiSpec | ||
extends FlatSpec | ||
with Matchers | ||
with XorMatchers | ||
with XorValues | ||
with TestUtils | ||
with MockGithubApiServer | ||
with DummyGithubConfig { | ||
|
||
val auth = new Auth | ||
val repos = new Repos | ||
val users = new Users | ||
|
||
"Auth >> NewAuth" should "return a valid token when valid credential is provided" in { | ||
val response = auth.newAuth(validUsername, "", validScopes, validNote, validClientId, "") | ||
response shouldBe right | ||
response.value.value.token.nonEmpty shouldBe true | ||
response.value.statusCode shouldBe okStatusCode | ||
} | ||
|
||
it should "return error on Left when invalid credential is provided" in { | ||
val response = auth.newAuth(validUsername, invalidPassword, validScopes, validNote, validClientId, "") | ||
response shouldBe left | ||
} | ||
|
||
"Auth >> AuthorizeUrl" should "return the expected URL for valid username" in { | ||
val response = auth.authorizeUrl(validClientId, validRedirectUri, validScopes) | ||
response shouldBe right | ||
response.value.value.url.contains(validRedirectUri) shouldBe true | ||
response.value.statusCode shouldBe okStatusCode | ||
} | ||
|
||
"Auth >> GetAccessToken" should "return a valid access_token when a valid code is provided" in { | ||
val response = auth.getAccessToken("", "", validCode, "", "") | ||
response shouldBe right | ||
response.value.value.access_token.nonEmpty shouldBe true | ||
response.value.statusCode shouldBe okStatusCode | ||
} | ||
|
||
it should "return error on Left when invalid code is provided" in { | ||
val response = auth.getAccessToken("", "", invalidCode, "", "") | ||
response shouldBe left | ||
} | ||
|
||
"Repos >> Get" should "return the expected name when valid repo is provided" in { | ||
|
||
val response = repos.get(accessToken, validRepoOwner, validRepoName) | ||
response shouldBe right | ||
response.value.value.name shouldBe validRepoName | ||
response.value.statusCode shouldBe okStatusCode | ||
} | ||
|
||
it should "return error when an invalid repo name is passed" in { | ||
val response = repos.get(accessToken, validRepoOwner, invalidRepoName) | ||
response shouldBe left | ||
} | ||
|
||
"Repos >> ListCommits" should "return the expected list of commits for valid data" in { | ||
val response = repos.listCommits( | ||
accessToken = accessToken, | ||
owner = validRepoOwner, | ||
repo = validRepoName, | ||
pagination = Option(Pagination(validPage, validPerPage)) | ||
) | ||
response shouldBe right | ||
response.value.value.nonEmpty shouldBe true | ||
response.value.statusCode shouldBe okStatusCode | ||
} | ||
|
||
it should "return an empty list of commits for invalid page parameter" in { | ||
val response = repos.listCommits( | ||
accessToken = accessToken, | ||
owner = validRepoOwner, | ||
repo = validRepoName, | ||
pagination = Option(Pagination(invalidPage, validPerPage)) | ||
) | ||
|
||
response shouldBe right | ||
response.value.value.isEmpty shouldBe true | ||
response.value.statusCode shouldBe okStatusCode | ||
} | ||
|
||
it should "return error for invalid repo name" in { | ||
val response = repos.listCommits(accessToken, validRepoOwner, invalidRepoName) | ||
response shouldBe left | ||
} | ||
|
||
"Users >> Get" should "return the expected login for a valid username" in { | ||
|
||
val response = users.get(accessToken, validUsername) | ||
|
||
response shouldBe right | ||
response.value.value.login shouldBe validUsername | ||
response.value.statusCode shouldBe okStatusCode | ||
} | ||
|
||
it should "return error on Left for invalid username" in { | ||
val response = users.get(accessToken, invalidUsername) | ||
response shouldBe left | ||
} | ||
|
||
"Users >> GetAuth" should "return the expected login for a valid accessToken" in { | ||
val response = users.getAuth(accessToken) | ||
response shouldBe right | ||
response.value.value.login shouldBe validUsername | ||
response.value.statusCode shouldBe okStatusCode | ||
} | ||
|
||
it should "return error on Left when no accessToken is provided" in { | ||
val response = users.getAuth(None) | ||
response shouldBe left | ||
} | ||
|
||
"Users >> GetUsers" should "return users for a valid since value" in { | ||
val response = users.getUsers(accessToken, validSinceInt) | ||
response shouldBe right | ||
response.value.value.nonEmpty shouldBe true | ||
response.value.statusCode shouldBe okStatusCode | ||
} | ||
|
||
it should "return an empty list when a invalid since value is provided" in { | ||
val response = users.getUsers(accessToken, invalidSinceInt) | ||
response shouldBe right | ||
response.value.value.isEmpty shouldBe true | ||
response.value.statusCode shouldBe okStatusCode | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package github4s.utils | ||
|
||
import github4s.GithubApiConfig | ||
|
||
trait DummyGithubConfig { | ||
|
||
val dummyConfigHocon = | ||
""" | ||
|github.baseUrl = "http://127.0.0.1:9999/" | ||
|github.authorizeUrl = "http://127.0.0.1:9999/authorize?client_id=%s&redirect_uri=%s&scope=%s&state=%s" | ||
|github.accessTokenUrl = "http://127.0.0.1:9999/login/oauth/access_token" | ||
|""".stripMargin | ||
|
||
implicit val dummyConfig = new GithubApiConfig(Option(dummyConfigHocon)) | ||
} |
Oops, something went wrong.