Skip to content

Commit

Permalink
ApiSpec unit tests for the Issues API
Browse files Browse the repository at this point in the history
  • Loading branch information
BenFradet committed May 6, 2017
1 parent 5af2217 commit b09a98f
Show file tree
Hide file tree
Showing 4 changed files with 457 additions and 3 deletions.
146 changes: 144 additions & 2 deletions github4s/jvm/src/test/scala/github4s/unit/ApiSpec.scala
Expand Up @@ -41,6 +41,7 @@ class ApiSpec
val gitData = new GitData[HttpResponse[String], Id]
val pullRequests = new PullRequests[HttpResponse[String], Id]
val statuses = new Statuses[HttpResponse[String], Id]
val issues = new Issues[HttpResponse[String], Id]

"Auth >> NewAuth" should "return a valid token when valid credential is provided" in {
val response = auth.newAuth(
Expand Down Expand Up @@ -448,7 +449,7 @@ class ApiSpec
headerUserAgent,
validRepoOwner,
validRepoName,
s"refs/$validRefSingle",
validRefSingle,
validCommitSha)
response should be('left)
}
Expand Down Expand Up @@ -711,7 +712,7 @@ class ApiSpec
}
}

"Statuses >> Create" should "return the create status if a valid sha is provided" in {
"Statuses >> Create" should "return the created status if a valid sha is provided" in {
val response = statuses.create(
accessToken,
headerUserAgent,
Expand Down Expand Up @@ -756,4 +757,145 @@ class ApiSpec
None)
response should be('left)
}

"Issues >> List" should "return the expected issues when a valid owner/repo is provided" in {
val response =
issues.list(accessToken, headerUserAgent, validRepoOwner, validRepoName)
response should be('right)

response.toOption map { r
r.result.nonEmpty shouldBe true
r.statusCode shouldBe okStatusCode
}
}

it should "return an error if invalid data is provided" in {
val response =
issues.list(accessToken, headerUserAgent, validRepoOwner, invalidRepoName)
response should be('left)
}

it should "return an error if no tokens are provided" in {
val response =
issues.list(None, headerUserAgent, validRepoOwner, validRepoName)
response should be('left)
}

"Issues >> Create" should "return the created issue if valid data is provided" in {
val response = issues.create(
accessToken,
headerUserAgent,
validRepoOwner,
validRepoName,
validIssueTitle,
validIssueBody,
None,
List.empty,
List.empty)
response should be('right)

response.toOption map { r =>
r.statusCode shouldBe createdStatusCode
}
}

it should "return an error if invalid data is provided" in {
val response = issues.create(
accessToken,
headerUserAgent,
validRepoOwner,
invalidRepoName,
validIssueTitle,
validIssueBody,
None,
List.empty,
List.empty)
response should be('left)
}

it should "return an error if no tokens are provided" in {
val response = issues.create(
None,
headerUserAgent,
validRepoOwner,
validRepoName,
validIssueTitle,
validIssueBody,
None,
List.empty,
List.empty)
response should be('left)
}

"Issues >> Edit" should "return the edited issue if valid data is provided" in {
val response = issues.edit(
accessToken,
headerUserAgent,
validRepoOwner,
validRepoName,
validIssueNumber,
validIssueState,
validIssueTitle,
validIssueBody,
None,
List.empty,
List.empty)
response should be('right)

response.toOption map { r =>
r.statusCode shouldBe okStatusCode
}
}

it should "return an error if invalid data is provided" in {
val response = issues.edit(
accessToken,
headerUserAgent,
validRepoOwner,
invalidRepoName,
validIssueNumber,
validIssueState,
validIssueTitle,
validIssueBody,
None,
List.empty,
List.empty)
response should be('left)
}

it should "return an error if no tokens are provided" in {
val response = issues.edit(
None,
headerUserAgent,
validRepoOwner,
validRepoName,
validIssueNumber,
validIssueState,
validIssueTitle,
validIssueBody,
None,
List.empty,
List.empty)
response should be('left)
}

"Issues >> Search" should "return the search result if valid data is provided" in {
val response = issues.search(accessToken, headerUserAgent, validSearchQuery, List.empty)
response should be('right)

response.toOption map { r =>
r.statusCode shouldBe okStatusCode
}
}

it should "return an empty result if a search query matching nothing is provided" in {
val response =
issues.search(accessToken, headerUserAgent, nonExistentSearchQuery, List.empty)
response should be('right)

response.toOption map { r
r.result.items.isEmpty shouldBe true
r.statusCode shouldBe okStatusCode
}
}
}
Expand Up @@ -20,6 +20,7 @@ import org.mockserver.model.HttpRequest._
import org.mockserver.model.HttpResponse._
import org.mockserver.model.JsonBody._
import org.mockserver.model.NottableString._
import org.mockserver.model.Parameter

trait MockGithubApiServer extends MockServerService with FakeResponses with TestUtilsJVM {

Expand Down Expand Up @@ -596,4 +597,98 @@ trait MockGithubApiServer extends MockServerService with FakeResponses with Test
.withPath(s"/repos/$validRepoOwner/$validRepoName/statuses/$invalidCommitSha")
.withHeader("Authorization", tokenHeader))
.respond(response.withStatusCode(notFoundStatusCode).withBody(notFoundResponse))

//Issues >> list
mockServer
.when(
request
.withMethod("GET")
.withPath(s"/repos/$validRepoOwner/$validRepoName/issues")
.withHeader("Authorization", tokenHeader))
.respond(response.withStatusCode(okStatusCode).withBody(listIssuesValidResponse))

mockServer
.when(
request
.withMethod("GET")
.withPath(s"/repos/$validRepoOwner/$validRepoName/issues")
.withHeader(not("Authorization")))
.respond(response.withStatusCode(unauthorizedStatusCode).withBody(unauthorizedResponse))

mockServer
.when(
request
.withMethod("GET")
.withPath(s"/repos/$validRepoOwner/$invalidRepoName/issues")
.withHeader(not("Authorization")))
.respond(response.withStatusCode(notFoundStatusCode).withBody(notFoundResponse))

//Issues >> create
mockServer
.when(
request
.withMethod("POST")
.withPath(s"/repos/$validRepoOwner/$validRepoName/issues")
.withHeader("Authorization", tokenHeader))
.respond(response.withStatusCode(createdStatusCode).withBody(createIssueValidResponse))

mockServer
.when(
request
.withMethod("POST")
.withPath(s"/repos/$validRepoOwner/$validRepoName/issues")
.withHeader(not("Authorization")))
.respond(response.withStatusCode(unauthorizedStatusCode).withBody(unauthorizedResponse))

mockServer
.when(
request
.withMethod("POST")
.withPath(s"/repos/$validRepoOwner/$invalidRepoName/issues")
.withHeader("Authorization", tokenHeader))
.respond(response.withStatusCode(notFoundStatusCode).withBody(notFoundResponse))

//Issues >> edit
mockServer
.when(
request
.withMethod("POST")
.withPath(s"/repos/$validRepoOwner/$validRepoName/issues/$validIssueNumber")
.withHeader("Authorization", tokenHeader))
.respond(response.withStatusCode(okStatusCode).withBody(createIssueValidResponse))

mockServer
.when(
request
.withMethod("POST")
.withPath(s"/repos/$validRepoOwner/$validRepoName/issues/$validIssueNumber")
.withHeader(not("Authorization")))
.respond(response.withStatusCode(unauthorizedStatusCode).withBody(unauthorizedResponse))

mockServer
.when(
request
.withMethod("POST")
.withPath(s"/repos/$validRepoOwner/$invalidRepoName/issues/$validIssueNumber")
.withHeader("Authorization", tokenHeader))
.respond(response.withStatusCode(notFoundStatusCode).withBody(notFoundResponse))

//Issues >> search
mockServer
.when(
request
.withMethod("GET")
.withPath(s"/search/issues")
.withQueryStringParameters(new Parameter("q", s".*$validSearchQuery.*"))
.withHeader("Authorization", tokenHeader))
.respond(response.withStatusCode(okStatusCode).withBody(searchIssuesValidResponse))

mockServer
.when(
request
.withMethod("GET")
.withPath(s"/search/issues")
.withQueryStringParameters(new Parameter("q", s".*$nonExistentSearchQuery.*"))
.withHeader("Authorization", tokenHeader))
.respond(response.withStatusCode(okStatusCode).withBody(searchIssuesEmptyResponse))
}
Expand Up @@ -47,7 +47,7 @@ class IssuesSpec extends BaseSpec {
val httpClientMock = httpClientMockGet[SearchIssuesResult](
url = s"search/issues",
response = response,
params = Map("q" -> "+repo:47deg/github4s+type:issue+in:title")
params = Map("q" -> s"+${validSearchParams.map(_.value).mkString("+")}")
)

val issues = new Issues[String, Id] {
Expand Down

0 comments on commit b09a98f

Please sign in to comment.