diff --git a/Govor.API.Tests/IntegrationTests/Controllers/RefreshControllerTests.cs b/Govor.API.Tests/IntegrationTests/Controllers/RefreshControllerTests.cs new file mode 100644 index 0000000..2ab1195 --- /dev/null +++ b/Govor.API.Tests/IntegrationTests/Controllers/RefreshControllerTests.cs @@ -0,0 +1,117 @@ +using AutoFixture; +using Govor.API.Controllers.Authentication; +using Govor.Application.Interfaces.UserSession; +using Govor.Contracts.Requests; +using Govor.Contracts.Responses; +using Microsoft.AspNetCore.Identity.Data; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Moq; + +namespace Govor.API.Tests.IntegrationTests.Controllers; + +[TestFixture] +public class RefreshControllerTests +{ + private Fixture _fixture; + private Mock> _mockLogger; + private Mock _mockSessionRefresher; + private RefreshController _controller; + private RefreshTokenRequest _request; + private RefreshResult _result; + + [SetUp] + public void SetUp() + { + _fixture = new Fixture(); + + _mockLogger = new Mock>(); + _mockSessionRefresher = new Mock(); + + _request = _fixture.Create(); + _result = _fixture.Create(); + + _controller = new RefreshController(_mockLogger.Object, _mockSessionRefresher.Object); + } + + // Tests for Refresh action + [Test] + public async Task Refresh_ValidRequest_ReturnsOkResult() + { + // Arrange + _mockSessionRefresher.Setup(f => f.RefreshTokenAsync(_request.RefreshToken)) + .ReturnsAsync(_result); + + // Act + var result = await _controller.Refresh(_request); + + //Assert + Assert.That(result, Is.InstanceOf()); + var okResult = result as OkObjectResult; + + var response = okResult?.Value as RefreshTokenResponse; + Assert.That(response, Is.Not.Null); + Assert.That(response.AccessToken, Is.EqualTo(_result.accessToken)); + Assert.That(response.RefreshToken, Is.EqualTo(_result.refreshToken)); + } + + [Test] + public async Task Refresh_InvalidModelState_ReturnsBadRequest() + { + // Arrange + _controller.ModelState.AddModelError("Error", "Sample error"); + + // Act + var result = await _controller.Refresh(_request); + + // Assert + Assert.That(result, Is.InstanceOf()); + } + + [Test] + public async Task Refresh_InvalidRefreshToken_ReturnsBadRequest() + { + // Arrange + _request.RefreshToken = string.Empty; + + // Act + var result = await _controller.Refresh(_request); + + // Assert + Assert.That(result, Is.InstanceOf()); + var badRequestResult = result as BadRequestObjectResult; + var response = badRequestResult?.Value; + Assert.That(response, Is.EqualTo("Refresh token cant be empty.")); + } + + [Test] + public async Task Refresh_UnauthorizedAccessException_ReturnsUnauthorizedRequest() + { + // Arrange + _mockSessionRefresher.Setup(f => f.RefreshTokenAsync(_request.RefreshToken)) + .ThrowsAsync(new UnauthorizedAccessException()); + + // Act + var result = await _controller.Refresh(_request); + + // Assert + Assert.That(result, Is.InstanceOf()); + var unauthorized = result as UnauthorizedObjectResult; + var response = unauthorized?.Value; + Assert.That(response, Is.EqualTo("Invalid refresh token")); + } + + [Test] + public async Task Refresh_InvalidOperationException_ReturnsBadRequest() + { + // Arrange + _mockSessionRefresher.Setup(f => f.RefreshTokenAsync(_request.RefreshToken)) + .ThrowsAsync(new InvalidOperationException()); + + // Act + var result = await _controller.Refresh(_request); + + // Assert + Assert.That(result, Is.InstanceOf()); + } +} \ No newline at end of file diff --git a/Govor.API/Controllers/Authentication/AuthController.cs b/Govor.API/Controllers/Authentication/AuthController.cs index 0b97228..7446985 100644 --- a/Govor.API/Controllers/Authentication/AuthController.cs +++ b/Govor.API/Controllers/Authentication/AuthController.cs @@ -46,11 +46,11 @@ public class AuthController : Controller _logger.LogInformation($"Register request for {user.Username} with id {user.Id} processed successfully"); - var token = await _userSession.OpenSessionAsync(user, registrationRequest.DeviceInfo); + var tokens = await _userSession.OpenSessionAsync(user, registrationRequest.DeviceInfo); _logger.LogInformation($"Session for user {user.Username} with id {user.Id} has been opened"); - return Ok(token); + return Ok(tokens); } catch (UserAlreadyExistException ex) { @@ -86,11 +86,11 @@ public class AuthController : Controller var user = await _accountService.LoginAsync(loginRequest.Name, loginRequest.Password); _logger.LogInformation($"Login request for {user.Username} with id {user.Id} processed successfully"); - var token = await _userSession.OpenSessionAsync(user, loginRequest.DeviceInfo); + var tokens = await _userSession.OpenSessionAsync(user, loginRequest.DeviceInfo); _logger.LogInformation($"Session for user {user.Username} with id {user.Id} has been opened"); - return Ok(token); + return Ok(tokens); } catch (UserNotRegisteredException ex) { diff --git a/Govor.API/Controllers/Authentication/RefreshController.cs b/Govor.API/Controllers/Authentication/RefreshController.cs index 1bdc065..426d4bb 100644 --- a/Govor.API/Controllers/Authentication/RefreshController.cs +++ b/Govor.API/Controllers/Authentication/RefreshController.cs @@ -6,6 +6,8 @@ using Microsoft.AspNetCore.Mvc; namespace Govor.API.Controllers.Authentication; +[ApiController] +[AllowAnonymous] [Route("api/auth/token")] public class RefreshController : Controller { @@ -19,8 +21,7 @@ public class RefreshController : Controller } [RequireHttps] - [AllowAnonymous] - [HttpPost("refresh")] + [HttpPost("refresh")] // api/auth/token/refresh public async Task Refresh([FromBody] RefreshTokenRequest refreshRequest) { try @@ -41,12 +42,12 @@ public class RefreshController : Controller } catch (InvalidOperationException ex) { - _logger.LogWarning(ex, "Invalid refresh token"); + _logger.LogWarning(ex, "Invalid refresh token."); return BadRequest(ex.Message); } catch (UnauthorizedAccessException ex) { - _logger.LogWarning(ex, "Refresh token failed"); + _logger.LogWarning(ex, "Refresh token failed."); return Unauthorized("Invalid refresh token"); } catch (Exception ex)