Skip to content

Commit

Permalink
Showing 3 changed files with 102 additions and 1 deletion.
98 changes: 98 additions & 0 deletions src/HttPlaceholder.Tests/Authorization/LoginServiceFacts.cs
Original file line number Diff line number Diff line change
@@ -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<string, string> _cookies = new Dictionary<string, string>();
private readonly Dictionary<string, string> _config = new Dictionary<string, string>();
private readonly Mock<IConfigurationService> _configurationServiceMock = new Mock<IConfigurationService>();
private readonly MockHttpContext _mockHttpContext = new MockHttpContext();
private LoginService _service;

[TestInitialize]
public void Initialize()
{
var accessorMock = new Mock<IHttpContextAccessor>();
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 3 additions & 0 deletions src/HttPlaceholder/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("HttPlaceholder.Tests")]

0 comments on commit c8e8e1f

Please sign in to comment.