Skip to content

Commit

Permalink
organization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BenFradet committed Sep 16, 2017
1 parent 069a113 commit c954b6a
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 16 deletions.
51 changes: 43 additions & 8 deletions github4s/jvm/src/test/scala/github4s/unit/ApiSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ class ApiSpec
with DummyGithubUrls
with ImplicitsJVM {

val auth = new Auth[HttpResponse[String], Id]
val repos = new Repos[HttpResponse[String], Id]
val users = new Users[HttpResponse[String], Id]
val gists = new Gists[HttpResponse[String], Id]
val gitData = new GitData[HttpResponse[String], Id]
val pullRequests = new PullRequests[HttpResponse[String], Id]
val issues = new Issues[HttpResponse[String], Id]
val activities = new Activities[HttpResponse[String], Id]
val auth = new Auth[HttpResponse[String], Id]
val repos = new Repos[HttpResponse[String], Id]
val users = new Users[HttpResponse[String], Id]
val gists = new Gists[HttpResponse[String], Id]
val gitData = new GitData[HttpResponse[String], Id]
val pullRequests = new PullRequests[HttpResponse[String], Id]
val issues = new Issues[HttpResponse[String], Id]
val activities = new Activities[HttpResponse[String], Id]
val organizations = new Organizations[HttpResponse[String], Id]

"Auth >> NewAuth" should "return a valid token when valid credential is provided" in {
val response = auth.newAuth(
Expand Down Expand Up @@ -1106,4 +1107,38 @@ class ApiSpec
response should be('left)
}

"Organizations >> ListMembers" should "return the expected list of users" in {
val response = organizations.listMembers(
accessToken = accessToken,
headers = headerUserAgent,
org = validRepoOwner,
pagination = Option(Pagination(validPage, validPerPage))
)
response should be('right)

response.toOption map { r
r.result.nonEmpty shouldBe true
r.statusCode shouldBe okStatusCode
}
}
it should "return an empty list of users for an invalid page parameter" in {
val response = organizations.listMembers(
accessToken = accessToken,
headers = headerUserAgent,
org = validRepoOwner,
pagination = Option(Pagination(invalidPage, validPerPage))
)

response should be('right)

response.toOption map { r
r.result.isEmpty shouldBe true
r.statusCode shouldBe okStatusCode
}
}
it should "return error for an invalid username" in {
val response = organizations.listMembers(accessToken, headerUserAgent, invalidUsername)
response should be('left)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -891,4 +891,27 @@ trait MockGithubApiServer extends MockServerService with FakeResponses with Test
.withPath(s"/users/$invalidUsername/starred"))
.respond(response.withStatusCode(notFoundStatusCode).withBody(notFoundResponse))

//Organizations >> List members
mockServer
.when(
request
.withMethod("GET")
.withPath(s"/orgs/$validRepoOwner/members")
.withQueryStringParameter("page", validPage.toString))
.respond(response.withStatusCode(okStatusCode).withBody(getUsersValidResponse))

mockServer
.when(
request
.withMethod("GET")
.withPath(s"/orgs/$validRepoOwner/members")
.withQueryStringParameter("page", invalidPage.toString))
.respond(response.withStatusCode(okStatusCode).withBody(emptyListResponse))

mockServer
.when(
request
.withMethod("GET")
.withPath(s"/orgs/$invalidUsername/members"))
.respond(response.withStatusCode(notFoundStatusCode).withBody(notFoundResponse))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.integration

import github4s.Github
import github4s.Github._
import github4s.free.domain.User
import github4s.implicits._
import github4s.utils.BaseIntegrationSpec

trait GHOrganizationsSpec[T] extends BaseIntegrationSpec[T] {

"Organization >> ListMembers" should "return the expected list of users" in {
val response =
Github(accessToken).organizations
.listMembers(validRepoOwner)
.execFuture[T](headerUserAgent)

testFutureIsRight[List[User]](response, { r =>
r.result.nonEmpty shouldBe true
r.statusCode shouldBe okStatusCode
})
}

it should "return error for an invalid org" in {
val response =
Github(accessToken).organizations
.listMembers(invalidUsername)
.execFuture[T](headerUserAgent)

testFutureIsLeft(response)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.{GHOrganizations, HttpClient}
import github4s.app.GitHub4s
import github4s.free.domain.User
import github4s.utils.BaseSpec

class GHOrganizationsSpec extends BaseSpec {

"Organizations.listMembers" should "call to OrganizationsOps with the right parameters" in {

val response: Free[GitHub4s, GHResponse[List[User]]] =
Free.pure(Right(GHResult(List(user), okStatusCode, Map.empty)))

val organizationOps = mock[OrganizationOpsTest]
(organizationOps.listMembers _)
.expects(validRepoOwner, None, None, None, sampleToken)
.returns(response)

val ghOrganizations = new GHOrganizations(sampleToken)(organizationOps)
ghOrganizations.listMembers(validRepoOwner)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.Organizations
import github4s.free.domain.User
import github4s.utils.BaseSpec

class OrganizationsSpec extends BaseSpec {

"Organization.listMembers" should "call to httpClient.get with the right parameters" in {

val response: GHResponse[List[User]] =
Right(GHResult(List(user), okStatusCode, Map.empty))

val httpClientMock = httpClientMockGet[List[User]](
url = s"orgs/$validRepoOwner/members",
response = response
)

val organizations = new Organizations[String, Id] {
override val httpClient: HttpClient[String, Id] = httpClientMock
}
organizations.listMembers(sampleToken, headerUserAgent, validRepoOwner)
}

}
17 changes: 9 additions & 8 deletions github4s/shared/src/test/scala/github4s/utils/BaseSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,14 @@ trait BaseSpec extends FlatSpec with Matchers with TestData with IdInstances wit
httpClientMock
}

class GitDataOpsTest extends GitDataOps[GitHub4s]
class PullRequestOpsTest extends PullRequestOps[GitHub4s]
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]
class GitDataOpsTest extends GitDataOps[GitHub4s]
class PullRequestOpsTest extends PullRequestOps[GitHub4s]
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]
class OrganizationOpsTest extends OrganizationOps[GitHub4s]

}

0 comments on commit c954b6a

Please sign in to comment.