diff --git a/github4s/jvm/src/test/scala/github4s/unit/ApiSpec.scala b/github4s/jvm/src/test/scala/github4s/unit/ApiSpec.scala index e967ae0e0..737de9c34 100644 --- a/github4s/jvm/src/test/scala/github4s/unit/ApiSpec.scala +++ b/github4s/jvm/src/test/scala/github4s/unit/ApiSpec.scala @@ -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( @@ -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) + } + } diff --git a/github4s/jvm/src/test/scala/github4s/utils/MockGithubApiServer.scala b/github4s/jvm/src/test/scala/github4s/utils/MockGithubApiServer.scala index 3adcdcda0..2eaefa236 100644 --- a/github4s/jvm/src/test/scala/github4s/utils/MockGithubApiServer.scala +++ b/github4s/jvm/src/test/scala/github4s/utils/MockGithubApiServer.scala @@ -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)) } diff --git a/github4s/shared/src/test/scala/github4s/integration/GHOrganizationsSpec.scala b/github4s/shared/src/test/scala/github4s/integration/GHOrganizationsSpec.scala new file mode 100644 index 000000000..1bdf42275 --- /dev/null +++ b/github4s/shared/src/test/scala/github4s/integration/GHOrganizationsSpec.scala @@ -0,0 +1,47 @@ +/* + * Copyright 2016-2017 47 Degrees, LLC. + * + * 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) + } +} diff --git a/github4s/shared/src/test/scala/github4s/unit/GHOrganizationsSpec.scala b/github4s/shared/src/test/scala/github4s/unit/GHOrganizationsSpec.scala new file mode 100644 index 000000000..20679a09f --- /dev/null +++ b/github4s/shared/src/test/scala/github4s/unit/GHOrganizationsSpec.scala @@ -0,0 +1,42 @@ +/* + * Copyright 2016-2017 47 Degrees, LLC. + * + * 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) + } + +} diff --git a/github4s/shared/src/test/scala/github4s/unit/OrganizationssSpec.scala b/github4s/shared/src/test/scala/github4s/unit/OrganizationssSpec.scala new file mode 100644 index 000000000..1c8e93c47 --- /dev/null +++ b/github4s/shared/src/test/scala/github4s/unit/OrganizationssSpec.scala @@ -0,0 +1,44 @@ +/* + * Copyright 2016-2017 47 Degrees, LLC. + * + * 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) + } + +} diff --git a/github4s/shared/src/test/scala/github4s/utils/BaseSpec.scala b/github4s/shared/src/test/scala/github4s/utils/BaseSpec.scala index 9657756c3..83ac43dc5 100644 --- a/github4s/shared/src/test/scala/github4s/utils/BaseSpec.scala +++ b/github4s/shared/src/test/scala/github4s/utils/BaseSpec.scala @@ -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] }