Skip to content

Commit

Permalink
Merge pull request #15 from rdingwall/oauth_web_flow
Browse files Browse the repository at this point in the history
Oauth web flow
  • Loading branch information
rdingwall committed Feb 3, 2016
2 parents 949c212 + e5595f8 commit 57ad61e
Show file tree
Hide file tree
Showing 13 changed files with 534 additions and 379 deletions.
1 change: 1 addition & 0 deletions Mondo.Tests/Mondo.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<ItemGroup>
<Compile Include="Extensions\DateTimeExtensionsTests.cs" />
<Compile Include="Messages\MerchantJsonConverterTests.cs" />
<Compile Include="MondoAuthorizationClientTests.cs" />
<Compile Include="MondoClientTests.cs" />
<Compile Include="PaginationOptionsTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
145 changes: 145 additions & 0 deletions Mondo.Tests/MondoAuthorizationClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using Microsoft.Owin.Testing;
using NUnit.Framework;
using Owin;

namespace Mondo.Tests
{
[TestFixture]
public sealed class MondoAuthorizationClientTests
{
[Test]
public void GetLoginPageUrl()
{
using (var client = new MondoAuthorizationClient("testClientId", "testClientSecret", "http://foo"))
{
var loginPageUrl = client.GetAuthorizeUrl("testState", "testRedirectUri");

Assert.AreEqual("https://auth.getmondo.co.uk/?response_type=code&client_id=testClientId&state=testState&redirect_uri=testRedirectUri", loginPageUrl);
}
}

[Test]
public async void ExchangeCodeForAccessTokenAsync()
{
using (var server = TestServer.Create(app =>
{
app.Run(async context =>
{
Assert.AreEqual("/oauth2/token", context.Request.Uri.PathAndQuery);
var formCollection = await context.Request.ReadFormAsync();
Assert.AreEqual("authorization_code", formCollection["grant_type"]);
Assert.AreEqual("testClientId", formCollection["client_id"]);
Assert.AreEqual("testClientSecret", formCollection["client_secret"]);
Assert.AreEqual("testRedirectUri", formCollection["redirect_uri"]);
Assert.AreEqual("testCode", formCollection["code"]);
await context.Response.WriteAsync(
@"{
'access_token': 'testAccessToken',
'client_id': 'client_id',
'expires_in': 21600,
'refresh_token': 'testRefreshToken',
'token_type': 'Bearer',
'user_id': 'testUserId'
}"
);
});
}))
{
using (var client = new MondoAuthorizationClient(server.HttpClient, "testClientId", "testClientSecret"))
{
var accessToken = await client.GetAccessTokenAsync("testCode", "testRedirectUri");

Assert.AreEqual("testAccessToken", accessToken.Value);
Assert.AreEqual("testRefreshToken", accessToken.RefreshToken);
Assert.AreEqual("testUserId", accessToken.UserId);
Assert.AreEqual(21600, accessToken.ExpiresIn);
}
}
}

[Test]
public async void Authenticate()
{
using (var server = TestServer.Create(app =>
{
app.Run(async context =>
{
Assert.AreEqual("/oauth2/token", context.Request.Uri.PathAndQuery);
var formCollection = await context.Request.ReadFormAsync();
Assert.AreEqual("password", formCollection["grant_type"]);
Assert.AreEqual("testClientId", formCollection["client_id"]);
Assert.AreEqual("testClientSecret", formCollection["client_secret"]);
Assert.AreEqual("testUsername", formCollection["username"]);
Assert.AreEqual("testPassword", formCollection["password"]);
await context.Response.WriteAsync(
@"{
'access_token': 'testAccessToken',
'client_id': 'client_id',
'expires_in': 21600,
'refresh_token': 'testRefreshToken',
'token_type': 'Bearer',
'user_id': 'testUserId'
}"
);
});
}))
{
using (var client = new MondoAuthorizationClient(server.HttpClient, "testClientId", "testClientSecret"))
{
var accessToken = await client.AuthenticateAsync("testUsername", "testPassword");

Assert.AreEqual("testAccessToken", accessToken.Value);
Assert.AreEqual("testRefreshToken", accessToken.RefreshToken);
Assert.AreEqual("testUserId", accessToken.UserId);
Assert.AreEqual(21600, accessToken.ExpiresIn);
}
}
}

[Test]
public async void RefreshAccessToken()
{
using (var server = TestServer.Create(app =>
{
app.Run(async context =>
{
Assert.AreEqual("/oauth2/token", context.Request.Uri.PathAndQuery);
var formCollection = await context.Request.ReadFormAsync();
Assert.AreEqual("refresh_token", formCollection["grant_type"]);
Assert.AreEqual("testClientId", formCollection["client_id"]);
Assert.AreEqual("testClientSecret", formCollection["client_secret"]);
Assert.AreEqual("testAccessToken1", formCollection["refresh_token"]);
await context.Response.WriteAsync(
@"{
'access_token': 'testAccessToken2',
'client_id': 'client_id',
'expires_in': 21600,
'refresh_token': 'testRefreshToken2',
'token_type': 'Bearer',
'user_id': 'testUserId'
}"
);
});
}))
{
using (var client = new MondoAuthorizationClient(server.HttpClient, "testClientId", "testClientSecret"))
{
var accessToken = await client.RefreshAccessTokenAsync("testAccessToken1");

Assert.AreEqual("testAccessToken2", accessToken.Value);
Assert.AreEqual("testRefreshToken2", accessToken.RefreshToken);
Assert.AreEqual(21600, accessToken.ExpiresIn);
}
}
}
}
}
Loading

0 comments on commit 57ad61e

Please sign in to comment.