Skip to content

Commit

Permalink
Merge pull request #1198 from dampir/issue1149
Browse files Browse the repository at this point in the history
Add ApiOption overloads to methods on IAuthorizationsClient
  • Loading branch information
ryangribble committed Mar 22, 2016
2 parents ac73fa3 + 1a5658d commit d01b510
Show file tree
Hide file tree
Showing 14 changed files with 312 additions and 21 deletions.
15 changes: 14 additions & 1 deletion Octokit.Reactive/Clients/IObservableAuthorizationsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,24 @@ public interface IObservableAuthorizationsClient
/// See <a href="http://developer.github.com/v3/oauth/#list-your-authorizations">API documentation</a> for more
/// details.
/// </remarks>
/// <returns>An <see cref="Authorization"/></returns>
/// <returns>A list of <see cref="Authorization"/>s for the authenticated user.</returns>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate",
Justification = "It's an API call, so it's not a property.")]
IObservable<Authorization> GetAll();

/// <summary>
/// Get all <see cref="Authorization"/>s for the authenticated user. This method requires basic auth.
/// </summary>
/// <remarks>
/// See <a href="http://developer.github.com/v3/oauth/#list-your-authorizations">API documentation</a> for more
/// details.
/// </remarks>
/// <param name="options">Options for changing the API response</param>
/// <returns>A list of <see cref="Authorization"/>s for the authenticated user.</returns>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate",
Justification = "It's an API call, so it's not a property.")]
IObservable<Authorization> GetAll(ApiOptions options);

/// <summary>
/// Get a specific <see cref="Authorization"/> for the authenticated user. This method requires basic auth.
/// </summary>
Expand Down
5 changes: 1 addition & 4 deletions Octokit.Reactive/Clients/ObservableAssigneesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ public ObservableAssigneesClient(IGitHubClient client)
/// <returns></returns>
public IObservable<User> GetAllForRepository(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");

return _connection.GetAndFlattenAllPages<User>(ApiUrls.Assignees(owner, name));
return GetAllForRepository(owner, name, ApiOptions.None);
}

/// <summary>
Expand Down
20 changes: 18 additions & 2 deletions Octokit.Reactive/Clients/ObservableAuthorizationsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,26 @@ public ObservableAuthorizationsClient(IGitHubClient client)
/// See <a href="http://developer.github.com/v3/oauth/#list-your-authorizations">API documentation</a> for more
/// details.
/// </remarks>
/// <returns>An <see cref="Authorization"/></returns>
/// <returns>A list of <see cref="Authorization"/>s for the authenticated user.</returns>
public IObservable<Authorization> GetAll()
{
return _connection.GetAndFlattenAllPages<Authorization>(ApiUrls.Authorizations());
return GetAll(ApiOptions.None);
}

/// <summary>
/// Get all <see cref="Authorization"/>s for the authenticated user. This method requires basic auth.
/// </summary>
/// <remarks>
/// See <a href="http://developer.github.com/v3/oauth/#list-your-authorizations">API documentation</a> for more
/// details.
/// </remarks>
/// <param name="options">Options for changing the API response</param>
/// <returns>A list of <see cref="Authorization"/>s for the authenticated user.</returns>
public IObservable<Authorization> GetAll(ApiOptions options)
{
Ensure.ArgumentNotNull(options, "options");

return _connection.GetAndFlattenAllPages<Authorization>(ApiUrls.Authorizations(), options);
}

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

namespace Octokit.Tests.Integration.Clients
Expand All @@ -13,7 +12,7 @@ public async Task CanCreatePersonalToken()
var note = Helper.MakeNameWithTimestamp("Testing authentication");
var newAuthorization = new NewAuthorization(
note,
new string[] { "user" });
new[] { "user" });

var created = await github.Authorization.Create(newAuthorization);

Expand All @@ -27,14 +26,47 @@ public async Task CanCreatePersonalToken()
Assert.Equal(created.Note, get.Note);
}

