public SpiderMiddleware(RequestDelegate next, IActionDescriptorCollectionProvider routeProvider,
                                IRouteDataService routeDataService, IPluginHelperService pluginHelperService,
                                IPluginTypesService pluginTypesService, ISettingsProvider settingsProvider)
        {
            if (routeProvider == null)
            {
                throw new ArgumentNullException(nameof(routeProvider));
            }

            if (routeDataService == null)
            {
                throw new ArgumentNullException(nameof(routeDataService));
            }

            if (pluginHelperService == null)
            {
                throw new ArgumentNullException(nameof(pluginHelperService));
            }

            _next = next;

            _userSessionManagerLoaded = pluginHelperService.PluginLoaded("UserSessionMiddleware.Plugin.dll", out int version);

            _deniedSpiderRoutes = new List <DeniedRoute>();
            LoadSpiderData(routeProvider, routeDataService, pluginTypesService);

            SpiderSettings settings = settingsProvider.GetSettings <SpiderSettings>("Spider.Plugin");

            _processStaticFiles = settings.ProcessStaticFiles;

            if (!String.IsNullOrEmpty(settings.StaticFileExtensions))
            {
                _staticFileExtensions = settings.StaticFileExtensions;
            }
        }
Example #2
0
        public BreadcrumbMiddleware(RequestDelegate next, IActionDescriptorCollectionProvider routeProvider,
                                    IRouteDataService routeDataService, IPluginHelperService pluginHelperService,
                                    IPluginTypesService pluginTypesService, ISettingsProvider settingsProvider,
                                    IPluginClassesService pluginClassesService, ILogger logger)
        {
            if (routeProvider == null)
            {
                throw new ArgumentNullException(nameof(routeProvider));
            }

            if (routeDataService == null)
            {
                throw new ArgumentNullException(nameof(routeDataService));
            }

            if (pluginHelperService == null)
            {
                throw new ArgumentNullException(nameof(pluginHelperService));
            }

            if (pluginClassesService == null)
            {
                throw new ArgumentNullException(nameof(pluginClassesService));
            }

            if (pluginTypesService == null)
            {
                throw new ArgumentNullException(nameof(pluginTypesService));
            }

            if (settingsProvider == null)
            {
                throw new ArgumentNullException(nameof(settingsProvider));
            }

            _next   = next;
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));

            if (pluginHelperService.PluginLoaded(Constants.PluginNameLocalization, out int _))
            {
                List <IStringLocalizer> stringLocalizers = pluginClassesService.GetPluginClasses <IStringLocalizer>();

                if (stringLocalizers.Count > 0)
                {
                    _stringLocalizer = stringLocalizers[0];
                }
            }

            BreadcrumbSettings settings = settingsProvider.GetSettings <BreadcrumbSettings>(Constants.PluginSettingBreadcrumb);

            _homeBreadCrumb = new BreadcrumbItem(settings.HomeName,
                                                 $"{Constants.ForwardSlash}{settings.HomeController}{Constants.ForwardSlash}{settings.DefaultAction}", false);

            LoadBreadcrumbData(routeProvider, routeDataService, pluginTypesService, settings);

            if (!String.IsNullOrEmpty(settings.StaticFileExtensions))
            {
                _staticFileExtensions = settings.StaticFileExtensions;
            }
        }
Example #3
0
        public async Task RetrieveUniqueId_InvalidRequest_SiteId_Returns404()
        {
            ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory());

            TestHttpRequest httpRequest = new TestHttpRequest();

            httpRequest.Path = "/smokeTest/SiteId";
            TestHttpResponse httpResponse = new TestHttpResponse();

            IPluginHelperService pluginHelperServices = _testPluginSmokeTest.GetRequiredService <IPluginHelperService>();
            IPluginTypesService  pluginTypesService   = _testPluginSmokeTest.GetRequiredService <IPluginTypesService>();

            TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse);
            ILogger         logger      = new Logger();


            using (WebSmokeTestMiddleware sut = new WebSmokeTestMiddleware(null, pluginHelperServices,
                                                                           pluginTypesService, settingsProvider, logger))
            {
                List <WebSmokeTestItem> smokeTests = sut.SmokeTests;

                Assert.IsTrue(smokeTests.Count > 1);

                await sut.Invoke(httpContext);

                Assert.AreEqual(404, httpResponse.StatusCode);
            }
        }
