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

Add GetAll method to OrganizationsClient #1469

Merged
Merged
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
30 changes: 30 additions & 0 deletions Octokit.Reactive/Clients/IObservableOrganizationsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public interface IObservableOrganizationsClient
/// </summary>
/// <param name="user">The login for the user</param>
/// <returns></returns>
[Obsolete("Please use IObservableOrganizationsClient.GetAllForUser() instead. This method will be removed in a future version")]
IObservable<Organization> GetAll(string user);

/// <summary>
Expand All @@ -54,8 +55,37 @@ public interface IObservableOrganizationsClient
/// <param name="user">The login for the user</param>
/// <param name="options">Options for changing the API response</param>
/// <returns></returns>
[Obsolete("Please use IObservableOrganizationsClient.GetAllForUser() instead. This method will be removed in a future version")]
IObservable<Organization> GetAll(string user, ApiOptions options);

/// <summary>
/// Returns all the organizations for the specified user
/// </summary>
/// <param name="user">The login for the user</param>
/// <returns></returns>
IObservable<Organization> GetAllForUser(string user);

/// <summary>
/// Returns all the organizations for the specified user
/// </summary>
/// <param name="user">The login for the user</param>
/// <param name="options">Options for changing the API response</param>
/// <returns></returns>
IObservable<Organization> GetAllForUser(string user, ApiOptions options);

/// <summary>
/// Returns all the organizations
/// </summary>
/// <returns></returns>
IObservable<Organization> GetAll();

/// <summary>
/// Returns all the organizations
/// </summary>
/// <param name="request">Search parameters of the last organization seen</param>
/// <returns></returns>
IObservable<Organization> GetAll(OrganizationRequest request);

/// <summary>
/// Update the specified organization with data from <see cref="OrganizationUpdate"/>.
/// </summary>
Expand Down
57 changes: 54 additions & 3 deletions Octokit.Reactive/Clients/ObservableOrganizationsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,20 @@ public IObservable<Organization> GetAllForCurrent(ApiOptions options)
{
Ensure.ArgumentNotNull(options, "options");

return _connection.GetAndFlattenAllPages<Organization>(ApiUrls.Organizations());
return _connection.GetAndFlattenAllPages<Organization>(ApiUrls.UserOrganizations());
}

/// <summary>
/// Returns all the organizations for the specified user
/// </summary>
/// <param name="user">The login for the user</param>
/// <returns></returns>
[Obsolete("Please use ObservableOrganizationsClient.GetAllForUser() instead. This method will be removed in a future version")]
public IObservable<Organization> GetAll(string user)
{
Ensure.ArgumentNotNullOrEmptyString(user, "user");

return _connection.GetAndFlattenAllPages<Organization>(ApiUrls.Organizations(user));
return _connection.GetAndFlattenAllPages<Organization>(ApiUrls.UserOrganizations(user));
}

/// <summary>
Expand All @@ -85,12 +86,62 @@ public IObservable<Organization> GetAll(string user)
/// <param name="user">The login for the user</param>
/// <param name="options">Options for changing the API response</param>
/// <returns></returns>
[Obsolete("Please use ObservableOrganizationsClient.GetAllForUser() instead. This method will be removed in a future version")]
public IObservable<Organization> GetAll(string user, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(user, "user");
Ensure.ArgumentNotNull(options, "options");

return _connection.GetAndFlattenAllPages<Organization>(ApiUrls.Organizations(user), options);
return _connection.GetAndFlattenAllPages<Organization>(ApiUrls.UserOrganizations(user), options);
}

/// <summary>
/// Returns all the organizations for the specified user
/// </summary>
/// <param name="user">The login for the user</param>
/// <returns></returns>
public IObservable<Organization> GetAllForUser(string user)
{
Ensure.ArgumentNotNullOrEmptyString(user, "user");

return _connection.GetAndFlattenAllPages<Organization>(ApiUrls.UserOrganizations(user));
}

/// <summary>
/// Returns all the organizations for the specified user
/// </summary>
/// <param name="user">The login for the user</param>
/// <param name="options">Options for changing the API response</param>
/// <returns></returns>
public IObservable<Organization> GetAllForUser(string user, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(user, "user");
Ensure.ArgumentNotNull(options, "options");

return _connection.GetAndFlattenAllPages<Organization>(ApiUrls.UserOrganizations(user), options);
}

