Merge pull request 'fixUserEntity' (#47) from fixUserEntity into master
continuous-integration/drone/push Build is passing Details

Reviewed-on: #47
pull/48/head
Victor GABORIT 1 year ago
commit dd44cf2842

@ -157,6 +157,16 @@ namespace API.Controllers
return BadRequest(); return BadRequest();
} }
if (userService.IsEmailTaken(dto.Email))
{
return BadRequest("Email déjà utilisé");
}
if (userService.IsUsernameTaken(dto.Username))
{
return BadRequest("Username déjà utilisé");
}
// return Ok(userService.CreateUser(username, password, email, isAdmin)); // return Ok(userService.CreateUser(username, password, email, isAdmin));
logger.LogInformation( logger.LogInformation(
"[INFORMATION] Un utilisateur a été créé : username - {username}, password - {password}, email - {email}, isAdmin - {isAdmin}", "[INFORMATION] Un utilisateur a été créé : username - {username}, password - {password}, email - {email}, isAdmin - {isAdmin}",

@ -38,4 +38,8 @@ public class UserDataServiceApi(IUserService<UserEntity> userService) : IUserSer
public UserDto CreateUser(string username, string password, string email, bool isAdmin) => public UserDto CreateUser(string username, string password, string email, bool isAdmin) =>
userService.CreateUser(username, password, email, isAdmin).FromEntityToDto(); userService.CreateUser(username, password, email, isAdmin).FromEntityToDto();
public bool IsEmailTaken(string email) => userService.IsEmailTaken(email);
public bool IsUsernameTaken(string username) => userService.IsUsernameTaken(username);
} }

@ -179,4 +179,16 @@ public class UserDataService : IUserService<UserEntity>
DbContext.SaveChangesAsync(); DbContext.SaveChangesAsync();
return newUserEntity; return newUserEntity;
} }
public bool IsEmailTaken(string email)
{
var isEmail = DbContext.Users.Any(u => u.Email == email);
return isEmail;
}
public bool IsUsernameTaken(string username)
{
var isUsername = DbContext.Users.Any(u => u.Username == username);
return isUsername;
}
} }

@ -1,8 +1,11 @@
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace Entities; namespace Entities;
[Table("User")] [Table("User")]
[Index(nameof(Username), IsUnique = true)]
[Index(nameof(Email), IsUnique = true)]
public class UserEntity public class UserEntity
{ {
public int Id { get; set; } public int Id { get; set; }

@ -14,5 +14,7 @@ namespace Shared
public bool DeleteUserByUsername(string username); public bool DeleteUserByUsername(string username);
public TUser UpdateUser(int id, TUser user); public TUser UpdateUser(int id, TUser user);
public TUser CreateUser(string username, string password, string email, bool isAdmin); public TUser CreateUser(string username, string password, string email, bool isAdmin);
public bool IsEmailTaken(string email);
public bool IsUsernameTaken(string username);
} }
} }

@ -285,6 +285,36 @@ public class UserUnitTest
Assert.Equal(400, bdObjectResult.StatusCode); Assert.Equal(400, bdObjectResult.StatusCode);
} }
} }
[Fact]
public void CreateUserWithExistingEmail()
{
GetUsersData();
_userService.Setup(x => x.CreateUser("Nom", "Passssss", "adressemail@gmail.com", true))
.Returns(new UserDto("Nom", "Passssss", "adressemail@gmail.com", true));
var usersController = new UsersController(new NullLogger<UsersController>(), _userService.Object);
var userResult = usersController.CreateUser(new UserDto(null, "Passssss", "adressemail@gmail.com", true));
if (userResult is BadRequestResult bdObjectResult)
{
Assert.Equal(400, bdObjectResult.StatusCode);
}
}
[Fact]
public void CreateUserWithExistingUsername()
{
GetUsersData();
_userService.Setup(x => x.CreateUser("Useruser", "Passssss", "heudfk@hdye.com", true))
.Returns(new UserDto("Useruser", "Passssss", "heudfk@hdye.com", true));
var usersController = new UsersController(new NullLogger<UsersController>(), _userService.Object);
var userResult = usersController.CreateUser(new UserDto("Useruser", "Passssss", "heudfk@hdye.com", true));
if (userResult is BadRequestResult bdObjectResult)
{
Assert.Equal(400, bdObjectResult.StatusCode);
}
}
[Fact] [Fact]
public void UpdateUserSuccess() public void UpdateUserSuccess()

