Skip to content
This repository has been archived by the owner on Dec 9, 2020. It is now read-only.

Commit

Permalink
added unit tests and refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
Micah Martin committed May 23, 2012
1 parent e856aa1 commit 1d9ea2b
Show file tree
Hide file tree
Showing 95 changed files with 2,985 additions and 124 deletions.
2 changes: 1 addition & 1 deletion samples/MVC3Sample/MVC3Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@
<ItemGroup>
<Compile Include="Controllers\SecureController.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="OAuthResult.cs" />
<Compile Include="Controllers\AccountController.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Controllers\OAuthController.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Models\AccountModels.cs" />
<Compile Include="OAuthResult.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
48 changes: 48 additions & 0 deletions samples/WebApiSample/Controllers/OAuthController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using CrackerJack.OAuth;
using CrackerJack.OAuth.Request;
using CrackerJack.OAuth.Response;

namespace WebApiSample.Controllers
{
public class OAuthController : ApiController
{

public ActionResult Token()
{
try
{
var oauthRequest = new TokenRequest(Request, MvcApplication.ServiceLocator);

var token = oauthRequest.Authorize();

if (token.RedirectsUri.HasValue())
{

var redirectUri = OAuthResponse
.TokenResponse(token.AccessToken, token.ExpiresIn, token.RefreshToken)
.SetLocation(token.RedirectsUri)
.BuildQueryMessage().LocationUri;

return Redirect(redirectUri);
}

var response = OAuthResponse
.TokenResponse(token.AccessToken, token.ExpiresIn, token.RefreshToken)
.BuildJsonMessage();

return this.OAuth(response);
}
catch (OAuthException ex)
{
var response = new ErrorResponseBuilder(ex).BuildJsonMessage();
return this.OAuth(response);
}
}
}
}
10 changes: 10 additions & 0 deletions samples/WebApiSample/WebApiSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="CrackerJack.OAuth">
<HintPath>..\..\binaries\CrackerJack.OAuth.dll</HintPath>
</Reference>
<Reference Include="EntityFramework">
<HintPath>..\packages\EntityFramework.4.1.10331.0\lib\net40\EntityFramework.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -127,6 +130,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Controllers\OAuthController.cs" />
<Compile Include="Controllers\ValuesController.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
Expand Down Expand Up @@ -211,6 +215,12 @@
<ItemGroup>
<Content Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Samples.Core\Samples.Core.csproj">
<Project>{06c23bea-1a09-4fb6-9766-5e474e7b32b1}</Project>
<Name>Samples.Core</Name>
</ProjectReference>
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
Expand Down
37 changes: 36 additions & 1 deletion src/OAuth.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 11
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CrackerJack.OAuth", "CrackerJack.OAuth\CrackerJack.OAuth.csproj", "{FC4244BA-992C-46CF-926F-B4B777363428}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OAuth2Provider", "OAuth2Provider\OAuth2Provider.csproj", "{FC4244BA-992C-46CF-926F-B4B777363428}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OAuth2Provider.Tests", "OAuth2Provider.Tests\OAuth2Provider.Tests.csproj", "{4593774A-0C78-444B-A99F-3327038F6D4D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -22,6 +24,36 @@ Global
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4593774A-0C78-444B-A99F-3327038F6D4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4593774A-0C78-444B-A99F-3327038F6D4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4593774A-0C78-444B-A99F-3327038F6D4D}.Debug|x64.ActiveCfg = Debug|x64
{4593774A-0C78-444B-A99F-3327038F6D4D}.Debug|x64.Build.0 = Debug|x64
{4593774A-0C78-444B-A99F-3327038F6D4D}.Debug|x86.ActiveCfg = Debug|x86
{4593774A-0C78-444B-A99F-3327038F6D4D}.Debug|x86.Build.0 = Debug|x86
{4593774A-0C78-444B-A99F-3327038F6D4D}.Mixed Platforms|Any CPU.ActiveCfg = Mixed Platforms|Any CPU
{4593774A-0C78-444B-A99F-3327038F6D4D}.Mixed Platforms|Any CPU.Build.0 = Mixed Platforms|Any CPU
{4593774A-0C78-444B-A99F-3327038F6D4D}.Mixed Platforms|x64.ActiveCfg = Mixed Platforms|x64
{4593774A-0C78-444B-A99F-3327038F6D4D}.Mixed Platforms|x64.Build.0 = Mixed Platforms|x64
{4593774A-0C78-444B-A99F-3327038F6D4D}.Mixed Platforms|x86.ActiveCfg = Mixed Platforms|x86
{4593774A-0C78-444B-A99F-3327038F6D4D}.Mixed Platforms|x86.Build.0 = Mixed Platforms|x86
{4593774A-0C78-444B-A99F-3327038F6D4D}.Production|Any CPU.ActiveCfg = Production|Any CPU
{4593774A-0C78-444B-A99F-3327038F6D4D}.Production|Any CPU.Build.0 = Production|Any CPU
{4593774A-0C78-444B-A99F-3327038F6D4D}.Production|x64.ActiveCfg = Production|x64
{4593774A-0C78-444B-A99F-3327038F6D4D}.Production|x64.Build.0 = Production|x64
{4593774A-0C78-444B-A99F-3327038F6D4D}.Production|x86.ActiveCfg = Production|x86
{4593774A-0C78-444B-A99F-3327038F6D4D}.Production|x86.Build.0 = Production|x86
{4593774A-0C78-444B-A99F-3327038F6D4D}.QA|Any CPU.ActiveCfg = QA|Any CPU
{4593774A-0C78-444B-A99F-3327038F6D4D}.QA|Any CPU.Build.0 = QA|Any CPU
{4593774A-0C78-444B-A99F-3327038F6D4D}.QA|x64.ActiveCfg = QA|x64
{4593774A-0C78-444B-A99F-3327038F6D4D}.QA|x64.Build.0 = QA|x64
{4593774A-0C78-444B-A99F-3327038F6D4D}.QA|x86.ActiveCfg = QA|x86
{4593774A-0C78-444B-A99F-3327038F6D4D}.QA|x86.Build.0 = QA|x86
{4593774A-0C78-444B-A99F-3327038F6D4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4593774A-0C78-444B-A99F-3327038F6D4D}.Release|Any CPU.Build.0 = Release|Any CPU
{4593774A-0C78-444B-A99F-3327038F6D4D}.Release|x64.ActiveCfg = Release|x64
{4593774A-0C78-444B-A99F-3327038F6D4D}.Release|x64.Build.0 = Release|x64
{4593774A-0C78-444B-A99F-3327038F6D4D}.Release|x86.ActiveCfg = Release|x86
{4593774A-0C78-444B-A99F-3327038F6D4D}.Release|x86.Build.0 = Release|x86
{FC4244BA-992C-46CF-926F-B4B777363428}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FC4244BA-992C-46CF-926F-B4B777363428}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC4244BA-992C-46CF-926F-B4B777363428}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand All @@ -46,4 +78,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
EnterpriseLibraryConfigurationToolBinariesPath = packages\Unity.2.1.505.0\lib\NET35
EndGlobalSection
EndGlobal
4 changes: 4 additions & 0 deletions src/OAuth.sln.DotSettings.user

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OAuth2Provider.Authorization;
using OAuth2Provider.Issuer;
using OAuth2Provider.Request;
using AutoMoq;
using NUnit.Framework;

