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

Implement User Emails API #356

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions Octokit.Tests.Integration/Clients/UsersClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,23 @@ public async Task RetrievesEmailsForUser()
Assert.True(email.Primary);
}
}

public class TheAddEmailsToCurrentMethod
{
[IntegrationTest]
public async Task CanAddEmailsToUser()
{
var github = new GitHubClient(new ProductHeaderValue("OctokitTests"))
{
Credentials = Helper.Credentials
};

var emailToAdd = string.Format("test_{0}@example.com", System.Guid.NewGuid());

await github.User.AddEmailsToCurrent(emailToAdd);
var emails = await github.User.GetEmails();

Assert.True(emails.Where(e => e.Email == emailToAdd).Any());
}
}
}
26 changes: 26 additions & 0 deletions Octokit.Tests/Clients/UsersClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,31 @@ public void SendsUpdateToCorrectUrl()
client.Received().GetAll<EmailAddress>(endpoint, null);
}
}

public class TheAddEmailsToCurrentMethod
{
[Fact]
public void ShouldThrowIfProvidedListIsEmpty()
{
var client = Substitute.For<IApiConnection>();
var usersClient = new UsersClient(client);

var result = Record.Exception(() => usersClient.AddEmailsToCurrent());

Assert.IsType<ArgumentException>(result);
Assert.Equal("emails", ((ArgumentException)result).ParamName);
}

[Fact]
public void ShouldSkipEmptyEmailAddresses()
{
var client = Substitute.For<IApiConnection>();
var usersClient = new UsersClient(client);

usersClient.AddEmailsToCurrent("[email protected]", "", null, "[email protected]");

client.Received().Post<string[]>(ApiUrls.Emails(), Arg.Is<string[]>(p => p.Length == 2));
}
}
}
}
7 changes: 7 additions & 0 deletions Octokit/Clients/IUsersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,12 @@ public interface IUsersClient
/// <returns></returns>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
Task<IReadOnlyList<EmailAddress>> GetEmails();

/// <summary>
/// Add email address(es) to the current user.
/// </summary>
/// <param name="emails">The email adresses to add.</param>
/// <returns>A read only list of the added email adresses.</returns>
Task<EmailAddress[]> AddEmailsToCurrent(params string[] emails);
Copy link
Contributor

Choose a reason for hiding this comment

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

We never return arrays. That should be an IReadOnlyList<EmailAddress>

}
}
18 changes: 18 additions & 0 deletions Octokit/Clients/UsersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#if NET_45
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
#endif
using System.Threading.Tasks;

Expand Down Expand Up @@ -68,5 +69,22 @@ public Task<IReadOnlyList<EmailAddress>> GetEmails()
{
return ApiConnection.GetAll<EmailAddress>(ApiUrls.Emails(), null);
}

/// <summary>
/// Add email address(es) to the current user.
/// </summary>
/// <param name="emails">The email adresses to add.</param>
/// <returns>A read only list of the added email adresses.</returns>
public Task<EmailAddress[]> AddEmailsToCurrent(params string[] emails)
{
if (emails == null || emails.Length == 0)
{
throw new ArgumentException("No email addresses provided.", "emails");
Copy link
Contributor

Choose a reason for hiding this comment

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

If emails is null, this should throw an ArgumentNullException. If it's empty, an ArgumentException is appropriate.

}

var data = emails.Where(e => !string.IsNullOrEmpty(e)).ToArray();
Copy link
Contributor

Choose a reason for hiding this comment

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

Couldn't this also result in an empty array?


return ApiConnection.Post<EmailAddress[]>(ApiUrls.Emails(), data);
}
}
}