Exemplo n.º 1
0
        public async Task CastVoteCommandHandler_NoteVoteRemoved_VoteSavedAndBroadcast()
        {
            // Given
            var securityValidator = Substitute.For <ISecurityValidator>();
            var mediatorMock      = Substitute.For <IMediator>();
            var handler           = new CastVoteCommandHandler(
                this.Context,
                this._currentParticipantService,
                securityValidator,
                mediatorMock,
                this.Mapper
                );

            Retrospective retro = await this.CreateRetrospectiveWithNote();

            Note note = retro.Notes.First();

            NoteVote noteVote = this.Context.NoteVotes.Add(new NoteVote {
                Note          = note,
                Participant   = note.Participant,
                ParticipantId = note.ParticipantId,
                Retrospective = retro
            }).Entity;

            await this.Context.SaveChangesAsync();

            CastVoteCommand command = CastVoteCommand.ForNote(note.Id, VoteMutationType.Removed);

            // When
            await handler.Handle(command, CancellationToken.None);

            // Then
            await securityValidator.Received().
            EnsureOperation(Arg.Any <Retrospective>(), SecurityOperation.Delete, Arg.Any <NoteVote>());

            var broadcastedNote = mediatorMock.ReceivedCalls().FirstOrDefault()?.GetArguments()[0] as VoteChangeNotification;

            if (broadcastedNote == null)
            {
                Assert.Fail("No broadcast has gone out");
            }

            Assert.That(broadcastedNote.VoteChange.RetroId, Is.EqualTo(retro.UrlId.StringId));
            Assert.That(broadcastedNote.VoteChange.Mutation, Is.EqualTo(VoteMutationType.Removed));
            Assert.That(broadcastedNote.VoteChange.Vote.NoteId, Is.EqualTo(note.Id));

            Assert.That(this.Context.NoteVotes.Select(x => x.Id), Does.Not.Contain(noteVote.Id));
        }
Exemplo n.º 2
0
        public void SecurityValidator_DisallowsOperationsOnNoteVote_InStages(RetrospectiveStage stage)
        {
            // Given
            Retrospective retro    = GetRetrospectiveInStage(stage);
            var           noteVote = new NoteVote {
                ParticipantId = 252
            };

            this._currentParticipantService.SetParticipant(new CurrentParticipantModel(252, String.Empty, false));

            // When
            TestDelegate action = () => this._securityValidator.EnsureOperation(retro, SecurityOperation.AddOrUpdate, noteVote).GetAwaiter().GetResult();

            // Then
            Assert.That(action, Throws.InstanceOf <OperationSecurityException>());
        }
Exemplo n.º 3
0
        public void SecurityValidator_AllowsOperationOnNoteVote_CorrectParticipant(SecurityOperation securityOperation)
        {
            // Given
            Retrospective retro    = GetRetrospectiveInStage(RetrospectiveStage.Voting);
            var           noteVote = new NoteVote {
                ParticipantId = 212
            };

            this._currentParticipantService.SetParticipant(new CurrentParticipantModel(212, String.Empty, false));

            // When
            TestDelegate action = () => this._securityValidator.EnsureOperation(retro, securityOperation, noteVote).GetAwaiter().GetResult();

            // Then
            Assert.That(action, Throws.Nothing);
        }
Exemplo n.º 4
0
        public void SecurityValidator_DisallowsOperationOnNoteVote_UnauthenticatedParticipant(SecurityOperation securityOperation)
        {
            // Given
            Retrospective retro    = GetRetrospectiveInStage(RetrospectiveStage.Voting);
            var           noteVote = new NoteVote {
                ParticipantId = 1
            };

            this._currentParticipantService.Reset();
            Assume.That(this._currentParticipantService.GetParticipant().Result, Is.EqualTo(default(CurrentParticipantModel)));

            // When
            TestDelegate action = () => this._securityValidator.EnsureOperation(retro, securityOperation, noteVote).GetAwaiter().GetResult();

            // Then
            Assert.That(action, Throws.InstanceOf <OperationSecurityException>());
        }
Exemplo n.º 5
0
        private async Task <Unit> HandleNoteGroupVote(int noteGroupId, VoteMutationType mutationType, CancellationToken cancellationToken)
        {
            using IReturnDbContext dbContext = this._returnDbContext.CreateForEditContext();

            // Get
            NoteGroup noteGroup = await dbContext.NoteGroups.Include(x => x.Retrospective)
                                  .Include(x => x.Lane).FirstOrDefaultAsync(x => x.Id == noteGroupId, cancellationToken);

            if (noteGroup == null)
            {
                throw new NotFoundException(nameof(NoteGroup), noteGroupId);
            }

            NoteVote vote = await(mutationType switch
            {
                VoteMutationType.Added => this.HandleNoteGroupVoteAdd(noteGroup: noteGroup, dbContext: dbContext, cancellationToken: cancellationToken),
                VoteMutationType.Removed => this.HandleNoteGroupVoteRemove(noteGroup: noteGroup, dbContext: dbContext, cancellationToken: cancellationToken),
                _ => throw new InvalidOperationException("Invalid vote mutation type: " + mutationType)
            });
Exemplo n.º 6
0
        public void ShouldMap_NoteVoteWithNoteGroup_ToVoteModel()
        {
            // Given
            var entity = new NoteVote
            {
                Id        = 342,
                NoteGroup = new NoteGroup
                {
                    Id   = 49,
                    Lane = new NoteLane
                    {
                        Id = KnownNoteLane.Stop
                    }
                },
                Participant = new Participant
                {
                    Id    = 43,
                    Color = Color.Aqua,
                    Name  = "Hans"
                }
            };

            // When
            var mapped = this.Mapper.Map <VoteModel>(entity);

            // Then
            Assert.That(mapped, Is.Not.Null);

            Assert.That(mapped.Id, Is.EqualTo(entity.Id));
            Assert.That(mapped.ParticipantId, Is.EqualTo(entity.Participant.Id));
            Assert.That(mapped.ParticipantName, Is.EqualTo(entity.Participant.Name));
            Assert.That(mapped.ParticipantColor.G, Is.EqualTo(entity.Participant.Color.G));

            Assert.That(mapped.LaneId, Is.EqualTo((int)KnownNoteLane.Stop));
            Assert.That(mapped.NoteGroupId, Is.EqualTo(entity.NoteGroup.Id));
            Assert.That(mapped.NoteId, Is.EqualTo(null));
            Assert.That(mapped.IsCast, Is.True);
        }