Example #4
0
        public RestrictIpMiddleware(RequestDelegate next, IActionDescriptorCollectionProvider routeProvider,
                                    IRouteDataService routeDataService, IPluginHelperService pluginHelperService,
                                    IPluginTypesService pluginTypesService, ISettingsProvider settingsProvider, ILogger logger)
        {
            if (routeProvider == null)
            {
                throw new ArgumentNullException(nameof(routeProvider));
            }

            if (routeDataService == null)
            {
                throw new ArgumentNullException(nameof(routeDataService));
            }

            if (pluginHelperService == null)
            {
                throw new ArgumentNullException(nameof(pluginHelperService));
            }

            _next             = next;
            _logger           = logger ?? throw new ArgumentNullException(nameof(logger));
            _localIpAddresses = new HashSet <string>();
            _restrictedRoutes = new Dictionary <string, List <string> >();
            GetLocalIpAddresses(_localIpAddresses);
            RestrictIpSettings settings = settingsProvider.GetSettings <RestrictIpSettings>("RestrictedIpRoutes.Plugin");

            _disabled = settings.Disabled;

            if (_disabled)
            {
                _logger.AddToLog(LogLevel.Information, RestrictedIpDisabled);
            }

            LoadRestrictedIpRouteData(routeProvider, routeDataService, pluginTypesService, settings);
        }
Example #5
0
        public void BadEggValidationInvalidRouteProvider()
        {
            ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory());

            TestHttpRequest  httpRequest  = new TestHttpRequest();
            TestHttpResponse httpResponse = new TestHttpResponse();

            IPluginClassesService pluginServices       = new pm.PluginServices(_testPluginBadEgg) as IPluginClassesService;
            IPluginHelperService  pluginHelperServices = _testPluginBadEgg.GetRequiredService <IPluginHelperService>();
            IPluginTypesService   pluginTypesService   = _testPluginBadEgg.GetRequiredService <IPluginTypesService>();
            INotificationService  notificationService  = _testPluginBadEgg.GetRequiredService <INotificationService>();
            IIpValidation         iPValidation         = new TestIPValidation();

            TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse);

            httpRequest.SetContext(httpContext);
            MockLoginProvider loginProvider = new MockLoginProvider();

            MockClaimsProvider        claimsProvider        = new MockClaimsProvider(pluginServices);
            TestAuthenticationService authenticationService = new TestAuthenticationService();
            RequestDelegate           requestDelegate       = async(context) => { await Task.Delay(0); };
            RouteDataServices         routeDataServices     = new RouteDataServices();

            BadEggMiddleware badEgg = new BadEggMiddleware(requestDelegate, null,
                                                           routeDataServices, pluginHelperServices, pluginTypesService, iPValidation,
                                                           settingsProvider, notificationService);
        }
Example #6
0
        public void BadEggValidationInvalidRequestDelegate()
        {
            ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory());

            TestHttpRequest  httpRequest  = new TestHttpRequest();
            TestHttpResponse httpResponse = new TestHttpResponse();

            IPluginClassesService pluginServices       = new pm.PluginServices(_testPluginBadEgg) as IPluginClassesService;
            IPluginHelperService  pluginHelperServices = _testPluginBadEgg.GetRequiredService <IPluginHelperService>();
            IPluginTypesService   pluginTypesService   = _testPluginBadEgg.GetRequiredService <IPluginTypesService>();
            INotificationService  notificationService  = _testPluginBadEgg.GetRequiredService <INotificationService>();
            IIpValidation         iPValidation         = new TestIPValidation();

            TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse);

            httpRequest.SetContext(httpContext);
            MockLoginProvider loginProvider = new MockLoginProvider();

            MockClaimsProvider                     claimsProvider                         = new MockClaimsProvider(pluginServices);
            TestAuthenticationService              authenticationService                  = new TestAuthenticationService();
            ActionDescriptorCollection             actionDescriptorCollection             = new ActionDescriptorCollection(new List <ActionDescriptor>(), 1);
            TestActionDescriptorCollectionProvider testActionDescriptorCollectionProvider = new TestActionDescriptorCollectionProvider(actionDescriptorCollection);
            RouteDataServices routeDataServices = new RouteDataServices();

            BadEggMiddleware badEgg = new BadEggMiddleware(null, testActionDescriptorCollectionProvider,
                                                           routeDataServices, pluginHelperServices, pluginTypesService, iPValidation,
                                                           settingsProvider, notificationService);
        }