namespace OAuth2Provider.Tests.Authorization
{
[TestFixture]
public class AuthorizationCodeAuthorizerTest
{
[Test]
public void RequiresAuthorizationCodeGrantType()
{
var mocker = new AutoMoqer();
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns<string>(null);

var authorizer = mocker.Resolve<AuthorizationCodeAuthorizer>();

try
{
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);
Assert.Fail("Exception not thrown.");
}
catch (OAuthException ex)
{
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode);
Assert.IsTrue(ex.ErrorDescription.HasValue());
}

mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns("");

try
{
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);
Assert.Fail("Exception not thrown.");
}
catch (OAuthException ex)
{
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode);
Assert.IsTrue(ex.ErrorDescription.HasValue());
}

mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(" ");

try
{
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);
Assert.Fail("Exception not thrown.");
}
catch (OAuthException ex)
{
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode);
Assert.IsTrue(ex.ErrorDescription.HasValue());
}

mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns("asdf");

try
{
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);
Assert.Fail("Exception not thrown.");
}
catch (OAuthException ex)
{
Assert.AreEqual(ErrorCode.InvalidGrant, ex.ErrorCode);
Assert.IsTrue(ex.ErrorDescription.HasValue());
}
}

[Test]
public void RequiresAuthorizationCode()
{
var mocker = new AutoMoqer();
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.AuthorizationCode);
mocker.GetMock<IOAuthRequest>().Setup(x => x.AuthorizationCode).Returns<string>(null);

