From eee2b12ec2815898f1960e361cdb766de5d703b2 Mon Sep 17 00:00:00 2001 From: "aedampir@gmail.com" Date: Thu, 19 May 2016 15:58:18 +0700 Subject: [PATCH] added tests for RepoCollaboratorsClient --- .../Clients/RepoCollaboratorsClientTests.cs | 110 ++++++++++++++++-- 1 file changed, 102 insertions(+), 8 deletions(-) diff --git a/Octokit.Tests/Clients/RepoCollaboratorsClientTests.cs b/Octokit.Tests/Clients/RepoCollaboratorsClientTests.cs index d79c82a8..22a4fe7c 100644 --- a/Octokit.Tests/Clients/RepoCollaboratorsClientTests.cs +++ b/Octokit.Tests/Clients/RepoCollaboratorsClientTests.cs @@ -35,6 +35,16 @@ namespace Octokit.Tests.Clients connection.Received().GetAll(Arg.Is(u => u.ToString() == "repos/owner/test/collaborators"), null, "application/vnd.github.ironman-preview+json", Args.ApiOptions); } + [Fact] + public void RequestsCorrectUrlWithRepositoryId() + { + var connection = Substitute.For(); + var client = new RepoCollaboratorsClient(connection); + + client.GetAll(1); + connection.Received().GetAll(Arg.Is(u => u.ToString() == "repositories/1/collaborators"), Args.ApiOptions); + } + [Fact] public void RequestsCorrectUrlWithApiOptions() { @@ -54,6 +64,25 @@ namespace Octokit.Tests.Clients .GetAll(Arg.Is(u => u.ToString() == "repos/owner/test/collaborators"), null, "application/vnd.github.ironman-preview+json", options); } + [Fact] + public void RequestsCorrectUrlWithApiOptionsAndRepositoryId() + { + var connection = Substitute.For(); + var client = new RepoCollaboratorsClient(connection); + + var options = new ApiOptions + { + PageSize = 1, + PageCount = 1, + StartPage = 1 + }; + + client.GetAll(1, options); + + connection.Received() + .GetAll(Arg.Is(u => u.ToString() == "repositories/1/collaborators"), options); + } + [Fact] public async Task EnsuresNonNullArguments() { @@ -67,6 +96,8 @@ namespace Octokit.Tests.Clients await Assert.ThrowsAsync(() => client.GetAll(null, "test", ApiOptions.None)); await Assert.ThrowsAsync(() => client.GetAll("owner", null, ApiOptions.None)); await Assert.ThrowsAsync(() => client.GetAll("owner", "test", null)); + + await Assert.ThrowsAsync(() => client.GetAll(1, null)); } } @@ -91,6 +122,25 @@ namespace Octokit.Tests.Clients Assert.Equal(expected, result); } + [Theory] + [InlineData(HttpStatusCode.NoContent, true)] + [InlineData(HttpStatusCode.NotFound, false)] + public async Task RequestsCorrectValueForStatusCodeWithRepositoryId(HttpStatusCode status, bool expected) + { + var response = Task.Factory.StartNew>(() => + new ApiResponse(new Response(status, null, new Dictionary(), "application/json"))); + var connection = Substitute.For(); + connection.Get(Arg.Is(u => u.ToString() == "repositories/1/collaborators/user1"), + null, null).Returns(response); + var apiConnection = Substitute.For(); + apiConnection.Connection.Returns(connection); + var client = new RepoCollaboratorsClient(apiConnection); + + var result = await client.IsCollaborator(1, "user1"); + + Assert.Equal(expected, result); + } + [Fact] public async Task ThrowsExceptionForInvalidStatusCode() { @@ -101,9 +151,24 @@ namespace Octokit.Tests.Clients null, null).Returns(response); var apiConnection = Substitute.For(); apiConnection.Connection.Returns(connection); - var client = new AssigneesClient(apiConnection); + var client = new RepoCollaboratorsClient(apiConnection); - await Assert.ThrowsAsync(() => client.CheckAssignee("foo", "bar", "cody")); + await Assert.ThrowsAsync(() => client.IsCollaborator("foo", "bar", "cody")); + } + + [Fact] + public async Task ThrowsExceptionForInvalidStatusCodeWithRepositoryId() + { + var response = Task.Factory.StartNew>(() => + new ApiResponse(new Response(HttpStatusCode.Conflict, null, new Dictionary(), "application/json"))); + var connection = Substitute.For(); + connection.Get(Arg.Is(u => u.ToString() == "repositories/1/assignees/cody"), + null, null).Returns(response); + var apiConnection = Substitute.For(); + apiConnection.Connection.Returns(connection); + var client = new RepoCollaboratorsClient(apiConnection); + + await Assert.ThrowsAsync(() => client.IsCollaborator(1, "cody")); } [Fact] @@ -112,11 +177,14 @@ namespace Octokit.Tests.Clients var client = new RepoCollaboratorsClient(Substitute.For()); await Assert.ThrowsAsync(() => client.IsCollaborator(null, "test", "user1")); - await Assert.ThrowsAsync(() => client.IsCollaborator("", "test", "user1")); await Assert.ThrowsAsync(() => client.IsCollaborator("owner", null, "user1")); + await Assert.ThrowsAsync(() => client.IsCollaborator("owner", "test", null)); + await Assert.ThrowsAsync(() => client.IsCollaborator(1, null)); + + await Assert.ThrowsAsync(() => client.IsCollaborator("", "test", "user1")); await Assert.ThrowsAsync(() => client.IsCollaborator("owner", "", "user1")); await Assert.ThrowsAsync(() => client.IsCollaborator("owner", "test", "")); - await Assert.ThrowsAsync(() => client.IsCollaborator("owner", "test", null)); + await Assert.ThrowsAsync(() => client.IsCollaborator(1, "")); } } @@ -132,17 +200,30 @@ namespace Octokit.Tests.Clients connection.Received().Put(Arg.Is(u => u.ToString() == "repos/owner/test/collaborators/user1")); } + [Fact] + public void RequestsCorrectUrlWithRepositoryId() + { + var connection = Substitute.For(); + var client = new RepoCollaboratorsClient(connection); + + client.Add(1, "user1"); + connection.Received().Put(Arg.Is(u => u.ToString() == "repositories/1/collaborators/user1")); + } + [Fact] public async Task EnsuresNonNullArguments() { var client = new RepoCollaboratorsClient(Substitute.For()); await Assert.ThrowsAsync(() => client.Add(null, "test", "user1")); - await Assert.ThrowsAsync(() => client.Add("", "test", "user1")); await Assert.ThrowsAsync(() => client.Add("owner", null, "user1")); + await Assert.ThrowsAsync(() => client.Add("owner", "test", null)); + await Assert.ThrowsAsync(() => client.Add(1, null)); + + await Assert.ThrowsAsync(() => client.Add("", "test", "user1")); await Assert.ThrowsAsync(() => client.Add("owner", "", "user1")); await Assert.ThrowsAsync(() => client.Add("owner", "test", "")); - await Assert.ThrowsAsync(() => client.Add("owner", "test", null)); + await Assert.ThrowsAsync(() => client.Add(1, "")); } } @@ -158,17 +239,30 @@ namespace Octokit.Tests.Clients connection.Received().Delete(Arg.Is(u => u.ToString() == "repos/owner/test/collaborators/user1")); } + [Fact] + public void RequestsCorrectUrlWithRepositoryId() + { + var connection = Substitute.For(); + var client = new RepoCollaboratorsClient(connection); + + client.Delete(1, "user1"); + connection.Received().Delete(Arg.Is(u => u.ToString() == "repositories/1/collaborators/user1")); + } + [Fact] public async Task EnsuresNonNullArguments() { var client = new RepoCollaboratorsClient(Substitute.For()); await Assert.ThrowsAsync(() => client.Delete(null, "test", "user1")); - await Assert.ThrowsAsync(() => client.Delete("", "test", "user1")); await Assert.ThrowsAsync(() => client.Delete("owner", null, "user1")); + await Assert.ThrowsAsync(() => client.Delete("owner", "test", null)); + await Assert.ThrowsAsync(() => client.Delete(1, null)); + + await Assert.ThrowsAsync(() => client.Delete("", "test", "user1"));; await Assert.ThrowsAsync(() => client.Delete("owner", "", "user1")); await Assert.ThrowsAsync(() => client.Delete("owner", "test", "")); - await Assert.ThrowsAsync(() => client.Delete("owner", "test", null)); + await Assert.ThrowsAsync(() => client.Delete(1, "")); } } }