Skip to content

Commit

Permalink
Merge pull request #437 from octokit/haacked/regression-fix
Browse files Browse the repository at this point in the history
Fix regression in throwing proper 2FA exception
  • Loading branch information
shiftkey committed Mar 21, 2014
2 parents 5f6181c + 90edfa6 commit ab4276c
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 17 deletions.
8 changes: 4 additions & 4 deletions Octokit.Tests.Integration/Clients/IssuesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,12 @@ public async Task FilteringByInvalidAccountThrowsError()
{
var owner = _repository.Owner.Login;

AssertEx.Throws<ApiValidationException>(
() => _issuesClient.GetForRepository(owner, _repository.Name,
await AssertEx.Throws<ApiValidationException>(
async () => await _issuesClient.GetForRepository(owner, _repository.Name,
new RepositoryIssueRequest { Creator = "some-random-account" }));

AssertEx.Throws<ApiValidationException>(
() => _issuesClient.GetForRepository(owner, _repository.Name,
await AssertEx.Throws<ApiValidationException>(
async () => await _issuesClient.GetForRepository(owner, _repository.Name,
new RepositoryIssueRequest { Assignee = "some-random-account" }));
}

Expand Down
14 changes: 6 additions & 8 deletions Octokit.Tests/Clients/AuthorizationsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,18 @@ public void GetsOrCreatesAuthenticationAtCorrectUrlUsingTwoFactor()
}

[Fact]
public void WrapsTwoFactorFailureWithTwoFactorException()
public async Task WrapsTwoFactorFailureWithTwoFactorException()
{
var data = new NewAuthorization();
var client = Substitute.For<IApiConnection>();
client.Put<Authorization>(Args.Uri, Args.Object, Args.String)
.Returns(_ =>
{
throw new AuthorizationException(
new ApiResponse<object> { StatusCode = HttpStatusCode.Unauthorized});
});
.ThrowsAsync<Authorization>(
new AuthorizationException(
new ApiResponse<object> { StatusCode = HttpStatusCode.Unauthorized }));
var authEndpoint = new AuthorizationsClient(client);

Assert.Throws<TwoFactorChallengeFailedException>(() =>
authEndpoint.GetOrCreateApplicationAuthentication("clientId", "secret", data, "authenticationCode"));
await AssertEx.Throws<TwoFactorChallengeFailedException>(async () =>
await authEndpoint.GetOrCreateApplicationAuthentication("clientId", "secret", data, "authenticationCode"));
}

[Fact]
Expand Down
5 changes: 2 additions & 3 deletions Octokit.Tests/Clients/GistsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ public async Task EnsuresArgumentsNotNull()
var connection = Substitute.For<IApiConnection>();
var client = new GistsClient(connection);

AssertEx.Throws<ArgumentNullException>(async () => await
client.Delete(null));
Assert.Throws<ArgumentNullException>(() => client.Delete(null));
}
}

Expand Down Expand Up @@ -224,7 +223,7 @@ public async Task ThrowsExceptionForInvalidStatusCode()

var client = new GistsClient(apiConnection);

AssertEx.Throws<ApiException>(async () => await client.IsStarred("1"));
await AssertEx.Throws<ApiException>(async () => await client.IsStarred("1"));
}
}

Expand Down
19 changes: 19 additions & 0 deletions Octokit.Tests/Helpers/NSubstituteExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Threading.Tasks;
using NSubstitute;
using NSubstitute.Core;

public static class NSubstituteExtensions
{
public static ConfiguredCall ReturnsAsync<TTask, TTaskType>(
this TTask value, Func<CallInfo, TTaskType> returnThis,
params Func<CallInfo, TTaskType>[] returnThese) where TTask : Task<TTaskType>
{
return value.Returns(callInfo => Task.Factory.StartNew(() => returnThis(callInfo)));
}

public static ConfiguredCall ThrowsAsync<T>(this Task<T> value, Exception exception)
{
return value.ReturnsAsync<Task<T>, T>(callInfo => { throw exception; });
}
}
1 change: 1 addition & 0 deletions Octokit.Tests/OctoKit.Tests-NetCore45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<Compile Include="Exceptions\RateLimitExceededExceptionTests.cs" />
<Compile Include="Exceptions\TwoFactorChallengeFailedException.cs" />
<Compile Include="Exceptions\TwoFactorRequiredExceptionTests.cs" />
<Compile Include="Helpers\NSubstituteExtensions.cs" />
<Compile Include="Helpers\UriExtensionsTests.cs" />
<Compile Include="Http\ApiConnectionTests.cs" />
<Compile Include="Clients\AuthorizationsClientTests.cs" />
Expand Down
1 change: 1 addition & 0 deletions Octokit.Tests/Octokit.Tests-Portable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<Compile Include="Exceptions\RateLimitExceededExceptionTests.cs" />
<Compile Include="Exceptions\TwoFactorChallengeFailedException.cs" />
<Compile Include="Exceptions\TwoFactorRequiredExceptionTests.cs" />
<Compile Include="Helpers\NSubstituteExtensions.cs" />
<Compile Include="Helpers\UriExtensionsTests.cs" />
<Compile Include="Http\ApiConnectionTests.cs" />
<Compile Include="Clients\AuthorizationsClientTests.cs" />
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 @@ -102,6 +102,7 @@
<Compile Include="Exceptions\LoginAttemptsExceededExceptionTests.cs" />
<Compile Include="Exceptions\RateLimitExceededExceptionTests.cs" />
<Compile Include="Exceptions\TwoFactorRequiredExceptionTests.cs" />
<Compile Include="Helpers\NSubstituteExtensions.cs" />
<Compile Include="Helpers\UriExtensionsTests.cs" />
<Compile Include="Http\ApiConnectionTests.cs" />
<Compile Include="Clients\AuthorizationsClientTests.cs" />
Expand Down
4 changes: 2 additions & 2 deletions Octokit/Clients/AuthorizationsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public Task<Authorization> GetOrCreateApplicationAuthentication(
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>The created <see cref="Authorization"/>.</returns>
public Task<Authorization> GetOrCreateApplicationAuthentication(
public async Task<Authorization> GetOrCreateApplicationAuthentication(
string clientId,
string clientSecret,
NewAuthorization newAuthorization,
Expand All @@ -139,7 +139,7 @@ public Task<Authorization> GetOrCreateApplicationAuthentication(

try
{
return ApiConnection.Put<Authorization>(
return await ApiConnection.Put<Authorization>(
endpoint,
requestData,
twoFactorAuthenticationCode);
Expand Down

0 comments on commit ab4276c

Please sign in to comment.