public void Setup()
        {
            primaryKey            = 123;
            _pollRepository       = Substitute.For <IPollRepository>();
            _pollOptionRepository = Substitute.For <IPollOptionRepository>();
            _voteRepository       = Substitute.For <IVoteRepository>();
            _requestHeaderHandler = Substitute.For <IRequestHeaderHandler>();

            _pollRendererController = new PollRendererController(_pollRepository, _pollOptionRepository,
                                                                 _voteRepository, _requestHeaderHandler);
            _pollRepository.Select(Arg.Any <string>()).Returns(new PollDto());
            _pollOptionRepository.SelectAllForPollPk(Arg.Any <int>()).Returns(new List <PollOptionDto>());
            _pollRendererController.ControllerContext.HttpContext = new DefaultHttpContext();
            _pollRendererController.ControllerContext.HttpContext.Request.Headers["someId"] = "someId";
            _requestHeaderHandler.GetHeader(_pollRendererController.ControllerContext, "User-Agent").Returns("someAgent");
            _requestHeaderHandler.GetUserIp(_pollRendererController.ControllerContext).Returns("someIp");
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> SubmitVote([FromBody] VoteRequest voteRequest)
        {
            bool hasVoted = CheckIfUserHasVoted(voteRequest.PollId);

            if (hasVoted || !voteRequest.SelectedVotes.Any())
            {
                return(BadRequest());
            }

            var userAgent = _requestHeaderHandler.GetHeader(ControllerContext, "User-Agent");
            var clientIp  = _requestHeaderHandler.GetUserIp(ControllerContext);

            foreach (var vote in voteRequest.SelectedVotes)
            {
                if (vote.Value.Length >= 100)
                {
                    vote.Value = vote.Value.Substring(0, 99);
                }
                vote.UserAgent = userAgent;
                vote.ClientIp  = clientIp;
            }

            var pollDto = await _pollRepository.Select(voteRequest.PollId);

            if (pollDto == null)
            {
                return(NotFound());
            }


            Response.Cookies.Append(voteRequest.PollId, voteRequest.PollId,
                                    new CookieOptions()
            {
                Expires = DateTime.Now.AddDays(1)
            });
            await _voteRepository.Insert(voteRequest.SelectedVotes.ToList(), pollDto.PrimaryKey);

            return(Ok());
        }