|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- using AutoMapper;
- using Diligent.WebAPI.Business.MappingProfiles;
- using Diligent.WebAPI.Contracts.DTOs;
- using Diligent.WebAPI.Contracts.DTOs.User;
- using Diligent.WebAPI.Contracts.Exceptions;
- using Diligent.WebAPI.Data.Entities;
-
- namespace Diligent.WebAPI.Tests.Controllers
- {
- public class UsersControllerTest
- {
- private IUserService _userService = Substitute.For<IUserService>();
- private readonly List<User> _users;
- private readonly User _user;
- private readonly IMapper _mapper;
-
- public UsersControllerTest()
- {
- _user = new User
- {
- Id = 1,
- PasswordHash = "AQAAAAEAACcQAAAAEJnWVhD/qftzqJq5XOUD0BxEBEwhd7vS46HeDD+9cwEsqO9ev9xEORJVjmFMASUGJg==",
- FirstName = "Dzenis",
- LastName = "Dzenis",
- UserName = "dzenis",
- NormalizedUserName = "DZENIS",
- Email = "dzenis@dilig.net",
- NormalizedEmail = "DZENIS@DILIG.NET",
- EmailConfirmed = false,
- IsEnabled = true,
- AccessFailedCount = 0,
- SecurityStamp = "2D3XPK2P5MAKO377AWFU3T4ZFFYTSOJX",
- ConcurrencyStamp = "2D3XPK2P5MAKO377AWFU3T4ZFFYTSOJX",
- };
-
- _users = new List<User>
- {
- _user,
- };
-
- // configure mapper
- var configuration = new MapperConfiguration(cfg => cfg.AddProfiles(
- new List<Profile>
- {
- new UserMappingProfile(),
- }));
- _mapper = new Mapper(configuration);
- }
-
- [Fact]
- public async Task GetUsers_ShouldReturn_200OK()
- {
- _userService.GetAll().Returns(_users);
- UsersController usersController = new(_userService, _mapper);
-
- var result = await usersController.GetAll();
-
- (result as OkObjectResult).StatusCode.Should().Be(200);
- }
-
- [Fact]
- public async Task GetUserById_ShouldReturn_200OK()
- {
- _userService.GetById(Arg.Any<int>()).Returns(_user);
- UsersController usersController = new(_userService, _mapper);
-
- var result = await usersController.GetUser(1);
-
- (result as OkObjectResult).StatusCode.Should().Be(200);
- }
-
- [Fact]
- public async Task GetUserById_ShouldReturn404_WhenUserDoesNotExist()
- {
- _userService.When(x => x.GetById(Arg.Any<int>()))
- .Do(x => { throw new EntityNotFoundException(); });
-
- UsersController usersController = new(_userService, _mapper);
-
- await Assert.ThrowsAsync<EntityNotFoundException>(() => usersController.GetUser(15));
- }
-
- [Fact]
- public async Task InviteUser_ShouldReturn200_WhenUserDoesNotExist()
- {
- _userService.SendRegistrationLink(Arg.Any<InviteDTO>()).Returns(x => new ServiceResponseDTO<object>
- {
- Data = new { Message = "Link has been sent!" }
- });
-
- UsersController usersController = new(_userService, _mapper);
-
- var result = await usersController.InviteUser(new InviteDTO());
-
- (result as OkObjectResult).StatusCode.Should().Be(200);
- }
-
- [Fact]
- public async Task InviteUser_ShouldReturn400_WhenUserExists()
- {
- _userService.SendRegistrationLink(Arg.Any<InviteDTO>()).Returns(x => new ServiceResponseDTO<object>
- {
- IsError = true,
- });
-
- UsersController usersController = new(_userService, _mapper);
-
- var result = await usersController.InviteUser(new InviteDTO());
-
- (result as BadRequestObjectResult).StatusCode.Should().Be(400);
- }
-
- [Fact]
- public async Task DeleteUser_ShouldReturn200_WhenUserExists()
- {
- _userService.GetById(Arg.Any<int>()).Returns(_user);
- UsersController usersController = new(_userService, _mapper);
-
- var result = await usersController.DeleteUser(1);
-
- (result as ObjectResult).StatusCode.Should().Be(200);
- }
-
- [Fact]
- public async Task DeleteUser_ShouldReturn400_WhenUserDoesNotExist()
- {
- _userService.When(x => x.GetById(Arg.Any<int>()))
- .Do(x => { throw new EntityNotFoundException(); });
-
- UsersController usersController = new(_userService, _mapper);
-
- await Assert.ThrowsAsync<EntityNotFoundException>(() => usersController.DeleteUser(15));
- }
-
- [Fact]
- public async Task ToggleEnable_ShouldReturn400_WhenUserDoesNotExist()
- {
- _userService.When(x => x.GetById(Arg.Any<int>()))
- .Do(x => { throw new EntityNotFoundException(); });
-
- UsersController usersController = new(_userService, _mapper);
-
- await Assert.ThrowsAsync<EntityNotFoundException>(() => usersController.ToggleEnable(15));
- }
-
- [Fact]
- public async Task ToggleEnable_ShouldReturnOk_WhenUserExists()
- {
- _userService.GetById(Arg.Any<int>()).Returns(_user);
-
- UsersController usersController = new(_userService, _mapper);
-
- var res = await usersController.DeleteUser(15);
-
- (res as ObjectResult).StatusCode.Should().Be(200);
- }
- }
- }
|