[IntegrationTest]
public async Task CanGetAuthorization()
{
var github = Helper.GetBasicAuthClient();

var authorizations = await github.Authorization.GetAll();
Assert.NotEmpty(authorizations);
}

[IntegrationTest]
public async Task CanGetAuthorizationWithApiOptions()
{
var github = Helper.GetBasicAuthClient();

var authorizations = await github.Authorization.GetAll(ApiOptions.None);
Assert.NotEmpty(authorizations);
}

[IntegrationTest]
public async Task ReturnsNotEmptyAuthorizationsWithoutStart()
{
var github = Helper.GetBasicAuthClient();

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

var authorizations = await github.Authorization.GetAll(options);
Assert.NotEmpty(authorizations);
}

[IntegrationTest]
public async Task CannotCreatePersonalTokenWhenUsingOauthTokenCredentials()
{
var github = Helper.GetAuthenticatedClient();
var note = Helper.MakeNameWithTimestamp("Testing authentication");
var newAuthorization = new NewAuthorization(
note,
new string[] { "user" });
new[] { "user" });

var error = Assert.ThrowsAsync<ForbiddenException>(() => github.Authorization.Create(newAuthorization));
Assert.True(error.Result.Message.Contains("username and password Basic Auth"));
Expand Down
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 @@ -138,6 +138,7 @@
<Compile Include="Reactive\Enterprise\ObservableEnterpriseSearchIndexingClientTests.cs" />
<Compile Include="Reactive\Enterprise\ObservableEnterpriseOrganizationClientTests.cs" />
<Compile Include="Reactive\ObservableAssigneesClientTests.cs" />
<Compile Include="Reactive\ObservableAuthorizationsClientTests.cs" />
<Compile Include="Reactive\ObservableIssuesClientTests.cs" />
<Compile Include="Reactive\ObservableMilestonesClientTests.cs" />
<Compile Include="Reactive\ObservableReleaseClientTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Reactive.Linq;
using System.Threading.Tasks;
using Octokit.Reactive;
using Xunit;

namespace Octokit.Tests.Integration.Reactive
{
public class ObservableAuthorizationsClientTests
{
readonly ObservableAuthorizationsClient _authorizationsClient;

public ObservableAuthorizationsClientTests()
{
var github = Helper.GetBasicAuthClient();

_authorizationsClient = new ObservableAuthorizationsClient(github);
}

[IntegrationTest]
public async Task CanGetAuthorization()
{
var authorization = await _authorizationsClient.GetAll();
Assert.NotNull(authorization);
}

[IntegrationTest]
public async Task CanGetAuthorizationWithApiOptions()
{
var authorization = await _authorizationsClient.GetAll(ApiOptions.None);
Assert.NotNull(authorization);
}

[IntegrationTest]
public async Task ReturnsNotEmptyAuthorizationsWithoutStart()
{
var options = new ApiOptions
{
PageSize = 5,
PageCount = 1
};

var authorizations = await _authorizationsClient.GetAll(options).ToList();
Assert.NotEmpty(authorizations);
}
}
}
26 changes: 24 additions & 2 deletions Octokit.Tests/Clients/AssigneesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
using System.Threading.Tasks;
using NSubstitute;
using Octokit.Internal;
using Octokit.Tests.Helpers;
using Xunit;

namespace Octokit.Tests.Clients
{
public class AssigneesClientTests
{
public class TheGetForRepositoryMethod
public class TheGetAllMethod
{
[Fact]
public void RequestsCorrectUrl()
Expand All @@ -28,6 +27,28 @@ public void RequestsCorrectUrl()
Args.ApiOptions);
}

[Fact]
public void RequestsCorrectUrlWithApiOptions()
{
var connection = Substitute.For<IApiConnection>();
var client = new AssigneesClient(connection);

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

client.GetAllForRepository("fake", "repo", options);

connection.Received().GetAll<User>(
Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/assignees"),
null,
AcceptHeaders.StableVersion,
options);
}

[Fact]
public async Task EnsuresNonNullArguments()
{
Expand All @@ -37,6 +58,7 @@ public async Task EnsuresNonNullArguments()
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository(null, ""));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", null));
await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("", null));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", null));
}
}

