diff --git a/VAR.WebFormsCore.Tests/Fakes/FakeGlobalConfig.cs b/VAR.WebFormsCore.Tests/Fakes/FakeGlobalConfig.cs new file mode 100644 index 0000000..99cb8b7 --- /dev/null +++ b/VAR.WebFormsCore.Tests/Fakes/FakeGlobalConfig.cs @@ -0,0 +1,44 @@ +using VAR.WebFormsCore.Code; + +namespace VAR.WebFormsCore.Tests.Fakes; + +public class FakeGlobalConfig : IGlobalConfig +{ + public string Title => string.Empty; + + public string TitleSeparator => string.Empty; + + public string Author => string.Empty; + + public string Copyright => string.Empty; + + public string DefaultHandler => string.Empty; + + private string _loginHandler = string.Empty; + + public void FakeSetLoginHandler(string loginHandler) + { + _loginHandler = loginHandler; + } + + public string LoginHandler => _loginHandler; + + public List AllowedExtensions { get; } = new(); + + private bool _authenticated; + + public void FakeSetAuthenticated(bool authenticated) + { + _authenticated = authenticated; + } + + public bool IsUserAuthenticated(IWebContext context) + { + return _authenticated; + } + + public void UserDeauthenticate(IWebContext context) + { + _authenticated = false; + } +} \ No newline at end of file diff --git a/VAR.WebFormsCore.Tests/Pages/PageCommonTests.cs b/VAR.WebFormsCore.Tests/Pages/PageCommonTests.cs index 6ad9e1e..caaf4f5 100644 --- a/VAR.WebFormsCore.Tests/Pages/PageCommonTests.cs +++ b/VAR.WebFormsCore.Tests/Pages/PageCommonTests.cs @@ -1,3 +1,4 @@ +using VAR.WebFormsCore.Code; using VAR.WebFormsCore.Pages; using VAR.WebFormsCore.Tests.Fakes; using Xunit; @@ -39,13 +40,31 @@ public class PageCommonTests [Fact] public void ProcessRequest__TestEmptyFormNotAuthenticated__RedirectToFrmLogin() { + string loginHandler = "FrmLogin"; + (GlobalConfig.Get() as FakeGlobalConfig)?.FakeSetLoginHandler(loginHandler); FakeWebContext fakeWebContext = new(); TestEmptyForm testEmptyForm = new(mustBeAuthenticated: true); testEmptyForm.ProcessRequest(fakeWebContext); Assert.Equal(302, fakeWebContext.ResponseStatusCode); - Assert.Equal(string.Empty, fakeWebContext.FakeResponseHeaders["location"]); + Assert.Equal(loginHandler, fakeWebContext.FakeResponseHeaders["location"]); + } + + [Fact] + public void ProcessRequest__TestEmptyFormPostClickLogout__RedirectToFrmLogin() + { + string loginHandler = "FrmLogin"; + (GlobalConfig.Get() as FakeGlobalConfig)?.FakeSetLoginHandler(loginHandler); + (GlobalConfig.Get() as FakeGlobalConfig)?.FakeSetAuthenticated(true); + FakeWebContext fakeWebContext = new(requestMethod: "POST"); + fakeWebContext.RequestForm.Add("ctl00_ctl02_btnLogout", "Logout"); + TestEmptyForm testEmptyForm = new(mustBeAuthenticated: true); + + testEmptyForm.ProcessRequest(fakeWebContext); + + Assert.Equal(302, fakeWebContext.ResponseStatusCode); + Assert.Equal(loginHandler, fakeWebContext.FakeResponseHeaders["location"]); } #endregion ProcessRequest TestForm