Skip to content

Commit

Permalink
Merge remote-tracking branch 'awanoodle/UpdateRoleBasedFeatureTests'
Browse files Browse the repository at this point in the history
  • Loading branch information
lholman committed Apr 5, 2014
2 parents dcfb239 + 13c70db commit 443520e
Showing 1 changed file with 57 additions and 58 deletions.
115 changes: 57 additions & 58 deletions Femah.Core.Tests/RoleBasedFeatureSwitchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,77 +9,76 @@ namespace Femah.Core.Tests
{
public class RoleBasedFeatureSwitchTests
{
private FemahContext _femahContext;
private const string _testUserRole = "testrole";

[SetUp]
public void Initialize()
public class TheIsOnMethod
{
// Mock out the HttpContext - mock uses our local cookie collection.
var httpContextMock = new Mock<HttpContextBase>();
httpContextMock.Setup(c => c.User.Identity.IsAuthenticated)
.Returns(true);
httpContextMock.Setup(c => c.User.IsInRole(It.IsAny<string>()))
.Returns((string s) => String.Equals(s, _testUserRole, StringComparison.InvariantCultureIgnoreCase));
private FemahContext _femahContext;
private const string _testUserRole = "testrole";
private RoleBasedFeatureSwitch _featureSwitch;

// Create new FemahContext using mock HttpContext.
_femahContext = new FemahContext(httpContextMock.Object);
}
[SetUp]
public void Initialize()
{
CreateTestFeatureSwitch();

[Test]
public void ReturnsTrueWhenUserInRole()
{
var featureSwitch = new RoleBasedFeatureSwitch
// Mock out the HttpContext - mock uses our local cookie collection.
var httpContextMock = CreateHttpContextMock();

// Create new FemahContext using mock HttpContext.
_femahContext = new FemahContext(httpContextMock.Object);
}

[Test]
public void ReturnsTrue_IfUserIsInRole()
{
IsEnabled = true,
Name = "testRoleBasedFeatureSwitch"
};
featureSwitch.AcceptedRoles.Add(_testUserRole);
_featureSwitch.AcceptedRoles.Add(_testUserRole);

var result = featureSwitch.IsOn(_femahContext);
result.ShouldBe(true);
}
var result = _featureSwitch.IsOn(_femahContext);
result.ShouldBe(true);
}

[Test]
public void ReturnsFalseWhenUserNotInRole()
{
var featureSwitch = new RoleBasedFeatureSwitch
[Test]
public void ReturnsFalse_IfUserIsNotInRole()
{
IsEnabled = true,
Name = "testRoleBasedFeatureSwitch"
};
featureSwitch.AcceptedRoles.Add("adifferentrole");
_featureSwitch.AcceptedRoles.Add("adifferentrole");

var result = featureSwitch.IsOn(_femahContext);
result.ShouldBe(false);
}
var result = _featureSwitch.IsOn(_femahContext);
result.ShouldBe(false);
}

[Test]
public void ReturnsFalseWhenNoRoles()
{
var featureSwitch = new RoleBasedFeatureSwitch
[Test]
public void ReturnsFalse_IfThereAreNoRoles()
{
IsEnabled = true,
Name = "testRoleBasedFeatureSwitch"
};
var result = _featureSwitch.IsOn(_femahContext);
result.ShouldBe(false);
}

var result = featureSwitch.IsOn(_femahContext);
result.ShouldBe(false);
}

[Test]
public void ReturnsTrueWhenUserInOneOfManyRoles()
{
var featureSwitch = new RoleBasedFeatureSwitch
[Test]
public void ReturnsTrue_IfUserIsInOneOfManyRoles()
{
IsEnabled = true,
Name = "testRoleBasedFeatureSwitch"
};
_featureSwitch.AcceptedRoles.AddRange(new[] { "adifferentrole", "role2", _testUserRole });

featureSwitch.AcceptedRoles.AddRange( new [] { "adifferentrole", "role2", _testUserRole });
var result = _featureSwitch.IsOn(_femahContext);
result.ShouldBe(true);
}

var result = featureSwitch.IsOn(_femahContext);
result.ShouldBe(true);
private static Mock<HttpContextBase> CreateHttpContextMock()
{
var httpContextMock = new Mock<HttpContextBase>();
httpContextMock.Setup(c => c.User.Identity.IsAuthenticated)
.Returns(true);
httpContextMock.Setup(c => c.User.IsInRole(It.IsAny<string>()))
.Returns((string s) => String.Equals(s, _testUserRole, StringComparison.InvariantCultureIgnoreCase));
return httpContextMock;
}

private void CreateTestFeatureSwitch()
{
_featureSwitch = new RoleBasedFeatureSwitch
{
IsEnabled = true,
Name = "testRoleBasedFeatureSwitch"
};
}
}
}
}
}

0 comments on commit 443520e

Please sign in to comment.