public void ShouldAddClaimSet()
        {
            var principal = new GenericPrincipal(new GenericIdentity("foo"), null);
            var httpContext = new HttpContext(
                new HttpRequest("foo", "http://foo", ""),
                new HttpResponse(Console.Out));
            HttpContext.Current = httpContext;
            httpContext.User = principal;

            object state = null;
            HttpContextIdentityPolicy policy = new HttpContextIdentityPolicy();

            var evaluationContextMock = new Mock<EvaluationContext>();

            var properties = new Dictionary<string, object>();

            evaluationContextMock
                .ExpectGet(ec => ec.Properties)
                .Returns(properties);

            evaluationContextMock.Expect(ec => ec.AddClaimSet(policy, It.IsAny<ClaimSet>())).Verifiable();

            policy.Evaluate(evaluationContextMock.Object, ref state);

            evaluationContextMock.Verify();
        }
        public void ShouldNotFailForNullHttpContext()
        {
            var evaluationContextMock = new Mock<EvaluationContext>();
            HttpContext.Current = null;

            object state = null;
            HttpContextIdentityPolicy policy = new HttpContextIdentityPolicy();

            bool result = policy.Evaluate(evaluationContextMock.Object, ref state);

            Assert.IsTrue(result);
        }
        public void ShouldNotFailForNullHttpContextUser()
        {
            var evaluationContextMock = new Mock<EvaluationContext>();
            var httpContext = new HttpContext(
                new HttpRequest("foo", "http://foo", ""),
                new HttpResponse(Console.Out));
            HttpContext.Current = httpContext;

            object state = null;
            HttpContextIdentityPolicy policy = new HttpContextIdentityPolicy();

            bool result = policy.Evaluate(evaluationContextMock.Object, ref state);

            Assert.IsTrue(result);
        }
        public void ShouldSetClaimForCurrentUserWithoutCachingIt()
        {
            var evaluationContextMock = new Mock<EvaluationContext>();
            var principal = new GenericPrincipal(new GenericIdentity("foo"), null);
            var httpContext = new HttpContext(
                new HttpRequest("foo", "http://foo", ""),
                new HttpResponse(Console.Out));
            HttpContext.Current = httpContext;
            httpContext.User = principal;

            var properties = new Dictionary<string, object>();

            evaluationContextMock
                .ExpectGet(ec => ec.Properties)
                .Returns(properties);

            object state = null;
            HttpContextIdentityPolicy policy = new HttpContextIdentityPolicy();

            policy.Evaluate(evaluationContextMock.Object, ref state);

            Assert.IsTrue(properties.ContainsKey("Principal"));
            Assert.IsTrue(properties["Principal"] is IPrincipal);
            Assert.AreSame(principal, (IPrincipal)properties["Principal"]);

            // Change the current principal
            principal = new GenericPrincipal(new GenericIdentity("bar"), null);
            httpContext.User = principal;

            policy.Evaluate(evaluationContextMock.Object, ref state);

            Assert.IsTrue(properties.ContainsKey("Principal"));
            Assert.IsTrue(properties["Principal"] is IPrincipal);
            Assert.AreSame(principal, (IPrincipal)properties["Principal"]);
        }
        public void ShouldSetIdentityProperty()
        {
            var evaluationContextMock = new Mock<EvaluationContext>();

            var properties = new Dictionary<string, object>();

            evaluationContextMock
                .ExpectGet(ec => ec.Properties)
                .Returns(properties);

            var principal = new GenericPrincipal(new GenericIdentity("foo"), null);
            var httpContext = new HttpContext(
                new HttpRequest("foo", "http://foo", ""),
                new HttpResponse(Console.Out));
            HttpContext.Current = httpContext;
            httpContext.User = principal;

            object state = null;
            HttpContextIdentityPolicy policy = new HttpContextIdentityPolicy();

            policy.Evaluate(evaluationContextMock.Object, ref state);

            Assert.IsTrue(properties.ContainsKey("Identities"));
            Assert.IsTrue(properties["Identities"] is List<IIdentity>);
            Assert.AreSame(principal.Identity, ((List<IIdentity>)properties["Identities"])[0]);
        }