AuthenticationModuleTests.cs :  » Bloggers » SubText » UnitTests » Subtext » Framework » Web » HttpModules » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » SubText 
SubText » UnitTests » Subtext » Framework » Web » HttpModules » AuthenticationModuleTests.cs
using System;
using System.Collections.Specialized;
using System.Security.Principal;
using System.Web;
using System.Web.Security;
using MbUnit.Framework;
using Moq;
using Moq.Stub;
using Subtext.Framework;
using Subtext.Framework.Web.HttpModules;

namespace UnitTests.Subtext.Framework.Web.HttpModules{
    [TestFixture]
    public class AuthenticationModuleTests
    {
        [Test]
        public void AuthenticateRequest_WithRequestForStaticFile_ReturnsImmediately()
        {
            // arrange
            var module = new AuthenticationModule();
            var httpContext = new Mock<HttpContextBase>();
            httpContext.Setup(c => c.Request.Cookies).Throws(new InvalidOperationException());
            var blogRequest = new BlogRequest("localhost", string.Empty, new Uri("http://localhost"), false,
                                              RequestLocation.StaticFile, "/");

            // act, assert
            module.AuthenticateRequest(httpContext.Object, blogRequest);
        }

        [Test]
        public void GetFormsAuthenticationTicket_WithRequestHavingNoCookies_ReturnsNull()
        {
            // arrange
            var module = new AuthenticationModule();
            
            // act
            var authTicket  = module.GetFormsAuthenticationTicket(null);
            
            // assert
            Assert.IsNull(authTicket);
        }

        [Test]
        public void GetFormsAuthenticationTicket_WithRequestHavingIndecipherableAuthCookies_ReturnsNull()
        {
            // arrange
            var module = new AuthenticationModule();
            var badCookie = new HttpCookie(".ASPXAUTH.42") {Value = "STEOHsuthosaeuthoes234234sThisIsGarbage", Expires = DateTime.Now};
            
            // act
            var ticket = module.GetFormsAuthenticationTicket(badCookie);

            // assert
            Assert.IsNull(ticket);
        }

        [Test]
        public void GetFormsAuthenticationTicket_WithRequestHavingNullAuthTicket_ReturnsNull()
        {
            // arrange
            var module = new AuthenticationModule();
            var authCookie = new HttpCookie(".ASPXAUTH.42") {Value = null};
            
            // act
            var ticket = module.GetFormsAuthenticationTicket(authCookie);

            // assert
            Assert.IsNull(ticket);
        }

        [Test]
        public void GetFormsAuthenticationTicket_WithRequestHavingExpiredAuthCookies_SetsUserToGenericPrincipalWithRoles()
        {
            // arrange
            var module = new AuthenticationModule();
            const string roles = "Admins|HostAdmins|Users";
            var ticket = new FormsAuthenticationTicket(1, ".ASPXAUTH.42", DateTime.Now, DateTime.Now.AddDays(-10), true,
                                                       roles);
            Assert.IsTrue(ticket.Expired);
            string cookieValue = FormsAuthentication.Encrypt(ticket);
            var authCookie = new HttpCookie(".ASPXAUTH.42") {Value = cookieValue};
            
            // act
            var authTicket = module.GetFormsAuthenticationTicket(authCookie);

            // assert
            Assert.IsNull(authTicket);
        }

        [Test]
        public void HandleFormsAuthenticationTicket_WithRequestHavingNullAuthTicket_WritesExpiredCookie()
        {
            // arrange
            Func<BlogRequest, HttpContextBase, string> loginFunc = (r, c) => "/foo/login.aspx";
            var module = new AuthenticationModule();
            var authCookie = new HttpCookie(".ASPXAUTH.42") { Value = null };
            var cookies = new HttpCookieCollection { authCookie };
            var httpContext = new Mock<HttpContextBase>();
            httpContext.Stub(c => c.User);
            httpContext.Setup(c => c.Request.Path).Returns("/");
            httpContext.Setup(c => c.Request.QueryString).Returns(new NameValueCollection());
            httpContext.Setup(c => c.Request.Cookies).Returns(cookies);
            httpContext.Setup(c => c.Response.Redirect(It.IsAny<string>(), true));
            var responseCookies = new HttpCookieCollection();
            httpContext.Setup(c => c.Response.Cookies).Returns(responseCookies);
            var blogRequest = new BlogRequest("localhost", string.Empty, new Uri("http://localhost"), false,
                                              RequestLocation.Blog, "/") { Blog = new Blog { Id = 42 } };

            // act
            module.HandleFormsAuthenticationTicket(blogRequest, httpContext.Object, null);

            // assert
            var principal = httpContext.Object.User as GenericPrincipal;
            Assert.IsNull(principal);
            Assert.AreEqual(1, responseCookies.Count);
            HttpCookie cookie = responseCookies[".ASPXAUTH.42"];
            Assert.IsTrue(cookie.Expires.AddYears(20) < DateTime.Now);
        }

        [Test]
        public void AuthenticateRequest_WithRequestHavingValidAuthCookies_SetsUserToGenericPrincipalWithRoles()
        {
            // arrange
            var module = new AuthenticationModule();
            const string roles = "Admins|HostAdmins|Users";
            var ticket = new FormsAuthenticationTicket(1, ".ASPXAUTH.42", DateTime.Now, DateTime.Now.AddDays(60), true,
                                                       roles);
            string cookieValue = FormsAuthentication.Encrypt(ticket);
            var authCookie = new HttpCookie(".ASPXAUTH.42") {Value = cookieValue};
            var cookies = new HttpCookieCollection {authCookie};
            var httpContext = new Mock<HttpContextBase>();
            httpContext.Stub(c => c.User);
            httpContext.Setup(c => c.Request.Path).Returns("/");
            httpContext.Setup(c => c.Request.QueryString).Returns(new NameValueCollection());
            httpContext.Setup(c => c.Request.Cookies).Returns(cookies);
            httpContext.Setup(c => c.Response.Cookies).Returns(cookies);
            var blogRequest = new BlogRequest("localhost", string.Empty, new Uri("http://localhost"), false,
                                              RequestLocation.Blog, "/") {Blog = new Blog {Id = 42}};

            // act
            module.AuthenticateRequest(httpContext.Object, blogRequest);

            // assert
            var principal = httpContext.Object.User as GenericPrincipal;
            Assert.IsNotNull(principal);
            Assert.IsTrue(principal.IsInRole("Admins"));
            Assert.IsTrue(principal.IsInRole("HostAdmins"));
            Assert.IsTrue(principal.IsInRole("Users"));
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.