Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented IUserEmailsClient GetAll and Add methods #323

Merged
merged 16 commits into from
Feb 10, 2014
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Octokit.Tests.Integration/Clients/UserEmailsClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Xunit;

namespace Octokit.Tests.Integration.Clients
{
public class UserEmailsClientTests
{
[IntegrationTest]
public async Task CanGetEmail()
{
var github = new GitHubClient(new ProductHeaderValue("OctokitTests"))
{
Credentials = Helper.Credentials
};

var emails = await github.User.Email.GetAll();
Assert.NotEmpty(emails);
}
}
}
18 changes: 0 additions & 18 deletions Octokit.Tests.Integration/Clients/UsersClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,4 @@ public async Task FailsWhenAuthenticatedWithBadCredentials()
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);
}
}
}
1 change: 1 addition & 0 deletions Octokit.Tests.Integration/Octokit.Tests.Integration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Compile Include="Clients\MilestonesClientTests.cs" />
<Compile Include="Clients\ReferencesClientTests.cs" />
<Compile Include="Clients\TreeClientTests.cs" />
<Compile Include="Clients\UserEmailsClientTests.cs" />
<Compile Include="Clients\FollowersClientTests.cs" />
<Compile Include="IntegrationTestAttribute.cs" />
<Compile Include="Clients\IssuesClientTests.cs" />
Expand Down
56 changes: 56 additions & 0 deletions Octokit.Tests/Clients/UserEmailsClientTests.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 0 additions & 15 deletions Octokit.Tests/Clients/UsersClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,5 @@ public async Task EnsuresArgumentsNotNull()
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 change: 1 addition & 0 deletions Octokit.Tests/Octokit.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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="Clients\WatchedClientTests.cs" />
<Compile Include="Clients\FollowersClientTests.cs" />
<Compile Include="Exceptions\ApiExceptionTests.cs" />
Expand Down
23 changes: 23 additions & 0 deletions Octokit/Clients/IUserEmailsClient.cs
Original file line number Diff line number Diff line change
@@ -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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finish up the returns elements here and I think this is ready to 🚢

[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);
}
}
9 changes: 2 additions & 7 deletions Octokit/Clients/IUsersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace Octokit
/// </remarks>
public interface IUsersClient
{
IUserEmailsClient Email { get; }

/// <summary>
/// Returns the user specified by the login.
/// </summary>
Expand All @@ -35,13 +37,6 @@ public interface IUsersClient
/// <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();

/// <summary>
/// A client for GitHub's User Followers API
/// </summary>
Expand Down
37 changes: 37 additions & 0 deletions Octokit/Clients/UserEmailsClient.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
12 changes: 3 additions & 9 deletions Octokit/Clients/UsersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ public class UsersClient : ApiClient, IUsersClient
/// <param name="apiConnection">An API connection</param>
public UsersClient(IApiConnection apiConnection) : base(apiConnection)
{
Email = new UserEmailsClient(apiConnection);
Followers = new FollowersClient(apiConnection);
}

public IUserEmailsClient Email { get; private set; }

/// <summary>
/// Returns the user specified by the login.
/// </summary>
Expand Down Expand Up @@ -61,15 +64,6 @@ public Task<User> Update(UserUpdate user)
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);
}

/// <summary>
/// A client for GitHub's User Followers API
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Octokit/Octokit-Mono.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,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" />
<Compile Include="Models\Response\Emoji.cs" />
<Compile Include="Clients\FollowersClient.cs" />
<Compile Include="Clients\IFollowersClient.cs" />
Expand Down
2 changes: 2 additions & 0 deletions Octokit/Octokit-MonoAndroid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
<Compile Include="Clients\IWatchedClient.cs" />
<Compile Include="Clients\WatchedClient.cs" />
<Compile Include="Models\Request\NewSubscription.cs" />
Expand Down
2 changes: 2 additions & 0 deletions Octokit/Octokit-Monotouch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
<Compile Include="Clients\IWatchedClient.cs" />
<Compile Include="Clients\WatchedClient.cs" />
<Compile Include="Models\Request\NewSubscription.cs" />
Expand Down
2 changes: 2 additions & 0 deletions Octokit/Octokit-netcore45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,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" />
<Compile Include="Models\Response\Emoji.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions Octokit/Octokit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="Clients\ActivitiesClient.cs" />
<Compile Include="Clients\IUserEmailsClient.cs" />
<Compile Include="Clients\UserEmailsClient.cs" />
<Compile Include="Clients\IWatchedClient.cs" />
<Compile Include="Clients\SearchClient.cs" />
<Compile Include="Clients\ISearchClient.cs" />
Expand Down