Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET + NUnit : Good unit testing strategy for HttpModule using .NET 4

I have the following HttpModule that I wanted to unit test. Problem is I am not allowed to change the access modifiers/static as they need to be as it is. I was wondering what would be the best method to test the following module. I am still pretty new in testing stuff and mainly looking for tips on testing strategy and in general testing HttpModules. Just for clarification, I am just trying to grab each requested URL(only .aspx pages) and checking if the requested url has permission (for specific users in our Intranet). So far it feels like I can't really test this module(from productive perspective).

public class PageAccessPermissionCheckerModule : IHttpModule
    {
        [Inject]
        public IIntranetSitemapProvider SitemapProvider { get; set; }
        [Inject]
        public IIntranetSitemapPermissionProvider PermissionProvider { get; set; }

        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += ValidatePage;
        }

        private void EnsureInjected()
        {
            if (PermissionProvider == null)
                KernelContainer.Inject(this);
        }

        private void ValidatePage(object sender, EventArgs e)
        {
            EnsureInjected();

            var context = HttpContext.Current ?? ((HttpApplication)sender).Context;

            var pageExtension = VirtualPathUtility.GetExtension(context.Request.Url.AbsolutePath);

            if (context.Session == null || pageExtension != ".aspx") return;

            if (!UserHasPermission(context))
            {
                KernelContainer.Get<UrlProvider>().RedirectToPageDenied("Access denied: " + context.Request.Url);
            }
        }

        private bool UserHasPermission(HttpContext context)
        {
            var permissionCode = FindPermissionCode(SitemapProvider.GetNodes(), context.Request.Url.PathAndQuery);

            return PermissionProvider.UserHasPermission(permissionCode);
        }

        private static string FindPermissionCode(IEnumerable<SitemapNode> nodes, string requestedUrl)
        {
            var matchingNode = nodes.FirstOrDefault(x => ComparePaths(x.SiteURL, requestedUrl));

            if (matchingNode != null)
                return matchingNode.PermissionCode;

            foreach(var node in nodes)
            {
                var code = FindPermissionCode(node.ChildNodes, requestedUrl);
                if (!string.IsNullOrEmpty(code))
                    return code;
            }

            return null;
        }  
        public void Dispose() { }
    }
like image 423
MSI Avatar asked Sep 06 '10 01:09

MSI


People also ask

Does NUnit work with .NET core?

Sdk version 17.0. 0 to your NUnit test projects will also enable the dotnet test command for . NET Core projects.

How do I write a unit test case in NUnit?

Creating the first testSave this file and execute the dotnet test command to build the tests and the class library and run the tests. The NUnit test runner contains the program entry point to run your tests. dotnet test starts the test runner using the unit test project you've created.


1 Answers

For other people still looking there is this post which explains a way to do it

Original page was deleted, you can get to the article here: https://web.archive.org/web/20151219105430/http://weblogs.asp.net/rashid/unit-testable-httpmodule-and-httphandler

like image 58
Riga Avatar answered Sep 18 '22 19:09

Riga