Example #7
0
        public async Task Validate_TestEndCalled_ISmokeTestProviderNotRegistered_Void()
        {
            ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory());

            TestHttpRequest httpRequest = new TestHttpRequest();

            httpRequest.Path = "/smokeTest/end/";
            TestHttpResponse httpResponse = new TestHttpResponse();

            IPluginHelperService pluginHelperServices = _testPluginSmokeTest.GetRequiredService <IPluginHelperService>();
            IPluginTypesService  pluginTypesService   = _testPluginSmokeTest.GetRequiredService <IPluginTypesService>();
            IServiceCollection   serviceCollection    = new ServiceCollection() as IServiceCollection;

            TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse,
                                                              serviceCollection.BuildServiceProvider());
            ILogger         logger             = new Logger();
            bool            nextDelegateCalled = false;
            RequestDelegate requestDelegate    = async(context) => { nextDelegateCalled = true; await Task.Delay(0); };

            using (WebSmokeTestMiddleware sut = new WebSmokeTestMiddleware(requestDelegate, pluginHelperServices,
                                                                           pluginTypesService, settingsProvider, logger))
            {
                List <WebSmokeTestItem> smokeTests = sut.SmokeTests;

                Assert.IsTrue(smokeTests.Count > 1);

                await sut.Invoke(httpContext);

                Assert.IsFalse(nextDelegateCalled);
                Assert.AreEqual(200, httpResponse.StatusCode);
                Assert.IsNull(httpResponse.ContentType);
            }
        }
Example #8
0
        public async Task BadEggValidationSuccess()
        {
            ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory());

            TestHttpRequest  httpRequest  = new TestHttpRequest();
            TestHttpResponse httpResponse = new TestHttpResponse();

            IPluginClassesService pluginServices       = new pm.PluginServices(_testPluginBadEgg) as IPluginClassesService;
            IPluginHelperService  pluginHelperServices = _testPluginBadEgg.GetRequiredService <IPluginHelperService>();
            IPluginTypesService   pluginTypesService   = _testPluginBadEgg.GetRequiredService <IPluginTypesService>();
            INotificationService  notificationService  = _testPluginBadEgg.GetRequiredService <INotificationService>();
            IIpValidation         iPValidation         = new TestIPValidation();

            TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse);

            httpRequest.SetContext(httpContext);
            MockLoginProvider loginProvider = new MockLoginProvider();

            MockClaimsProvider        claimsProvider                                      = new MockClaimsProvider(pluginServices);
            TestAuthenticationService authenticationService                               = new TestAuthenticationService();
            bool                                   nextDelegateCalled                     = false;
            RequestDelegate                        requestDelegate                        = async(context) => { nextDelegateCalled = true; await Task.Delay(0); };
            ActionDescriptorCollection             actionDescriptorCollection             = new ActionDescriptorCollection(new List <ActionDescriptor>(), 1);
            TestActionDescriptorCollectionProvider testActionDescriptorCollectionProvider = new TestActionDescriptorCollectionProvider(actionDescriptorCollection);
            RouteDataServices                      routeDataServices                      = new RouteDataServices();

            BadEggMiddleware badEgg = new BadEggMiddleware(requestDelegate, testActionDescriptorCollectionProvider,
                                                           routeDataServices, pluginHelperServices, pluginTypesService, iPValidation,
                                                           settingsProvider, notificationService);

            await badEgg.Invoke(httpContext);

            Assert.IsTrue(nextDelegateCalled);
        }
        public UserSessionMiddleware(RequestDelegate next, IActionDescriptorCollectionProvider routeProvider,
                                     IRouteDataService routeDataService, IPluginTypesService pluginTypesService, ISettingsProvider settingsProvider)
        {
            if (routeProvider == null)
            {
                throw new ArgumentNullException(nameof(routeProvider));
            }

            if (routeDataService == null)
            {
                throw new ArgumentNullException(nameof(routeDataService));
            }

            if (pluginTypesService == null)
            {
                throw new ArgumentNullException(nameof(pluginTypesService));
            }

            if (settingsProvider == null)
            {
                throw new ArgumentNullException(nameof(settingsProvider));
            }

            _next = next;

            _routeData = new List <RouteData>();

            UserSessionSettings Settings = settingsProvider.GetSettings <UserSessionSettings>(Constants.UserSessionConfiguration);

            Settings.SessionTimeout = Shared.Utilities.CheckMinMax(Settings.SessionTimeout, 15, 200);

            UserSessionManager.InitialiseSessionManager(new TimeSpan(0, (int)Settings.SessionTimeout, 0));

            SessionHelper.InitSessionHelper();

            if (!String.IsNullOrWhiteSpace(Settings.CookieName))
            {
                _cookieName = Settings.CookieName;
            }

            if (!String.IsNullOrWhiteSpace(Settings.EncryptionKey))
            {
                _cookieEncryptionKey = Settings.EncryptionKey;
            }

            if (!String.IsNullOrWhiteSpace(Settings.StaticFileExtensions))
            {
                _staticFileExtension = Settings.StaticFileExtensions;
            }

            _defaultCulture = Settings.DefaultCulture;

            LoadLoggedInOutData(routeProvider, routeDataService, pluginTypesService);
            LoadLoggedInData(routeProvider, routeDataService, pluginTypesService);
            LoadLoggedOutData(routeProvider, routeDataService, pluginTypesService);
        }
