Implemented UserEmailsClient GetAll and Add...

methods plus unit tests.
This commit is contained in:
Peter MacNaughton
2014-01-23 11:57:21 -07:00
parent 8e275b5220
commit 38aec1694b
13 changed files with 127 additions and 49 deletions
@@ -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);
}
}
}
@@ -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<IApiConnection>();
var client = new UserEmailsClient(connection);
client.GetAll();
connection.Received(1)
.GetAll<EmailAddress>(Arg.Is<Uri>(u => u.ToString() == "user/emails"));
}
}
public class TheAddMethod
{
[Fact]
public void PostsToCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new UserEmailsClient(connection);
client.Add("octocat@github.com");
connection.Received(1)
.Post<IReadOnlyList<string>>(Arg.Is<Uri>(u => u.ToString() == "user/emails"), Arg.Any<string[]>());
}
[Fact]
public async Task EnsuresNonNullArgument()
{
var client = new UserEmailsClient(Substitute.For<IApiConnection>());
await AssertEx.Throws<ArgumentNullException>(async () => await client.Add(null));
}
[Fact]
public async Task EnsuresNoNullEmails()
{
var client = new UserEmailsClient(Substitute.For<IApiConnection>());
await AssertEx.Throws<ArgumentException>(async () => await client.Add("octokit@github.com", null));
}
}
}
}
-15
View File
@@ -90,20 +90,5 @@ namespace Octokit.Tests.Clients
await AssertEx.Throws<ArgumentNullException>(() => userEndpoint.Update(null));
}
}
public class TheGetEmailsMethod
{
[Fact]
public void SendsUpdateToCorrectUrl()
{
var endpoint = new Uri("user/emails", UriKind.Relative);
var client = Substitute.For<IApiConnection>();
var usersClient = new UsersClient(client);
usersClient.GetEmails();
client.Received().GetAll<EmailAddress>(endpoint, null);
}
}
}
}
+1
View File
@@ -86,6 +86,7 @@
<Compile Include="Clients\ReleasesClientTests.cs" />
<Compile Include="Clients\SshKeysClientTests.cs" />
<Compile Include="Clients\TreesClientTests.cs" />
<Compile Include="Clients\UserEmailsClientTests.cs" />
<Compile Include="Exceptions\ApiExceptionTests.cs" />
<Compile Include="Exceptions\ApiValidationExceptionTests.cs" />
<Compile Include="Exceptions\TwoFactorChallengeFailedException.cs" />
+23
View File
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
namespace Octokit
{
public interface IUserEmailsClient
{
/// <summary>
/// Gets all email addresses for the authenticated user.
/// </summary>
/// <returns></returns>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
Task<IReadOnlyList<EmailAddress>> GetAll();
/// <summary>
/// Adds email addresses for the authenticated user.
/// </summary>
/// <param name="emailAddresses">The email addresses to add.</param>
/// <returns></returns>
Task<IReadOnlyList<string>> Add(params string[] emailAddresses);
}
}
-7
View File
@@ -34,12 +34,5 @@ namespace Octokit
/// <exception cref="AuthorizationException">Thrown if the client is not authenticated.</exception>
/// <returns>A <see cref="User"/></returns>
Task<User> Update(UserUpdate user);
/// <summary>
/// Returns emails for the current user.
/// </summary>
/// <returns></returns>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
Task<IReadOnlyList<EmailAddress>> GetEmails();
}
}
+37
View File
@@ -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)
{ }
/// <summary>
/// Gets all email addresses for the authenticated user.
/// </summary>
/// <returns></returns>
public Task<IReadOnlyList<EmailAddress>> GetAll()
{
return ApiConnection.GetAll<EmailAddress>(ApiUrls.Emails());
}
/// <summary>
/// Adds email addresses for the authenticated user.
/// </summary>
/// <param name="emailAddresses">The email addresses to add.</param>
/// <returns></returns>
public Task<IReadOnlyList<string>> 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<IReadOnlyList<string>>(ApiUrls.Emails(), emailAddresses);
}
}
}
-9
View File
@@ -59,14 +59,5 @@ namespace Octokit
return ApiConnection.Patch<User>(_userEndpoint, user);
}
/// <summary>
/// Returns emails for the current user.
/// </summary>
/// <returns></returns>
public Task<IReadOnlyList<EmailAddress>> GetEmails()
{
return ApiConnection.GetAll<EmailAddress>(ApiUrls.Emails(), null);
}
}
}
+2
View File
@@ -248,6 +248,8 @@
<Compile Include="Models\Request\BodyWrapper.cs" />
<Compile Include="Models\Request\BaseSearchRequest.cs" />
<Compile Include="Helpers\EnumExtensions.cs" />
<Compile Include="Clients\IUserEmailsClient.cs" />
<Compile Include="Clients\UserEmailsClient.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
+2
View File
@@ -258,6 +258,8 @@
<Compile Include="Models\Response\Branch.cs" />
<Compile Include="Models\Request\BaseSearchRequest.cs" />
<Compile Include="Helpers\EnumExtensions.cs" />
<Compile Include="Clients\IUserEmailsClient.cs" />
<Compile Include="Clients\UserEmailsClient.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
</Project>
+2
View File
@@ -253,6 +253,8 @@
<Compile Include="Models\Response\Branch.cs" />
<Compile Include="Models\Request\BaseSearchRequest.cs" />
<Compile Include="Helpers\EnumExtensions.cs" />
<Compile Include="Clients\IUserEmailsClient.cs" />
<Compile Include="Clients\UserEmailsClient.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
+2
View File
@@ -246,6 +246,8 @@
<Compile Include="Models\Response\GistComment.cs" />
<Compile Include="Models\Request\BodyWrapper.cs" />
<Compile Include="Models\Request\BaseSearchRequest.cs" />
<Compile Include="Clients\IUserEmailsClient.cs" />
<Compile Include="Clients\UserEmailsClient.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">
+2
View File
@@ -54,8 +54,10 @@
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="Clients\ActivitiesClient.cs" />
<Compile Include="Clients\IUserEmailsClient.cs" />
<Compile Include="Clients\SearchClient.cs" />
<Compile Include="Clients\ISearchClient.cs" />
<Compile Include="Clients\UserEmailsClient.cs" />
<Compile Include="Models\Request\BaseSearchRequest.cs" />
<Compile Include="Helpers\EnumExtensions.cs">
<SubType>Code</SubType>