Coder Social home page Coder Social logo

jimisweden / leeway Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 54 KB

LeeWay, freedom by constraints. Made to ensure Your code behaves as You intended. With easy to use unit test configurations. C# Web API Controller Authorization Attributes xUnit unit test

License: MIT License

C# 100.00%

leeway's Introduction

LeeWay - freedom by constraints.

Made to ensure Your code behaves as You intended.
With easy to use unit test configurations.

C# Web API Controller Authorization Attributes xUnit unit test integration test #DefensiveCoding #TDD

LeeWay.Ensure.ControllerAttributes

Do you have a nice visual overview of the Authorization levels in your API and Controllers?
Do you have unit / integration tests covering your Authorize attributes and Policies?

Use this package to Ensure Controllers and Controller Actions has correct Attributes Currently supported attributes are AllowAnonymousAttribute and AuthorizeAttribute (with or without policy)

Nuget

PM> Install-Package LeeWay.Ensure.ControllerAttributes

Supported plattforms

LeeWay.Ensure.ControllerAttributes targets .NET Standard 2.0 and hence supports the following plattforms*

*including later versions such as .NET Core 3

the demo project "WebApi" and its test project targets netcoreapp3.0

Example of how to configure the validation for Authorize attributes

Note that the below code is found in the demo project "WebApi.ConfigurationTests"

  • The project "WebApi" is the "production assembly" under test.
  • WebApi.Controllers are configured with different attributes etc to support the example configuration below
//install nuget package LeeWay.Ensure.ControllerAttributes; or download and add project dependency to LeeWay.Ensure.ControllerAttributes;

 public class WebApi_Ensure
    {
        private readonly ITestOutputHelper _outputHelper;

        /// <summary>
        /// This is an example for how to configure the test rules
        /// <br/>
        /// Here we use the TestOutputHelper to print validation results in the test output console,
        ///  to see the settings and validation result for all controller actions
        /// </summary>
        /// <param name="outputHelperHelper">Xunit.Abstractions.ITestOutputHelper</param>
        public WebApi_Ensure(ITestOutputHelper outputHelperHelper)
        {
            _outputHelper = outputHelperHelper;
        }

        /// <summary>
        /// This is how you configure the rules for authorization attributes
        /// (Authorize and AllowAnonymous)
        /// </summary>
        [Fact]
        public void Controllers_and_actions_have_correct_Authorization()
        {
            /*
             * create the validator builder
             * - with the assembly under test; in this case 'WebApi'
             * - with the default required attribute
             *  (the attribute that will be required on all controller actions without explicit rules configured)
             */
            var assemblyName = nameof(WebApi);
            var defaultAttributeRequired = new AuthorizeAttribute(PolicyNames.RequireAuthorizedAdmin);
            
            var validatorBuilder = new ValidatorBuilder(assemblyName, defaultAttributeRequired);

            /*
             * It is possible to exclude controllers from validation with path
             * This can be used to exclude a folder/namespace path or single controller (FullName)
             * read the summary of the method
             */
            var excludeControllersInPath = typeof(ExcludedFromValidationController).FullName;

            /*
             * The builder returns a validator that is used to trigger validation of configured rules
             * From the validator you can
             * - get all validation results as objects
             * - print all results in unit test output
             * - throw on failure (prints all invalid results in test output)
             */
            var validator = validatorBuilder
                
                //exclude a controller from validation
                .ExcludeControllersInPath(excludeControllersInPath)

                //add a controller rule
                .AddControllerRule<AccountController>(new AllowAnonymousAttribute())

                // add an action rule;
                // note that action rules takes presence over controller rules, always.
                // If you have more than one action with the same name
                // this rule will only be applied to the first one found in the controller
                .AddActionRule<AccountController>(
                    nameof(AccountController.Get),
                    new AuthorizeAttribute("MyPolicyToGetAll"))

                //add an action rule matching on parameters
                // my recommendation is that you don't do this, instead name the actions differently =)
                .AddActionRule<AccountController>(
                    nameof(AccountController.Get),
                    new MyParameterInfo
                    {
                        Name = "id",
                        Type = typeof(int)
                    },
                    new AuthorizeAttribute("MyPolicyToGetSingle"))

                .AddActionRule<AccountController>(
                    nameof(AccountController.Delete),
                    new AuthorizeAttribute("MyPolicyToDelete"))

                //note: this action name, GetWithSimpleRule, can match two actions in the controller
                // actions will always (?) be ordered as they appear in the controller (top down)
                .AddActionRule<AccountController>(
                    nameof(AccountController.GetWithSimpleRule),
                    new AuthorizeAttribute("MyPolicyToGetWithSimpleRule"))

                .AddActionRule<AccountController>(
                    nameof(AccountController.Post),
                    new List<MyParameterInfo>
                    {
                        new MyParameterInfo {Name = "command", Type = typeof(AccountController.CreateSomethingCommand)}
                    },
                    new AuthorizeAttribute("MyPolicyToPost"))


                .AddControllerRule<UserController>(new AuthorizeAttribute(PolicyNames.RequireAuthorizedUser))


                .AddControllerRule<PublicController>(new AllowAnonymousAttribute())

                .AddActionRule<PublicController>(
                    nameof(PublicController.Get),
                    new MyParameterInfo
                    {
                        Name = "id",
                        Type = typeof(int)
                    },
                    new AuthorizeAttribute("MyPolicy"))

                .AddActionRule<PublicController>(
                    nameof(PublicController.Get),
                    new AuthorizeAttribute("MyPolicy"))

                .AddActionRule<PublicController>(
                    nameof(PublicController.Delete),
                    new AuthorizeAttribute("DeletePolicy"))

                
                //configuration is completed
                .Build();

            //run the tests and throw on failed validation, prints all failed results
            validator.ValidateAndThrowOnError();
            
            //print all results in test output no matter if validation is successful or not.
            validator.PrintValidatedResults(_outputHelper);

            //get the results and do what you like with them
            var results = validator.ValidatedResults();
        }
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.