/// <summary>
/// Returns all the organizations
/// </summary>
/// <returns></returns>
public IObservable<Organization> GetAll()
{
return _connection.GetAndFlattenAllPages<Organization>(ApiUrls.AllOrganizations());
}

/// <summary>
/// Returns all the organizations
/// </summary>
/// <param name="request">Search parameters of the last organization seen</param>
/// <returns></returns>
public IObservable<Organization> GetAll(OrganizationRequest request)
{
Ensure.ArgumentNotNull(request, "request");

var url = ApiUrls.AllOrganizations(request.Since);

return _connection.GetAndFlattenAllPages<Organization>(url);
}

/// <summary>
Expand Down
77 changes: 68 additions & 9 deletions Octokit.Tests.Integration/Clients/OrganizationClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Collections;
using System.Threading.Tasks;
using Xunit;

namespace Octokit.Tests.Integration.Clients
Expand All @@ -18,30 +19,88 @@ public TheGetAllMethod()
}

[GitHubEnterpriseTest]
public async Task CanListOrganizations()
public async Task CanListAllOrganizations()
{
string orgLogin1 = Helper.MakeNameWithTimestamp("MyOrganization1");
string orgName1 = string.Concat(orgLogin1, " Display Name 1");
string orgLogin2 = Helper.MakeNameWithTimestamp("MyOrganization2");
string orgName2 = string.Concat(orgLogin2, " Display Name 2");

var newOrganization1 = new NewOrganization(orgLogin1, EnterpriseHelper.UserName, orgName1);
var newOrganization2 = new NewOrganization(orgLogin2, EnterpriseHelper.UserName, orgName2);
await _github.Enterprise.Organization.Create(newOrganization1);
await _github.Enterprise.Organization.Create(newOrganization2);

var organizations = await _organizationsClient.GetAll();

Assert.Contains(organizations, (org => org.Login == orgLogin1));
Assert.Contains(organizations, (org => org.Login == orgLogin2));
}

[GitHubEnterpriseTest]
public async Task ReturnsCorrectOrganizationsWithSince()
{
string orgLogin1 = Helper.MakeNameWithTimestamp("MyOrganization1");
string orgName1 = string.Concat(orgLogin1, " Display Name 1");
string orgLogin2 = Helper.MakeNameWithTimestamp("MyOrganization2");
string orgName2 = string.Concat(orgLogin2, " Display Name 2");
string orgLogin3 = Helper.MakeNameWithTimestamp("MyOrganization3");
string orgName3 = string.Concat(orgLogin3, " Display Name 3");

var newOrganization1 = new NewOrganization(orgLogin1, EnterpriseHelper.UserName, orgName1);
var newOrganization2 = new NewOrganization(orgLogin2, EnterpriseHelper.UserName, orgName2);
var newOrganization3 = new NewOrganization(orgLogin3, EnterpriseHelper.UserName, orgName3);

var createdOrganization1 = await _github.Enterprise.Organization.Create(newOrganization1);
var createdOrganization2 = await _github.Enterprise.Organization.Create(newOrganization2);
var createdOrganization3 = await _github.Enterprise.Organization.Create(newOrganization3);

var requestParameter = new OrganizationRequest(createdOrganization1.Id);

var organizations = await _organizationsClient.GetAll(requestParameter);

Assert.DoesNotContain(organizations, (org => org.Login == orgLogin1));
Assert.Contains(organizations, (org => org.Login == orgLogin2));
Assert.Contains(organizations, (org => org.Login == orgLogin3));
}
}

