Skip to content

Commit

Permalink
Build Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Boggin committed Aug 22, 2019
1 parent d3b2a9b commit 6c0247f
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 187 deletions.
2 changes: 1 addition & 1 deletion tests/FeatureFlagger.Tests/DisabledFeatureFlagger.cs
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
namespace IntermediaryManagement.Api.FeatureFlagger
namespace FeatureFlagger.Tests
{
public class DisabledFeatureFlagger : IFeatureFlagger
{
Expand Down
2 changes: 1 addition & 1 deletion tests/FeatureFlagger.Tests/ExampleFeatureFlagger.cs
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
namespace IntermediaryManagement.Api.FeatureFlagger
namespace FeatureFlagger.Tests
{
public class ExampleFeatureFlagger : IFeatureFlagger
{
Expand Down
11 changes: 10 additions & 1 deletion tests/FeatureFlagger.Tests/FeatureFlagger.Tests.csproj
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>
130 changes: 7 additions & 123 deletions tests/FeatureFlagger.Tests/FeatureFlaggerTests.cs
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);
}
}
}
58 changes: 0 additions & 58 deletions tests/FeatureFlagger.Tests/FeatureFlaggerTests2.cs

This file was deleted.

2 changes: 1 addition & 1 deletion tests/FeatureFlagger.Tests/FromFeatureFlagger.cs
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
namespace RoyalLondon.IntermediaryManagement.Api.FeatureFlagger
namespace FeatureFlagger.Tests
{
public class FromFeatureFlagger : IFeatureFlagger
{
Expand Down
2 changes: 1 addition & 1 deletion tests/FeatureFlagger.Tests/UnuserFeatureFlagger.cs
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
namespace RoyalLondon.IntermediaryManagement.Api.FeatureFlagger
namespace FeatureFlagger.Tests
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Unuser", Justification = "No, really")]
public class UnuserFeatureFlagger : IFeatureFlagger
Expand Down
112 changes: 112 additions & 0 deletions tests/FeatureFlagger.Tests/UserAuthorisationTests.cs
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;
}
}
}
2 changes: 1 addition & 1 deletion tests/FeatureFlagger.Tests/UserFeatureFlagger.cs
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
namespace RoyalLondon.IntermediaryManagement.Api.FeatureFlagger
namespace FeatureFlagger.Tests
{
public class UserFeatureFlagger : IFeatureFlagger
{
Expand Down

0 comments on commit 6c0247f

Please sign in to comment.