107 lines
3.5 KiB
C#
107 lines
3.5 KiB
C#
using e_suite.API.Common.exceptions;
|
|
using e_suite.Database.Core.Tables.UserManager;
|
|
using eSuite.API.SingleSignOn;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Moq;
|
|
using NUnit.Framework;
|
|
|
|
namespace eSuite.API.UnitTests.SingleSignOn.CookieManager;
|
|
|
|
[TestFixture]
|
|
public class GetUserIdFromLinkCookieUnitTests : CookieManagerTestBase
|
|
{
|
|
[SetUp]
|
|
public override async Task Setup()
|
|
{
|
|
await base.Setup();
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetUserIdFromLinkCookie_WhenCookieNotPresent_ReturnsNull()
|
|
{
|
|
//Arrange
|
|
var fakeRequestCookieCollection = new FakeRequestCookieCollection();
|
|
|
|
var httpRequestMock = new Mock<HttpRequest>();
|
|
httpRequestMock.Setup(x => x.Cookies).Returns(fakeRequestCookieCollection);
|
|
|
|
//Act
|
|
var result = await _cookieManager.GetUserIdFromLinkCookie(httpRequestMock.Object, CancellationToken.None);
|
|
|
|
//Assert
|
|
Assert.That(result, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void GetUserIdFromLinkCookie_WhenCookieValueIsNull_ThrowsNotFoundException()
|
|
{
|
|
//Arrange
|
|
var fakeRequestCookieCollection = new FakeRequestCookieCollection
|
|
{
|
|
{ "eSuiteProfileLinkCookie", null! }
|
|
};
|
|
|
|
var httpRequestMock = new Mock<HttpRequest>();
|
|
httpRequestMock.Setup(x => x.Cookies).Returns(fakeRequestCookieCollection);
|
|
|
|
//Act & Assert
|
|
Assert.ThrowsAsync<NotFoundException>(async () =>
|
|
{
|
|
var result =
|
|
await _cookieManager.GetUserIdFromLinkCookie(httpRequestMock.Object, CancellationToken.None);
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetUserIdFromLinkCookie_WhenCookieValueIsInvalidCookie_ReturnsNullUser()
|
|
{
|
|
//Arrange
|
|
var guid = new Guid("{AF29B4B6-3FBF-4C96-8577-2F12B9E56FBE}");
|
|
|
|
var fakeRequestCookieCollection = new FakeRequestCookieCollection
|
|
{
|
|
{ "eSuiteProfileLinkCookie", guid.ToString() }
|
|
};
|
|
|
|
var httpRequestMock = new Mock<HttpRequest>();
|
|
httpRequestMock.Setup(x => x.Cookies).Returns(fakeRequestCookieCollection);
|
|
|
|
//Act
|
|
var result = await _cookieManager.GetUserIdFromLinkCookie(httpRequestMock.Object, CancellationToken.None);
|
|
|
|
//Assert
|
|
Assert.That(result?.User, Is.Null);
|
|
Assert.That(result?.LinkType, Is.EqualTo(LinkType.Profile));
|
|
_userManagerMock.Verify( x=> x.GetUserWithSingleUseGuid(guid, It.IsAny<CancellationToken>()),Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetUserIdFromLinkCookie_WhenCookieValueIsValidCookie_ReturnsUser()
|
|
{
|
|
//Arrange
|
|
var guid = new Guid("{AF29B4B6-3FBF-4C96-8577-2F12B9E56FBE}");
|
|
|
|
var fakeRequestCookieCollection = new FakeRequestCookieCollection
|
|
{
|
|
{ "eSuiteProfileLinkCookie", guid.ToString() }
|
|
};
|
|
|
|
var httpRequestMock = new Mock<HttpRequest>();
|
|
httpRequestMock.Setup(x => x.Cookies).Returns(fakeRequestCookieCollection);
|
|
|
|
var user = new User
|
|
{
|
|
Id = 72,
|
|
FirstName = "Testy",
|
|
LastName = "McTester"
|
|
};
|
|
|
|
_userManagerMock.Setup(x => x.GetUserWithSingleUseGuid(guid, It.IsAny<CancellationToken>())).ReturnsAsync(user);
|
|
|
|
//Act
|
|
var result = await _cookieManager.GetUserIdFromLinkCookie(httpRequestMock.Object, CancellationToken.None);
|
|
|
|
//Assert
|
|
Assert.That(result?.User, Is.EqualTo(user));
|
|
}
|
|
} |