using System; using System.Collections.Generic; using System.Threading.Tasks; using NSubstitute; using Xunit; namespace Octokit.Tests.Clients { public class RepositoryHooksClientTests { public class TheCtor { [Fact] public void EnsuresNonNullArguments() { Assert.Throws( () => new RepositoryHooksClient(null)); } } public class TheGetAllMethod { [Fact] public async Task RequestsCorrectUrl() { var connection = Substitute.For(); var client = new RepositoryHooksClient(connection); await client.GetAll("fake", "repo"); connection.Received().GetAll(Arg.Is(u => u.ToString() == "repos/fake/repo/hooks"), Args.ApiOptions); } [Fact] public async Task RequestsCorrectUrlWithRepositoryId() { var connection = Substitute.For(); var client = new RepositoryHooksClient(connection); await client.GetAll(1); connection.Received().GetAll(Arg.Is(u => u.ToString() == "repositories/1/hooks"), Args.ApiOptions); } [Fact] public async Task RequestsCorrectUrlWithApiOptions() { var connection = Substitute.For(); var client = new RepositoryHooksClient(connection); var options = new ApiOptions { PageCount = 1, PageSize = 1, StartPage = 1 }; await client.GetAll("fake", "repo", options); connection.Received(1) .GetAll(Arg.Is(u => u.ToString() == "repos/fake/repo/hooks"), options); } [Fact] public async Task RequestsCorrectUrlWithRepositoryIdWithApiOptions() { var connection = Substitute.For(); var client = new RepositoryHooksClient(connection); var options = new ApiOptions { PageCount = 1, PageSize = 1, StartPage = 1 }; await client.GetAll(1, options); connection.Received(1) .GetAll(Arg.Is(u => u.ToString() == "repositories/1/hooks"), options); } [Fact] public async Task EnsuresNonNullArguments() { var client = new RepositoryHooksClient(Substitute.For()); await Assert.ThrowsAsync(() => client.GetAll(null, "name")); await Assert.ThrowsAsync(() => client.GetAll("owner", null)); await Assert.ThrowsAsync(() => client.GetAll(null, "name", ApiOptions.None)); await Assert.ThrowsAsync(() => client.GetAll("owner", null, ApiOptions.None)); await Assert.ThrowsAsync(() => client.GetAll("owner", "name", null)); await Assert.ThrowsAsync(() => client.GetAll(1, null)); await Assert.ThrowsAsync(() => client.GetAll("", "name")); await Assert.ThrowsAsync(() => client.GetAll("owner", "")); await Assert.ThrowsAsync(() => client.GetAll("", "name", ApiOptions.None)); await Assert.ThrowsAsync(() => client.GetAll("owner", "", ApiOptions.None)); } } public class TheGetMethod { [Fact] public async Task RequestsCorrectUrl() { var connection = Substitute.For(); var client = new RepositoryHooksClient(connection); await client.Get("fake", "repo", 12345678); connection.Received().Get(Arg.Is(u => u.ToString() == "repos/fake/repo/hooks/12345678")); } [Fact] public async Task RequestsCorrectUrlWithRepositoryId() { var connection = Substitute.For(); var client = new RepositoryHooksClient(connection); await client.Get(1, 12345678); connection.Received().Get(Arg.Is(u => u.ToString() == "repositories/1/hooks/12345678")); } [Fact] public async Task EnsuresNonNullArguments() { var client = new RepositoryHooksClient(Substitute.For()); await Assert.ThrowsAsync(() => client.Get(null, "name", 123)); await Assert.ThrowsAsync(() => client.Get("owner", null, 123)); await Assert.ThrowsAsync(() => client.Get("", "name", 123)); await Assert.ThrowsAsync(() => client.Get("owner", "", 123)); } } public class TheCreateMethod { [Fact] public void RequestsCorrectUrl() { var connection = Substitute.For(); var client = new RepositoryHooksClient(connection); var hook = new NewRepositoryHook("name", new Dictionary { { "config", "" } }); client.Create("fake", "repo", hook); connection.Received().Post(Arg.Is(u => u.ToString() == "repos/fake/repo/hooks"), hook); } [Fact] public void RequestsCorrectUrlWithRepositoryId() { var connection = Substitute.For(); var client = new RepositoryHooksClient(connection); var hook = new NewRepositoryHook("name", new Dictionary { { "config", "" } }); client.Create(1, hook); connection.Received().Post(Arg.Is(u => u.ToString() == "repositories/1/hooks"), hook); } [Fact] public async Task EnsuresNonNullArguments() { var client = new RepositoryHooksClient(Substitute.For()); var config = new Dictionary { { "config", "" } }; await Assert.ThrowsAsync(() => client.Create(null, "name", new NewRepositoryHook("name", config))); await Assert.ThrowsAsync(() => client.Create("owner", null, new NewRepositoryHook("name", config))); await Assert.ThrowsAsync(() => client.Create("owner", "name", null)); await Assert.ThrowsAsync(() => client.Create(1, null)); await Assert.ThrowsAsync(() => client.Create("", "name", new NewRepositoryHook("name", config))); await Assert.ThrowsAsync(() => client.Create("owner", "", new NewRepositoryHook("name", config))); } } public class TheEditMethod { [Fact] public void RequestsCorrectUrl() { var connection = Substitute.For(); var client = new RepositoryHooksClient(connection); var hook = new EditRepositoryHook(); client.Edit("fake", "repo", 12345678, hook); connection.Received().Patch(Arg.Is(u => u.ToString() == "repos/fake/repo/hooks/12345678"), hook); } [Fact] public void RequestsCorrectUrlWithRepositoryId() { var connection = Substitute.For(); var client = new RepositoryHooksClient(connection); var hook = new EditRepositoryHook(); client.Edit(1, 12345678, hook); connection.Received().Patch(Arg.Is(u => u.ToString() == "repositories/1/hooks/12345678"), hook); } [Fact] public async Task EnsuresNonNullArguments() { var client = new RepositoryHooksClient(Substitute.For()); await Assert.ThrowsAsync(() => client.Edit(null, "name", 12345678, new EditRepositoryHook())); await Assert.ThrowsAsync(() => client.Edit("owner", null, 12345678, new EditRepositoryHook())); await Assert.ThrowsAsync(() => client.Edit("owner", "name", 12345678, null)); await Assert.ThrowsAsync(() => client.Edit(1, 12345678, null)); await Assert.ThrowsAsync(() => client.Edit("", "name", 12345678, new EditRepositoryHook())); await Assert.ThrowsAsync(() => client.Edit("owner", "", 12345678, new EditRepositoryHook())); } } public class TheTestMethod { [Fact] public void RequestsCorrectUrl() { var connection = Substitute.For(); var client = new RepositoryHooksClient(connection); client.Test("fake", "repo", 12345678); connection.Received().Post(Arg.Is(u => u.ToString() == "repos/fake/repo/hooks/12345678/tests")); } [Fact] public void RequestsCorrectUrlWithRepositoryId() { var connection = Substitute.For(); var client = new RepositoryHooksClient(connection); client.Test(1, 12345678); connection.Received().Post(Arg.Is(u => u.ToString() == "repositories/1/hooks/12345678/tests")); } [Fact] public async Task EnsuresNonNullArguments() { var client = new RepositoryHooksClient(Substitute.For()); await Assert.ThrowsAsync(() => client.Test(null, "name", 12345678)); await Assert.ThrowsAsync(() => client.Test("owner", null, 12345678)); await Assert.ThrowsAsync(() => client.Test("", "name", 12345678)); await Assert.ThrowsAsync(() => client.Test("owner", "", 12345678)); } } public class ThePingMethod { [Fact] public void RequestsCorrectUrl() { var connection = Substitute.For(); var client = new RepositoryHooksClient(connection); client.Ping("fake", "repo", 12345678); connection.Received().Post(Arg.Is(u => u.ToString() == "repos/fake/repo/hooks/12345678/pings")); } [Fact] public void RequestsCorrectUrlWithRepositoryId() { var connection = Substitute.For(); var client = new RepositoryHooksClient(connection); client.Ping(1, 12345678); connection.Received().Post(Arg.Is(u => u.ToString() == "repositories/1/hooks/12345678/pings")); } [Fact] public async Task EnsuresNonNullArguments() { var client = new RepositoryHooksClient(Substitute.For()); await Assert.ThrowsAsync(() => client.Ping(null, "name", 12345678)); await Assert.ThrowsAsync(() => client.Ping("owner", null, 12345678)); await Assert.ThrowsAsync(() => client.Ping("", "name", 12345678)); await Assert.ThrowsAsync(() => client.Ping("owner", "", 12345678)); } } public class TheDeleteMethod { [Fact] public void RequestsCorrectUrl() { var connection = Substitute.For(); var client = new RepositoryHooksClient(connection); client.Delete("fake", "repo", 12345678); connection.Received().Delete(Arg.Is(u => u.ToString() == "repos/fake/repo/hooks/12345678")); } [Fact] public void RequestsCorrectUrlWithRepositoryId() { var connection = Substitute.For(); var client = new RepositoryHooksClient(connection); client.Delete(1, 12345678); connection.Received().Delete(Arg.Is(u => u.ToString() == "repositories/1/hooks/12345678")); } [Fact] public async Task EnsuresNonNullArguments() { var client = new RepositoryHooksClient(Substitute.For()); await Assert.ThrowsAsync(() => client.Delete(null, "name", 12345678)); await Assert.ThrowsAsync(() => client.Delete("owner", null, 12345678)); await Assert.ThrowsAsync(() => client.Delete("", "name", 12345678)); await Assert.ThrowsAsync(() => client.Delete("owner", "", 12345678)); } } } }