Skip to content

Commit

Permalink
Add tests for listComments of an Issue
Browse files Browse the repository at this point in the history
  • Loading branch information
GRBurst committed Jan 21, 2018
1 parent 7d2a829 commit c5e18ac
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
31 changes: 31 additions & 0 deletions github4s/jvm/src/test/scala/github4s/unit/ApiSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,37 @@ class ApiSpec
}
}

"Issues >> ListComments" should "return the expected issue comments when a valid issue number is provided" in {
val response =
issues.listComments(
accessToken,
headerUserAgent,
validRepoOwner,
validRepoName,
validIssueNumber)
response should be('right)

response.toOption map { r
r.result.nonEmpty shouldBe true
r.statusCode shouldBe okStatusCode
}
}
it should "return an error if an invalid issue number is provided" in {
val response =
issues.listComments(
accessToken,
headerUserAgent,
validRepoOwner,
validRepoName,
invalidIssueNumber)
response should be('left)
}
it should "return an error if no tokens are provided" in {
val response =
issues.listComments(None, headerUserAgent, validRepoOwner, validRepoName, validIssueNumber)
response should be('left)
}

"Issues >> Create a Comment" should "return the comment created when a valid issue number is provided" in {
val response =
issues.createComment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,31 @@ trait MockGithubApiServer extends MockServerService with FakeResponses with Test
.withQueryStringParameters(new Parameter("q", s".*$nonExistentSearchQuery.*"))
.withHeader("Authorization", tokenHeader))
.respond(response.withStatusCode(okStatusCode).withBody(searchIssuesEmptyResponse))

//Issues >> List comments of an Issue
mockServer
.when(
request
.withMethod("GET")
.withPath(s"/repos/$validRepoOwner/$validRepoName/issues/$validIssueNumber/comments")
.withHeader("Authorization", tokenHeader))
.respond(response.withStatusCode(okStatusCode).withBody(listCommentsValidResponse))

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

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

//Issues >> Create a comment
mockServer
Expand Down
13 changes: 13 additions & 0 deletions github4s/shared/src/test/scala/github4s/unit/GHIssuesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ class GHIssuesSpec extends BaseSpec {
List.empty)
}

"Issues.ListComments" should "call to IssuesOps with the right parameters" in {
val response: Free[GitHub4s, GHResponse[List[Comment]]] =
Free.pure(Right(GHResult(List(comment), okStatusCode, Map.empty)))

val commentOps = mock[IssueOpsTest]
(commentOps.listComments _)
.expects(validRepoOwner, validRepoName, validIssueNumber, sampleToken)
.returns(response)

val ghIssues = new GHIssues(sampleToken)(commentOps)
ghIssues.listComments(validRepoOwner, validRepoName, validIssueNumber)
}

"Issues.CreateComment" should "call to IssueOps with the right parameters" in {

val response: Free[GitHub4s, GHResponse[Comment]] =
Expand Down
20 changes: 20 additions & 0 deletions github4s/shared/src/test/scala/github4s/unit/IssuesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,26 @@ class IssuesSpec extends BaseSpec {
List.empty)
}

"Issues.ListComment" should "call httpClient.get with the right parameters" in {
val response: GHResponse[List[Comment]] =
Right(GHResult(List(comment), okStatusCode, Map.empty))

val httpClientMock = httpClientMockGet[List[Comment]](
url = s"repos/$validRepoOwner/$validRepoName/issues/$validIssueNumber/comments",
response = response
)

val issues = new Issues[String, Id] {
override val httpClient: HttpClient[String, Id] = httpClientMock
}
issues.listComments(
sampleToken,
headerUserAgent,
validRepoOwner,
validRepoName,
validIssueNumber)
}

"Issue.CreateComment" should "call to httpClient.post with the right parameters" in {

val response: GHResponse[Comment] =
Expand Down
64 changes: 64 additions & 0 deletions github4s/shared/src/test/scala/github4s/utils/FakeResponses.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,70 @@ trait FakeResponses {
| "body": "We should take some time to update the docs and the microsite to reflect the changes that have been included in the library, maybe splitting the docs in several sections for the JVM and JS case uses."
|}""".stripMargin

val listCommentsValidResponse =
"""
|[
| {
| "url": "https://api.github.com/repos/47deg/github4s/issues/comments/300130363",
| "html_url": "https://github.com/47deg/github4s/issues/48#issuecomment-300130363",
| "issue_url": "https://api.github.com/repos/47deg/github4s/issues/48",
| "id": 300130363,
| "user": {
| "login": "47degdev",
| "id": 5580770,
| "avatar_url": "https://avatars3.githubusercontent.com/u/5580770?v=4",
| "gravatar_id": "",
| "url": "https://api.github.com/users/47degdev",
| "html_url": "https://github.com/47degdev",
| "followers_url": "https://api.github.com/users/47degdev/followers",
| "following_url": "https://api.github.com/users/47degdev/following{/other_user}",
| "gists_url": "https://api.github.com/users/47degdev/gists{/gist_id}",
| "starred_url": "https://api.github.com/users/47degdev/starred{/owner}{/repo}",
| "subscriptions_url": "https://api.github.com/users/47degdev/subscriptions",
| "organizations_url": "https://api.github.com/users/47degdev/orgs",
| "repos_url": "https://api.github.com/users/47degdev/repos",
| "events_url": "https://api.github.com/users/47degdev/events{/privacy}",
| "received_events_url": "https://api.github.com/users/47degdev/received_events",
| "type": "User",
| "site_admin": false
| },
| "created_at": "2017-05-09T10:59:27Z",
| "updated_at": "2017-05-09T10:59:27Z",
| "author_association": "CONTRIBUTOR",
| "body": "the comment"
| },
| {
| "url": "https://api.github.com/repos/47deg/github4s/issues/comments/300130514",
| "html_url": "https://github.com/47deg/github4s/issues/48#issuecomment-300130514",
| "issue_url": "https://api.github.com/repos/47deg/github4s/issues/48",
| "id": 300130514,
| "user": {
| "login": "47degdev",
| "id": 5580770,
| "avatar_url": "https://avatars3.githubusercontent.com/u/5580770?v=4",
| "gravatar_id": "",
| "url": "https://api.github.com/users/47degdev",
| "html_url": "https://github.com/47degdev",
| "followers_url": "https://api.github.com/users/47degdev/followers",
| "following_url": "https://api.github.com/users/47degdev/following{/other_user}",
| "gists_url": "https://api.github.com/users/47degdev/gists{/gist_id}",
| "starred_url": "https://api.github.com/users/47degdev/starred{/owner}{/repo}",
| "subscriptions_url": "https://api.github.com/users/47degdev/subscriptions",
| "organizations_url": "https://api.github.com/users/47degdev/orgs",
| "repos_url": "https://api.github.com/users/47degdev/repos",
| "events_url": "https://api.github.com/users/47degdev/events{/privacy}",
| "received_events_url": "https://api.github.com/users/47degdev/received_events",
| "type": "User",
| "site_admin": false
| },
| "created_at": "2017-05-09T11:00:18Z",
| "updated_at": "2017-05-09T11:00:18Z",
| "author_association": "CONTRIBUTOR",
| "body": "the comment"
| }
|]
""".stripMargin

val searchIssuesValidResponse =
"""{
| "total_count": 1,
Expand Down

0 comments on commit c5e18ac

Please sign in to comment.