public void RequestedBallots_DoNotBelongToPoll()
            {
                var ballotManageGuid = new Guid("1AC3FABB-A077-4EF3-84DC-62074BA8FDF1");

                IDbSet <Poll> polls = DbSetTestHelper.CreateMockDbSet <Poll>();
                var           poll  = CreatePoll();

                IDbSet <Ballot> ballots = DbSetTestHelper.CreateMockDbSet <Ballot>();
                var             ballot  = new Ballot()
                {
                    ManageGuid = ballotManageGuid
                };

                ballots.Add(ballot);
                polls.Add(poll);

                IContextFactory       contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls, ballots);
                ManageVoterController controller     = CreateManageVoteController(contextFactory);


                var request             = new DeleteVotersRequestModel();
                var ballotDeleteRequest = new DeleteBallotRequestModel()
                {
                    BallotManageGuid = ballotManageGuid
                };
                DeleteVoteRequestModel voteRequest = new DeleteVoteRequestModel();

                ballotDeleteRequest.VoteDeleteRequests.Add(voteRequest);
                request.BallotDeleteRequests.Add(ballotDeleteRequest);


                controller.Delete(PollManageGuid, request);
            }
            public void AllVotesRequestedRemovalForBallot_RemovesBallot()
            {
                const int pollChoiceNumber = 1;

                IDbSet <Poll> polls = DbSetTestHelper.CreateMockDbSet <Poll>();
                var           poll  = CreatePoll();

                IDbSet <Ballot> ballots = DbSetTestHelper.CreateMockDbSet <Ballot>();
                var             ballot  = new Ballot()
                {
                    ManageGuid = PollManageGuid
                };

                IDbSet <Vote> votes = DbSetTestHelper.CreateMockDbSet <Vote>();
                var           vote  = new Vote()
                {
                    Choice = new Choice()
                    {
                        PollChoiceNumber = pollChoiceNumber
                    }
                };



                ballot.Votes.Add(vote);
                poll.Ballots.Add(ballot);

                votes.Add(vote);
                ballots.Add(ballot);
                polls.Add(poll);

                IContextFactory       contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls, ballots, votes);
                ManageVoterController controller     = CreateManageVoteController(contextFactory);

                var voteRequest = new DeleteVoteRequestModel {
                    ChoiceNumber = pollChoiceNumber
                };
                var ballotRequest = new DeleteBallotRequestModel {
                    BallotManageGuid = PollManageGuid
                };

                ballotRequest.VoteDeleteRequests.Add(voteRequest);

                var request = new DeleteVotersRequestModel();

                request.BallotDeleteRequests.Add(ballotRequest);


                controller.Delete(PollManageGuid, request);


                Assert.AreEqual(0, ballots.Count());
                Assert.AreEqual(0, poll.Ballots.Count());
            }
            public void NoBallotRequestsInRequest_ThrowsBadRequest()
            {
                IDbSet <Poll> polls = DbSetTestHelper.CreateMockDbSet <Poll>();

                IContextFactory       contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls);
                ManageVoterController controller     = CreateManageVoteController(contextFactory);

                var request = new DeleteVotersRequestModel();


                controller.Delete(PollManageGuid, request);
            }
            public void RequestedVotes_DoNotBelongToBallot()
            {
                const int pollChoiceNumber1 = 1;
                const int pollChoiceNumber2 = 2;

                IDbSet <Poll> polls = DbSetTestHelper.CreateMockDbSet <Poll>();
                var           poll  = CreatePoll();

                IDbSet <Ballot> ballots = DbSetTestHelper.CreateMockDbSet <Ballot>();
                var             ballot  = new Ballot()
                {
                    ManageGuid = PollManageGuid
                };

                IDbSet <Vote> votes = DbSetTestHelper.CreateMockDbSet <Vote>();
                var           vote  = new Vote()
                {
                    Choice = new Choice()
                    {
                        PollChoiceNumber = pollChoiceNumber1
                    }
                };



                ballot.Votes.Add(vote);
                poll.Ballots.Add(ballot);

                votes.Add(vote);
                ballots.Add(ballot);
                polls.Add(poll);

                IContextFactory       contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls, ballots, votes);
                ManageVoterController controller     = CreateManageVoteController(contextFactory);

                var voteRequest = new DeleteVoteRequestModel {
                    ChoiceNumber = pollChoiceNumber2
                };
                var ballotRequest = new DeleteBallotRequestModel {
                    BallotManageGuid = PollManageGuid
                };

                ballotRequest.VoteDeleteRequests.Add(voteRequest);

                var request = new DeleteVotersRequestModel();

                request.BallotDeleteRequests.Add(ballotRequest);


                controller.Delete(PollManageGuid, request);
            }
            public void NoMatchingPoll_ThrowsNotFound()
            {
                IDbSet <Poll> polls = DbSetTestHelper.CreateMockDbSet <Poll>();

                IContextFactory       contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls);
                ManageVoterController controller     = CreateManageVoteController(contextFactory);

                var deleteBallotRequestModel = new DeleteBallotRequestModel();

                deleteBallotRequestModel.VoteDeleteRequests.Add(new DeleteVoteRequestModel());

                var request = new DeleteVotersRequestModel();

                request.BallotDeleteRequests.Add(deleteBallotRequestModel);

                controller.Delete(PollManageGuid, request);
            }
