Add unit test for reactive auth method

This commit is contained in:
Haacked
2013-10-10 14:52:05 -07:00
parent ae41b81025
commit a3d4238f91
4 changed files with 145 additions and 7 deletions
@@ -165,24 +165,25 @@ namespace Octokit.Tests.Clients
"secret",
Arg.Any<NewAuthorization>(),
"two-factor-code")
.Returns(Task.Factory.StartNew(() => new Authorization { Token = "xyz" }));
.Returns(Task.Factory.StartNew(() => new Authorization { Token = "OAUTHSECRET" }));
var result = await client.GetOrCreateApplicationAuthentication("clientId",
"secret",
data,
e => Task.Factory.StartNew(() => challengeResults.Dequeue()));
client.Received(2).GetOrCreateApplicationAuthentication("clientId",
"secret",
Args.NewAuthorization);
client.Received().GetOrCreateApplicationAuthentication("clientId",
"secret",
Arg.Any<NewAuthorization>());
client.Received().GetOrCreateApplicationAuthentication("clientId",
"secret",
Arg.Any<NewAuthorization>(), "two-factor-code");
Assert.Equal("xyz", result.Token);
Args.NewAuthorization,
"two-factor-code");
Assert.Equal("OAUTHSECRET", result.Token);
}
[Fact]
public async Task CallsCallbackAgainWhenUserSubmitsBadCode()
public async Task ThrowsTwoFactorChallengeFailedExceptionWhenProvidedCodeIsIncorrect()
{
var challengeResults = new Queue<TwoFactorChallengeResult>(new[]
{
+14
View File
@@ -38,6 +38,15 @@
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Reactive.Core">
<HintPath>..\packages\Rx-Core.2.1.30214.0\lib\Net45\System.Reactive.Core.dll</HintPath>
</Reference>
<Reference Include="System.Reactive.Interfaces">
<HintPath>..\packages\Rx-Interfaces.2.1.30214.0\lib\Net45\System.Reactive.Interfaces.dll</HintPath>
</Reference>
<Reference Include="System.Reactive.Linq">
<HintPath>..\packages\Rx-Linq.2.1.30214.0\lib\Net45\System.Reactive.Linq.dll</HintPath>
</Reference>
<Reference Include="System.XML" />
<Reference Include="System.Xml.Linq" />
<Reference Include="xunit, Version=1.9.2.1705, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
@@ -78,10 +87,15 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Helpers\StringExtensionsTests.cs" />
<Compile Include="Clients\RepositoriesClientTests.cs" />
<Compile Include="Reactive\AuthorizationExtensionsTests.cs" />
<Compile Include="SimpleJsonSerializerTests.cs" />
<Compile Include="Clients\UsersClientTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Octokit.Reactive\Octokit.Reactive.csproj">
<Project>{674b69b8-0780-4d54-ae2b-c15821fa51cb}</Project>
<Name>Octokit.Reactive</Name>
</ProjectReference>
<ProjectReference Include="..\Octokit\Octokit.csproj">
<Project>{08dd4305-7787-4823-a53f-4d0f725a07f3}</Project>
<Name>Octokit</Name>
@@ -0,0 +1,120 @@
using System.Collections.Generic;
using System.Reactive.Linq;
using System.Threading.Tasks;
using NSubstitute;
using Octokit.Reactive;
using Octokit.Tests.Helpers;
using Xunit;
namespace Octokit.Tests.Reactive
{
public class AuthorizationExtensionsTests
{
public class TheGetOrCreateApplicationAuthenticationMethod
{
[Fact]
public async Task UsesCallbackToRetrievTwoFactorCode()
{
var firstResponse = new TwoFactorRequiredException("doh", TwoFactorType.AuthenticatorApp);
var twoFactorChallengeResult = new TwoFactorChallengeResult("two-factor-code");
var secondResponse = new Authorization {Token = "OAUTHSECRET"};
var client = Substitute.For<IObservableAuthorizationsClient>();
client.GetOrCreateApplicationAuthentication(Args.String, Args.String, Args.NewAuthorization)
.Returns(Observable.Throw<Authorization>(firstResponse));
client.GetOrCreateApplicationAuthentication(
Args.String,
Args.String,
Args.NewAuthorization,
"two-factor-code")
.Returns(Observable.Return(secondResponse));
var result = await client.GetOrCreateApplicationAuthentication(
"clientId",
"secret",
new NewAuthorization { Note = "Was it this one?"},
_ => Observable.Return(twoFactorChallengeResult));
Assert.Equal("OAUTHSECRET", result.Token);
client.Received().GetOrCreateApplicationAuthentication(
"clientId", "secret", Arg.Is<NewAuthorization>(a => a.Note == "Was it this one?"));
client.Received().GetOrCreateApplicationAuthentication(
"clientId", "secret", Arg.Is<NewAuthorization>(a => a.Note == "Was it this one?"),
"two-factor-code");
}
[Fact]
public async Task RetriesWhenResendRequested()
{
var firstResponse = new TwoFactorRequiredException("doh", TwoFactorType.AuthenticatorApp);
var challengeResults = new Queue<TwoFactorChallengeResult>(new[]
{
TwoFactorChallengeResult.RequestResendCode,
new TwoFactorChallengeResult("two-factor-code")
});
var secondResponse = new Authorization { Token = "OAUTHSECRET" };
var client = Substitute.For<IObservableAuthorizationsClient>();
client.GetOrCreateApplicationAuthentication(Args.String, Args.String, Args.NewAuthorization)
.Returns(Observable.Throw<Authorization>(firstResponse));
client.GetOrCreateApplicationAuthentication(
Args.String,
Args.String,
Args.NewAuthorization,
"two-factor-code")
.Returns(Observable.Return(secondResponse));
var result = await client.GetOrCreateApplicationAuthentication(
"clientId",
"secret",
new NewAuthorization { Note = "Was it this one?" },
_ => Observable.Return(challengeResults.Dequeue()));
client.Received(2).GetOrCreateApplicationAuthentication("clientId",
"secret",
Arg.Any<NewAuthorization>());
client.Received().GetOrCreateApplicationAuthentication("clientId",
"secret",
Arg.Any<NewAuthorization>(), "two-factor-code");
Assert.Equal("OAUTHSECRET", result.Token);
}
[Fact]
public void ThrowsTwoFactorChallengeFailedExceptionWhenProvidedCodeIsIncorrect()
{
var challengeResults = new Queue<TwoFactorChallengeResult>(new[]
{
TwoFactorChallengeResult.RequestResendCode,
new TwoFactorChallengeResult("wrong-code"),
});
var twoFactorFailedException = new TwoFactorChallengeFailedException();
var data = new NewAuthorization();
var client = Substitute.For<IObservableAuthorizationsClient>();
client.GetOrCreateApplicationAuthentication("clientId", "secret", Arg.Any<NewAuthorization>())
.Returns(Observable.Throw<Authorization>(new TwoFactorRequiredException()));
client.GetOrCreateApplicationAuthentication("clientId",
"secret",
Arg.Any<NewAuthorization>(),
"wrong-code")
.Returns(Observable.Throw<Authorization>(twoFactorFailedException));
var observer = Substitute.For<System.IObserver<Authorization>>();
client.GetOrCreateApplicationAuthentication(
"clientId",
"secret",
data,
_ => Observable.Return(challengeResults.Dequeue()))
.Subscribe(observer);
observer.Received().OnError(twoFactorFailedException);
client.Received().GetOrCreateApplicationAuthentication("clientId",
"secret",
Arg.Any<NewAuthorization>());
client.Received().GetOrCreateApplicationAuthentication("clientId",
"secret",
Arg.Any<NewAuthorization>(),
"wrong-code");
}
}
}
}
+3
View File
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NSubstitute" version="1.6.1.0" targetFramework="net45" />
<package id="Rx-Core" version="2.1.30214.0" targetFramework="net45" />
<package id="Rx-Interfaces" version="2.1.30214.0" targetFramework="net45" />
<package id="Rx-Linq" version="2.1.30214.0" targetFramework="net45" />
<package id="xunit" version="1.9.2" targetFramework="net45" />
<package id="xunit.extensions" version="1.9.2" targetFramework="net45" />
</packages>