From 7f9429b8e962b3db1374a32e9b0e5407126ac4fd Mon Sep 17 00:00:00 2001 From: Henrik Andersson Date: Sun, 15 Jun 2014 08:34:39 +1000 Subject: [PATCH] Added unit tests --- .../RepositoryDeployKeysClientTests.cs | 128 ++++++++++++++++++ Octokit.Tests/Helpers/Arg.cs | 5 + Octokit.Tests/Octokit.Tests.csproj | 1 + .../Clients/IRepositoryDeployKeysClient.cs | 2 +- Octokit/Clients/RepositoryDeployKeysClient.cs | 13 +- Octokit/Helpers/ApiUrls.cs | 2 +- 6 files changed, 142 insertions(+), 9 deletions(-) create mode 100644 Octokit.Tests/Clients/RepositoryDeployKeysClientTests.cs diff --git a/Octokit.Tests/Clients/RepositoryDeployKeysClientTests.cs b/Octokit.Tests/Clients/RepositoryDeployKeysClientTests.cs new file mode 100644 index 00000000..ab86ac63 --- /dev/null +++ b/Octokit.Tests/Clients/RepositoryDeployKeysClientTests.cs @@ -0,0 +1,128 @@ +using NSubstitute; +using System; +using Xunit; + +namespace Octokit.Tests.Clients +{ + /// + /// Client tests mostly just need to make sure they call the IApiConnection with the correct + /// relative Uri. No need to fake up the response. All *those* tests are in ApiConnectionTests.cs. + /// + public class RepositoryDeployKeysClientTests + { + public class TheConstructor + { + [Fact] + public void ThrowsForBadArgs() + { + Assert.Throws(() => new RepositoryDeployKeysClient(null)); + } + } + + public class TheGetMethod + { + [Fact] + public void GetsADeployKey() + { + var apiConnection = Substitute.For(); + var deployKeysClient = new RepositoryDeployKeysClient(apiConnection); + + deployKeysClient.Get("user", "repo", 42); + + apiConnection.Received().Get(Arg.Is(u => u.ToString() == "repos/user/repo/keys/42"), + null); + } + + [Fact] + public void EnsureNonNullArguments() + { + var deployKeysClient = new RepositoryDeployKeysClient(Substitute.For()); + + Assert.Throws(() => deployKeysClient.Get(null, "repo", 1)); + Assert.Throws(() => deployKeysClient.Get("", "repo", 1)); + Assert.Throws(() => deployKeysClient.Get("user", null, 1)); + Assert.Throws(() => deployKeysClient.Get("user", "", 1)); + } + } + + public class TheGetAllMethod + { + [Fact] + public void GetsAListOfDeployKeys() + { + var apiConnection = Substitute.For(); + var deployKeysClient = new RepositoryDeployKeysClient(apiConnection); + + deployKeysClient.GetAll("user", "repo"); + + apiConnection.Received().GetAll(Arg.Is(u => u.ToString() == "repos/user/repo/keys")); + } + + [Fact] + public void EnsuresNonNullArguments() + { + var deployKeysClient = new RepositoryDeployKeysClient(Substitute.For()); + + Assert.Throws(() => deployKeysClient.GetAll(null, "repo")); + Assert.Throws(() => deployKeysClient.GetAll("", "repo")); + Assert.Throws(() => deployKeysClient.GetAll("user", null)); + Assert.Throws(() => deployKeysClient.GetAll("user", "")); + } + } + + public class TheCreateMethod + { + [Fact] + public void SendsCreateToCorrectUrl() + { + var apiConnection = Substitute.For(); + var deployKeysClient = new RepositoryDeployKeysClient(apiConnection); + + deployKeysClient.Create("user", "repo", new NewDeployKey { Key = "ABC123", Title = "user@repo" }); + + apiConnection.Received().Post(Arg.Is(u => u.ToString() == "repos/user/repo/keys"), + Args.NewDeployKey); + } + + [Fact] + public void EnsuresNonNullArguments() + { + var deployKeysClient = new RepositoryDeployKeysClient(Substitute.For()); + + Assert.Throws(() => deployKeysClient.Create(null, "repo", new NewDeployKey())); + Assert.Throws(() => deployKeysClient.Create("", "repo", new NewDeployKey())); + Assert.Throws(() => deployKeysClient.Create("user", null, new NewDeployKey())); + Assert.Throws(() => deployKeysClient.Create("user", "", new NewDeployKey())); + Assert.Throws(() => deployKeysClient.Create("user", "repo", null)); + Assert.Throws(() => deployKeysClient.Create("user", "repo", new NewDeployKey())); + Assert.Throws(() => deployKeysClient.Create("user", "repo", new NewDeployKey { Key = "ABC123" })); + Assert.Throws(() => deployKeysClient.Create("user", "repo", new NewDeployKey { Title = "user@repo" })); + } + } + + public class TheDeleteMethod + { + [Fact] + public void DeletesCorrectUrl() + { + var apiConnection = Substitute.For(); + var deployKeysClient = new RepositoryDeployKeysClient(apiConnection); + + deployKeysClient.Delete("user", "repo", 42); + + apiConnection.Received().Delete(Arg.Is(u => u.ToString() == "repos/user/repo/keys/42")); + } + + [Fact] + public void EnsuresNonNullArguments() + { + var deployKeysClient = new RepositoryDeployKeysClient(Substitute.For()); + + Assert.Throws(() => deployKeysClient.Delete(null, "repo", 1)); + Assert.Throws(() => deployKeysClient.Delete("", "repo", 1)); + Assert.Throws(() => deployKeysClient.Delete("user", null, 1)); + Assert.Throws(() => deployKeysClient.Delete("user", "", 1)); + } + } + } +} diff --git a/Octokit.Tests/Helpers/Arg.cs b/Octokit.Tests/Helpers/Arg.cs index 4fce3434..db8d1d67 100644 --- a/Octokit.Tests/Helpers/Arg.cs +++ b/Octokit.Tests/Helpers/Arg.cs @@ -57,5 +57,10 @@ namespace Octokit.Tests { get { return Arg.Any(); } } + + public static NewDeployKey NewDeployKey + { + get { return Arg.Any(); } + } } } diff --git a/Octokit.Tests/Octokit.Tests.csproj b/Octokit.Tests/Octokit.Tests.csproj index cde48c61..355667a7 100644 --- a/Octokit.Tests/Octokit.Tests.csproj +++ b/Octokit.Tests/Octokit.Tests.csproj @@ -67,6 +67,7 @@ + diff --git a/Octokit/Clients/IRepositoryDeployKeysClient.cs b/Octokit/Clients/IRepositoryDeployKeysClient.cs index cf4af6bd..8f6b09c9 100644 --- a/Octokit/Clients/IRepositoryDeployKeysClient.cs +++ b/Octokit/Clients/IRepositoryDeployKeysClient.cs @@ -28,7 +28,7 @@ namespace Octokit /// /// The owner of the repository. /// The name of the repository. - Task> GetForRepository(string owner, string name); + Task> GetAll(string owner, string name); /// /// Creates a new deploy key for a repository. diff --git a/Octokit/Clients/RepositoryDeployKeysClient.cs b/Octokit/Clients/RepositoryDeployKeysClient.cs index fcb5a287..9783911f 100644 --- a/Octokit/Clients/RepositoryDeployKeysClient.cs +++ b/Octokit/Clients/RepositoryDeployKeysClient.cs @@ -1,7 +1,7 @@ using System; +#if NET_45 using System.Collections.Generic; -using System.Linq; -using System.Text; +#endif using System.Threading.Tasks; namespace Octokit @@ -36,7 +36,6 @@ namespace Octokit { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); - Ensure.ArgumentNotNull(number, "number"); return ApiConnection.Get(ApiUrls.RepositoryDeployKey(owner, name, number)); } @@ -49,12 +48,12 @@ namespace Octokit /// /// The owner of the repository. /// The name of the repository. - public Task> GetForRepository(string owner, string name) + public Task> GetAll(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); - return ApiConnection.Get>(ApiUrls.RepositoryDeployKeys(owner, name)); + return ApiConnection.GetAll(ApiUrls.RepositoryDeployKeys(owner, name)); } /// @@ -82,11 +81,12 @@ namespace Octokit return ApiConnection.Post(ApiUrls.RepositoryDeployKeys(owner, name), newDeployKey); } + /// /// Deploy keys are immutable. If you need to update a key, remove the key and create a new one instead. /// /// - /// https://developer.github.com/v3/repos/keys/#edit + /// See the API documentation for more information. /// /// /// @@ -94,7 +94,6 @@ namespace Octokit /// /// /// Task Update(string owner, string name, int number, NewDeployKey newDeployKey); - /// /// Deletes a deploy key from a repository. /// diff --git a/Octokit/Helpers/ApiUrls.cs b/Octokit/Helpers/ApiUrls.cs index b5c03f0c..58397c24 100644 --- a/Octokit/Helpers/ApiUrls.cs +++ b/Octokit/Helpers/ApiUrls.cs @@ -1121,7 +1121,7 @@ namespace Octokit /// public static Uri RepositoryDeployKey(string owner, string name, int number) { - return "repos/{0}/{1}/keys/{1}".FormatUri(owner, name, number); + return "repos/{0}/{1}/keys/{2}".FormatUri(owner, name, number); } ///