public BusinessController(IRepositoryWrapper repo, IAuthorizationService authorizationService, UserManager <User> userManager, UploadImageService uploadImageService) { _repo = repo; _authorizationService = authorizationService; _userManager = userManager; _uploadImageService = uploadImageService; }
public async void UploadedImages() { // arrange var imageWriter = new ImageWriter(); var imageService = new UploadImageService(imageWriter); var controller = new ImagesController(imageService); var path = Directory.GetCurrentDirectory() + @"/Resources/test.png"; IActionResult result; // act using (var stream = File.OpenRead(path)) { result = await controller.UploadImage(new FormCollection(null, new FormFileCollection { new FormFile(stream, 0, stream.Length, null, Path.GetFileName(path)) { Headers = new HeaderDictionary(), ContentType = "multipart/form-data" } })); } // assert var okObjectResult = result as OkObjectResult; Assert.NotNull(okObjectResult); var isSucceed = (bool)okObjectResult.Value; Assert.True(isSucceed); }
public void ExecuteAsync_Fail_CancellationTokenCalled_Cancelled() { // Arrange var memorySTream = new MemoryStream(); Mock <IFormFile> file = new Mock <IFormFile>(MockBehavior.Strict); file.Setup(x => x.OpenReadStream()).Returns(memorySTream); file.Setup(x => x.FileName).Returns("TestFileName"); file.Setup(x => x.Length).Returns(memorySTream.Length); file.Setup(x => x.Name).Returns("ImageName.png"); var outputFile = file.Object; var cancellationToken = new CancellationTokenSource(); cancellationToken.Cancel(); Mock <IExternalImageServiceClient> externalImageServiceClient = new Mock <IExternalImageServiceClient>(MockBehavior.Strict); //externalImageServiceClient.Setup(x => x.UploadImage(It.IsAny<CancellationToken>())).Returns()) Mock <IProcessingChannel> processingChannel = new Mock <IProcessingChannel>(); processingChannel.Setup(x => x.TryRead(out outputFile)).Returns(true); UploadImageService sut = new UploadImageService(externalImageServiceClient.Object, processingChannel.Object); Type sutType = typeof(UploadImageService); MethodInfo executeAsyncMethodInfo = sutType.GetMethod("ExecuteAsync", BindingFlags.NonPublic | BindingFlags.Instance); // Act executeAsyncMethodInfo.Invoke(sut, new object[] { cancellationToken.Token }); // Assert externalImageServiceClient.Verify(x => x.UploadImage(file.Object), Times.Never); }
public async Task <IActionResult> Register(RegisterModel model) { if (!ModelState.IsValid) { return(View(model)); } if (model.AvatarPhoto != null) { var filePath = Path.GetTempFileName(); using (var stream = System.IO.File.Create(filePath)) { await model.AvatarPhoto.CopyToAsync(stream); } model.AvatarURL = UploadImageService.Instance().Upload(model.UserName, filePath); model.AvatarPhoto = null; } model.AvatarPhoto = null; //string url = model.AvatarPhotoURL; var user = await _accountService.Register(model); if (user != null) { if (!string.IsNullOrEmpty(user.msg)) { ViewData["msg"] = user.msg; return(View(model)); } ViewData["IsRegisterSuccess"] = true; return(View(new RegisterModel())); } ViewData["IsRegisterSuccess"] = false; return(View(model)); }
public PetAccountsController(PetAppDbContext context, IRepositoryWrapper repo, UserManager <User> userManager, SignInManager <User> signInManager, UploadImageService uploadImageService) { _context = context; _repo = repo; _userManager = userManager; _signInManager = signInManager; _uploadImageService = uploadImageService; }
public async Task SaveImage() { var uploadPathName = "upload"; var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, uploadPathName); IUploadImageService imageService = new UploadImageService(uploadPathName); string guid = await imageService.SaveAsync(GetHttpRequest().Content); Assert.IsNotNull(guid); var newFilePath = Path.Combine(path, guid); var fileExist = File.Exists(newFilePath); Assert.IsTrue(fileExist); var deleteResult = imageService.Delete(guid); Assert.IsTrue(deleteResult); }
public async Task <IActionResult> OnPostUpdateProfile(int id, UserViewModel model) { if (!ModelState.IsValid) { return(RedirectToAction(nameof(UpdateProfile), new { id })); } if (model.AvatarPhoto != null) { var filePath = Path.GetTempFileName(); using (var stream = System.IO.File.Create(filePath)) { await model.AvatarPhoto.CopyToAsync(stream); } model.AvatarURL = UploadImageService.Instance().Upload(model.UserName, filePath); model.AvatarPhoto = null; } var userID = Int32.Parse(User.FindFirst("UserID").Value); if (id != userID) { return(Redirect("/Views/Account/AccessDenied.cshtml")); } var access_token = CookieEncoder.DecodeToken(Request.Cookies["access_token_cookie"]); var result = await _accountService.UpdateProfile(model, access_token); // success if (result != null && result.data != null) { return(RedirectToAction(nameof(ShowProfile))); } // fail ViewData["msg"] = "Update failed"; return(RedirectToAction(nameof(UpdateProfile), new { id = id })); }