From 38aec1694b341434f87e5136d76e2d9d569eba27 Mon Sep 17 00:00:00 2001 From: Peter MacNaughton Date: Thu, 23 Jan 2014 11:57:21 -0700 Subject: [PATCH] Implemented UserEmailsClient GetAll and Add... methods plus unit tests. --- .../Clients/UsersClientTests.cs | 18 ------ .../Clients/UserEmailsClientTests.cs | 56 +++++++++++++++++++ Octokit.Tests/Clients/UsersClientTests.cs | 15 ----- Octokit.Tests/Octokit.Tests.csproj | 1 + Octokit/Clients/IUserEmailsClient.cs | 23 ++++++++ Octokit/Clients/IUsersClient.cs | 7 --- Octokit/Clients/UserEmailsClient.cs | 37 ++++++++++++ Octokit/Clients/UsersClient.cs | 9 --- Octokit/Octokit-Mono.csproj | 2 + Octokit/Octokit-MonoAndroid.csproj | 2 + Octokit/Octokit-Monotouch.csproj | 2 + Octokit/Octokit-netcore45.csproj | 2 + Octokit/Octokit.csproj | 2 + 13 files changed, 127 insertions(+), 49 deletions(-) create mode 100644 Octokit.Tests/Clients/UserEmailsClientTests.cs create mode 100644 Octokit/Clients/IUserEmailsClient.cs create mode 100644 Octokit/Clients/UserEmailsClient.cs diff --git a/Octokit.Tests.Integration/Clients/UsersClientTests.cs b/Octokit.Tests.Integration/Clients/UsersClientTests.cs index 23de66ec..c4e1bfd2 100644 --- a/Octokit.Tests.Integration/Clients/UsersClientTests.cs +++ b/Octokit.Tests.Integration/Clients/UsersClientTests.cs @@ -99,22 +99,4 @@ public class UsersClientTests Assert.Equal(HttpStatusCode.Unauthorized, e.StatusCode); } } - - public class TheGetEmailsMethod - { - [IntegrationTest] - public async Task RetrievesEmailsForUser() - { - var github = new GitHubClient(new ProductHeaderValue("OctokitTests")) - { - Credentials = Helper.Credentials - }; - - var emails = await github.User.GetEmails(); - - Assert.NotEmpty(emails); - var email = emails.First(); - Assert.True(email.Primary); - } - } } diff --git a/Octokit.Tests/Clients/UserEmailsClientTests.cs b/Octokit.Tests/Clients/UserEmailsClientTests.cs new file mode 100644 index 00000000..d74f0bed --- /dev/null +++ b/Octokit.Tests/Clients/UserEmailsClientTests.cs @@ -0,0 +1,56 @@ +using NSubstitute; +using Octokit.Tests.Helpers; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Xunit; + +namespace Octokit.Tests.Clients +{ + public class UserEmailsClientTests + { + public class TheGetAllMethod + { + [Fact] + public void GetsCorrectUrl() + { + var connection = Substitute.For(); + var client = new UserEmailsClient(connection); + + client.GetAll(); + + connection.Received(1) + .GetAll(Arg.Is(u => u.ToString() == "user/emails")); + } + } + + public class TheAddMethod + { + [Fact] + public void PostsToCorrectUrl() + { + var connection = Substitute.For(); + var client = new UserEmailsClient(connection); + + client.Add("octocat@github.com"); + + connection.Received(1) + .Post>(Arg.Is(u => u.ToString() == "user/emails"), Arg.Any()); + } + + [Fact] + public async Task EnsuresNonNullArgument() + { + var client = new UserEmailsClient(Substitute.For()); + await AssertEx.Throws(async () => await client.Add(null)); + } + + [Fact] + public async Task EnsuresNoNullEmails() + { + var client = new UserEmailsClient(Substitute.For()); + await AssertEx.Throws(async () => await client.Add("octokit@github.com", null)); + } + } + } +} diff --git a/Octokit.Tests/Clients/UsersClientTests.cs b/Octokit.Tests/Clients/UsersClientTests.cs index dcdc85de..783c49eb 100644 --- a/Octokit.Tests/Clients/UsersClientTests.cs +++ b/Octokit.Tests/Clients/UsersClientTests.cs @@ -90,20 +90,5 @@ namespace Octokit.Tests.Clients await AssertEx.Throws(() => userEndpoint.Update(null)); } } - - public class TheGetEmailsMethod - { - [Fact] - public void SendsUpdateToCorrectUrl() - { - var endpoint = new Uri("user/emails", UriKind.Relative); - var client = Substitute.For(); - var usersClient = new UsersClient(client); - - usersClient.GetEmails(); - - client.Received().GetAll(endpoint, null); - } - } } } diff --git a/Octokit.Tests/Octokit.Tests.csproj b/Octokit.Tests/Octokit.Tests.csproj index 6ad0f6c0..f99c2008 100644 --- a/Octokit.Tests/Octokit.Tests.csproj +++ b/Octokit.Tests/Octokit.Tests.csproj @@ -86,6 +86,7 @@ + diff --git a/Octokit/Clients/IUserEmailsClient.cs b/Octokit/Clients/IUserEmailsClient.cs new file mode 100644 index 00000000..a2cd66fb --- /dev/null +++ b/Octokit/Clients/IUserEmailsClient.cs @@ -0,0 +1,23 @@ +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Threading.Tasks; + +namespace Octokit +{ + public interface IUserEmailsClient + { + /// + /// Gets all email addresses for the authenticated user. + /// + /// + [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] + Task> GetAll(); + + /// + /// Adds email addresses for the authenticated user. + /// + /// The email addresses to add. + /// + Task> Add(params string[] emailAddresses); + } +} diff --git a/Octokit/Clients/IUsersClient.cs b/Octokit/Clients/IUsersClient.cs index e5c91a88..2cc4e7e4 100644 --- a/Octokit/Clients/IUsersClient.cs +++ b/Octokit/Clients/IUsersClient.cs @@ -34,12 +34,5 @@ namespace Octokit /// Thrown if the client is not authenticated. /// A Task Update(UserUpdate user); - - /// - /// Returns emails for the current user. - /// - /// - [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] - Task> GetEmails(); } } diff --git a/Octokit/Clients/UserEmailsClient.cs b/Octokit/Clients/UserEmailsClient.cs new file mode 100644 index 00000000..03b48659 --- /dev/null +++ b/Octokit/Clients/UserEmailsClient.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Octokit +{ + public class UserEmailsClient : ApiClient, IUserEmailsClient + { + public UserEmailsClient(IApiConnection apiConnection) + : base(apiConnection) + { } + + /// + /// Gets all email addresses for the authenticated user. + /// + /// + public Task> GetAll() + { + return ApiConnection.GetAll(ApiUrls.Emails()); + } + + /// + /// Adds email addresses for the authenticated user. + /// + /// The email addresses to add. + /// + public Task> Add(params string[] emailAddresses) + { + Ensure.ArgumentNotNull(emailAddresses, "emailAddresses"); + if (emailAddresses.Any(String.IsNullOrWhiteSpace)) + throw new ArgumentException("Cannot contain null, empty or whitespace values", "emailAddresses"); + + return ApiConnection.Post>(ApiUrls.Emails(), emailAddresses); + } + } +} \ No newline at end of file diff --git a/Octokit/Clients/UsersClient.cs b/Octokit/Clients/UsersClient.cs index 48ff6746..7aa51c46 100644 --- a/Octokit/Clients/UsersClient.cs +++ b/Octokit/Clients/UsersClient.cs @@ -59,14 +59,5 @@ namespace Octokit return ApiConnection.Patch(_userEndpoint, user); } - - /// - /// Returns emails for the current user. - /// - /// - public Task> GetEmails() - { - return ApiConnection.GetAll(ApiUrls.Emails(), null); - } } } diff --git a/Octokit/Octokit-Mono.csproj b/Octokit/Octokit-Mono.csproj index 522b4bfb..16ea0d70 100644 --- a/Octokit/Octokit-Mono.csproj +++ b/Octokit/Octokit-Mono.csproj @@ -248,6 +248,8 @@ + + \ No newline at end of file diff --git a/Octokit/Octokit-MonoAndroid.csproj b/Octokit/Octokit-MonoAndroid.csproj index 932a66d0..d5de964e 100644 --- a/Octokit/Octokit-MonoAndroid.csproj +++ b/Octokit/Octokit-MonoAndroid.csproj @@ -258,6 +258,8 @@ + + \ No newline at end of file diff --git a/Octokit/Octokit-Monotouch.csproj b/Octokit/Octokit-Monotouch.csproj index a9f1d577..f47f912a 100644 --- a/Octokit/Octokit-Monotouch.csproj +++ b/Octokit/Octokit-Monotouch.csproj @@ -253,6 +253,8 @@ + + \ No newline at end of file diff --git a/Octokit/Octokit-netcore45.csproj b/Octokit/Octokit-netcore45.csproj index bb077574..a1670eda 100644 --- a/Octokit/Octokit-netcore45.csproj +++ b/Octokit/Octokit-netcore45.csproj @@ -246,6 +246,8 @@ + + diff --git a/Octokit/Octokit.csproj b/Octokit/Octokit.csproj index febcad07..fec5c20d 100644 --- a/Octokit/Octokit.csproj +++ b/Octokit/Octokit.csproj @@ -54,8 +54,10 @@ Properties\SolutionInfo.cs + + Code