public class TheGetAllForCurrentMethod
{
readonly IGitHubClient _github;
readonly IOrganizationsClient _organizationsClient;

public TheGetAllForCurrentMethod()
{
_github = EnterpriseHelper.GetAuthenticatedClient();

_organizationsClient = _github.Organization;
}

[GitHubEnterpriseTest]
public async Task CanListUserOrganizations()
{
string orgLogin = Helper.MakeNameWithTimestamp("MyOrganization");
string orgName = string.Concat(orgLogin, " Display Name");

var newOrganization = new NewOrganization(orgLogin, EnterpriseHelper.UserName, orgName);
var organization = await
_github.Enterprise.Organization.Create(newOrganization);
var organization = await _github.Enterprise.Organization.Create(newOrganization);

Assert.NotNull(organization);

var milestones = await _organizationsClient.GetAllForCurrent();
var organizations = await _organizationsClient.GetAllForCurrent();

Assert.NotEmpty(milestones);
Assert.NotEmpty(organizations);
}

[GitHubEnterpriseTest]
public async Task ReturnsCorrectCountOfOrganizationsWithoutStart()
public async Task ReturnsCorrectCountOfUserOrganizationsWithoutStart()
{
string orgLogin1 = Helper.MakeNameWithTimestamp("MyOrganization1");
string orgName1 = string.Concat(orgLogin1, " Display Name 1");
string orgLogin2 = Helper.MakeNameWithTimestamp("MyOrganization2");
string orgName2 = string.Concat(orgLogin2, " Display Name 2");

var newOrganization1 = new NewOrganization(orgLogin1, EnterpriseHelper.UserName, orgName1);
var newOrganization2 = new NewOrganization(orgLogin2, EnterpriseHelper.UserName, orgName2);
await _github.Enterprise.Organization.Create(newOrganization1);
Expand All @@ -59,7 +118,7 @@ public async Task ReturnsCorrectCountOfOrganizationsWithoutStart()
}

[GitHubEnterpriseTest]
public async Task ReturnsCorrectCountOfOrganizationsWithStart()
public async Task ReturnsCorrectCountOfUserOrganizationsWithStart()
{
string orgLogin1 = Helper.MakeNameWithTimestamp("MyOrganization1");
string orgName1 = string.Concat(orgLogin1, " Display Name 1");
Expand Down
84 changes: 83 additions & 1 deletion Octokit.Tests/Clients/OrganizationsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public async Task EnsuresNonNullArguments()
var connection = Substitute.For<IApiConnection>();
var client = new OrganizationsClient(connection);

await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll(null));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll((string)null));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll(null, ApiOptions.None));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll("username", null));

Expand All @@ -90,6 +90,52 @@ public async Task EnsuresNonNullArguments()
}
}

public class TheGetAllForUserMethod
{
[Fact]
public async Task RequestsTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new OrganizationsClient(connection);

await client.GetAllForUser("username");

connection.Received().GetAll<Organization>(Arg.Is<Uri>(u => u.ToString() == "users/username/orgs"), Args.ApiOptions);
}

[Fact]
public async Task RequestsTheCorrectUrlWithApiOptions()
{
var connection = Substitute.For<IApiConnection>();
var client = new OrganizationsClient(connection);

var options = new ApiOptions
{
StartPage = 1,
PageCount = 1,
PageSize = 1
};

await client.GetAllForUser("username", options);

connection.Received().GetAll<Organization>(Arg.Is<Uri>(u => u.ToString() == "users/username/orgs"), options);
}

[Fact]
public async Task EnsuresNonNullArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new OrganizationsClient(connection);

await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForUser(null));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForUser(null, ApiOptions.None));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForUser("username", null));

await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForUser(""));
await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForUser("", ApiOptions.None));
}
}

public class TheGetAllForCurrentMethod
{
[Fact]
Expand Down Expand Up @@ -130,7 +176,43 @@ public async Task EnsuresNonNullArguments()
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForCurrent(null));
}
}

public class TheGetAllOrganizationsMethod
{
[Fact]
public async Task RequestsTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new OrganizationsClient(connection);

await client.GetAll();

connection.Received().GetAll<Organization>(Arg.Is<Uri>(u => u.ToString() == "organizations"));
}

[Fact]
public async Task RequestsTheCorrectUrlWithRequestParameter()
{
var connection = Substitute.For<IApiConnection>();
var client = new OrganizationsClient(connection);

var request = new OrganizationRequest(1);

await client.GetAll(request);

connection.Received().GetAll<Organization>(Arg.Is<Uri>(u => u.ToString() == "organizations?since=1"));
}

[Fact]
public async Task EnsuresNonNullArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new OrganizationsClient(connection);

await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll((OrganizationRequest)null));
}
}

public class TheUpdateMethod
{
[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void EnsuresNonNullArguments()
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableOrganizationsClient(gitHubClient);

Assert.Throws<ArgumentNullException>(() => client.GetAll(null));
Assert.Throws<ArgumentNullException>(() => client.GetAll((string)null));
Assert.Throws<ArgumentNullException>(() => client.GetAll(null, ApiOptions.None));
Assert.Throws<ArgumentNullException>(() => client.GetAll("username", null));

Expand Down
Loading