|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using NSubstitute; |
| 5 | +using Octokit; |
| 6 | +using Octokit.Models.Response; |
| 7 | +using Octokit.Tests; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | + |
| 11 | +namespace Octokit.Tests.Clients |
| 12 | +{ |
| 13 | + public class EnvironmentsClientTests |
| 14 | + { |
| 15 | + public class TheGetAllMethod |
| 16 | + { |
| 17 | + const string name = "name"; |
| 18 | + const string owner = "owner"; |
| 19 | + const long repositoryId = 1; |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public async Task EnsuresNonNullArguments() |
| 23 | + { |
| 24 | + var client = new DeploymentEnvironmentsClient(Substitute.For<IApiConnection>()); |
| 25 | + |
| 26 | + await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll(null, name)); |
| 27 | + await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll(owner, null)); |
| 28 | + await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll(owner, name, null)); |
| 29 | + |
| 30 | + await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll(repositoryId, null)); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public async Task EnsuresNonEmptyArguments() |
| 35 | + { |
| 36 | + var client = new DeploymentEnvironmentsClient(Substitute.For<IApiConnection>()); |
| 37 | + |
| 38 | + await Assert.ThrowsAsync<ArgumentException>(() => client.GetAll("", name)); |
| 39 | + await Assert.ThrowsAsync<ArgumentException>(() => client.GetAll(owner, "")); |
| 40 | + } |
| 41 | + |
| 42 | + [Theory] |
| 43 | + [InlineData(" ")] |
| 44 | + [InlineData("\n")] |
| 45 | + [InlineData("\t")] |
| 46 | + [InlineData(" ")] |
| 47 | + [InlineData("\n\r")] |
| 48 | + public async Task EnsuresNonWhitespaceArguments(string whitespace) |
| 49 | + { |
| 50 | + var client = new DeploymentEnvironmentsClient(Substitute.For<IApiConnection>()); |
| 51 | + |
| 52 | + await Assert.ThrowsAsync<ArgumentException>(() => client.GetAll(whitespace, name)); |
| 53 | + await Assert.ThrowsAsync<ArgumentException>(() => client.GetAll(owner, whitespace)); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public async Task RequestsCorrectUrl() |
| 58 | + { |
| 59 | + var connection = Substitute.For<IApiConnection>(); |
| 60 | + var client = new DeploymentEnvironmentsClient(connection); |
| 61 | + var expectedUrl = string.Format("repos/{0}/{1}/environments", owner, name); |
| 62 | + |
| 63 | + var res = await client.GetAll(owner, name); |
| 64 | + |
| 65 | + connection.Received() |
| 66 | + .Get<DeploymentEnvironmentsResponse>( |
| 67 | + Arg.Is<Uri>(u => u.ToString() == expectedUrl)); |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public async Task RequestsCorrectUrlWithRepositoryId() |
| 72 | + { |
| 73 | + var connection = Substitute.For<IApiConnection>(); |
| 74 | + var client = new DeploymentEnvironmentsClient(connection); |
| 75 | + var expectedUrl = string.Format("repositories/{0}/environments", repositoryId); |
| 76 | + |
| 77 | + await client.GetAll(repositoryId); |
| 78 | + |
| 79 | + connection.Received() |
| 80 | + .Get<DeploymentEnvironmentsResponse>(Arg.Is<Uri>(u => u.ToString() == expectedUrl)); |
| 81 | + } |
| 82 | + |
| 83 | + [Fact] |
| 84 | + public async Task RequestsCorrectUrlWithApiOptions() |
| 85 | + { |
| 86 | + var connection = Substitute.For<IApiConnection>(); |
| 87 | + var client = new DeploymentEnvironmentsClient(connection); |
| 88 | + var expectedUrl = string.Format("repos/{0}/{1}/environments", owner, name); |
| 89 | + |
| 90 | + var options = new ApiOptions |
| 91 | + { |
| 92 | + PageSize = 10, |
| 93 | + StartPage = 1 |
| 94 | + }; |
| 95 | + |
| 96 | + await client.GetAll(owner, name, options); |
| 97 | + |
| 98 | + connection.Received() |
| 99 | + .Get<DeploymentEnvironmentsResponse>(Arg.Is<Uri>(u => u.ToString() == expectedUrl), |
| 100 | + Arg.Is<IDictionary<string, string>>(u => u["per_page"] == "10" && u["page"] == "1")); |
| 101 | + } |
| 102 | + |
| 103 | + [Fact] |
| 104 | + public async Task RequestsCorrectUrlWithRepositoryIdWithApiOptions() |
| 105 | + { |
| 106 | + var connection = Substitute.For<IApiConnection>(); |
| 107 | + var client = new DeploymentEnvironmentsClient(connection); |
| 108 | + var expectedUrl = string.Format("repositories/{0}/environments", repositoryId); |
| 109 | + |
| 110 | + var options = new ApiOptions |
| 111 | + { |
| 112 | + PageSize = 10, |
| 113 | + StartPage = 1 |
| 114 | + }; |
| 115 | + |
| 116 | + await client.GetAll(repositoryId, options); |
| 117 | + |
| 118 | + connection.Received() |
| 119 | + .Get<DeploymentEnvironmentsResponse>(Arg.Is<Uri>(u => u.ToString() == expectedUrl), |
| 120 | + Arg.Is<IDictionary<string, string>>(u => u["per_page"] == "10" && u["page"] == "1")); |
| 121 | + } |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public void RequestsCorrectUrlWithPreviewAcceptHeaders() |
| 125 | + { |
| 126 | + var connection = Substitute.For<IApiConnection>(); |
| 127 | + var client = new DeploymentEnvironmentsClient(connection); |
| 128 | + var expectedUrl = string.Format("repos/{0}/{1}/environments", owner, name); |
| 129 | + |
| 130 | + client.GetAll(owner, name); |
| 131 | + connection.Received() |
| 132 | + .Get<DeploymentEnvironmentsResponse>(Arg.Is<Uri>(u => u.ToString() == expectedUrl)); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public class TheCtor |
| 137 | + { |
| 138 | + [Fact] |
| 139 | + public void EnsuresNonNullArguments() |
| 140 | + { |
| 141 | + Assert.Throws<ArgumentNullException>(() => new DeploymentEnvironmentsClient(null)); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments