public void NonInviteOnly_XTokenGuidHeader_ReturnsHeaderValueAsTokenGuid()
            {
                IDbSet <Poll>   polls   = DbSetTestHelper.CreateMockDbSet <Poll>();
                IDbSet <Ballot> ballots = DbSetTestHelper.CreateMockDbSet <Ballot>();

                var ballot = new Ballot()
                {
                    TokenGuid = TokenGuid
                };

                Poll nonInviteOnlyPoll = CreateNonInviteOnlyPoll();

                nonInviteOnlyPoll.Ballots.Add(ballot);

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

                IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls);

                PollController controller = CreatePollController(contextFactory);

                AddXTokenGuidHeader(controller, TokenGuid);


                PollRequestResponseModel response = controller.Get(PollId);

                Assert.AreEqual(TokenGuid, response.TokenGuid);
            }
            public void NoXTokenGuidHeader_Response_Choices_ContainsVoteValueZero()
            {
                const int choiceId = 42;

                var choice = new Choice()
                {
                    Id               = choiceId,
                    Name             = "A choice",
                    Description      = "Some description",
                    PollChoiceNumber = 13
                };

                Poll poll = CreateNonInviteOnlyPoll();

                poll.Choices.Add(choice);

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

                polls.Add(poll);
                IDbSet <Choice> choices = DbSetTestHelper.CreateMockDbSet <Choice>();

                choices.Add(choice);

                IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls, choices);

                PollController controller = CreatePollController(contextFactory);


                PollRequestResponseModel response = controller.Get(PollId);

                PollRequestChoiceResponseModel responseChoice = response.Choices.Single();

                Assert.AreEqual(0, responseChoice.VoteValue);
            }
            public void InviteOnly_XTokenGuidHeader_BallotInPoll_ReturnsHeaderValueAsTokenGuid()
            {
                var ballot = new Ballot()
                {
                    TokenGuid = TokenGuid
                };

                Poll poll = CreateInviteOnlyPoll();

                poll.Ballots.Add(ballot);

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

                polls.Add(poll);
                IDbSet <Ballot> ballots = DbSetTestHelper.CreateMockDbSet <Ballot>();

                ballots.Add(ballot);

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

                PollController controller = CreatePollController(contextFactory);

                AddXTokenGuidHeader(controller, TokenGuid);


                controller.Get(PollId);
            }
        public void GetPollNewest()
        {
            var searchResultsByCategory = new Mock <IReadOnlyListBaseCore <IPollSearchResultsByCategory> >(MockBehavior.Strict);

            searchResultsByCategory.Setup(_ => _.GetEnumerator()).Returns(new List <IPollSearchResultsByCategory>().GetEnumerator());

            var pollSearchResults = new Mock <IPollSearchResults>(MockBehavior.Strict);

            pollSearchResults.SetupGet(_ => _.SearchResultsByCategory).Returns(searchResultsByCategory.Object);

            var pollSearchResultsFactory = new Mock <IObjectFactory <IPollSearchResults> >(MockBehavior.Strict);

            pollSearchResultsFactory.Setup(_ => _.Fetch(PollSearchResultsQueryType.Newest)).Returns(pollSearchResults.Object);

            var controller = new PollController();

            controller.PollSearchResultsFactory = new Lazy <IObjectFactory <IPollSearchResults> >(() => pollSearchResultsFactory.Object);
            var polls = controller.Get("newest");

            Assert.AreEqual(0, polls.Count());

            pollSearchResultsFactory.VerifyAll();
            pollSearchResults.VerifyAll();
            searchResultsByCategory.VerifyAll();
        }
            public void InviteOnly_XTokenGuidHeader_BallotInPoll_ReturnsPoll()
            {
                const string   pollName = "Why are we here?";
                const PollType pollType = PollType.UpDown;

                var ballot = new Ballot()
                {
                    TokenGuid = TokenGuid
                };

                var poll = new Poll()
                {
                    UUID     = PollId,
                    Name     = pollName,
                    PollType = pollType,

                    MaxPoints  = 5,
                    MaxPerVote = 1,

                    ExpiryDateUtc = null,

                    NamedVoting  = false,
                    ChoiceAdding = false,
                    InviteOnly   = true
                };

                poll.Ballots.Add(ballot);

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

                polls.Add(poll);
                IDbSet <Ballot> ballots = DbSetTestHelper.CreateMockDbSet <Ballot>();

                ballots.Add(ballot);
                IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls, ballots);

                PollController controller = CreatePollController(contextFactory);

                AddXTokenGuidHeader(controller, TokenGuid);


                PollRequestResponseModel response = controller.Get(PollId);


                Assert.AreEqual(pollName, response.Name);
                Assert.AreEqual(pollType.ToString(), response.PollType);
                Assert.IsNull(response.ExpiryDateUtc);

                Assert.AreEqual(5, response.MaxPoints);
                Assert.AreEqual(1, response.MaxPerVote);

                Assert.IsFalse(response.NamedVoting);
                Assert.IsFalse(response.ChoiceAdding);
                Assert.IsFalse(response.UserHasVoted);
            }
            public void XTokenGuidHeader_Response_Choices_ContainsVoteValuesForBallot()
            {
                const int choiceId = 42;

                var choice = new Choice()
                {
                    Id               = choiceId,
                    Name             = "A choice",
                    Description      = "Some description",
                    PollChoiceNumber = 13
                };

                var vote = new Vote()
                {
                    Choice = choice, VoteValue = 3
                };

                var ballot = new Ballot()
                {
                    TokenGuid = TokenGuid
                };

                ballot.Votes.Add(vote);

                Poll poll = CreateNonInviteOnlyPoll();

                poll.Ballots.Add(ballot);
                poll.Choices.Add(choice);

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

                polls.Add(poll);
                IDbSet <Choice> choices = DbSetTestHelper.CreateMockDbSet <Choice>();

                choices.Add(choice);
                IDbSet <Vote> votes = DbSetTestHelper.CreateMockDbSet <Vote>();

                votes.Add(vote);
                IDbSet <Ballot> ballots = DbSetTestHelper.CreateMockDbSet <Ballot>();

                ballots.Add(ballot);
                IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls, ballots, votes, choices);

                PollController controller = CreatePollController(contextFactory);

                AddXTokenGuidHeader(controller, TokenGuid);


                PollRequestResponseModel response = controller.Get(PollId);

                PollRequestChoiceResponseModel responseChoice = response.Choices.Single();

                Assert.AreEqual(3, responseChoice.VoteValue);
            }
            public void InviteOnly_NoXTokenGuidHeader_ThrowsUnauthorized()
            {
                IDbSet <Poll> polls = DbSetTestHelper.CreateMockDbSet <Poll>();

                polls.Add(CreateInviteOnlyPoll());
                IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls);

                PollController controller = CreatePollController(contextFactory);

                controller.Get(PollId);
            }
            public void NonExistentPoll_ThrowsNotFound()
            {
                Guid unknownPollId = new Guid("0D5C94A2-F219-4327-9EED-4DCE2ECFAB6A");

                IDbSet <Poll>   polls          = DbSetTestHelper.CreateMockDbSet <Poll>();
                IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls);

                PollController controller = CreatePollController(contextFactory);


                controller.Get(unknownPollId);
            }
            public void InviteOnly_NoXTokenGuidHeader_DoesNotCreateBallot()
            {
                IDbSet <Poll> polls = DbSetTestHelper.CreateMockDbSet <Poll>();

                polls.Add(CreateInviteOnlyPoll());
                IDbSet <Ballot> ballots        = DbSetTestHelper.CreateMockDbSet <Ballot>();
                IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls, ballots);

                PollController controller = CreatePollController(contextFactory);

                controller.Get(PollId);

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

                polls.Add(CreateNonInviteOnlyPoll());
                IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls);

                PollController controller = CreatePollController(contextFactory);

                AddXTokenGuidHeader(controller, TokenGuid);


                controller.Get(PollId);
            }
            public void NonInviteOnly_MultipleXTokenGuidHeader_ThrowsBadRequest()
            {
                IDbSet <Poll> polls = DbSetTestHelper.CreateMockDbSet <Poll>();

                polls.Add(CreateNonInviteOnlyPoll());
                IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls);

                PollController controller = CreatePollController(contextFactory);

                AddXTokenGuidHeader(controller, TokenGuid);
                AddXTokenGuidHeader(controller, TokenGuid);


                controller.Get(PollId);
            }
            public void NonInviteOnly_NoXTokenGuidHeader_ReturnsNonEmptyTokenGuid()
            {
                IDbSet <Poll> polls = DbSetTestHelper.CreateMockDbSet <Poll>();

                polls.Add(new Poll()
                {
                    UUID = PollId
                });
                IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls);

                PollController controller = CreatePollController(contextFactory);


                PollRequestResponseModel response = controller.Get(PollId);

                Assert.AreNotEqual(Guid.Empty, response.TokenGuid);
            }
            public void ElectionPoll_BallotHasVoted_UserHasVotedIsTrue()
            {
                const string   pollName = "Why are we here?";
                const PollType pollType = PollType.Basic;

                var ballot = new Ballot()
                {
                    TokenGuid = TokenGuid, HasVoted = true
                };

                var poll = new Poll()
                {
                    UUID     = PollId,
                    Name     = pollName,
                    PollType = pollType,

                    ExpiryDateUtc = null,

                    NamedVoting  = false,
                    ChoiceAdding = false,
                    InviteOnly   = true
                };

                poll.Ballots.Add(ballot);

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

                polls.Add(poll);
                IDbSet <Ballot> ballots = DbSetTestHelper.CreateMockDbSet <Ballot>();

                ballots.Add(ballot);
                IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls, ballots);

                PollController controller = CreatePollController(contextFactory);

                AddXTokenGuidHeader(controller, TokenGuid);


                PollRequestResponseModel response = controller.Get(PollId);

                Assert.IsTrue(response.UserHasVoted);
            }
        public void GetPollDefault()
        {
            var generator       = new RandomObjectGenerator();
            var category        = generator.Generate <string>();
            var id              = generator.Generate <int>();
            var imageLink       = generator.Generate <string>();
            var question        = generator.Generate <string>();
            var submissionCount = generator.Generate <int>();

            var pollSearchResult = new Mock <IPollSearchResult>(MockBehavior.Strict);

            pollSearchResult.SetupGet(_ => _.Id).Returns(id);
            pollSearchResult.SetupGet(_ => _.ImageLink).Returns(imageLink);
            pollSearchResult.SetupGet(_ => _.Question).Returns(question);
            pollSearchResult.SetupGet(_ => _.SubmissionCount).Returns(submissionCount);

            var searchResults = new Mock <IReadOnlyListBaseCore <IPollSearchResult> >(MockBehavior.Strict);

            searchResults.Setup(_ => _.GetEnumerator()).Returns(new List <IPollSearchResult> {
                pollSearchResult.Object
            }.GetEnumerator());

            var pollSearchResultsByCategory = new Mock <IPollSearchResultsByCategory>(MockBehavior.Strict);

            pollSearchResultsByCategory.SetupGet(_ => _.SearchResults).Returns(searchResults.Object);
            pollSearchResultsByCategory.SetupGet(_ => _.Category).Returns(category);

            var searchResultsByCategory = new Mock <IReadOnlyListBaseCore <IPollSearchResultsByCategory> >(MockBehavior.Strict);

            searchResultsByCategory.Setup(_ => _.GetEnumerator()).Returns(new List <IPollSearchResultsByCategory> {
                pollSearchResultsByCategory.Object
            }.GetEnumerator());

            var pollSearchResults = new Mock <IPollSearchResults>(MockBehavior.Strict);

            pollSearchResults.SetupGet(_ => _.SearchResultsByCategory).Returns(searchResultsByCategory.Object);

            var pollSearchResultsFactory = new Mock <IObjectFactory <IPollSearchResults> >(MockBehavior.Strict);

            pollSearchResultsFactory.Setup(_ => _.Fetch(PollSearchResultsQueryType.MostPopular)).Returns(pollSearchResults.Object);

            var controller = new PollController();

            controller.PollSearchResultsFactory = new Lazy <IObjectFactory <IPollSearchResults> >(() => pollSearchResultsFactory.Object);
            var polls = controller.Get();

            Assert.AreEqual(1, polls.Count());
            var poll = polls.First();

            Assert.AreEqual(category, poll.Category);
            Assert.AreEqual(id, poll.Id);
            Assert.AreEqual(imageLink, poll.ImageLink);
            Assert.AreEqual(question, poll.Question);
            Assert.AreEqual(submissionCount, poll.SubmissionCount);

            pollSearchResultsFactory.VerifyAll();
            pollSearchResults.VerifyAll();
            searchResultsByCategory.VerifyAll();
            pollSearchResultsByCategory.VerifyAll();
            searchResults.VerifyAll();
            pollSearchResult.VerifyAll();
        }
        public void GetSinglePoll()
        {
            var generator            = new RandomObjectGenerator();
            var pollId               = generator.Generate <int>();
            var userId               = generator.Generate <int>();
            var pollCategoryID       = generator.Generate <int>();
            var pollQuestion         = generator.Generate <string>();
            var pollImageLink        = generator.Generate <string>();
            var pollMaxAnswers       = generator.Generate <short>();
            var pollMinAnswers       = generator.Generate <short>();
            var pollStartDate        = generator.Generate <DateTime>();
            var pollEndDate          = generator.Generate <DateTime>();
            var pollAdminRemovedFlag = generator.Generate <bool>();
            var pollDateRemoved      = generator.Generate <DateTime>();
            var pollDeletedFlag      = generator.Generate <bool>();
            var pollDeletedDate      = generator.Generate <DateTime>();
            var pollDescription      = generator.Generate <string>();
            var pollOptionId         = generator.Generate <int>();
            var optionPosition       = generator.Generate <short>();
            var optionText           = generator.Generate <string>();

            var pollOption = new Mock <IPollOption>(MockBehavior.Strict);

            pollOption.Setup(_ => _.SetParent(It.IsAny <BusinessList <IPollOption> >()));
            pollOption.SetupGet(_ => _.IsChild).Returns(true);
            pollOption.SetupGet(_ => _.PollID).Returns(pollId);
            pollOption.SetupGet(_ => _.PollOptionID).Returns(pollOptionId);
            pollOption.SetupGet(_ => _.OptionPosition).Returns(optionPosition);
            pollOption.SetupGet(_ => _.OptionText).Returns(optionText);
            pollOption.SetupGet(_ => _.EditLevel).Returns(0);
            pollOption.SetupSet(_ => _.EditLevelAdded = 0);

            var poll = new Mock <IPoll>(MockBehavior.Strict);

            poll.SetupGet(_ => _.PollID).Returns(pollId);
            poll.SetupGet(_ => _.UserID).Returns(userId);
            poll.SetupGet(_ => _.PollCategoryID).Returns(pollCategoryID);
            poll.SetupGet(_ => _.PollQuestion).Returns(pollQuestion);
            poll.SetupGet(_ => _.PollImageLink).Returns(pollImageLink);
            poll.SetupGet(_ => _.PollMaxAnswers).Returns(pollMaxAnswers);
            poll.SetupGet(_ => _.PollMinAnswers).Returns(pollMinAnswers);
            poll.SetupGet(_ => _.PollStartDate).Returns(pollStartDate);
            poll.SetupGet(_ => _.PollEndDate).Returns(pollEndDate);
            poll.SetupGet(_ => _.PollAdminRemovedFlag).Returns(pollAdminRemovedFlag);
            poll.SetupGet(_ => _.PollDateRemoved).Returns(pollDateRemoved);
            poll.SetupGet(_ => _.PollDeletedFlag).Returns(pollDeletedFlag);
            poll.SetupGet(_ => _.PollDeletedDate).Returns(pollDeletedDate);
            poll.SetupGet(_ => _.PollDescription).Returns(pollDescription);
            poll.SetupGet(_ => _.PollOptions).Returns(new BusinessList <IPollOption> {
                pollOption.Object
            });

            var pollFactory = new Mock <IObjectFactory <IPoll> >(MockBehavior.Strict);

            pollFactory.Setup(_ => _.Fetch(pollId)).Returns(poll.Object);

            var controller = new PollController();

            controller.PollFactory = new Lazy <IObjectFactory <IPoll> >(() => pollFactory.Object);
            var pollResult = controller.Get(pollId);

            Assert.AreEqual(pollId, pollResult.PollID);
            Assert.AreEqual(userId, pollResult.UserID);
            Assert.AreEqual(pollCategoryID, pollResult.PollCategoryID);
            Assert.AreEqual(pollQuestion, pollResult.PollQuestion);
            Assert.AreEqual(pollImageLink, pollResult.PollImageLink);
            Assert.AreEqual(pollMaxAnswers, pollResult.PollMaxAnswers);
            Assert.AreEqual(pollMinAnswers, pollResult.PollMinAnswers);
            Assert.AreEqual(pollStartDate, pollResult.PollStartDate);
            Assert.AreEqual(pollEndDate, pollResult.PollEndDate);
            Assert.AreEqual(pollAdminRemovedFlag, pollResult.PollAdminRemovedFlag);
            Assert.AreEqual(pollDateRemoved, pollResult.PollDateRemoved);
            Assert.AreEqual(pollDeletedFlag, pollResult.PollDeletedFlag);
            Assert.AreEqual(pollDeletedDate, pollResult.PollDeletedDate);
            Assert.AreEqual(pollDescription, pollResult.PollDescription);
            Assert.AreEqual(1, pollResult.PollOptions.Count);
            var pollOptionResult = pollResult.PollOptions[0];

            Assert.AreEqual(pollOptionId, pollOptionResult.PollOptionID);
            Assert.AreEqual(pollId, pollOptionResult.PollID);
            Assert.AreEqual(optionPosition, pollOptionResult.OptionPosition);
            Assert.AreEqual(optionText, pollOptionResult.OptionText);

            pollFactory.VerifyAll();
            pollOption.VerifyAll();
            poll.VerifyAll();
        }