Example #10
0
        public async Task Validate_TestStartCalled_ISmokeTestProviderRegistered_Returns_NvpCodecValues()
        {
            ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory());

            TestHttpRequest httpRequest = new TestHttpRequest();

            httpRequest.Path = "/smokeTest/Start/";
            TestHttpResponse httpResponse = new TestHttpResponse();

            IPluginHelperService pluginHelperServices = _testPluginSmokeTest.GetRequiredService <IPluginHelperService>();
            IPluginTypesService  pluginTypesService   = _testPluginSmokeTest.GetRequiredService <IPluginTypesService>();
            IServiceCollection   serviceCollection    = new ServiceCollection() as IServiceCollection;
            NVPCodec             codecValues          = new NVPCodec();

            codecValues.Add("username", "admin");
            MockSmokeTestProvider smokeTestProvider = new MockSmokeTestProvider(codecValues);

            serviceCollection.AddSingleton <ISmokeTestProvider>(smokeTestProvider);

            TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse,
                                                              serviceCollection.BuildServiceProvider());
            ILogger         logger             = new Logger();
            bool            nextDelegateCalled = false;
            RequestDelegate requestDelegate    = async(context) => { nextDelegateCalled = true; await Task.Delay(0); };

            using (WebSmokeTestMiddleware sut = new WebSmokeTestMiddleware(requestDelegate, pluginHelperServices,
                                                                           pluginTypesService, settingsProvider, logger))
            {
                List <WebSmokeTestItem> smokeTests = sut.SmokeTests;

                Assert.IsTrue(smokeTests.Count > 1);

                await sut.Invoke(httpContext);

                Assert.IsFalse(nextDelegateCalled);
                Assert.AreEqual(200, httpResponse.StatusCode);
                Assert.IsNull(httpResponse.ContentType);
                Assert.IsTrue(smokeTestProvider.StartCalled);

                byte[] data = new byte[httpResponse.Body.Length];
                httpResponse.Body.Position = 0;
                httpResponse.Body.Read(data, 0, data.Length);
                string test = Decrypt(Encoding.UTF8.GetString(data), EncryptionKey);

                Assert.IsFalse(String.IsNullOrEmpty(test));
                NVPCodec codec = new NVPCodec();
                codec.Decode(test);

                Assert.AreEqual(1, codec.AllKeys.Length);

                Assert.IsTrue(codec.AllKeys.Contains("username"));
                Assert.AreEqual("admin", codec["username"]);
            }
        }
        public BadEggMiddleware(RequestDelegate next, IActionDescriptorCollectionProvider routeProvider,
                                IRouteDataService routeDataService, IPluginHelperService pluginHelperService,
                                IPluginTypesService pluginTypesService, IIpValidation ipValidation,
                                ISettingsProvider settingsProvider, INotificationService notificationService)
        {
            if (routeProvider == null)
            {
                throw new ArgumentNullException(nameof(routeProvider));
            }

            if (routeDataService == null)
            {
                throw new ArgumentNullException(nameof(routeDataService));
            }

            if (pluginHelperService == null)
            {
                throw new ArgumentNullException(nameof(pluginHelperService));
            }

            if (pluginTypesService == null)
            {
                throw new ArgumentNullException(nameof(pluginTypesService));
            }

            if (settingsProvider == null)
            {
                throw new ArgumentNullException(nameof(settingsProvider));
            }

            _next = next;

            _userSessionManagerLoaded = pluginHelperService.PluginLoaded(Constants.PluginNameUserSession, out int version);
            _ipValidation             = ipValidation ?? throw new ArgumentNullException(nameof(ipValidation));
            _notificationService      = notificationService ?? throw new ArgumentNullException(nameof(notificationService));

            _managedRoutes = new List <ManagedRoute>();
            LoadRouteData(routeProvider, routeDataService, pluginTypesService);

            _badEggSettings = settingsProvider.GetSettings <BadEggSettings>("BadEgg.Plugin");

            _validateConnections = new ValidateConnections(
                _badEggSettings.ConnectionTimeOut,
                _badEggSettings.ConnectionsPerMinute);
            _validateConnections.ConnectionAdd      += ValidateConnections_ConnectionAdd;
            _validateConnections.ConnectionRemove   += ValidateConnections_ConnectionRemove;
            _validateConnections.OnReportConnection += ValidateConnections_OnReportConnection;
            _validateConnections.OnBanIPAddress     += ValidateConnections_OnBanIPAddress;

            ThreadManager.ThreadStart(_validateConnections, "Bad Egg Validation", ThreadPriority.Lowest);
        }
        private void LoadRouteData(IActionDescriptorCollectionProvider routeProvider,
                                   IRouteDataService routeDataService, IPluginTypesService pluginTypesService)
        {
            List <Type> badeggAttributes = pluginTypesService.GetPluginTypesWithAttribute <BadEggAttribute>();

            if (badeggAttributes.Count > 0)
            {
                // Cycle through all classes and methods which have the bad egg attribute
                foreach (Type type in badeggAttributes)
                {
                    // is it a class attribute
                    BadEggAttribute attribute = (BadEggAttribute)type.GetCustomAttributes(true)
                                                .Where(a => a.GetType() == typeof(BadEggAttribute)).FirstOrDefault();

                    if (attribute != null)
                    {
                        string route = routeDataService.GetRouteFromClass(type, routeProvider);

                        if (String.IsNullOrEmpty(route))
                        {
                            continue;
                        }

                        _managedRoutes.Add(new ManagedRoute($"{route.ToLower()}", attribute.ValidateQueryFields, attribute.ValidateFormFields));
                    }

                    // look for specific method disallows

                    foreach (MethodInfo method in type.GetMethods())
                    {
                        attribute = (BadEggAttribute)method.GetCustomAttributes(true)
                                    .Where(a => a.GetType() == typeof(BadEggAttribute)).FirstOrDefault();

                        if (attribute != null)
                        {
                            string route = routeDataService.GetRouteFromMethod(method, routeProvider);

                            if (String.IsNullOrEmpty(route))
                            {
                                continue;
                            }


                            _managedRoutes.Add(new ManagedRoute($"{route.ToLower()}", attribute.ValidateQueryFields, attribute.ValidateFormFields));
                        }
                    }
                }
            }
        }