@ -26,24 +26,24 @@ public class TestBlackListDataService
[Fact] [Fact]
public void BanUser_Success() public void BanUser_Success()
{ {
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test101", Email = "example101@email.com", Password = "password", IsAdmin = true });
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test102", Email = "example102@email.com", Password = "password", IsAdmin = false });
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test3", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test103", Email = "example103@email.com", Password = "password", IsAdmin = true });
_dbContext.SaveChanges(); _dbContext.SaveChanges();
var banResult = _blackListDataService.BanUser("Test1"); var banResult = _blackListDataService.BanUser("Test101");
Assert.True(banResult); Assert.True(banResult);
} }
[Fact] [Fact]
public void GetNbBannedUsers() public void GetNbBannedUsers()
{ {
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example1@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test61", Email = "example61@email.com", Password = "password", IsAdmin = true });
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example2@email.com", Password = "password", IsAdmin = false }); _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test62", Email = "example62@email.com", Password = "password", IsAdmin = false });
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test3", Email = "example3@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test63", Email = "example63@email.com", Password = "password", IsAdmin = true });
_dbContext.SaveChanges(); _dbContext.SaveChanges();
var banResult1 = _blackListDataService.BanUser("Test1"); var banResult1 = _blackListDataService.BanUser("Test61");
var banResult2 = _blackListDataService.BanUser("Test2"); var banResult2 = _blackListDataService.BanUser("Test62");
var banResult3 = _blackListDataService.BanUser("Test3"); var banResult3 = _blackListDataService.BanUser("Test63");
Assert.True(banResult1); Assert.True(banResult1);
Assert.True(banResult2); Assert.True(banResult2);
Assert.True(banResult3); Assert.True(banResult3);
@ -53,9 +53,9 @@ public class TestBlackListDataService
[Fact] [Fact]
public void BanUser_Fail() public void BanUser_Fail()
{ {
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test71", Email = "example71@email.com", Password = "password", IsAdmin = true });
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test72", Email = "example72@email.com", Password = "password", IsAdmin = false });
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test3", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test73", Email = "example73@email.com", Password = "password", IsAdmin = true });
_dbContext.SaveChanges(); _dbContext.SaveChanges();
var banResult = _blackListDataService.BanUser("Test42"); var banResult = _blackListDataService.BanUser("Test42");
Assert.False(banResult); Assert.False(banResult);
@ -64,24 +64,24 @@ public class TestBlackListDataService
[Fact] [Fact]
public void IsBanned_Success() public void IsBanned_Success()
{ {
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example1@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test81", Email = "example81@email.com", Password = "password", IsAdmin = true });
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test82", Email = "example82@email.com", Password = "password", IsAdmin = false });
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test3", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test83", Email = "example83@email.com", Password = "password", IsAdmin = true });
_dbContext.SaveChanges(); _dbContext.SaveChanges();
var banResult = _blackListDataService.BanUser("Test1"); var banResult = _blackListDataService.BanUser("Test81");
Assert.True(banResult); Assert.True(banResult);
Assert.NotNull(_blackListDataService.GetUserBannedByEmail("example1@email.com")); Assert.NotNull(_blackListDataService.GetUserBannedByEmail("example81@email.com"));
} }
[Fact] [Fact]
public void UnbanUser_Success() public void UnbanUser_Success()
{ {
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example1@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test91", Email = "example91@email.com", Password = "password", IsAdmin = true });
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test92", Email = "example92@email.com", Password = "password", IsAdmin = false });
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test3", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test93", Email = "example93@email.com", Password = "password", IsAdmin = true });
_dbContext.SaveChanges(); _dbContext.SaveChanges();
var banResult = _blackListDataService.BanUser("Test1"); var banResult = _blackListDataService.BanUser("Test91");
Assert.True(banResult); Assert.True(banResult);
Assert.True(_blackListDataService.UnbanUser("example1@email.com")); Assert.True(_blackListDataService.UnbanUser("example91@email.com"));
} }
} }

