public async void AddLostDogSuccessfulForValidPostDogWithImage() { var shelterRepo = new Mock <IShelterRepository>(); var shelterDogRepo = new Mock <IShelterDogRepository>(); var security = new Mock <ISecurityService>(); var account = new Mock <IAccountService>(); using var memoryStream = new MemoryStream(new byte[] { 1, 2, 3, 4 }); var picture = new FormFile(memoryStream, 0, memoryStream.Length, "name", "filename") { Headers = new HeaderDictionary(), ContentType = "image/jpeg" }; var dogDto = new UploadShelterDogDto(); var dog = mapper.Map <ShelterDog>(dogDto); shelterDogRepo.Setup(o => o.AddShelterDog(It.IsAny <ShelterDog>())).Returns((ShelterDog d) => Task.FromResult(new RepositoryResponse <ShelterDog>() { Data = d })); security.Setup(s => s.IsPictureValid(It.IsAny <IFormFile>())).Returns((IFormFile f) => new ServiceResponse()); var service = new ShelterService(shelterRepo.Object, shelterDogRepo.Object, account.Object, security.Object, mapper, logger); Assert.True((await service.AddShelterDog(dogDto, picture)).Successful); }
public async Task <IActionResult> AddShelterDog(int shelterId, [ModelBinder(BinderType = typeof(JsonModelBinder))] UploadShelterDogDto dog, IFormFile picture) { if (picture is null) { return(BadRequest(new ControllerResponse() { Message = "No image was provided!", Successful = false })); } var tokenShelterId = int.Parse(User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier)?.Value); if (shelterId != tokenShelterId) { return(Unauthorized(new ControllerResponse() { Message = "Cannot add dogs to other shelters!", Successful = false })); } dog.ShelterId = tokenShelterId; var serviceResponse = await shelterService.AddShelterDog(dog, picture); var controllerResponse = mapper.Map <ControllerResponse <GetShelterDogDto> >(serviceResponse); return(StatusCode(serviceResponse.StatusCode, controllerResponse)); }
public async Task <ServiceResponse <GetShelterDogDto> > AddShelterDog(UploadShelterDogDto shelterDogDto, IFormFile picture) { var serviceResponse = new ServiceResponse <GetShelterDogDto>(); var shelterDog = mapper.Map <ShelterDog>(shelterDogDto); byte[] data; if (picture is null) { serviceResponse.Successful = false; serviceResponse.StatusCode = StatusCodes.Status400BadRequest; serviceResponse.Message = "No picture was provided!"; } else { var pictureValidationResult = securityService.IsPictureValid(picture); if (pictureValidationResult.Successful) { using (var ms = new MemoryStream()) { picture.CopyTo(ms); data = ms.ToArray(); } shelterDog.Picture = new PictureDog() { FileName = picture.FileName, FileType = picture.ContentType, Data = data }; var repoResponse = await shelterDogRepository.AddShelterDog(shelterDog); serviceResponse = mapper.Map <ServiceResponse <GetShelterDogDto> >(repoResponse); if (!serviceResponse.Successful) { serviceResponse.StatusCode = StatusCodes.Status400BadRequest; } } else { serviceResponse.Successful = false; serviceResponse.StatusCode = StatusCodes.Status400BadRequest; serviceResponse.Message = pictureValidationResult.Message; } } return(serviceResponse); }
public async void AddLostDogFailsForValidPostDogAndInvalidImage() { var shelterRepo = new Mock <IShelterRepository>(); var shelterDogRepo = new Mock <IShelterDogRepository>(); var security = new Mock <ISecurityService>(); var account = new Mock <IAccountService>(); var picture = new FormFile(null, 0, 0, "name", "filename"); var dogDto = new UploadShelterDogDto(); var dog = mapper.Map <ShelterDog>(dogDto); shelterDogRepo.Setup(o => o.AddShelterDog(dog)).Returns(Task.FromResult(new RepositoryResponse <ShelterDog>() { Data = dog })); security.Setup(s => s.IsPictureValid(It.IsAny <IFormFile>())).Returns((IFormFile f) => new ServiceResponse() { Successful = false }); var service = new ShelterService(shelterRepo.Object, shelterDogRepo.Object, account.Object, security.Object, mapper, logger); Assert.False((await service.AddShelterDog(dogDto, null)).Successful); }