public async Task <UseCaseResult> Handle([NotNull] GetEditAvatar request, CancellationToken cancellationToken) { if (request == null) { throw new ArgumentNullException(nameof(request)); } var diver = await diverRepository.FindByIdAsync(request.UserId); if (diver == null) { return(UseCaseResult.NotFound()); } var currentUserDiver = await currentUser.GetCurrentDiverAsync(); var isAdmin = await currentUser.GetIsAdminAsync(); if (!isAdmin && diver.Id != currentUserDiver.Id) { return(UseCaseResult.AccessDenied()); } request.OutputPort?.Output(new GetEditAvatarOutput(diver.Id, diver.Realname)); return(UseCaseResult.Success()); }
public async Task <UseCaseResult> Handle([NotNull] GetEditUserProfile request, CancellationToken cancellationToken) { if (request == null) { throw new ArgumentNullException(nameof(request)); } var diver = await diverRepository.FindByIdAsync(request.UserId); if (diver == null) { return(UseCaseResult.NotFound()); } request.OutputPort?.Output( new GetEditUserProfileOutput( diver.Id, diver.User.UserName, diver.Fullname, diver.Firstname, diver.Lastname, diver.Slogan, diver.Education, diver.Experience, diver.MobilePhone, diver.WebsiteUrl, diver.TwitterHandle, diver.FacebookId, diver.SkypeId)); return(UseCaseResult.Success()); }
public PublishParticipationChangedPolicyTests() { foundParticipant = new Participant { Id = validParticipantId, EventId = validEventId, ParticipatingDiverId = validDiverId, Status = ParticipantStatus.None, CountPeople = 1, BuddyTeamName = "team1" }; A.CallTo(() => participantRepository.GetParticipantByIdAsync(A <Guid> ._)) .ReturnsLazily(call => Task.FromResult( (Guid)call.Arguments[0] == validParticipantId ? foundParticipant : null )); A.CallTo(() => eventRepository.FindByIdAsync(A <Guid> ._)) .ReturnsLazily(call => Task.FromResult( (Guid)call.Arguments[0] == validEventId ? new Event { Id = validEventId, Name = "Testevent", } : null )); A.CallTo(() => diverRepository.FindByIdAsync(A <Guid> ._)) .ReturnsLazily(call => Task.FromResult( (Guid)call.Arguments[0] == validDiverId ? new Diver { Id = validDiverId, Fullname = "John Doe" } : null )); A.CallTo(() => recipientsBuilder.GetAllTauchboldeButDeclinedParticipantsAsync(validDiverId, validEventId)) .ReturnsLazily(() => Task.FromResult( new List <Diver> { new Diver { Id = new Guid("A00D5B04-B86D-4DAF-828F-00D286F4093B"), Fullname = "Jane Doe" }, new Diver { Id = new Guid("0DE86468-0F8C-4C02-A3CB-80460D45B0FF"), Fullname = "Jim Doe" }, } )); policy = new PublishParticipationChangedPolicy(logger, diverRepository, eventRepository, participantRepository, notificationPublisher, recipientsBuilder); }
public async Task Handle_NotCurrentDiverAndNotAdmin_MustReturnAccessDenied() { // Arrange var request = new EditAvatar(DiverFactory.JaneDoeDiverId, validAvatarFile); A.CallTo(() => diverRepository.FindByIdAsync(DiverFactory.JaneDoeDiverId)) .ReturnsLazily(() => Task.FromResult(DiverFactory.CreateJaneDoe())); // Act var useCaseResult = await interactor.Handle(request, CancellationToken.None); // Assert useCaseResult.IsSuccessful.Should().BeFalse(); useCaseResult.ResultCategory.Should().Be(ResultCategory.AccessDenied); A.CallTo(() => avatarStore.StoreAvatarAsync( DiverFactory.JohnDoeFirstName, A <string> ._, A <string> ._, A <Stream> ._)).MustNotHaveHappened(); }
private async Task <string> GetDiverRealNameAsync(Guid diverId) { var result = await diverRepository.FindByIdAsync(diverId); if (result == null) { logger.LogError("Diver with ID {id} not found!", diverId); throw new InvalidOperationException("Diver not found!"); } return(result.Realname ?? "Unbekannt"); }
public GetEditAvatarInteractorTests() { editJohnDoeAvatarRequest = new GetEditAvatar(DiverFactory.JohnDoeDiverId, outputPort); A.CallTo(() => diverRepository.FindByIdAsync(A <Guid> ._)) .ReturnsLazily(call => Task.FromResult( (Guid)call.Arguments[0] == DiverFactory.JohnDoeDiverId ? DiverFactory.CreateJohnDoe() : null)); interactor = new GetEditAvatarInteractor(logger, diverRepository, currentUser); }
public GetUserProfileInteractorTests() { A.CallTo(() => diverRepository.FindByIdAsync(A <Guid> ._)) .ReturnsLazily((call) => Task.FromResult( (Guid)call.Arguments[0] == DiverFactory.JohnDoeDiverId ? DiverFactory.CreateJohnDoe() : null)); var logger = A.Fake <ILogger <GetUserProfileInteractor> >(); interactor = new GetUserProfileInteractor(diverRepository, currentUser, userManager, logger); }
public async Task <UseCaseResult> Handle([NotNull] EditUserProfile request, CancellationToken cancellationToken) { if (request == null) { throw new ArgumentNullException(nameof(request)); } logger.LogInformation("Edit user [{diverId}]", request.UserId, request); var diver = await diverRepository.FindByIdAsync(request.UserId); if (diver == null) { logger.LogWarning("Diver for editing not found. Id [{diverId}]", request.UserId); return(UseCaseResult.NotFound()); } var user = await currentUser.GetCurrentDiverAsync(); var isAdmin = await currentUser.GetIsAdminAsync(); if (diver.Id != user.Id && !isAdmin) { logger.LogError("Access denied: User [{editorId}] is not allowed to edit user [{userId}]!", user.Id, request.UserId); return(UseCaseResult.AccessDenied()); } diver.Edit( user.Id, request.Fullname, request.Firstname, request.Lastname, request.Education, request.Experience, request.Slogan, request.MobilePhone, request.WebsiteUrl, request.FacebookId, request.TwitterHandle, request.SkypeId); await diverRepository.UpdateAsync(diver); logger.LogError("User [{editorId}] successfully edited by user [{userId}]!", user.Id, request.UserId, request); return(UseCaseResult.Success()); }
public async Task <UseCaseResult> Handle([NotNull] GetUserProfile request, CancellationToken cancellationToken) { if (request == null) { throw new ArgumentNullException(nameof(request)); } var diver = await diverRepository.FindByIdAsync(request.UserId); if (diver == null) { return(UseCaseResult.NotFound()); } var currentUserDiver = await currentUser.GetCurrentDiverAsync(); if (currentUserDiver == null) { logger.LogWarning("Diver record for current user [{id}] not found!", currentUser.Username); return(UseCaseResult.Fail()); } var allowEdit = currentUserDiver.Id == diver.Id || await currentUser.GetIsAdminAsync(); var roles = await userManager.GetRolesAsync(diver.User); request.OutputPort?.Output(new GetUserProfileOutput( allowEdit, request.UserId, roles, diver.User.Email, diver.AvatarId, diver.Realname, diver.Firstname, diver.Lastname, diver.MemberSince, diver.Slogan, diver.Education, diver.Experience, diver.MobilePhone, diver.WebsiteUrl, diver.TwitterHandle, diver.FacebookId, diver.SkypeId)); return(UseCaseResult.Success()); }
public async Task Handle([NotNull] LogbookEntryPublishedEvent notification, CancellationToken cancellationToken) { if (notification == null) { throw new ArgumentNullException(nameof(notification)); } var recipients = await diverRepository.GetAllTauchboldeUsersAsync(); var logbookEntry = await logbookEntryRepository.FindByIdAsync(notification.LogbookEntryId); var author = await diverRepository.FindByIdAsync(logbookEntry.OriginalAuthorId); var message = $"Neuer Logbucheintrag '{logbookEntry.Title}' von {author.Realname}."; await notificationPublisher.PublishAsync( NotificationType.NewLogbookEntry, message, recipients, relatedLogbookEntryId : notification.LogbookEntryId); }
public async Task Handle(CommentEditedEvent notification, CancellationToken cancellationToken) { var comment = await commentRepository.FindByIdAsync(notification.CommentId); var author = await diverRepository.FindByIdAsync(notification.AuthorId); var evt = await eventRepository.FindByIdAsync(notification.EventId); var recipients = await recipientsBuilder.GetAllTauchboldeButDeclinedParticipantsAsync( author.Id, notification.EventId); var startEndTimeAsString = evt.StartTime.FormatTimeRange(evt.EndTime); var message = $"Neuer Kommentar von '{author.Realname}' für Event '{evt.Name}' ({startEndTimeAsString}): {comment?.Text}"; await notificationPublisher.PublishAsync( NotificationType.Commented, message, recipients, relatedEventId : notification.EventId); }
public async Task Handle([NotNull] CommentCreatedEvent notification, CancellationToken cancellationToken) { if (notification == null) { throw new ArgumentNullException(nameof(notification)); } var author = await diverRepository.FindByIdAsync(notification.AuthorId); var evt = await eventRepository.FindByIdAsync(notification.EventId); var recipients = await recipientsBuilder.GetAllTauchboldeButDeclinedParticipantsAsync( author.Id, notification.EventId); var startEndTimeString = evt.StartTime.FormatTimeRange(evt.EndTime); var message = $"Neuer Kommentar von '{author.Realname}' für Event '{evt.Name}' ({startEndTimeString}): {notification.Text}"; await notificationPublisher.PublishAsync( NotificationType.Commented, message, recipients, relatedEventId : notification.EventId); }
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()); }