public async Task Handle_WhenCalled_AttemptToUpdatePhotoForUserAndRemoveItFromFilesStorageExpected()
        {
            // ARRANGE
            var filesStorageProvider = Substitute.For <IFilesStorageProvider>();
            var fileDownloadResult   = FileDownloadResult.Create(new MemoryStream(), "aa37acdc7bbf4260922a25066948db9e");

            filesStorageProvider.DownloadAsync(fileDownloadResult.FileId, Arg.Any <string>()).Returns(fileDownloadResult);
            var photoStorageProvider = Substitute.For <IPhotoStorageProvider>();
            var imageUploadResult    = ImageUploadResult.Create(new Uri(@"https:\\veileye.com"),
                                                                new Uri(@"https:\\veileye.com"), "jpg", DateTime.Now, "publicId", 1, "signature", "version");

            photoStorageProvider.Upload(fileDownloadResult.FileId, fileDownloadResult.FileStream)
            .Returns(imageUploadResult);
            var photoService = Substitute.For <IPhotoServiceScoped>();
            var userService  = Substitute.For <IUserService>();

            userService.GetUser(Arg.Any <int>()).Returns(new UserForDetailedDto());

            var sut = new PhotoUploadedNotificationHandler(userService, photoStorageProvider, photoService, filesStorageProvider);
            var photoUploadedNotificationEvent = new PhotoUploadedNotificationEvent(fileDownloadResult.FileId, 99);
            var expectedPhotoForUser           = new PhotoForUserDto
            {
                FileId      = photoUploadedNotificationEvent.FileId,
                PublicId    = imageUploadResult.PublicId,
                UserId      = photoUploadedNotificationEvent.UserId,
                Url         = imageUploadResult.Uri,
                Title       = photoUploadedNotificationEvent.Title,
                Subtitle    = photoUploadedNotificationEvent.Subtitle,
                Description = photoUploadedNotificationEvent.Description
            };
            PhotoForUserDto actualPhotoForUser = null;

            photoService.When(x => x.UpdatePhotoForUser(Arg.Any <PhotoForUserDto>()))
            .Do(x => actualPhotoForUser = x.ArgAt <PhotoForUserDto>(0));

            // ACT
            await sut.Handle(photoUploadedNotificationEvent, new CancellationToken());

            // ASSERT
            actualPhotoForUser.Should().BeEquivalentTo(expectedPhotoForUser);
            await filesStorageProvider.Received().Remove(photoUploadedNotificationEvent.FileId);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> ConfirmPendingUploads([FromBody] PhotoForUploadMetadataDto[] pendingFilesMetadata)
        {
            var user = GetUser();

            var pendingUploadFiles = await _filesStorageProvider.GetFiles(user.PendingUploadPhotosFolderName);

            if (!pendingUploadFiles.Any())
            {
                return(BadRequest("Attempt to confirm pending uploads failed because there are no awaiting uploads"));
            }

            var pendingUploadsToConfirmIds = pendingFilesMetadata.Select(x => _filesStorageProvider.CreateFileName(
                                                                             new PhotoForStreamUploadMetadataDto
            {
                FileId        = x.FileId,
                FileExtension = x.FileExtension
            }));
            var pendingUploadsToConfirm = pendingUploadFiles.Select(x => x.FileId)
                                          .Intersect(pendingUploadsToConfirmIds.AsEnumerable()).ToList();

            if (!pendingUploadsToConfirm.Any())
            {
                return(BadRequest("Attempt to confirm pending uploads failed because of wrong passed ids"));
            }

            await Task.Run(() => pendingFilesMetadata.ToList().ForEach(async x =>
            {
                var fileName = _filesStorageProvider.CreateFileName(new PhotoForStreamUploadMetadataDto
                {
                    FileId        = x.FileId,
                    FileExtension = x.FileExtension
                });
                var @event = new PhotoUploadedNotificationEvent(fileName, user.Id, x.Title, x.Subtitle, x.Description);
                await _mediator.Publish(@event);
            }));

            return(NoContent());
        }