Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

UsersControllerTest.cs 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using AutoMapper;
  2. using Diligent.WebAPI.Business.MappingProfiles;
  3. using Diligent.WebAPI.Contracts.DTOs;
  4. using Diligent.WebAPI.Contracts.DTOs.User;
  5. using Diligent.WebAPI.Contracts.Exceptions;
  6. using Diligent.WebAPI.Data.Entities;
  7. namespace Diligent.WebAPI.Tests.Controllers
  8. {
  9. public class UsersControllerTest
  10. {
  11. private IUserService _userService = Substitute.For<IUserService>();
  12. private readonly List<User> _users;
  13. private readonly User _user;
  14. private readonly IMapper _mapper;
  15. public UsersControllerTest()
  16. {
  17. _user = new User
  18. {
  19. Id = 1,
  20. PasswordHash = "AQAAAAEAACcQAAAAEJnWVhD/qftzqJq5XOUD0BxEBEwhd7vS46HeDD+9cwEsqO9ev9xEORJVjmFMASUGJg==",
  21. FirstName = "Dzenis",
  22. LastName = "Dzenis",
  23. UserName = "dzenis",
  24. NormalizedUserName = "DZENIS",
  25. Email = "dzenis@dilig.net",
  26. NormalizedEmail = "DZENIS@DILIG.NET",
  27. EmailConfirmed = false,
  28. IsEnabled = true,
  29. AccessFailedCount = 0,
  30. SecurityStamp = "2D3XPK2P5MAKO377AWFU3T4ZFFYTSOJX",
  31. ConcurrencyStamp = "2D3XPK2P5MAKO377AWFU3T4ZFFYTSOJX",
  32. };
  33. _users = new List<User>
  34. {
  35. _user,
  36. };
  37. // configure mapper
  38. var configuration = new MapperConfiguration(cfg => cfg.AddProfiles(
  39. new List<Profile>
  40. {
  41. new UserMappingProfile(),
  42. }));
  43. _mapper = new Mapper(configuration);
  44. }
  45. [Fact]
  46. public async Task GetUsers_ShouldReturn_200OK()
  47. {
  48. _userService.GetAll().Returns(_users);
  49. UsersController usersController = new(_userService, _mapper);
  50. var result = await usersController.GetAll();
  51. (result as OkObjectResult).StatusCode.Should().Be(200);
  52. }
  53. [Fact]
  54. public async Task GetUserById_ShouldReturn_200OK()
  55. {
  56. _userService.GetById(Arg.Any<int>()).Returns(_user);
  57. UsersController usersController = new(_userService, _mapper);
  58. var result = await usersController.GetUser(1);
  59. (result as OkObjectResult).StatusCode.Should().Be(200);
  60. }
  61. [Fact]
  62. public async Task GetUserById_ShouldReturn404_WhenUserDoesNotExist()
  63. {
  64. _userService.When(x => x.GetById(Arg.Any<int>()))
  65. .Do(x => { throw new EntityNotFoundException(); });
  66. UsersController usersController = new(_userService, _mapper);
  67. await Assert.ThrowsAsync<EntityNotFoundException>(() => usersController.GetUser(15));
  68. }
  69. [Fact]
  70. public async Task InviteUser_ShouldReturn200_WhenUserDoesNotExist()
  71. {
  72. _userService.SendRegistrationLink(Arg.Any<InviteDTO>()).Returns(x => new ServiceResponseDTO<object>
  73. {
  74. Data = new { Message = "Link has been sent!" }
  75. });
  76. UsersController usersController = new(_userService, _mapper);
  77. var result = await usersController.InviteUser(new InviteDTO());
  78. (result as OkObjectResult).StatusCode.Should().Be(200);
  79. }
  80. [Fact]
  81. public async Task InviteUser_ShouldReturn400_WhenUserExists()
  82. {
  83. _userService.SendRegistrationLink(Arg.Any<InviteDTO>()).Returns(x => new ServiceResponseDTO<object>
  84. {
  85. IsError = true,
  86. });
  87. UsersController usersController = new(_userService, _mapper);
  88. var result = await usersController.InviteUser(new InviteDTO());
  89. (result as BadRequestObjectResult).StatusCode.Should().Be(400);
  90. }
  91. [Fact]
  92. public async Task DeleteUser_ShouldReturn200_WhenUserExists()
  93. {
  94. _userService.GetById(Arg.Any<int>()).Returns(_user);
  95. UsersController usersController = new(_userService, _mapper);
  96. var result = await usersController.DeleteUser(1);
  97. (result as ObjectResult).StatusCode.Should().Be(200);
  98. }
  99. [Fact]
  100. public async Task DeleteUser_ShouldReturn400_WhenUserDoesNotExist()
  101. {
  102. _userService.When(x => x.GetById(Arg.Any<int>()))
  103. .Do(x => { throw new EntityNotFoundException(); });
  104. UsersController usersController = new(_userService, _mapper);
  105. await Assert.ThrowsAsync<EntityNotFoundException>(() => usersController.DeleteUser(15));
  106. }
  107. [Fact]
  108. public async Task ToggleEnable_ShouldReturn400_WhenUserDoesNotExist()
  109. {
  110. _userService.When(x => x.GetById(Arg.Any<int>()))
  111. .Do(x => { throw new EntityNotFoundException(); });
  112. UsersController usersController = new(_userService, _mapper);
  113. await Assert.ThrowsAsync<EntityNotFoundException>(() => usersController.ToggleEnable(15));
  114. }
  115. [Fact]
  116. public async Task ToggleEnable_ShouldReturnOk_WhenUserExists()
  117. {
  118. _userService.GetById(Arg.Any<int>()).Returns(_user);
  119. UsersController usersController = new(_userService, _mapper);
  120. var res = await usersController.DeleteUser(15);
  121. (res as ObjectResult).StatusCode.Should().Be(200);
  122. }
  123. }
  124. }