Adding AuthController tests - wip
This commit is contained in:
80
MainProject.Tests/Controllers/AuthController_Tests.cs
Normal file
80
MainProject.Tests/Controllers/AuthController_Tests.cs
Normal file
@@ -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<IAuthService>();
|
||||
_ = 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<IAuthService>();
|
||||
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<AuthenticateRequestData>())).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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -12,8 +12,10 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -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<string>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<AuthenticateResponse>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<BaseResponse>(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType<BaseResponse>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<BaseResponse>(StatusCodes.Status500InternalServerError)]
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
<PackageReference Include="Microsoft.Identity.Web" Version="3.7.1" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.28.0" />
|
||||
<PackageReference Include="MongoDB.EntityFrameworkCore" Version="8.1.0" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="NLog" Version="5.2.8" />
|
||||
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
|
||||
|
||||
@@ -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) {}
|
||||
}
|
||||
Reference in New Issue
Block a user