public async Task <IActionResult> Add([FromRoute] int residentialId, [FromBody] HouseToAdd house) { if (!ModelState.IsValid) { return(BadRequest()); } if (!await _residentialManager.ExistsAsync(residentialId)) { return(NotFound(_localizer.GetValue(LocalizationMessage.ResidentialNotFound))); } var loggedUser = User.GetUserId(); if (!await _userManager.IsAdminAsync(residentialId, loggedUser)) { return(Forbid(_localizer.GetValue(LocalizationMessage.YouDoNotHavePermissionToAccessThisResource))); } if (await _houseManager.ExistsAsync(residentialId, house.Name)) { return(BadRequest(_localizer.GetValue(LocalizationMessage.NameOfHouseAlreadyExists))); } await _houseManager.AddAsync(house); return(NoContent()); }
public async Task Should_Call_AddAsync() { //Arrange const string name = "House Name"; var model = new HouseToAdd { Name = name }; var entity = new HouseEf { Name = name }; _mapperMock.Setup(x => x.Map <HouseEf>(It.IsAny <HouseToAdd>())) .Returns(entity); _manager = new HouseManager(_mapperMock.Object, _houseRepositoryMock.Object); //Act await _manager.AddAsync(model); //Assert _mapperMock.Verify(x => x.Map <HouseEf>(It.IsAny <HouseToAdd>())); _houseRepositoryMock.Verify(x => x.AddAsync(entity), Times.Once); }
public async Task Should_Add_ReturnsABadRequestResult() { //Arrange const int userId = 2; const int residentialId = 3; var houseModel = new HouseToAdd(); _controller = new HouseController(_userManagerMock.Object, _houseManagerMock.Object, _residentialManagerMock.Object, _localizerMock.Object); _controller.ValidateViewModel(houseModel); //Act var result = await _controller.Add(residentialId, houseModel); //Assert _userManagerMock.Verify(x => x.IsAdminAsync(residentialId, userId), Times.Never); _residentialManagerMock.Verify(x => x.ExistsAsync(residentialId), Times.Never); _houseManagerMock.Verify(x => x.AddAsync(houseModel), Times.Never); Assert.IsType <BadRequestResult>(result); }
public async Task Should_Add_ReturnsANotContentResult() { //Arrange const int userId = 2; const int residentialId = 3; var houseModel = new HouseToAdd { Name = "House Name", Street = "Street Name", ResidentialId = residentialId }; _userManagerMock.Setup(x => x.IsAdminAsync(residentialId, userId)) .ReturnsAsync(true); _residentialManagerMock.Setup(x => x.ExistsAsync(residentialId)) .ReturnsAsync(true); var claims = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(Business.Utils.ClaimsCustomTypes.Id, userId + ""), new Claim(Business.Utils.ClaimsCustomTypes.RoleId, ((int)RoleEnum.Admin) + "") })); var controllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext { User = claims } }; _controller = new HouseController(_userManagerMock.Object, _houseManagerMock.Object, _residentialManagerMock.Object, _localizerMock.Object) { ControllerContext = controllerContext }; _controller.ValidateViewModel(houseModel); //Act var result = await _controller.Add(residentialId, houseModel); //Assert _userManagerMock.Verify(x => x.IsAdminAsync(residentialId, userId), Times.Once); _residentialManagerMock.Verify(x => x.ExistsAsync(residentialId), Times.Once); _houseManagerMock.Verify(x => x.AddAsync(houseModel), Times.Once); }
private static HouseEf ConvertFrom_HouseToAdd_To_Entity(HouseToAdd model, HouseEf entity) { var instance = entity ?? new HouseEf(); instance.IsActive = true; instance.CreationDate = DateTime.Now; instance.Name = model.Name.RemoveSpace(); instance.Street = model.Street.RemoveSpace(); instance.ResidentialId = model.ResidentialId; instance.OutdoorNumber = model.OutdoorNumber.RemoveSpace(); instance.InteriorNumber = model.InteriorNumber.RemoveSpace(); instance.HouseUsers = model.Users? .Select(user => new HouseUserEf { UserId = user.Id }).ToList(); return(instance); }
public async Task AddAsync(HouseToAdd model) { var entity = _mapper.Map <HouseEf>(model); await _repository.AddAsync(entity); }