diff --git a/MainProject.Tests/Controllers/AuthController_Tests.cs b/MainProject.Tests/Controllers/AuthController_Tests.cs new file mode 100644 index 0000000..ca795ee --- /dev/null +++ b/MainProject.Tests/Controllers/AuthController_Tests.cs @@ -0,0 +1,80 @@ +using System; +using System.Reflection; +using System.Net; +using System.Net.Http; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Infrastructure; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.Extensions.Configuration; +using Moq; +using Newtonsoft.Json; +using BasicDotnetTemplate.MainProject; +using BasicDotnetTemplate.MainProject.Controllers; +using BasicDotnetTemplate.MainProject.Services; +using BasicDotnetTemplate.MainProject.Models.Api.Response; +using BasicDotnetTemplate.MainProject.Models.Settings; +using BasicDotnetTemplate.MainProject.Models.Api.Request.Auth; +using BasicDotnetTemplate.MainProject.Models.Api.Data.Auth; +using BasicDotnetTemplate.MainProject.Models.Api.Common.User; +using BasicDotnetTemplate.MainProject.Models.Api.Common.Role; +using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer; +using BasicDotnetTemplate.MainProject.Models.Api.Response.Auth; + + +namespace BasicDotnetTemplate.MainProject.Tests; + +[TestClass] +public class AuthController_Tests +{ + [TestMethod] + public void AuthController_NullConfiguration() + { + Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development"); + var exception = true; + try + { + var authServiceMock = new Mock(); + _ = new AuthController(null, authServiceMock.Object); + exception = false; + Assert.Fail($"This test should not pass as configuration is null"); + } + catch (Exception) + { + Assert.IsTrue(exception); + } + } + + + [TestMethod] + public async Task AuthenticateAsync_Should_Return_200_When_Successful() + { + IConfiguration configuration = TestUtils.CreateConfiguration(); + var authServiceMock = new Mock(); + var controller = new AuthController(configuration, authServiceMock.Object); + DatabaseSqlServer.User user = new DatabaseSqlServer.User() + { + Username = "test", + FirstName = "test", + LastName = "test", + Email = "test", + PasswordHash = "test", + Role = new DatabaseSqlServer.Role() + { + Name = "test" + } + }; + AuthenticatedUser authenticatedUser = new AuthenticatedUser(user); + + var request = new AuthenticateRequest { Data = new AuthenticateRequestData { Username = "user", Password = "pass" } }; + authServiceMock.Setup(s => s.AuthenticateAsync(It.IsAny())).ReturnsAsync(authenticatedUser); + ObjectResult result = (ObjectResult)(await controller.AuthenticateAsync(request)); + + var response = (BaseResponse)result.Value; + + Assert.IsTrue(result.StatusCode == 200); + Assert.IsTrue(response.Status == 200); + Assert.IsInstanceOfType(response.Data, typeof(AuthenticatedUser)); + } + +} diff --git a/MainProject.Tests/Controllers/VersionController_Tests.cs b/MainProject.Tests/Controllers/VersionController_Tests.cs index 5ddf205..27dbeff 100644 --- a/MainProject.Tests/Controllers/VersionController_Tests.cs +++ b/MainProject.Tests/Controllers/VersionController_Tests.cs @@ -15,10 +15,10 @@ using BasicDotnetTemplate.MainProject.Models.Settings; namespace BasicDotnetTemplate.MainProject.Tests; [TestClass] -public class BaseController_Tests +public class VersionController_Tests { [TestMethod] - public void BaseController_NullConfiguration() + public void VersionController_NullConfiguration() { Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development"); var exception = true; @@ -34,7 +34,6 @@ public class BaseController_Tests } } - [TestMethod] public void VersionController_GetVersion_Valid() { diff --git a/MainProject.Tests/MainProject.Tests.csproj b/MainProject.Tests/MainProject.Tests.csproj index f5104f0..c926d4b 100644 --- a/MainProject.Tests/MainProject.Tests.csproj +++ b/MainProject.Tests/MainProject.Tests.csproj @@ -12,8 +12,10 @@ + + diff --git a/MainProject/Controllers/AuthController.cs b/MainProject/Controllers/AuthController.cs index 6a0e990..bdf070f 100644 --- a/MainProject/Controllers/AuthController.cs +++ b/MainProject/Controllers/AuthController.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc; using BasicDotnetTemplate.MainProject.Core.Attributes; using BasicDotnetTemplate.MainProject.Models.Api.Request.Auth; using BasicDotnetTemplate.MainProject.Models.Api.Response; +using BasicDotnetTemplate.MainProject.Models.Api.Response.Auth; using BasicDotnetTemplate.MainProject.Services; namespace BasicDotnetTemplate.MainProject.Controllers @@ -21,7 +22,7 @@ namespace BasicDotnetTemplate.MainProject.Controllers } [HttpPost("authenticate")] - [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] diff --git a/MainProject/MainProject.csproj b/MainProject/MainProject.csproj index 3abde16..3f5eabd 100644 --- a/MainProject/MainProject.csproj +++ b/MainProject/MainProject.csproj @@ -32,6 +32,7 @@ + diff --git a/MainProject/Models/Api/Response/Auth/AuthenticateResponse.cs b/MainProject/Models/Api/Response/Auth/AuthenticateResponse.cs new file mode 100644 index 0000000..c02fd2d --- /dev/null +++ b/MainProject/Models/Api/Response/Auth/AuthenticateResponse.cs @@ -0,0 +1,9 @@ +using BasicDotnetTemplate.MainProject.Models.Api.Data.Auth; +using BasicDotnetTemplate.MainProject.Models.Api.Common.User; + +namespace BasicDotnetTemplate.MainProject.Models.Api.Response.Auth; + +public class AuthenticateResponse: BaseResponse +{ + public AuthenticateResponse(int status, string? message, AuthenticatedUser? data) : base(status, message, data) {} +} \ No newline at end of file