Added UserService tests
This commit is contained in:
77
MainProject.Tests/Services/UserService_Tests.cs
Normal file
77
MainProject.Tests/Services/UserService_Tests.cs
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using BasicDotnetTemplate.MainProject.Utils;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Moq;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||||
|
using BasicDotnetTemplate.MainProject.Services;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||||
|
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 BasicDotnetTemplate.MainProject.Models.Api.Response.Auth;
|
||||||
|
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public class UserService_Tests
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void Inizialize()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var userService = TestUtils.CreateUserService();
|
||||||
|
if(userService != null)
|
||||||
|
{
|
||||||
|
Assert.IsInstanceOfType(userService, typeof(UserService));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"UserService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task GetUsers()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var userService = TestUtils.CreateUserService();
|
||||||
|
var testString = "test";
|
||||||
|
if(userService != null)
|
||||||
|
{
|
||||||
|
var user = await userService.GetUserByUsernameAndPassword(testString, testString);
|
||||||
|
Assert.IsTrue(user == null);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"UserService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -52,6 +52,15 @@ public static class TestUtils
|
|||||||
var userServiceMock = new Mock<IUserService>();
|
var userServiceMock = new Mock<IUserService>();
|
||||||
return new AuthService(configuration, sqlServerContext, userServiceMock.Object);
|
return new AuthService(configuration, sqlServerContext, userServiceMock.Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static UserService CreateUserService()
|
||||||
|
{
|
||||||
|
IConfiguration configuration = CreateConfiguration();
|
||||||
|
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
||||||
|
optionsBuilder.UseSqlServer("test");
|
||||||
|
SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options);
|
||||||
|
return new UserService(configuration, sqlServerContext);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -27,9 +27,7 @@ public class UserService : BaseService, IUserService
|
|||||||
|
|
||||||
private IQueryable<User> GetUserByUsername(string username)
|
private IQueryable<User> GetUserByUsername(string username)
|
||||||
{
|
{
|
||||||
return this._sqlServerContext.Users
|
return this.GetUsers().Where(x =>
|
||||||
.Where(x =>
|
|
||||||
!x.IsDeleted &&
|
|
||||||
String.Equals(x.Username, username, StringComparison.CurrentCultureIgnoreCase)
|
String.Equals(x.Username, username, StringComparison.CurrentCultureIgnoreCase)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user