public void IfMembershipNotFoundThenMembershipIsNotRemoved() { const long accountId = 2; var membership = new Membership { UserId = 1, AccountId = accountId, Role = Role.Owner }; var command = new RemoveTeamMemberCommand { UserId = membership.UserId, HashedAccountId = "as123", ExternalUserId = Guid.NewGuid().ToString() }; _membershipRepository.Setup(x => x.Get(membership.UserId, membership.AccountId)).ReturnsAsync(() => null); var exception = Assert.ThrowsAsync <InvalidRequestException>(() => _handler.Handle(command)); Assert.That(exception.ErrorMessages.Count, Is.EqualTo(1)); Assert.That(exception.ErrorMessages.FirstOrDefault(x => x.Key == "Membership"), Is.Not.Null); _membershipRepository.Verify(x => x.Remove(It.IsAny <long>(), It.IsAny <long>()), Times.Never); }
public async Task <IActionResult> RemoveTeamMember([FromRoute] string teamId, [FromRoute] string memberId, CancellationToken cancellationToken) { var command = new RemoveTeamMemberCommand ( ObjectId.Parse(teamId), ObjectId.Parse(memberId) ); await _commandDispatcher.DispatchAsync(command, cancellationToken); return(StatusCode(204)); }
public void Setup() { _user = new User { UserRef = Guid.NewGuid().ToString() }; _teamMember = new Membership { UserId = 12, AccountId = ExpectedAccountId, Role = Role.Owner, User = _user }; _owner = new MembershipView { UserId = 1, AccountId = ExpectedAccountId, Role = Role.Owner }; _command = new RemoveTeamMemberCommand { UserId = _teamMember.UserId, UserRef = Guid.NewGuid(), HashedAccountId = "123a", ExternalUserId = Guid.NewGuid().ToString() }; _validator = new Mock <IValidator <RemoveTeamMemberCommand> >(); _validator.Setup(x => x.Validate(It.IsAny <RemoveTeamMemberCommand>())).Returns(new ValidationResult { ValidationDictionary = new Dictionary <string, string>() }); _mediator = new Mock <IMediator>(); _membershipRepository = new Mock <IMembershipRepository>(); _membershipRepository.Setup(x => x.Get(_teamMember.UserId, _teamMember.AccountId)).ReturnsAsync(_teamMember); _membershipRepository.Setup(x => x.GetCaller(_command.HashedAccountId, _command.ExternalUserId)).ReturnsAsync(_owner); _publisher = new Mock <IEventPublisher>(); _handler = new RemoveTeamMemberCommandHandler(_mediator.Object, _membershipRepository.Object, _validator.Object, _publisher.Object); }
public void IfTryingToRemoveYourselfThenMembershipIsNotRemoved() { const long accountId = 2; var ownerUser = new User { Id = 2, UserRef = Guid.NewGuid().ToString() }; var membership = new Membership { UserId = ownerUser.Id, AccountId = accountId, Role = Role.Owner }; var ownerMembership = new MembershipView { UserId = ownerUser.Id, AccountId = accountId, Role = Role.Owner }; var command = new RemoveTeamMemberCommand { UserId = membership.UserId, HashedAccountId = "", ExternalUserId = ownerUser.UserRef }; _membershipRepository.Setup(x => x.Get(membership.UserId, membership.AccountId)).ReturnsAsync(membership); _membershipRepository.Setup(x => x.GetCaller(ExpectedAccountId, command.ExternalUserId)).ReturnsAsync(ownerMembership); var exception = Assert.ThrowsAsync <InvalidRequestException>(() => _handler.Handle(command)); Assert.That(exception.ErrorMessages.Count, Is.EqualTo(1)); Assert.That(exception.ErrorMessages.FirstOrDefault(x => x.Key == "Membership"), Is.Not.Null); _membershipRepository.Verify(x => x.Remove(command.UserId, ExpectedAccountId), Times.Never); }