@ -27,9 +27,9 @@ public class TestUserDataService
[Fact] [Fact]
public void GetUsers_ReturnsCorrectNumberOfUsers() public void GetUsers_ReturnsCorrectNumberOfUsers()
{ {
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test10", Email = "example1@email.com", Password = "password", IsAdmin = true });
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test12", Email = "example2@email.com", Password = "password", IsAdmin = false });
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test3", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test13", Email = "example3@email.com", Password = "password", IsAdmin = true });
_dbContext.SaveChanges(); _dbContext.SaveChanges();
var result = _userDataService.GetUsers(1, 2, UserOrderCriteria.None); var result = _userDataService.GetUsers(1, 2, UserOrderCriteria.None);
Assert.Equal(2, result.Count()); Assert.Equal(2, result.Count());
@ -38,9 +38,9 @@ public class TestUserDataService
[Fact] [Fact]
public void GetNotAdminUsers_ReturnsCorrectNumberOfUsers() public void GetNotAdminUsers_ReturnsCorrectNumberOfUsers()
{ {
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test14", Email = "example4@email.com", Password = "password", IsAdmin = true });
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test15", Email = "example5@email.com", Password = "password", IsAdmin = false });
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test3", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test16", Email = "example6@email.com", Password = "password", IsAdmin = true });
_dbContext.SaveChanges(); _dbContext.SaveChanges();
var result = _userDataService.GetNotAdminUsers(1, 2, UserOrderCriteria.None); var result = _userDataService.GetNotAdminUsers(1, 2, UserOrderCriteria.None);
Assert.Equal(1, result.Count()); Assert.Equal(1, result.Count());
@ -49,9 +49,9 @@ public class TestUserDataService
[Fact] [Fact]
public void GetNotAdminUsers_BadPage_ReturnsCorrectNumberOfUsers() public void GetNotAdminUsers_BadPage_ReturnsCorrectNumberOfUsers()
{ {
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test17", Email = "example7@email.com", Password = "password", IsAdmin = true });
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test18", Email = "example8@email.com", Password = "password", IsAdmin = false });
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test3", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test19", Email = "example9@email.com", Password = "password", IsAdmin = true });
_dbContext.SaveChanges(); _dbContext.SaveChanges();
var result = _userDataService.GetNotAdminUsers(-42, 2, UserOrderCriteria.None); var result = _userDataService.GetNotAdminUsers(-42, 2, UserOrderCriteria.None);
Assert.Equal(1, result.Count()); Assert.Equal(1, result.Count());
@ -60,9 +60,9 @@ public class TestUserDataService
[Fact] [Fact]
public void GetNotAdminUsers_BadNumber_ReturnsCorrectNumberOfUsers() public void GetNotAdminUsers_BadNumber_ReturnsCorrectNumberOfUsers()
{ {
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test20", Email = "example10@email.com", Password = "password", IsAdmin = true });
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test21", Email = "example11@email.com", Password = "password", IsAdmin = false });
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test3", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test22", Email = "example12@email.com", Password = "password", IsAdmin = true });
_dbContext.SaveChanges(); _dbContext.SaveChanges();
var result = _userDataService.GetNotAdminUsers(1, -42, UserOrderCriteria.None); var result = _userDataService.GetNotAdminUsers(1, -42, UserOrderCriteria.None);
Assert.Equal(1, result.Count()); Assert.Equal(1, result.Count());
@ -71,9 +71,9 @@ public class TestUserDataService
[Fact] [Fact]
public void GetNotAdminUsers_OrderedById_ReturnsCorrectNumberOfUsers() public void GetNotAdminUsers_OrderedById_ReturnsCorrectNumberOfUsers()
{ {
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test23", Email = "example13@email.com", Password = "password", IsAdmin = true });
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test24", Email = "example14@email.com", Password = "password", IsAdmin = false });
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test3", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test51", Email = "example15@email.com", Password = "password", IsAdmin = true });
_dbContext.SaveChanges(); _dbContext.SaveChanges();
var result = _userDataService.GetNotAdminUsers(1, 2, UserOrderCriteria.ById); var result = _userDataService.GetNotAdminUsers(1, 2, UserOrderCriteria.ById);
Assert.Equal(1, result.Count()); Assert.Equal(1, result.Count());
@ -82,9 +82,9 @@ public class TestUserDataService
[Fact] [Fact]
public void GetNotAdminUsers_OrderedByUsername_ReturnsCorrectNumberOfUsers() public void GetNotAdminUsers_OrderedByUsername_ReturnsCorrectNumberOfUsers()
{ {
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test25", Email = "example16@email.com", Password = "password", IsAdmin = true });
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test26", Email = "example17@email.com", Password = "password", IsAdmin = false });
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test3", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test27", Email = "example18@email.com", Password = "password", IsAdmin = true });
_dbContext.SaveChanges(); _dbContext.SaveChanges();
var result = _userDataService.GetNotAdminUsers(1, 2, UserOrderCriteria.ByUsername); var result = _userDataService.GetNotAdminUsers(1, 2, UserOrderCriteria.ByUsername);
Assert.Equal(1, result.Count()); Assert.Equal(1, result.Count());
@ -93,9 +93,9 @@ public class TestUserDataService
[Fact] [Fact]
public void GetNotAdminUsers_OrderedByEmail_ReturnsCorrectNumberOfUsers() public void GetNotAdminUsers_OrderedByEmail_ReturnsCorrectNumberOfUsers()
{ {
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test52", Email = "example45@email.com", Password = "password", IsAdmin = true });
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test53", Email = "example46@email.com", Password = "password", IsAdmin = false });
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test3", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test54", Email = "example17@email.com", Password = "password", IsAdmin = true });
_dbContext.SaveChanges(); _dbContext.SaveChanges();
var result = _userDataService.GetNotAdminUsers(1, 2, UserOrderCriteria.ByEmail); var result = _userDataService.GetNotAdminUsers(1, 2, UserOrderCriteria.ByEmail);
Assert.Equal(1, result.Count()); Assert.Equal(1, result.Count());
@ -104,9 +104,9 @@ public class TestUserDataService
[Fact] [Fact]
public void GetNotAdminUsers_OrderedByIsAdmin_ReturnsCorrectNumberOfUsers() public void GetNotAdminUsers_OrderedByIsAdmin_ReturnsCorrectNumberOfUsers()
{ {
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test28", Email = "example19@email.com", Password = "password", IsAdmin = true });
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test29", Email = "example20@email.com", Password = "password", IsAdmin = false });
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test3", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test30", Email = "example21@email.com", Password = "password", IsAdmin = true });
_dbContext.SaveChanges(); _dbContext.SaveChanges();
var result = _userDataService.GetNotAdminUsers(1, 2, UserOrderCriteria.ByIsAdmin); var result = _userDataService.GetNotAdminUsers(1, 2, UserOrderCriteria.ByIsAdmin);
Assert.Equal(1, result.Count()); Assert.Equal(1, result.Count());
@ -115,9 +115,9 @@ public class TestUserDataService
[Fact] [Fact]
public void GetNumberOfUsers_ReturnsCorrectNumberOfUsers() public void GetNumberOfUsers_ReturnsCorrectNumberOfUsers()
{ {
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test31", Email = "example22@email.com", Password = "password", IsAdmin = true });
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test32", Email = "example23@email.com", Password = "password", IsAdmin = false });
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test3", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test33", Email = "example24@email.com", Password = "password", IsAdmin = true });
_dbContext.SaveChanges(); _dbContext.SaveChanges();
var result = _userDataService.GetNumberOfUsers(); var result = _userDataService.GetNumberOfUsers();
Assert.Equal(3, result); Assert.Equal(3, result);
@ -126,29 +126,29 @@ public class TestUserDataService
[Fact] [Fact]
public void GetUserById_ReturnsCorrectUser() public void GetUserById_ReturnsCorrectUser()
{ {
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test34", Email = "example25@email.com", Password = "password", IsAdmin = true });
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test35", Email = "example26@email.com", Password = "password", IsAdmin = false });
_dbContext.SaveChanges(); _dbContext.SaveChanges();
var result = _userDataService.GetUserById(1); var result = _userDataService.GetUserById(1);
Assert.Equal("Test1", result.Username); Assert.Equal("Test34", result.Username);
} }
[Fact] [Fact]
public void GetUserByUsername_ReturnsCorrectUser() public void GetUserByUsername_ReturnsCorrectUser()
{ {
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test36", Email = "example27@email.com", Password = "password", IsAdmin = true });
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test37", Email = "example28@email.com", Password = "password", IsAdmin = false });
_dbContext.SaveChanges(); _dbContext.SaveChanges();
var result = _userDataService.GetUserByUsername("Test1"); var result = _userDataService.GetUserByUsername("Test36");
Assert.Equal(1, result.Id); Assert.Equal(1, result.Id);
} }
[Fact] [Fact]
public void GetUserByEmail_ReturnsCorrectUser() public void GetUserByEmail_ReturnsCorrectUser()
{ {
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test38", Email = "example29@email.com", Password = "password", IsAdmin = true });
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test39", Email = "example30@email.com", Password = "password", IsAdmin = false });
_dbContext.SaveChanges(); _dbContext.SaveChanges();
var result = _userDataService.GetUserByEmail("example@email.com"); var result = _userDataService.GetUserByEmail("example29@email.com");
Assert.Equal(1, result.Id); Assert.Equal(1, result.Id);
} }
@ -196,8 +196,8 @@ public class TestUserDataService
[Fact] [Fact]
public void GetUsers_WithBadPage_ReturnsClassicUsers() public void GetUsers_WithBadPage_ReturnsClassicUsers()
{ {
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test40", Email = "example31@email.com", Password = "password", IsAdmin = true });
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test41", Email = "example32@email.com", Password = "password", IsAdmin = false });
_dbContext.SaveChanges(); _dbContext.SaveChanges();
var result = _userDataService.GetUsers(-1, 2, UserOrderCriteria.None); var result = _userDataService.GetUsers(-1, 2, UserOrderCriteria.None);
@ -208,8 +208,8 @@ public class TestUserDataService
[Fact] [Fact]
public void GetUsers_WithBadNumber_ReturnsClassicUsers() public void GetUsers_WithBadNumber_ReturnsClassicUsers()
{ {
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test43", Email = "example33@email.com", Password = "password", IsAdmin = true });
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test44", Email = "example34@email.com", Password = "password", IsAdmin = false });
_dbContext.SaveChanges(); _dbContext.SaveChanges();
var result = _userDataService.GetUsers(1, -42, UserOrderCriteria.None); var result = _userDataService.GetUsers(1, -42, UserOrderCriteria.None);
@ -253,6 +253,26 @@ public class TestUserDataService
Assert.False(result); Assert.False(result);
} }
[Fact]
public void isEmailTaken_ReturnFalse()
{
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test81", Email = "example81@email.com", Password = "password", IsAdmin = true });
_dbContext.SaveChanges();
var result = _userDataService.IsEmailTaken("example895@email.com");
Assert.False(result);
}
[Fact]
public void isEmailTaken_ReturnTrue()
{
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test82", Email = "example82@email.com", Password = "password", IsAdmin = true });
_dbContext.SaveChanges();
var result = _userDataService.IsEmailTaken("example82@email.com");
Assert.True(result);
}
[Fact] [Fact]
public void DeleteUserByUsername_WithoutExisting_ReturnsFalse() public void DeleteUserByUsername_WithoutExisting_ReturnsFalse()

Loading…
Cancel
Save