Example #13
0
        public void LoadSmokeTests()
        {
            ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory());

            IPluginHelperService pluginHelperServices = _testPluginSmokeTest.GetRequiredService <IPluginHelperService>();
            IPluginTypesService  pluginTypesService   = _testPluginSmokeTest.GetRequiredService <IPluginTypesService>();

            ILogger logger = new Logger();

            using (WebSmokeTestMiddleware sut = new WebSmokeTestMiddleware(null, pluginHelperServices,
                                                                           pluginTypesService, settingsProvider, logger))
            {
                List <WebSmokeTestItem> smokeTests = sut.SmokeTests;

                Assert.IsTrue(smokeTests.Count > 1);
            }
        }
Example #14
0
        public async Task RetrieveUniqueId_ValidRequest_Count_Enabled_Returns200()
        {
            ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory());

            TestHttpRequest httpRequest = new TestHttpRequest();

            httpRequest.Path = "/smokeTest/Count/";
            TestHttpResponse httpResponse = new TestHttpResponse();

            IPluginHelperService pluginHelperServices = _testPluginSmokeTest.GetRequiredService <IPluginHelperService>();
            IPluginTypesService  pluginTypesService   = _testPluginSmokeTest.GetRequiredService <IPluginTypesService>();

            TestHttpContext httpContext        = new TestHttpContext(httpRequest, httpResponse);
            ILogger         logger             = new Logger();
            bool            nextDelegateCalled = false;
            RequestDelegate requestDelegate    = async(context) => { nextDelegateCalled = true; await Task.Delay(0); };

            using (WebSmokeTestMiddleware sut = new WebSmokeTestMiddleware(requestDelegate, pluginHelperServices,
                                                                           pluginTypesService, settingsProvider, logger))
            {
                List <WebSmokeTestItem> smokeTests = sut.SmokeTests;

                Assert.IsTrue(smokeTests.Count > 1);
                Assert.IsFalse(nextDelegateCalled);

                await sut.Invoke(httpContext);

                Assert.AreEqual(200, httpResponse.StatusCode);
                byte[] data = new byte[httpResponse.Body.Length];
                httpResponse.Body.Position = 0;
                httpResponse.Body.Read(data, 0, data.Length);
                string count = Decrypt(Encoding.UTF8.GetString(data), EncryptionKey);

                if (Int32.TryParse(count, out int actualCount))
                {
                    Assert.IsTrue(actualCount > 1);
                }
                else
                {
                    throw new InvalidCastException("Failed to convert returned count");
                }
            }
        }
Example #15
0
        public async Task BadEggValidationIgnoreValidation()
        {
            ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory());
            BadEggSettings    badEggSettings   = settingsProvider.GetSettings <BadEggSettings>(SharedPluginFeatures.Constants.BadEggSettingsName);

            TestHttpRequest  httpRequest  = new TestHttpRequest();
            TestHttpResponse httpResponse = new TestHttpResponse();

            IPluginClassesService pluginServices       = new pm.PluginServices(_testPluginBadEgg) as IPluginClassesService;
            IPluginHelperService  pluginHelperServices = _testPluginBadEgg.GetRequiredService <IPluginHelperService>();
            IPluginTypesService   pluginTypesService   = _testPluginBadEgg.GetRequiredService <IPluginTypesService>();
            INotificationService  notificationService  = _testPluginBadEgg.GetRequiredService <INotificationService>();
            IIpValidation         iPValidation         = new TestIPValidation();
            IIpManagement         ipManagement         = _testPluginBadEgg.GetRequiredService <IIpManagement>();

            TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse);

            httpRequest.SetContext(httpContext);
            MockLoginProvider loginProvider = new MockLoginProvider();

            MockClaimsProvider        claimsProvider                                      = new MockClaimsProvider(pluginServices);
            TestAuthenticationService authenticationService                               = new TestAuthenticationService();
            bool                                   nextDelegateCalled                     = false;
            RequestDelegate                        requestDelegate                        = async(context) => { nextDelegateCalled = true; await Task.Delay(0); };
            ActionDescriptorCollection             actionDescriptorCollection             = new ActionDescriptorCollection(new List <ActionDescriptor>(), 1);
            TestActionDescriptorCollectionProvider testActionDescriptorCollectionProvider = new TestActionDescriptorCollectionProvider(actionDescriptorCollection);
            RouteDataServices                      routeDataServices                      = new RouteDataServices();


            httpRequest.Headers.Add(SharedPluginFeatures.Constants.BadEggValidationIgnoreHeaderName,
                                    badEggSettings.IgnoreValidationHeaderCode);

            BadEggMiddleware badEgg = new BadEggMiddleware(requestDelegate, testActionDescriptorCollectionProvider,
                                                           routeDataServices, pluginHelperServices, pluginTypesService, iPValidation,
                                                           settingsProvider, notificationService);

            await badEgg.Invoke(httpContext);

            Assert.IsTrue(httpContext.Response.Headers.ContainsKey(Constants.BadEggValidationIgnoreHeaderName));
            Assert.AreEqual(httpContext.Response.Headers[Constants.BadEggValidationIgnoreHeaderName], Boolean.TrueString);
            Assert.IsTrue(nextDelegateCalled);
            Assert.AreEqual(200, httpContext.Response.StatusCode);
        }