Expand Down
31 changes: 30 additions & 1 deletion Octokit.Tests/Clients/AuthorizationsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void ThrowsForBadArgs()
public class TheGetAllMethod
{
[Fact]
public void GetsAListOfAuthorizations()
public void RequestsCorrectUrl()
{
var client = Substitute.For<IApiConnection>();
var authEndpoint = new AuthorizationsClient(client);
Expand All @@ -37,6 +37,35 @@ public void GetsAListOfAuthorizations()
Arg.Is<Uri>(u => u.ToString() == "authorizations"),
Args.ApiOptions);
}

[Fact]
public void RequestsCorrectUrlWithApiOptions()
{
var client = Substitute.For<IApiConnection>();
var authEndpoint = new AuthorizationsClient(client);

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

authEndpoint.GetAll(options);

client.Received().GetAll<Authorization>(
Arg.Is<Uri>(u => u.ToString() == "authorizations"),
options);
}

[Fact]
public async Task EnsuresArgumentsNotNull()
{
var client = Substitute.For<IApiConnection>();
var authEndpoint = new AuthorizationsClient(client);

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

public class TheGetMethod
Expand Down
22 changes: 22 additions & 0 deletions Octokit.Tests/Clients/ReleasesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,35 @@ public void RequestsCorrectUrl()
Args.ApiOptions);
}

[Fact]
public void RequestsCorrectUrlWithApiOptions()
{
var client = Substitute.For<IApiConnection>();
var releasesClient = new ReleasesClient(client);

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

releasesClient.GetAll("fake", "repo", options);

client.Received().GetAll<Release>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/releases"),
null,
"application/vnd.github.v3",
options);
}

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

await Assert.ThrowsAsync<ArgumentNullException>(() => releasesClient.GetAll(null, "name"));
await Assert.ThrowsAsync<ArgumentNullException>(() => releasesClient.GetAll("owner", null));
await Assert.ThrowsAsync<ArgumentNullException>(() => releasesClient.GetAll("owner", "name", null));
}
}

Expand Down
24 changes: 20 additions & 4 deletions Octokit.Tests/Clients/UserEmailsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class UserEmailsClientTests
public class TheGetAllMethod
{
[Fact]
public void GetsCorrectUrl()
public void RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new UserEmailsClient(connection);
Expand All @@ -24,15 +24,31 @@ public void GetsCorrectUrl()
}

[Fact]
public void GetsCorrectUrlWithApiOptions()
public void RequestsCorrectUrlWithApiOptions()
{
var connection = Substitute.For<IApiConnection>();
var client = new UserEmailsClient(connection);

client.GetAll(ApiOptions.None);
var options = new ApiOptions
{
PageCount = 1,
PageSize = 1,
StartPage = 1
};

client.GetAll(options);

connection.Received(1)
.GetAll<EmailAddress>(Arg.Is<Uri>(u => u.ToString() == "user/emails"), Args.ApiOptions);
.GetAll<EmailAddress>(Arg.Is<Uri>(u => u.ToString() == "user/emails"),
options);
}

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

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

Expand Down
1 change: 1 addition & 0 deletions Octokit.Tests/Octokit.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
<Compile Include="Reactive\Enterprise\ObservableEnterpriseSearchIndexingClientTests.cs" />
<Compile Include="Reactive\Enterprise\ObservableEnterpriseOrganizationClientTests.cs" />
<Compile Include="Reactive\Enterprise\ObservableEnterpriseLicenseClientTests.cs" />
<Compile Include="Reactive\ObservableAuthorizationsClientTests.cs" />
<Compile Include="Reactive\ObservableBlobClientTests.cs" />
<Compile Include="Reactive\ObservableCommitsClientTests.cs" />
<Compile Include="Reactive\ObservableDeploymentsClientTests.cs" />
Expand Down
Loading

0 comments on commit d01b510

Please sign in to comment.