Example #1
0
        public async Task Should_Save_Vote()
        {
            _currentUser.UserDetail.AuthorityPercent = 30;
            var poll = new MultipleChoicePoll
            {
                Name = "test",
                OptionsJsonString = JsonConvert.SerializeObject(new List <string> {
                    "a", "b", "c"
                }),
                Active     = true,
                CreateTime = DateTime.UtcNow.AddHours(1),
                Deadline   = DateTime.UtcNow.AddDays(1)
            };

            _context.Polls.Add(poll);
            _context.SaveChanges();
            var model = new MultipleChoicePollVoteViewModel
            {
                PollId = poll.Id,
                Value  = 1
            };
            var result = await _controller.Vote(model);

            var vote = _context.Votes.FirstOrDefault(x =>
                                                     x.PollId == poll.Id && x.Value == model.Value && x.VoterId == _currentUser.Id);
            var actionResult    = Assert.IsType <OkObjectResult>(result);
            var actionResultObj = actionResult.Value as PollListViewModel;

            Assert.NotNull(vote);
            Assert.True(actionResultObj.UserVoted);
        }
Example #2
0
        public async Task Should_Return_Error_IfPollTypeIsNotMultipleChoice()
        {
            var completedPoll = new AuthorityPoll
            {
                Name         = "Authority Poll",
                Active       = false,
                CreateTime   = DateTime.UtcNow.AddDays(-1),
                Deadline     = DateTime.UtcNow.AddDays(-1),
                QuestionBody = "test"
            };

            _context.AuthorityPolls.Add(completedPoll);
            _context.SaveChanges();

            var model = new MultipleChoicePollVoteViewModel
            {
                PollId = (int)completedPoll.Id,
                Value  = 1
            };
            var key             = "PollTypeNotMultipleChoice";
            var localizedString = new LocalizedString(key, key);

            _multipleChoiceLocalizerMock.Setup(_ => _[key]).Returns(localizedString);
            var result = await _controller.Vote(model);

            var actionResult     = Assert.IsType <BadRequestObjectResult>(result);
            var actionResultList = actionResult.Value as List <ErrorViewModel>;

            Assert.Equal(key, actionResultList[0].Description);
        }
Example #3
0
        public async Task Should_Return_Error_IfUserIsNotaVoter()
        {
            var poll = new MultipleChoicePoll
            {
                Name = "test",
                OptionsJsonString = JsonConvert.SerializeObject(new List <string> {
                    "a", "b", "c"
                }),
                Active     = true,
                CreateTime = DateTime.UtcNow.AddHours(1),
                Deadline   = DateTime.UtcNow.AddDays(1)
            };

            _context.Polls.Add(poll);
            _context.SaveChanges();
            var model = new MultipleChoicePollVoteViewModel
            {
                PollId = poll.Id,
                Value  = 1
            };
            var key             = "PollVoterError";
            var localizedString = new LocalizedString(key, key);

            _multipleChoiceLocalizerMock.Setup(_ => _[key]).Returns(localizedString);
            var result = await _controller.Vote(model);

            var actionResult     = Assert.IsType <BadRequestObjectResult>(result);
            var actionResultList = actionResult.Value as List <ErrorViewModel>;

            Assert.Equal(key, actionResultList[0].Description);
        }
        public async Task <IActionResult> Vote([FromBody] MultipleChoicePollVoteViewModel model)
        {
            if (ModelState.IsValid)
            {
                var poll = await _pollService.GetPoll(model.PollId);

                if (poll == null)
                {
                    ModelState.AddModelError("", _localizer["PollNotFound"]);
                    return(BadRequest(Errors.GetErrorList(ModelState)));
                }

                if (poll.GetType() != typeof(MultipleChoicePoll))
                {
                    return(BadRequest(Errors.GetSingleErrorList("", _localizer["PollTypeNotMultipleChoice"])));
                }

                if (poll.Deadline <= DateTime.UtcNow)
                {
                    ModelState.AddModelError("", _localizer["PollCompleted"]);
                    return(BadRequest(Errors.GetErrorList(ModelState)));
                }

                var userVoted = await _voteService.UserVotedInPoll(User.ApiGetUserId(), model.PollId);

                if (userVoted)
                {
                    ModelState.AddModelError("", _localizer["PollRecordExist"]);
                    return(BadRequest(Errors.GetErrorList(ModelState)));
                }

                if (!await _pollService.UserCanVote(poll.Id, User.ApiGetUserId()))
                {
                    return(BadRequest(Errors.GetSingleErrorList("", _localizer["UserCannotVoteAfterAddedPollStart"])));
                }

                if (!await _userService.IsVoter(User.ApiGetUserId()))
                {
                    return(BadRequest(Errors.GetSingleErrorList("", _localizer["PollVoterError"])));
                }


                var vote = new Vote {
                    PollId = model.PollId, VoterId = User.ApiGetUserId(), Value = model.Value
                };
                await _voteService.AddVote(vote);

                var result = _mapper.Map <Poll, PollListViewModel>(poll);
                result.UserVoted = true;
                return(Ok(result));
            }

            return(BadRequest(Errors.GetErrorList(ModelState)));
        }
Example #5
0
        public async Task Should_Return_Error_If_PollDoesntExists()
        {
            var model = new MultipleChoicePollVoteViewModel
            {
                PollId = 1,
                Value  = 1
            };
            var key             = "PollNotFound";
            var localizedString = new LocalizedString(key, key);

            _multipleChoiceLocalizerMock.Setup(_ => _[key]).Returns(localizedString);
            var result = await _controller.Vote(model);

            var actionResult     = Assert.IsType <BadRequestObjectResult>(result);
            var actionResultList = actionResult.Value as List <ErrorViewModel>;

            Assert.Equal(key, actionResultList[0].Description);
        }