mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-07 20:30:41 +00:00
Implement GetOrCreateApplicationAuthentication
Implements the endpoint for creating an application authorization token.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using NSubstitute;
|
||||
using Octokit.Internal;
|
||||
using Octokit.Tests.Helpers;
|
||||
using Xunit;
|
||||
|
||||
namespace Octokit.Tests.Clients
|
||||
@@ -91,5 +93,93 @@ namespace Octokit.Tests.Clients
|
||||
client.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "/authorizations/1"));
|
||||
}
|
||||
}
|
||||
|
||||
public class TheGetOrCreateApplicationAuthenticationMethod
|
||||
{
|
||||
[Fact]
|
||||
public void GetsOrCreatesAuthenticationAtCorrectUrl()
|
||||
{
|
||||
var data = new AuthorizationUpdate();
|
||||
var client = Substitute.For<IApiConnection<Authorization>>();
|
||||
var authEndpoint = new AuthorizationsClient(client);
|
||||
|
||||
authEndpoint.GetOrCreateApplicationAuthentication("clientId", "secret", data);
|
||||
|
||||
client.Received().GetOrCreate(Arg.Is<Uri>(u => u.ToString() == "/authorizations/clients/clientId"),
|
||||
Args.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WrapsTwoFactorFailureWithTwoFactorException()
|
||||
{
|
||||
var data = new AuthorizationUpdate();
|
||||
var client = Substitute.For<IApiConnection<Authorization>>();
|
||||
client.GetOrCreate(Args.Uri, Args.Object, Args.String).Returns(_ => {throw new AuthorizationException();});
|
||||
var authEndpoint = new AuthorizationsClient(client);
|
||||
|
||||
AssertEx.Throws<TwoFactorChallengeFailedException>(async () =>
|
||||
await authEndpoint.GetOrCreateApplicationAuthentication("clientId", "secret", data));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UsesCallbackToRetrieveTwoFactorCode()
|
||||
{
|
||||
var twoFactorChallengeResult = new TwoFactorChallengeResult("two-factor-code");
|
||||
var data = new AuthorizationUpdate { Note = "note" };
|
||||
var client = Substitute.For<IAuthorizationsClient>();
|
||||
client.GetOrCreateApplicationAuthentication("clientId", "secret", Arg.Any<AuthorizationUpdate>())
|
||||
.Returns(_ => {throw new TwoFactorRequiredException();});
|
||||
client.GetOrCreateApplicationAuthentication("clientId",
|
||||
"secret",
|
||||
Arg.Any<AuthorizationUpdate>(),
|
||||
"two-factor-code")
|
||||
.Returns(Task.Factory.StartNew(() => new Authorization {Token = "xyz"}));
|
||||
|
||||
var result = await client.GetOrCreateApplicationAuthentication("clientId",
|
||||
"secret",
|
||||
data,
|
||||
e => Task.Factory.StartNew(() => twoFactorChallengeResult));
|
||||
|
||||
client.Received().GetOrCreateApplicationAuthentication("clientId",
|
||||
"secret",
|
||||
Arg.Is<AuthorizationUpdate>(u => u.Note == "note"));
|
||||
client.Received().GetOrCreateApplicationAuthentication("clientId",
|
||||
"secret",
|
||||
Arg.Any<AuthorizationUpdate>(), "two-factor-code");
|
||||
Assert.Equal("xyz", result.Token);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RetriesWhenResendRequested()
|
||||
{
|
||||
var challengeResults = new Queue<TwoFactorChallengeResult>(new []
|
||||
{
|
||||
TwoFactorChallengeResult.RequestResendCode,
|
||||
new TwoFactorChallengeResult("two-factor-code")
|
||||
});
|
||||
var data = new AuthorizationUpdate();
|
||||
var client = Substitute.For<IAuthorizationsClient>();
|
||||
client.GetOrCreateApplicationAuthentication("clientId", "secret", Arg.Any<AuthorizationUpdate>())
|
||||
.Returns(_ => { throw new TwoFactorRequiredException(); });
|
||||
client.GetOrCreateApplicationAuthentication("clientId",
|
||||
"secret",
|
||||
Arg.Any<AuthorizationUpdate>(),
|
||||
"two-factor-code")
|
||||
.Returns(Task.Factory.StartNew(() => new Authorization { Token = "xyz" }));
|
||||
|
||||
var result = await client.GetOrCreateApplicationAuthentication("clientId",
|
||||
"secret",
|
||||
data,
|
||||
e => Task.Factory.StartNew(() => challengeResults.Dequeue()));
|
||||
|
||||
client.Received().GetOrCreateApplicationAuthentication("clientId",
|
||||
"secret",
|
||||
Arg.Any<AuthorizationUpdate>());
|
||||
client.Received().GetOrCreateApplicationAuthentication("clientId",
|
||||
"secret",
|
||||
Arg.Any<AuthorizationUpdate>(), "two-factor-code");
|
||||
Assert.Equal("xyz", result.Token);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user