This repository has been archived by the owner on Dec 27, 2024. It is now read-only.
forked from okta/okta-aspnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request okta#6 from okta/OKTA-167118-add-testing-to-aspnet
Okta 167118 add testing to aspnet
- Loading branch information
Showing
28 changed files
with
425 additions
and
51 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
Okta.AspNet.Abstractions.Test/Okta.AspNet.Abstractions.Test.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" /> | ||
<PackageReference Include="xunit" Version="2.3.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" /> | ||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Okta.AspNet.Abstractions\Okta.AspNet.Abstractions.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
96 changes: 96 additions & 0 deletions
96
Okta.AspNet.Abstractions.Test/OktaMvcOptionsValidatorShould.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using Okta.AspNet.Abstractions; | ||
using System; | ||
using Xunit; | ||
|
||
namespace Okta.AspNet.Abstractions.Test | ||
{ | ||
public class OktaMvcOptionsValidatorShould | ||
{ | ||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
public void FailWhenClientSecretIsNullOrEmpty(String clientSecret) | ||
{ | ||
var options = new OktaMvcOptions() | ||
{ | ||
OrgUrl = "OrgUrl", | ||
ClientId = "ClientId", | ||
ClientSecret = clientSecret | ||
}; | ||
|
||
ShouldFailValidation(options, nameof(OktaMvcOptions.ClientSecret)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
public void FailWhenRedirectUriIsNullOrEmpty(String redirectUri) | ||
{ | ||
var options = new OktaMvcOptions() | ||
{ | ||
OrgUrl = "OrgUrl", | ||
ClientId = "ClientId", | ||
ClientSecret = "ClientSecret", | ||
RedirectUri = redirectUri | ||
}; | ||
|
||
ShouldFailValidation(options, nameof(OktaMvcOptions.RedirectUri)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
public void FailWhenClientIdIsNullOrEmpty(String clientId) | ||
{ | ||
var options = new OktaMvcOptions() | ||
{ | ||
OrgUrl = "OrgUrl", | ||
ClientId = clientId, | ||
}; | ||
|
||
ShouldFailValidation(options, nameof(OktaMvcOptions.ClientId)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
public void FailIfOrgUrlIsNullOrEmpty(String orgUrl) | ||
{ | ||
var options = new OktaMvcOptions() | ||
{ | ||
OrgUrl = orgUrl, | ||
ClientId = "ClientId" | ||
}; | ||
|
||
ShouldFailValidation(options, nameof(OktaMvcOptions.OrgUrl)); | ||
} | ||
|
||
[Fact] | ||
public void NotThrowWhenParamsAreProvided() | ||
{ | ||
var options = new OktaMvcOptions() | ||
{ | ||
OrgUrl = "OrgUrl", | ||
ClientId = "ClientId", | ||
ClientSecret = "ClientSecret", | ||
RedirectUri = "RedirectUri" | ||
}; | ||
|
||
new OktaMvcOptionsValidator().Validate(options); | ||
Assert.True(true, "No exception was thrown."); | ||
} | ||
|
||
private void ShouldFailValidation(OktaMvcOptions options, string paramName) | ||
{ | ||
try | ||
{ | ||
new OktaMvcOptionsValidator().Validate(options); | ||
Assert.True(false, "No exception was thrown."); | ||
} | ||
catch (ArgumentNullException e) | ||
{ | ||
Assert.Contains(e.ParamName, paramName); | ||
} | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
Okta.AspNet.Abstractions.Test/OktaWebApiOptionsValidatorShould.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Okta.AspNet.Abstractions.Test | ||
{ | ||
public class OktaWebApiOptionsValidatorShould | ||
{ | ||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
public void FailWhenClientIdIsNullOrEmpty(String clientId) | ||
{ | ||
var options = new OktaWebApiOptions() | ||
{ | ||
OrgUrl = "OrgUrl", | ||
ClientId = clientId, | ||
}; | ||
|
||
ShouldFailValidation(options, nameof(OktaWebApiOptions.ClientId)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
public void FailIfOrgUrlIsNullOrEmpty(String orgUrl) | ||
{ | ||
var options = new OktaWebApiOptions() | ||
{ | ||
OrgUrl = orgUrl, | ||
ClientId = "ClientId" | ||
}; | ||
|
||
ShouldFailValidation(options, nameof(OktaWebApiOptions.OrgUrl)); | ||
} | ||
|
||
[Fact] | ||
public void NotThrowWhenParamsAreProvided() | ||
{ | ||
var options = new OktaWebApiOptions() | ||
{ | ||
OrgUrl = "OrgUrl", | ||
ClientId = "ClientId", | ||
}; | ||
|
||
new OktaWebApiOptionsValidator().Validate(options); | ||
Assert.True(true, "No exception was thrown."); | ||
} | ||
|
||
private void ShouldFailValidation(OktaWebApiOptions options, string paramName) | ||
{ | ||
try | ||
{ | ||
new OktaWebApiOptionsValidator().Validate(options); | ||
Assert.True(false, "No exception was thrown."); | ||
} | ||
catch (ArgumentNullException e) | ||
{ | ||
Assert.Contains(e.ParamName, paramName); | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
|
||
namespace Okta.AspNet.Abstractions | ||
{ | ||
public class OktaMvcOptionsValidator : OktaOptionsValidator | ||
{ | ||
public void Validate(OktaMvcOptions options) | ||
{ | ||
base.ValidateBaseOktaOptions(options); | ||
|
||
if (string.IsNullOrEmpty(options.ClientSecret)) | ||
{ | ||
throw new ArgumentNullException(nameof(options.ClientSecret), | ||
"Your Okta Application client secret is missing. You can find it in the Okta Developer Console in the details for the Application you created."); | ||
} | ||
|
||
if (string.IsNullOrEmpty(options.RedirectUri)) | ||
{ | ||
throw new ArgumentNullException(nameof(options.RedirectUri), | ||
"Your Okta Application redirect URI is missing. You can find it in the Okta Developer Console in the details for the Application you created."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
|
||
namespace Okta.AspNet.Abstractions | ||
{ | ||
public class OktaOptionsValidator | ||
{ | ||
public virtual void ValidateBaseOktaOptions(OktaOptions options) | ||
{ | ||
if (options == null) | ||
{ | ||
throw new ArgumentNullException(nameof(options)); | ||
} | ||
|
||
if (string.IsNullOrEmpty(options.OrgUrl)) | ||
{ | ||
throw new ArgumentNullException(nameof(options.OrgUrl), | ||
"Your Okta Org URL is missing. You can find it in the Okta Developer Console. It'll look like: https://{yourOktaDomain}.com"); | ||
} | ||
|
||
if (string.IsNullOrEmpty(options.ClientId)) | ||
{ | ||
throw new ArgumentNullException(nameof(options.ClientId), | ||
"Your Okta Application client ID is missing. You can find it in the Okta Developer Console in the details for the Application you created."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Okta.AspNet.Abstractions | ||
{ | ||
public class OktaWebApiOptionsValidator : OktaOptionsValidator | ||
{ | ||
public void Validate(OktaWebApiOptions options) | ||
{ | ||
base.ValidateBaseOktaOptions(options); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Microsoft.Owin.Testing; | ||
using Owin; | ||
using System; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading.Tasks; | ||
using System.Web.Http; | ||
using System.Web.Http.Dispatcher; | ||
using Xunit; | ||
|
||
namespace Okta.AspNet.Test.WebApi.Tests | ||
{ | ||
public class MiddlewareShould : IDisposable | ||
{ | ||
private TestServer _server; | ||
private string BaseUrl { get; set; } | ||
private string ProtectedEndpoint { get; set; } | ||
|
||
public MiddlewareShould() | ||
{ | ||
BaseUrl = "http://localhost:8080"; | ||
ProtectedEndpoint = String.Format("{0}/api/messages", BaseUrl); | ||
|
||
_server = TestServer.Create(app => | ||
{ | ||
var startup = new Startup(); | ||
startup.Configuration(app); | ||
|
||
HttpConfiguration config = new HttpConfiguration(); | ||
config.Services.Replace(typeof(IAssembliesResolver), new WebApiResolver()); | ||
config.MapHttpAttributeRoutes(); | ||
app.UseWebApi(config); | ||
}); | ||
|
||
_server.BaseAddress = new Uri(BaseUrl); | ||
} | ||
|
||
[Fact] | ||
public async Task Returns401WhenAccessToProtectedRouteWithoutTokenAsync() | ||
{ | ||
using (var client = new HttpClient(_server.Handler)) | ||
{ | ||
var response = await client.GetAsync(ProtectedEndpoint); | ||
Assert.True(response.StatusCode == System.Net.HttpStatusCode.Unauthorized); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Returns401WhenAccessToProtectedRouteWithInvalidTokenAsync() | ||
{ | ||
var accessToken = "thisIsAnInvalidToken"; | ||
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, ProtectedEndpoint); | ||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); | ||
|
||
using (var client = new HttpClient(_server.Handler)) | ||
{ | ||
var response = await client.SendAsync(request); | ||
Assert.True(response.StatusCode == System.Net.HttpStatusCode.Unauthorized); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_server.Dispose(); | ||
} | ||
} | ||
} |
Oops, something went wrong.