using e_suite.API.Common.exceptions; using e_suite.API.Common.models; using eSuite.API.Models; using eSuite.Core.Miscellaneous; using Microsoft.AspNetCore.Mvc; using Moq; using NUnit.Framework; namespace eSuite.API.UnitTests.Controllers.AuthenticationControllerUnitTests; [TestFixture] public class RefreshTokenUnitTests : AuthenticationControllerTestBase { [SetUp] public override async Task Setup() { await base.Setup(); } [Test] public async Task RefreshToken_UserIdNotFound_Unauthorised() { //Arrange const long id = 2; const string email = "test@test.test"; const string displayName = "Testy McTester"; AddAuthorisedUserToController(id, email, displayName); var loginResponse = new LoginResponse { Result = LoginResult.Failed }; _userManagerMock.Setup(x => x.RefreshToken(It.IsAny(), It.IsAny())).Returns( (generalIdRef, cancellationToken) => { if (generalIdRef is { Id: id }) { return Task.FromResult(loginResponse); } throw new NotFoundException(); }); //Act var actualResult = await _authenticationController.RefreshToken(); //Assert Assert.That(actualResult.GetType(), Is.EqualTo(typeof(UnauthorizedObjectResult))); var objectResult = actualResult as UnauthorizedObjectResult; Assert.That(objectResult?.StatusCode, Is.EqualTo(401)); Assert.That(objectResult?.Value, Is.Not.Null); if (objectResult?.Value != null) { Assert.That(objectResult.Value.GetType, Is.EqualTo(typeof(ProblemDetails))); var problemDetails = objectResult.Value as ProblemDetails; Assert.That(problemDetails?.Title, Is.EqualTo(AccessDeniedText)); } } [Test] public async Task RefreshToken_Success_OKAndToken() { //Arrange const long id = -1; const string email = "testuser1@sun-strategy.com"; const string displayName = "Test1 User"; AddAuthorisedUserToController(id, email, displayName); var loginResponse = new LoginResponse { Result = LoginResult.Success, Token = "This is a test token" }; _userManagerMock.Setup(x => x.RefreshToken(It.IsAny(), It.IsAny())).Returns((generalIdRef, cancellationToken) => { if (generalIdRef is { Id: id }) { return Task.FromResult(loginResponse); } throw new NotFoundException(); }); //Act var actualResult = await _authenticationController.RefreshToken(); //Assert Assert.That(actualResult.GetType(), Is.EqualTo(typeof(OkObjectResult))); var objectResult = actualResult as OkObjectResult; Assert.That(objectResult?.StatusCode, Is.EqualTo(200)); Assert.That(objectResult?.Value, Is.Not.Null); if (objectResult?.Value != null) { Assert.That(objectResult.Value.GetType, Is.EqualTo(typeof(SuccessfulLogin))); var problemDetails = objectResult.Value as SuccessfulLogin; Assert.That(problemDetails?.Title, Is.EqualTo("Access Granted")); Assert.That(problemDetails?.Token, Is.EqualTo(loginResponse.Token)); } } }