From c8e8e1fb63c272f992861fb409263bc5f177312f Mon Sep 17 00:00:00 2001 From: Duco Date: Fri, 31 May 2019 19:37:43 +0200 Subject: [PATCH] Added unit tests for LoginService class --- .../Authorization/LoginServiceFacts.cs | 98 +++++++++++++++++++ .../Implementations/LoginService.cs | 2 +- src/HttPlaceholder/Properties/AssemblyInfo.cs | 3 + 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/HttPlaceholder.Tests/Authorization/LoginServiceFacts.cs create mode 100644 src/HttPlaceholder/Properties/AssemblyInfo.cs diff --git a/src/HttPlaceholder.Tests/Authorization/LoginServiceFacts.cs b/src/HttPlaceholder.Tests/Authorization/LoginServiceFacts.cs new file mode 100644 index 000000000..077011731 --- /dev/null +++ b/src/HttPlaceholder.Tests/Authorization/LoginServiceFacts.cs @@ -0,0 +1,98 @@ +using Ducode.Essentials.Mvc.TestUtilities; +using HttPlaceholder.Authorization.Implementations; +using HttPlaceholder.Models; +using HttPlaceholder.Services; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Internal; +using Microsoft.Extensions.ObjectPool; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using System.Collections.Generic; +using System.Text; + +namespace HttPlaceholder.Tests.Authorization +{ + [TestClass] + public class LoginServiceFacts + { + private readonly Dictionary _cookies = new Dictionary(); + private readonly Dictionary _config = new Dictionary(); + private readonly Mock _configurationServiceMock = new Mock(); + private readonly MockHttpContext _mockHttpContext = new MockHttpContext(); + private LoginService _service; + + [TestInitialize] + public void Initialize() + { + var accessorMock = new Mock(); + accessorMock + .Setup(m => m.HttpContext) + .Returns(_mockHttpContext); + + _configurationServiceMock + .Setup(m => m.GetConfiguration()) + .Returns(_config); + + _mockHttpContext + .HttpRequestMock + .Setup(m => m.Cookies) + .Returns(() => new RequestCookieCollection(_cookies)); + + _service = new LoginService(_configurationServiceMock.Object, accessorMock.Object); + } + + [TestMethod] + public void LoginService_CheckLoginCookie_NoUsernameAndPasswordSet_ShouldReturnTrue() + { + // Act + bool result = _service.CheckLoginCookie(); + + // Assert + Assert.IsTrue(result); + } + + [TestMethod] + public void LoginService_CheckLoginCookie_UsernameAndPasswordSet_NoCookieSet_ShouldReturnFalse() + { + // Arrange + _config.Add(Constants.ConfigKeys.ApiUsernameKey, "user"); + _config.Add(Constants.ConfigKeys.ApiPasswordKey, "pass"); + + // Act + bool result = _service.CheckLoginCookie(); + + // Assert + Assert.IsFalse(result); + } + + [TestMethod] + public void LoginService_CheckLoginCookie_UsernameAndPasswordSet_HashIncorrect_ShouldReturnFalse() + { + // Arrange + _config.Add(Constants.ConfigKeys.ApiUsernameKey, "user"); + _config.Add(Constants.ConfigKeys.ApiPasswordKey, "pass"); + _cookies.Add(Constants.CookieKeys.LoginCookieKey, "INCORRECT"); + + // Act + bool result = _service.CheckLoginCookie(); + + // Assert + Assert.IsFalse(result); + } + + [TestMethod] + public void LoginService_CheckLoginCookie_UsernameAndPasswordSet_HashCorrect_ShouldReturnTrue() + { + // Arrange + _config.Add(Constants.ConfigKeys.ApiUsernameKey, "user"); + _config.Add(Constants.ConfigKeys.ApiPasswordKey, "pass"); + _cookies.Add(Constants.CookieKeys.LoginCookieKey, "qkUYd4wTaLeznD/nN1v9ei9/5XUekWt1hyOctq3bQZ9DMhSk7FJz+l1ILk++kyYlu+VguxVcuEC9R4Ryk763GA=="); + + // Act + bool result = _service.CheckLoginCookie(); + + // Assert + Assert.IsTrue(result); + } + } +} diff --git a/src/HttPlaceholder/Authorization/Implementations/LoginService.cs b/src/HttPlaceholder/Authorization/Implementations/LoginService.cs index 679317fee..138d71908 100644 --- a/src/HttPlaceholder/Authorization/Implementations/LoginService.cs +++ b/src/HttPlaceholder/Authorization/Implementations/LoginService.cs @@ -28,7 +28,7 @@ public bool CheckLoginCookie() var config = _configurationService.GetConfiguration(); string username = config.GetValue(Constants.ConfigKeys.ApiUsernameKey, string.Empty); string password = config.GetValue(Constants.ConfigKeys.ApiPasswordKey, string.Empty); - if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password)) + if (string.IsNullOrWhiteSpace(username) && string.IsNullOrWhiteSpace(password)) { return true; } diff --git a/src/HttPlaceholder/Properties/AssemblyInfo.cs b/src/HttPlaceholder/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..e56a1bf8a --- /dev/null +++ b/src/HttPlaceholder/Properties/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("HttPlaceholder.Tests")] \ No newline at end of file