Exemple #1
0
        public async Task Handle_Success()
        {
            // Arrange
            var request = new EditAvatar(DiverFactory.JohnDoeDiverId, validAvatarFile);

            // Act
            var useCaseResult = await interactor.Handle(request, CancellationToken.None);

            // Assert
            useCaseResult.IsSuccessful.Should().BeTrue();
            A.CallTo(() => avatarStore.StoreAvatarAsync(
                         DiverFactory.JohnDoeFirstName,
                         A <string> ._,
                         A <string> ._,
                         A <Stream> ._)).MustHaveHappenedOnceExactly();
        }
        public async Task <UseCaseResult> Handle([NotNull] EditAvatar request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            logger.LogInformation("Changing avatar for user [{id}]", request.UserId, request.Avatar.Filename, request.Avatar.ContentType);

            var diver = await diverRepository.FindByIdAsync(request.UserId);

            if (diver == null)
            {
                logger.LogError("Diver with Id [{id}] not found!", request.UserId);
                return(UseCaseResult.NotFound());
            }

            if (!await currentUser.GetIsDiverOrAdmin(request.UserId))
            {
                logger.LogError("Current user [{currentUser}] not allowed to change avatar of user [{user}]", currentUser.Username, diver.Realname);
                return(UseCaseResult.AccessDenied());
            }

            var newAvatarId = await avatarStore.StoreAvatarAsync(
                diver.Firstname,
                diver.AvatarId,
                GetAvatarFileExt(request),
                request.Avatar.Content);

            diver.ChangeAvatar(newAvatarId);

            await diverRepository.UpdateAsync(diver);

            logger.LogInformation("Avatar of user [{userId}] change by user [{currentUser}]", diver.Realname, currentUser.Username);
            return(UseCaseResult.Success());
        }