Skip to content

Instantly share code, notes, and snippets.

@agross
Created September 26, 2009 18:53
Show Gist options
  • Save agross/194355 to your computer and use it in GitHub Desktop.
Save agross/194355 to your computer and use it in GitHub Desktop.
using System;
using System.Security.Principal;
using Crimson.Web.Behaviors;
using Crimson.Web.Controllers;
using Crimson.Web.Services;
using FubuMVC.Core.Behaviors;
using FubuMVC.Core.Controller.Config;
using FubuMVC.Core.Routing;
using FubuMVC.Core.Security;
using Machine.Specifications;
using Microsoft.Practices.ServiceLocation;
using Rhino.Mocks;
namespace Crimson.Web.Tests.Behaviors
{
public abstract class With_security_check
{
protected static IAreaInspector AreaInspector;
protected static Check_if_the_current_principal_is_allowed_to_access_the_requested_area Behavior;
static IControllerConfigContext Context;
protected static ICurrentRequest Request;
protected static ISecurityContext SecurityContext;
protected static IServiceLocator ServiceLocator;
static IUrlResolver UrlResolver;
Establish context = () =>
{
Context = MockRepository.GenerateStub<IControllerConfigContext>();
Context.CurrentConfig = new ControllerActionConfig(typeof(object));
SecurityContext = MockRepository.GenerateStub<ISecurityContext>();
ServiceLocator = MockRepository.GenerateStub<IServiceLocator>();
AreaInspector = MockRepository.GenerateStub<IAreaInspector>();
UrlResolver = MockRepository.GenerateStub<IUrlResolver>();
UrlResolver.Stub(x => x.UrlFor<LoginController>()).Return("http://server/login");
Request = MockRepository.GenerateStub<ICurrentRequest>();
Behavior = new Check_if_the_current_principal_is_allowed_to_access_the_requested_area(Context,
SecurityContext,
ServiceLocator,
AreaInspector,
UrlResolver,
Request);
Behavior.InsideBehavior = MockRepository.GenerateStub<IControllerActionBehavior>();
};
}
[Subject(typeof(Check_if_the_current_principal_is_allowed_to_access_the_requested_area))]
public class When_an_unauthenticated_user_requests_an_area_page : With_security_check
{
static IOutputWriter Writer;
Establish context = () =>
{
SecurityContext
.Stub(x => x.IsAuthenticated)
.Return(false);
AreaInspector
.Stub(x => x.GetAreaName(Arg<ControllerActionConfig>.Is.Anything))
.Return("TheArea");
Writer = MockRepository.GenerateStub<IOutputWriter>();
ServiceLocator.Stub(x => x.GetInstance<IOutputWriter>()).Return(Writer);
Request.Stub(x => x.GetUrl()).Return(new Uri("http://server/TheArea/is/forbidden"));
};
Because of = () => Behavior.Invoke(new object(), x => x);
It should_redirect_to_the_login_page =
() => Writer.AssertWasCalled(x => x.RedirectToUrl(Arg<string>.Is.Anything));
It should_append_the_return_url =
() => Writer.AssertWasCalled(x => x.RedirectToUrl(Arg<string>.Matches(y => y.EndsWith("ReturnUrl=http://server/TheArea/is/forbidden"))));
}
[Subject(typeof(Check_if_the_current_principal_is_allowed_to_access_the_requested_area))]
public class When_an_authenticated_user_requests_an_area_page_and_the_user_is_in_the_area_role
: With_security_check
{
static IOutputWriter Writer;
Establish context = () =>
{
SecurityContext
.Stub(x => x.IsAuthenticated)
.Return(true);
AreaInspector
.Stub(x => x.GetAreaName(Arg<ControllerActionConfig>.Is.Anything))
.Return("TheArea");
SecurityContext.CurrentUser = MockRepository.GenerateStub<IPrincipal>();
SecurityContext.CurrentUser.Stub(x => x.IsInRole("TheArea")).Return(true);
Writer = MockRepository.GenerateStub<IOutputWriter>();
ServiceLocator.Stub(x => x.GetInstance<IOutputWriter>()).Return(Writer);
};
Because of = () => Behavior.Invoke(new object(), x => x);
It should_check_if_the_user_is_in_the_requested_area_s_role =
() => SecurityContext.CurrentUser.AssertWasCalled(x => x.IsInRole("TheArea"));
It should_not_redirect_to_the_login_page =
() => Writer.AssertWasNotCalled(x => x.RedirectToUrl(Arg<string>.Is.Anything));
}
[Subject(typeof(Check_if_the_current_principal_is_allowed_to_access_the_requested_area))]
public class When_an_authenticated_user_requests_an_area_page_but_the_user_is_not_in_the_area_role
: With_security_check
{
static IOutputWriter Writer;
Establish context = () =>
{
SecurityContext
.Stub(x => x.IsAuthenticated)
.Return(true);
AreaInspector
.Stub(x => x.GetAreaName(Arg<ControllerActionConfig>.Is.Anything))
.Return("TheArea");
SecurityContext.CurrentUser = MockRepository.GenerateStub<IPrincipal>();
SecurityContext.CurrentUser.Stub(x => x.IsInRole("TheArea")).Return(false);
Writer = MockRepository.GenerateStub<IOutputWriter>();
ServiceLocator.Stub(x => x.GetInstance<IOutputWriter>()).Return(Writer);
};
Because of = () => Behavior.Invoke(new object(), x => x);
It should_check_if_the_user_is_in_the_requested_area_s_role =
() => SecurityContext.CurrentUser.AssertWasCalled(x => x.IsInRole("TheArea"));
It should_redirect_to_the_login_page =
() => Writer.AssertWasCalled(x => x.RedirectToUrl(Arg<string>.Is.NotNull));
}
[Subject(typeof(Check_if_the_current_principal_is_allowed_to_access_the_requested_area))]
public class When_the_current_request_is_outside_of_any_area : With_security_check
{
static IOutputWriter Writer;
Establish context = () =>
{
AreaInspector
.Stub(x => x.GetAreaName(Arg<ControllerActionConfig>.Is.Anything))
.Return(null);
Writer = MockRepository.GenerateStub<IOutputWriter>();
ServiceLocator.Stub(x => x.GetInstance<IOutputWriter>()).Return(Writer);
};
Because of = () => Behavior.Invoke(new object(), x => x);
It should_not_redirect_to_the_login_page =
() => Writer.AssertWasNotCalled(x => x.RedirectToUrl(Arg<string>.Is.Anything));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment