public void Should_disable_load_balancing_when_requested()
        {
            ApplicationInstance instance = new ApplicationInstance();

            instance.AvailableForLoadBalancing = false;

            MockRepository mocks = new MockRepository();
            IApplicationInstanceContext instanceContext = mocks.CreateMock <IApplicationInstanceContext>();
            IWebContext context = mocks.CreateMock <IWebContext>();
            ISecureAvailabilityStatusUpdater updater = mocks.CreateMock <ISecureAvailabilityStatusUpdater>();

            using (mocks.Record())
            {
                Expect.Call(instanceContext.GetCurrent()).Return(instance);
                Expect.Call(context.GetRequestItem(LoadBalanceStatusManager.ENABLED_PARAM)).Return("FALSE");
                Expect.Call(updater.SetStatus(false)).Return(string.Empty);
            }

            using (mocks.Playback())
            {
                ILoadBalanceStatusManager manager = new LoadBalanceStatusManager(instanceContext, context, updater);
                string errorMessage = manager.HandleLoadBalanceRequest();

                Assert.That(errorMessage, Is.Empty);
            }
        }
        public void SetAvailabilityStatus(bool enabled)
        {
            ApplicationInstance instance = _context.GetCurrent();

            instance.AvailableForLoadBalancing = enabled;
            _repository.Save(instance);
        }
        public void Return_status_400_message_if_instance_should_be_offline_and_status_not_being_changed()
        {
            ApplicationInstance instance = new ApplicationInstance();

            instance.AvailableForLoadBalancing = false;

            MockRepository mocks = new MockRepository();
            IApplicationInstanceContext instanceContext = mocks.CreateMock <IApplicationInstanceContext>();
            IWebContext context = mocks.CreateMock <IWebContext>();

            using (mocks.Record())
            {
                Expect.Call(instanceContext.GetCurrent()).Return(instance);
                Expect.Call(context.GetRequestItem(LoadBalanceStatusManager.ENABLED_PARAM)).Return(null);
                context.SetHttpResponseStatus(400, "This application has been turned off");
            }

            using (mocks.Playback())
            {
                ILoadBalanceStatusManager manager = new LoadBalanceStatusManager(instanceContext, context, null);
                string errorMessage = manager.HandleLoadBalanceRequest();

                Assert.That(errorMessage, Is.Empty);
            }
        }
Esempio n. 4
0
        public string BuildHtml(string errorMessage)
        {
            ApplicationInstance instance = _context.GetCurrent();

            string template = _fileLocator.ReadTextFile("Tarantino.Core", LoadBalancerBodyTemplate);

            _replacer.Text = template;

            _replacer.Replace("ERROR_MESSAGE", errorMessage);

            _replacer.Replace("CURRENT_STATE", instance.AvailableForLoadBalancing ? "enabled" : "disabled");
            _replacer.Replace("MACHINE", instance.MachineName);

            string changeStateLink = string.Empty;

            if (_securityChecker.IsCurrentUserAdministrator())
            {
                bool   newState      = !instance.AvailableForLoadBalancing;
                string newStateLabel = newState ? "enable" : "disable";

                changeStateLink = string.Format(_changeStateLinkTemplate, LoadBalanceStatusManager.ENABLED_PARAM, newState, newStateLabel);
            }

            _replacer.Replace("CHANGE_STATE_LINK", changeStateLink);

            return(_replacer.Text);
        }
Esempio n. 5
0
        public bool ShouldBeRedirectedToMaintenancePage()
        {
            ApplicationInstance instance = _context.GetCurrent();

            bool downForMaintenance = instance.DownForMaintenance;
            bool isExternalUrl      = _urlChecker.CurrentUrlIsExternal();
            bool redirectableUrl    = _extensionChecker.CurrentUrlCanBeRedirected();

            return(redirectableUrl && downForMaintenance && isExternalUrl);
        }
Esempio n. 6
0
        public string HandleLoadBalanceRequest()
        {
            string errorMessage = string.Empty;

            ApplicationInstance currentInstance = _instanceContext.GetCurrent();

            string enabledParameter = _context.GetRequestItem(ENABLED_PARAM);

            if (enabledParameter != null)
            {
                bool enabled = bool.Parse(enabledParameter);
                errorMessage = _statusUpdater.SetStatus(enabled);
            }
            else if (!currentInstance.AvailableForLoadBalancing)
            {
                _context.SetHttpResponseStatus(400, "This application has been turned off");
            }

            return(errorMessage);
        }
        public void Correctly_returns_html_for_error_authenticated_page_with_disabled_instance()
        {
            string errorMessage = "An error has occured!";
            string bodyTemplate = "body template";

            ApplicationInstance instance = new ApplicationInstance();

            instance.MachineName = "MyMachine";
            instance.AvailableForLoadBalancing = false;

            MockRepository mocks = new MockRepository();
            IApplicationInstanceContext context     = mocks.CreateMock <IApplicationInstanceContext>();
            IResourceFileLocator        fileLocator = mocks.CreateMock <IResourceFileLocator>();
            ITokenReplacer replacer = mocks.CreateMock <ITokenReplacer>();
            IAdministratorSecurityChecker securityChecker = mocks.CreateMock <IAdministratorSecurityChecker>();

            using (mocks.Record())
            {
                Expect.Call(securityChecker.IsCurrentUserAdministrator()).Return(true);

                Expect.Call(context.GetCurrent()).Return(instance);
                Expect.Call(fileLocator.ReadTextFile("Tarantino.Core", LoadBalancerBodyView.LoadBalancerBodyTemplate)).Return(bodyTemplate);
                replacer.Text = bodyTemplate;

                replacer.Replace("ERROR_MESSAGE", errorMessage);
                replacer.Replace("CURRENT_STATE", "disabled");
                replacer.Replace("MACHINE", "MyMachine");
                replacer.Replace("CHANGE_STATE_LINK", "<a href=\"?enabled=True\">enable</a>");

                Expect.Call(replacer.Text).Return("formatted HTML");
            }

            using (mocks.Playback())
            {
                ILoadBalancerBodyView view = new LoadBalancerBodyView(context, fileLocator, replacer, securityChecker);
                string html = view.BuildHtml(errorMessage);

                Assert.That(html, Is.EqualTo("formatted HTML"));
            }
        }
        public void Correctly_disables_application_instance()
        {
            ApplicationInstance instance = new ApplicationInstance();

            MockRepository mocks = new MockRepository();
            IApplicationInstanceRepository repository = mocks.CreateMock <IApplicationInstanceRepository>();
            IApplicationInstanceContext    context    = mocks.CreateMock <IApplicationInstanceContext>();

            using (mocks.Record())
            {
                Expect.Call(context.GetCurrent()).Return(instance);
                repository.Save(instance);
            }

            using (mocks.Playback())
            {
                IAvailabilityStatusUpdater updater = new AvailabilityStatusUpdater(repository, context);
                updater.SetAvailabilityStatus(false);

                Assert.That(instance.AvailableForLoadBalancing, Is.False);
            }

            mocks.VerifyAll();
        }
Esempio n. 9
0
        private void performTest(bool isRedirectableExtension, bool isExternalUrl, bool isDownForMaintenance, bool shouldBeRedirected)
        {
            ApplicationInstance instance = new ApplicationInstance();

            instance.DownForMaintenance = isDownForMaintenance;

            MockRepository              mocks            = new MockRepository();
            IFileExtensionChecker       extensionChecker = mocks.CreateMock <IFileExtensionChecker>();
            IExternalUrlChecker         urlChecker       = mocks.CreateMock <IExternalUrlChecker>();
            IApplicationInstanceContext context          = mocks.CreateMock <IApplicationInstanceContext>();

            using (mocks.Record())
            {
                Expect.Call(extensionChecker.CurrentUrlCanBeRedirected()).Return(isRedirectableExtension);
                Expect.Call(urlChecker.CurrentUrlIsExternal()).Return(isExternalUrl);
                Expect.Call(context.GetCurrent()).Return(instance);
            }

            using (mocks.Playback())
            {
                IMaintenanceRedirectionChecker checker = new MaintenanceRedirectionChecker(extensionChecker, urlChecker, context);
                Assert.That(checker.ShouldBeRedirectedToMaintenancePage(), Is.EqualTo(shouldBeRedirected));
            }
        }