Example #16
0
        public async Task RetrieveUniqueId_ValidRequest_RetrieveTest_Enabled_Returns200()
        {
            ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory());

            TestHttpRequest httpRequest = new TestHttpRequest();

            httpRequest.Path = "/smokeTest/Test/1";
            TestHttpResponse httpResponse = new TestHttpResponse();

            IPluginHelperService pluginHelperServices = _testPluginSmokeTest.GetRequiredService <IPluginHelperService>();
            IPluginTypesService  pluginTypesService   = _testPluginSmokeTest.GetRequiredService <IPluginTypesService>();

            TestHttpContext httpContext        = new TestHttpContext(httpRequest, httpResponse);
            ILogger         logger             = new Logger();
            bool            nextDelegateCalled = false;
            RequestDelegate requestDelegate    = async(context) => { nextDelegateCalled = true; await Task.Delay(0); };

            using (WebSmokeTestMiddleware sut = new WebSmokeTestMiddleware(requestDelegate, pluginHelperServices,
                                                                           pluginTypesService, settingsProvider, logger))
            {
                List <WebSmokeTestItem> smokeTests = sut.SmokeTests;

                Assert.IsTrue(smokeTests.Count > 1);

                await sut.Invoke(httpContext);

                Assert.IsFalse(nextDelegateCalled);
                Assert.AreEqual(200, httpResponse.StatusCode);
                Assert.AreEqual("application/json", httpResponse.ContentType);

                byte[] data = new byte[httpResponse.Body.Length];
                httpResponse.Body.Position = 0;
                httpResponse.Body.Read(data, 0, data.Length);
                string test = Decrypt(Encoding.UTF8.GetString(data), EncryptionKey);

                Assert.IsTrue(test.Contains("Please try again"));
                Assert.IsTrue(test.Contains("Method\":\"POST") || test.Contains("Method\":\"GET"));
            }
        }
Example #17
0
        public async Task Validate_TestStartCalled_ISmokeTestProviderNotRegistered_Returns_EmptyString()
        {
            ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory());

            TestHttpRequest httpRequest = new TestHttpRequest();

            httpRequest.Path = "/smokeTest/Start/";
            TestHttpResponse httpResponse = new TestHttpResponse();

            IPluginHelperService pluginHelperServices = _testPluginSmokeTest.GetRequiredService <IPluginHelperService>();
            IPluginTypesService  pluginTypesService   = _testPluginSmokeTest.GetRequiredService <IPluginTypesService>();

            TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse,
                                                              _testPluginSmokeTest.GetServiceProvider());
            ILogger         logger             = new Logger();
            bool            nextDelegateCalled = false;
            RequestDelegate requestDelegate    = async(context) => { nextDelegateCalled = true; await Task.Delay(0); };

            using (WebSmokeTestMiddleware sut = new WebSmokeTestMiddleware(requestDelegate, pluginHelperServices,
                                                                           pluginTypesService, settingsProvider, logger))
            {
                List <WebSmokeTestItem> smokeTests = sut.SmokeTests;

                Assert.IsTrue(smokeTests.Count > 1);

                await sut.Invoke(httpContext);

                Assert.IsFalse(nextDelegateCalled);
                Assert.AreEqual(200, httpResponse.StatusCode);
                Assert.IsNull(httpResponse.ContentType);

                byte[] data = new byte[httpResponse.Body.Length];
                httpResponse.Body.Position = 0;
                httpResponse.Body.Read(data, 0, data.Length);
                string test = Decrypt(Encoding.UTF8.GetString(data), EncryptionKey);

                Assert.IsTrue(String.IsNullOrEmpty(test));
            }
        }
Example #18
0
        public async Task RetrieveUniqueId_ValidRequest_SiteId_Disabled_Returns200()
        {
            ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory());

            TestHttpRequest httpRequest = new TestHttpRequest();

            httpRequest.Path = "/smokeTest/SiteId/";
            TestHttpResponse httpResponse = new TestHttpResponse();

            IPluginHelperService pluginHelperServices = _testPluginSmokeTest.GetRequiredService <IPluginHelperService>();
            IPluginTypesService  pluginTypesService   = _testPluginSmokeTest.GetRequiredService <IPluginTypesService>();

            TestHttpContext httpContext        = new TestHttpContext(httpRequest, httpResponse);
            ILogger         logger             = new Logger();
            bool            nextDelegateCalled = false;
            RequestDelegate requestDelegate    = async(context) => { nextDelegateCalled = true; await Task.Delay(0); };

            using (WebSmokeTestMiddleware sut = new WebSmokeTestMiddleware(requestDelegate, pluginHelperServices,
                                                                           pluginTypesService, settingsProvider, logger))
            {
                sut.Enabled = false;
                List <WebSmokeTestItem> smokeTests = sut.SmokeTests;

                Assert.IsTrue(smokeTests.Count > 1);

                await sut.Invoke(httpContext);

                Assert.IsTrue(nextDelegateCalled);
                Assert.AreEqual(200, httpResponse.StatusCode);

                byte[] data = new byte[httpResponse.Body.Length];
                httpResponse.Body.Position = 0;
                httpResponse.Body.Read(data, 0, data.Length);
                string id = Encoding.UTF8.GetString(data);

                Assert.AreEqual("", id);
            }
        }
