using System; using System.Collections.Generic; using System.Net; using System.Threading.Tasks; using NSubstitute; using Octokit.Internal; using Octokit.Tests.Helpers; using Xunit; namespace Octokit.Tests.Clients { public class AssignessClientTests { public class TheGetForRepositoryMethod { [Fact] public void RequestsCorrectUrl() { var connection = Substitute.For(); var client = new AssigneesClient(connection); client.GetAllForRepository("fake", "repo"); connection.Received().GetAll(Arg.Is(u => u.ToString() == "repos/fake/repo/assignees")); } [Fact] public async Task EnsuresNonNullArguments() { var client = new AssigneesClient(Substitute.For()); await Assert.ThrowsAsync(() => client.GetAllForRepository(null, "name")); await Assert.ThrowsAsync(() => client.GetAllForRepository(null, "")); await Assert.ThrowsAsync(() => client.GetAllForRepository("owner", null)); await Assert.ThrowsAsync(() => client.GetAllForRepository("", null)); } } public class TheCheckAssigneeMethod { [Theory] [InlineData(HttpStatusCode.NoContent, true)] [InlineData(HttpStatusCode.NotFound, false)] public async Task RequestsCorrectValueForStatusCode(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() == "repos/foo/bar/assignees/cody"), null, null).Returns(response); var apiConnection = Substitute.For(); apiConnection.Connection.Returns(connection); var client = new AssigneesClient(apiConnection); var result = await client.CheckAssignee("foo", "bar", "cody"); Assert.Equal(expected, result); } [Fact] public async Task ThrowsExceptionForInvalidStatusCode() { 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() == "repos/foo/bar/assignees/cody"), null, null).Returns(response); var apiConnection = Substitute.For(); apiConnection.Connection.Returns(connection); var client = new AssigneesClient(apiConnection); await Assert.ThrowsAsync(() => client.CheckAssignee("foo", "bar", "cody")); } [Fact] public async Task EnsuresNonNullArguments() { var client = new AssigneesClient(Substitute.For()); await Assert.ThrowsAsync(() => client.CheckAssignee(null, "name", "tweety")); await Assert.ThrowsAsync(() => client.CheckAssignee(null, "", "tweety")); await Assert.ThrowsAsync(() => client.CheckAssignee("owner", null, "tweety")); await Assert.ThrowsAsync(() => client.CheckAssignee("", null, "tweety")); await Assert.ThrowsAsync(() => client.CheckAssignee("owner", "name", null)); await Assert.ThrowsAsync(() => client.CheckAssignee("owner", "name", "")); } } public class TheCtor { [Fact] public void EnsuresArgument() { Assert.Throws(() => new AssigneesClient(null)); } } } }