Ejemplo n.º 6
0
        public void Delete(Guid manageId, DeleteVotersRequestModel request)
        {
            if (request == null)
            {
                ThrowError(HttpStatusCode.BadRequest);
            }

            if (!request.BallotDeleteRequests.Any())
            {
                ThrowError(HttpStatusCode.BadRequest);
            }

            if (request.BallotDeleteRequests.Any(b => !b.VoteDeleteRequests.Any()))
            {
                ThrowError(HttpStatusCode.BadRequest);
            }

            using (IVotingContext context = _contextFactory.CreateContext())
            {
                Poll poll = PollByManageId(manageId, context);

                List <Guid> requestBallotGuids = request
                                                 .BallotDeleteRequests
                                                 .Select(b => b.BallotManageGuid)
                                                 .ToList();

                List <Guid> pollBallotGuids = poll
                                              .Ballots
                                              .Select(b => b.ManageGuid)
                                              .ToList();

                if (requestBallotGuids.Except(pollBallotGuids).Any())
                {
                    ThrowError(HttpStatusCode.NotFound, String.Format("Ballots requested for delete do not all belong to poll {0}", manageId));
                }

                List <Ballot> ballots = poll.Ballots.ToList();

                foreach (DeleteBallotRequestModel ballotRequest in request.BallotDeleteRequests)
                {
                    Ballot ballot = ballots.Single(b => b.ManageGuid == ballotRequest.BallotManageGuid);

                    foreach (DeleteVoteRequestModel voteRequest in ballotRequest.VoteDeleteRequests)
                    {
                        Vote vote = ballot.Votes.ToList().SingleOrDefault(v => v.Choice.PollChoiceNumber == voteRequest.ChoiceNumber);

                        if (vote == null)
                        {
                            ThrowError(HttpStatusCode.NotFound, String.Format("Ballot {0} does not contain an option {1}", ballotRequest.BallotManageGuid, voteRequest.ChoiceNumber));
                        }

                        _metricHandler.HandleVoteDeletedEvent(vote, poll.UUID);
                        ballot.Votes.Remove(vote);
                        context.Votes.Remove(vote);
                    }

                    if (!ballot.Votes.Any())
                    {
                        poll.Ballots.Remove(ballot);
                        context.Ballots.Remove(ballot);
                    }
                }

                poll.LastUpdatedUtc = DateTime.UtcNow;

                context.SaveChanges();
            }
        }
            public void ClearedVotesGenerateVoteDeletionMetric()
            {
                // Arrange
                Choice option = new Choice()
                {
                    PollChoiceNumber = 1, Id = 1, Name = "Choice"
                };
                Poll poll = new Poll()
                {
                    UUID = Guid.NewGuid(), ManageId = Guid.NewGuid(), Choices = new List <Choice>()
                    {
                        option
                    }
                };
                Ballot ballot = new Ballot()
                {
                    TokenGuid = Guid.NewGuid(), Votes = new List <Vote>(), ManageGuid = Guid.NewGuid()
                };
                Vote voteToClear = new Vote()
                {
                    Ballot = ballot, Poll = poll, VoteValue = 1, Choice = option
                };

                ballot.Votes.Add(voteToClear);
                poll.Ballots.Add(ballot);

                var options = DbSetTestHelper.CreateMockDbSet <Choice>();
                var polls   = DbSetTestHelper.CreateMockDbSet <Poll>();
                var ballots = DbSetTestHelper.CreateMockDbSet <Ballot>();
                var votes   = DbSetTestHelper.CreateMockDbSet <Vote>();

                options.Add(option);
                polls.Add(poll);
                ballots.Add(ballot);
                votes.Add(voteToClear);

                var metricHandler  = new Mock <IMetricHandler>();
                var contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls, ballots, votes, options);
                ManageVoterController controller = CreateManageVoteController(contextFactory, metricHandler.Object);

                // Act
                DeleteVoteRequestModel voteDelete = new DeleteVoteRequestModel()
                {
                    ChoiceNumber = 1
                };
                DeleteBallotRequestModel deletion = new DeleteBallotRequestModel()
                {
                    BallotManageGuid = ballot.ManageGuid, VoteDeleteRequests = new List <DeleteVoteRequestModel>()
                    {
                        voteDelete
                    }
                };
                List <DeleteBallotRequestModel> ballotDeletions = new List <DeleteBallotRequestModel>()
                {
                    deletion
                };
                DeleteVotersRequestModel request = new DeleteVotersRequestModel()
                {
                    BallotDeleteRequests = ballotDeletions
                };

                controller.Delete(poll.ManageId, request);

                // Assert
                metricHandler.Verify(m => m.HandleVoteDeletedEvent(voteToClear, poll.UUID), Times.Once());
            }