Example #19
0
        public WebSmokeTestMiddleware(RequestDelegate next,
                                      IPluginHelperService pluginHelperService,
                                      IPluginTypesService pluginTypesService,
                                      ISettingsProvider settingsProvider,
                                      ILogger logger)
        {
            if (pluginHelperService == null)
            {
                throw new ArgumentNullException(nameof(pluginHelperService));
            }

            if (pluginTypesService == null)
            {
                throw new ArgumentNullException(nameof(pluginTypesService));
            }

            if (settingsProvider == null)
            {
                throw new ArgumentNullException(nameof(settingsProvider));
            }

            _testDataStream = new FileStream(_savedData, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
            _next           = next;
            _logger         = logger ?? throw new ArgumentNullException(nameof(logger));

            _settings = settingsProvider.GetSettings <WebSmokeTestSettings>(nameof(WebSmokeTest));

            if (_settings.Enabled)
            {
                LoadSmokeTestData(pluginTypesService);
            }

            if (!String.IsNullOrEmpty(_settings.StaticFileExtensions))
            {
                _staticFileExtensions = _settings.StaticFileExtensions;
            }
        }
        private void LoadSpiderData(IActionDescriptorCollectionProvider routeProvider,
                                    IRouteDataService routeDataService, IPluginTypesService pluginTypesService)
        {
            string      spiderTextFile   = String.Empty;
            List <Type> spiderAttributes = pluginTypesService.GetPluginTypesWithAttribute <DenySpiderAttribute>();

            if (spiderAttributes.Count == 0)
            {
                spiderTextFile = "# Allow all from Spider.Plugin\r\n\r\nUser-agent: *";
            }
            else
            {
                // Cycle through all classes and methods which have the spider attribute
                foreach (Type type in spiderAttributes)
                {
                    // is it a class attribute
                    DenySpiderAttribute attribute = (DenySpiderAttribute)type.GetCustomAttributes(true)
                                                    .Where(a => a.GetType() == typeof(DenySpiderAttribute)).FirstOrDefault();

                    if (attribute != null)
                    {
                        string route = routeDataService.GetRouteFromClass(type, routeProvider);

                        if (String.IsNullOrEmpty(route))
                        {
                            continue;
                        }

                        if (!String.IsNullOrEmpty(attribute.Comment))
                        {
                            spiderTextFile += $"# {attribute.Comment}\r\n\r\n";
                        }

                        spiderTextFile += $"User-agent: {attribute.UserAgent}\r\n";
                        spiderTextFile += $"Disallow: /{route}/\r\n\r\n";

                        _deniedSpiderRoutes.Add(new DeniedRoute($"/{route.ToLower()}/", attribute.UserAgent));
                    }

                    // look for specific method disallows

                    foreach (MethodInfo method in type.GetMethods())
                    {
                        attribute = (DenySpiderAttribute)method.GetCustomAttributes(true)
                                    .Where(a => a.GetType() == typeof(DenySpiderAttribute)).FirstOrDefault();

                        if (attribute != null)
                        {
                            string route = routeDataService.GetRouteFromMethod(method, routeProvider);

                            if (String.IsNullOrEmpty(route))
                            {
                                continue;
                            }

                            if (!String.IsNullOrEmpty(attribute.Comment))
                            {
                                spiderTextFile += $"# {attribute.Comment}\r\n\r\n";
                            }

                            spiderTextFile += $"User-agent: {attribute.UserAgent}\r\n";
                            spiderTextFile += $"Disallow: {route}\r\n\r\n";

                            _deniedSpiderRoutes.Add(new DeniedRoute($"{route.ToLower()}", attribute.UserAgent));
                        }
                    }
                }
            }

            _spiderData = Encoding.UTF8.GetBytes(spiderTextFile);
        }