var authorizer = mocker.Resolve<AuthorizationCodeAuthorizer>();

try
{
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);
Assert.Fail("Exception not thrown.");
}
catch (OAuthException ex)
{
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode);
Assert.IsTrue(ex.ErrorDescription.HasValue());
}

mocker.GetMock<IOAuthRequest>().Setup(x => x.AuthorizationCode).Returns("");

try
{
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);
Assert.Fail("Exception not thrown.");
}
catch (OAuthException ex)
{
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode);
Assert.IsTrue(ex.ErrorDescription.HasValue());
}

mocker.GetMock<IOAuthRequest>().Setup(x => x.AuthorizationCode).Returns(" ");

try
{
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);
Assert.Fail("Exception not thrown.");
}
catch (OAuthException ex)
{
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode);
Assert.IsTrue(ex.ErrorDescription.HasValue());
}
}

[Test]
public void WhenAuthorizationCodeHasExpired_ThenThrowException()
{
var mocker = new AutoMoqer();
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.AuthorizationCode);
mocker.GetMock<IConfiguration>().Setup(x => x.AuthorizationTokenExpirationLength).Returns(300);
mocker.SetInstance<IOAuthIssuer>(new OAuthIssuer());
var issuer = new OAuthIssuer();
var token = issuer.GenerateAuthorizationToken(new TokenData { ConsumerId = 1, Timestamp = DateTime.UtcNow.AddHours(-1).Ticks });
mocker.GetMock<IOAuthRequest>().Setup(x => x.AuthorizationCode).Returns(token);

var authorizer = mocker.Resolve<AuthorizationCodeAuthorizer>();

try
{
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);
Assert.Fail("Exception not thrown");
}
catch (OAuthException ex)
{
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode);
Assert.IsTrue(ex.ErrorDescription.HasValue());
}
}

[Test]
public void WhenRedirectUriDoesNotMatch_ThenExceptionIsThrown()
{
var mocker = new AutoMoqer();
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.AuthorizationCode);
mocker.GetMock<IConfiguration>().Setup(x => x.AuthorizationTokenExpirationLength).Returns(300);
mocker.GetMock<IConfiguration>().Setup(x => x.AccessTokenExpirationLength).Returns(500);
var issuer = new OAuthIssuer();
mocker.SetInstance<IOAuthIssuer>(issuer);
var token = issuer.GenerateAuthorizationToken(new TokenData { ConsumerId = 1, Timestamp = DateTime.UtcNow.Ticks, RedirectUri = "http://test.com" });
mocker.GetMock<IOAuthRequest>().Setup(x => x.AuthorizationCode).Returns(token);

var authorizer = mocker.Resolve<AuthorizationCodeAuthorizer>();


try
{
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);
Assert.Fail("Exception not thrown");
}
catch (OAuthException ex)
{
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode);
Assert.IsTrue(ex.ErrorDescription.HasValue());
}

mocker.GetMock<IOAuthRequest>().Setup(x => x.RedirectUri).Returns("http://test.com");
var result = authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);

Assert.IsNotNull(result);
Assert.IsTrue(result.AccessToken.HasValue());
Assert.AreEqual(500, result.ExpiresIn);
Assert.IsTrue(result.RefreshToken.HasValue());
}

[Test]
public void ReturnsAccessToken()
{
var mocker = new AutoMoqer();
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.AuthorizationCode);
mocker.GetMock<IConfiguration>().Setup(x => x.AuthorizationTokenExpirationLength).Returns(300);
mocker.GetMock<IConfiguration>().Setup(x => x.AccessTokenExpirationLength).Returns(500);
var issuer = new OAuthIssuer();
mocker.SetInstance<IOAuthIssuer>(issuer);
var token = issuer.GenerateAuthorizationToken(new TokenData {ConsumerId = 1, Timestamp = DateTime.UtcNow.Ticks});
mocker.GetMock<IOAuthRequest>().Setup(x => x.AuthorizationCode).Returns(token);

var authorizer = mocker.Resolve<AuthorizationCodeAuthorizer>();
var result = authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);

Assert.IsNotNull(result);
Assert.IsTrue(result.AccessToken.HasValue());
Assert.AreEqual(500, result.ExpiresIn);
Assert.IsTrue(result.RefreshToken.HasValue());
}
}
}
Loading

0 comments on commit 1d9ea2b

Please sign in to comment.