-
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.
- Loading branch information
Showing
9 changed files
with
134 additions
and
187 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 1,15 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>netcoreapp1.1</TargetFramework> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Moq" Version="4.12.0" /> | ||
<PackageReference Include="xunit.assert" Version="2.4.1" /> | ||
<PackageReference Include="xunit.extensibility.execution" Version="2.4.1" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\FeatureFlagger.Authorisation\FeatureFlagger.Authorisation.csproj" /> | ||
<ProjectReference Include="..\..\src\FeatureFlagger\FeatureFlagger.csproj" /> | ||
</ItemGroup> | ||
</Project> |
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 |
---|---|---|
@@ -1,168 1,52 @@ | ||
namespace RoyalLondon.IntermediaryManagement.Api.FeatureFlagger | ||
namespace FeatureFlagger.Tests | ||
{ | ||
using System.Collections.ObjectModel; | ||
using System.Net.Http; | ||
using System.Net.Http.Formatting; | ||
using System.Security.Principal; | ||
using System.Web.Http; | ||
using System.Web.Http.Controllers; | ||
using Xunit; | ||
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
using Moq; | ||
|
||
using RoyalLondon.IntermediaryManagement.Api.FeatureFlagger.CustomAttributes; | ||
|
||
[TestClass] | ||
public class FeatureFlaggerTests | ||
{ | ||
[TestMethod] | ||
public void ShouldUseBasicEnabledFlag() | ||
{ | ||
var featureFlag = new ExampleFeatureFlagger(); | ||
|
||
var isEnabled = featureFlag.IsEnabled(); | ||
|
||
Assert.IsTrue(isEnabled); | ||
Assert.True(isEnabled); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldFailDisabledFlag() | ||
{ | ||
var featureFlag = new DisabledFeatureFlagger(); | ||
|
||
var isEnabled = featureFlag.IsEnabled(); | ||
|
||
Assert.IsFalse(isEnabled); | ||
Assert.False(isEnabled); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldUseFromFlag() | ||
{ | ||
var featureFlag = new FromFeatureFlagger(); | ||
|
||
var isEnabled = featureFlag.IsEnabled(); | ||
|
||
Assert.IsTrue(isEnabled); | ||
Assert.True(isEnabled); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldUseUserFlag() | ||
{ | ||
var featureFlag = new UserFeatureFlagger(); | ||
|
||
var isEnabled = featureFlag.IsEnabled(); | ||
|
||
Assert.IsTrue(isEnabled); | ||
Assert.True(isEnabled); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldFailUserNotListedFlag() | ||
{ | ||
var featureFlag = new UnuserFeatureFlagger(); | ||
|
||
var isEnabled = featureFlag.IsEnabled(); | ||
|
||
Assert.IsFalse(isEnabled); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldPassCustomAttribute() | ||
{ | ||
var attribute = | ||
new UserAuthorisationAttribute("test") | ||
{ | ||
Lookup = "users", | ||
UsersList = "dummy" | ||
}; | ||
|
||
var mockActionContext = ContextUtil(); | ||
|
||
attribute.OnAuthorization(mockActionContext); | ||
|
||
Assert.AreEqual( | ||
System.Net.HttpStatusCode.OK, | ||
mockActionContext.Response.StatusCode); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldNotPassCustomAttribute() | ||
{ | ||
var attribute = | ||
new UserAuthorisationAttribute("test") | ||
{ | ||
Lookup = "users" | ||
}; | ||
|
||
var mockActionContext = ContextUtil(); | ||
|
||
attribute.OnAuthorization(mockActionContext); | ||
|
||
Assert.AreEqual( | ||
System.Net.HttpStatusCode.Unauthorized, | ||
mockActionContext.Response.StatusCode); | ||
} | ||
|
||
private static HttpActionContext ContextUtil() | ||
{ | ||
IPrincipal principal = | ||
new GenericPrincipal( | ||
new GenericIdentity("TestName"), | ||
new[] { "TestRole" }); | ||
|
||
var config = new HttpConfiguration(); | ||
|
||
HttpActionContext mockActionContext; | ||
try | ||
{ | ||
mockActionContext = | ||
new HttpActionContext | ||
{ | ||
ControllerContext = | ||
new HttpControllerContext | ||
{ | ||
Request = new HttpRequestMessage(), | ||
RequestContext = | ||
new HttpRequestContext | ||
{ | ||
Principal = principal | ||
}, | ||
ControllerDescriptor = | ||
CreateControllerDescriptor() | ||
}, | ||
ActionArguments = { { "SomeArgument", "null" } }, | ||
ActionDescriptor = CreateActionDescriptor(), | ||
Response = new HttpResponseMessage() | ||
}; | ||
|
||
mockActionContext.ControllerContext.Configuration = config; | ||
mockActionContext.ControllerContext.Configuration.Formatters | ||
.Add(new JsonMediaTypeFormatter()); | ||
} | ||
finally | ||
{ | ||
config.Dispose(); | ||
} | ||
|
||
return mockActionContext; | ||
} | ||
|
||
private static HttpActionDescriptor CreateActionDescriptor() | ||
{ | ||
var mock = new Mock<HttpActionDescriptor> { CallBase = true }; | ||
mock.SetupGet(d => d.ActionName).Returns("Foo"); | ||
|
||
return mock.Object; | ||
} | ||
|
||
private static HttpControllerDescriptor CreateControllerDescriptor() | ||
{ | ||
var mock = new Mock<HttpControllerDescriptor> { CallBase = true }; | ||
mock | ||
.Setup(d => d.GetCustomAttributes<AllowAnonymousAttribute>()) | ||
.Returns(new Collection<AllowAnonymousAttribute>()); | ||
|
||
return mock.Object; | ||
Assert.False(isEnabled); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,112 @@ | ||
namespace FeatureFlagger.Tests | ||
{ | ||
using System.Collections.ObjectModel; | ||
using System.Net.Http; | ||
using System.Net.Http.Formatting; | ||
using System.Security.Principal; | ||
using System.Web.Http; | ||
using System.Web.Http.Controllers; | ||
|
||
using Moq; | ||
using Xunit; | ||
|
||
public class UserAuthorisationTests | ||
{ | ||
public void ShouldPassCustomAttribute() | ||
{ | ||
var attribute = | ||
new Authorisation.UserAuthorisationAttribute("test") | ||
{ | ||
Lookup = "users", | ||
UsersList = "dummy" | ||
}; | ||
|
||
var mockActionContext = ContextUtil(); | ||
|
||
attribute.OnAuthorization(mockActionContext); | ||
|
||
Assert.Equal( | ||
System.Net.HttpStatusCode.OK, | ||
mockActionContext.Response.StatusCode); | ||
} | ||
|
||
public void ShouldNotPassCustomAttribute() | ||
{ | ||
var attribute = | ||
new Authorisation.UserAuthorisationAttribute("test") | ||
{ | ||
Lookup = "users" | ||
}; | ||
|
||
var mockActionContext = ContextUtil(); | ||
|
||
attribute.OnAuthorization(mockActionContext); | ||
|
||
Assert.Equal( | ||
System.Net.HttpStatusCode.Unauthorized, | ||
mockActionContext.Response.StatusCode); | ||
} | ||
|
||
private static HttpActionContext ContextUtil() | ||
{ | ||
IPrincipal principal = | ||
new GenericPrincipal( | ||
new GenericIdentity("TestName"), | ||
new[] { "TestRole" }); | ||
|
||
var config = new HttpConfiguration(); | ||
|
||
HttpActionContext mockActionContext; | ||
try | ||
{ | ||
mockActionContext = | ||
new HttpActionContext | ||
{ | ||
ControllerContext = | ||
new HttpControllerContext | ||
{ | ||
Request = new HttpRequestMessage(), | ||
RequestContext = | ||
new HttpRequestContext | ||
{ | ||
Principal = principal | ||
}, | ||
ControllerDescriptor = | ||
CreateControllerDescriptor() | ||
}, | ||
ActionArguments = { { "SomeArgument", "null" } }, | ||
ActionDescriptor = CreateActionDescriptor(), | ||
Response = new HttpResponseMessage() | ||
}; | ||
|
||
mockActionContext.ControllerContext.Configuration = config; | ||
mockActionContext.ControllerContext.Configuration.Formatters | ||
.Add(new JsonMediaTypeFormatter()); | ||
} | ||
finally | ||
{ | ||
config.Dispose(); | ||
} | ||
|
||
return mockActionContext; | ||
} | ||
|
||
private static HttpActionDescriptor CreateActionDescriptor() | ||
{ | ||
var mock = new Mock<HttpActionDescriptor> { CallBase = true }; | ||
mock.SetupGet(d => d.ActionName).Returns("Foo"); | ||
|
||
return mock.Object; | ||
} | ||
|
||
private static HttpControllerDescriptor CreateControllerDescriptor() | ||
{ | ||
var mock = new Mock<HttpControllerDescriptor> { CallBase = true }; | ||
mock | ||
.Setup(d => d.GetCustomAttributes<AllowAnonymousAttribute>()) | ||
.Returns(new Collection<AllowAnonymousAttribute>()); | ||
|
||
return mock.Object; | ||
} | ||
} | ||
} |
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