Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public async Task Should_Get_MultipleChoicePollValues()
        {
            var poll = new MultipleChoicePoll
            {
                Name = "test",
                OptionsJsonString = JsonConvert.SerializeObject(new List <string> {
                    "a", "b", "c"
                }),
                Active     = true,
                CreateTime = DateTime.UtcNow,
                Deadline   = DateTime.UtcNow.AddDays(2)
            };

            _context.MultipleChoicePolls.Add(poll);
            _context.SaveChanges();
            var result = await _controller.GetPollValues(poll.Id);

            var actionResult    = Assert.IsType <OkObjectResult>(result);
            var actionResultObj = actionResult.Value as MultipleChoicePollViewModel;

            Assert.NotNull(actionResultObj);
            Assert.Equal(poll.Id, actionResultObj.PollId);
            Assert.Equal(poll.Name, actionResultObj.Name);
            Assert.Equal(poll.Deadline, actionResultObj.Deadline);
            Assert.Equal(poll.QuestionBody, actionResultObj.Description);
            Assert.Equal(PollListTypes.UserNotVoted.ToString().FirstCharacterToLower(), actionResultObj.ListType);
            Assert.Equal(JsonConvert.DeserializeObject(poll.OptionsJsonString), actionResultObj.Options);
        }
Exemplo n.º 4
0
        public async Task Should_Calculate_MultipleChoicePoll_Result()
        {
            var tenant = new Tenant
            {
                Id       = "test",
                HostName = "test.decidehub.com",
                InActive = false,
                Lang     = "tr"
            };

            _tenantsDbContext.Tenants.Add(tenant);
            _tenantsDbContext.SaveChanges();
            var option = new List <string> {
                "test1", "test2", "test3"
            };
            var poll = new MultipleChoicePoll
            {
                Name              = "test",
                Active            = true,
                CreateTime        = DateTime.UtcNow.AddHours(-12),
                QuestionBody      = "test dfs",
                TenantId          = "test",
                Deadline          = DateTime.UtcNow.AddHours(-1),
                OptionsJsonString = JsonConvert.SerializeObject(option)
            };

            _context.Polls.Add(poll);
            _context.Users.Add(new ApplicationUser
            {
                Email          = "*****@*****.**",
                FirstName      = "test",
                LastName       = "Test",
                TenantId       = "test",
                CreatedAt      = DateTime.UtcNow,
                SecurityStamp  = new Guid().ToString(),
                EmailConfirmed = false,
                Id             = 1.ToString(),
                IsDeleted      = false,
                UserDetail     = new UserDetail {
                    AuthorityPercent = 1, LanguagePreference = "tr"
                }
            });
            _context.SaveChanges();

            _context.Votes.Add(new Vote {
                PollId = poll.Id, Value = 0, VoterId = 1.ToString()
            });
            _context.SaveChanges();
            await _pollJobService.CheckPollCompletion();

            var getPoll = _context.Polls.FirstOrDefault(p => p.Id == poll.Id);

            Assert.NotNull(getPoll);
            Assert.Contains(option[0], getPoll.Result);
        }
Exemplo n.º 5
0
        public MultipleChoicePollViewModel MultipleChoicePollToViewModel(MultipleChoicePoll poll)
        {
            MultipleChoicePollViewModel model = null;

            if (poll != null)
            {
                model = _mapper.Map <MultipleChoicePoll, MultipleChoicePollViewModel>(poll);
            }

            return(model);
        }
Exemplo n.º 6
0
        public async Task <Poll> NewMultipleChoicePoll(MultipleChoicePollViewModel model)
        {
            var poll = new MultipleChoicePoll
            {
                UserId            = model.UserId,
                CreateTime        = DateTime.UtcNow,
                Active            = true,
                Name              = model.Name,
                QuestionBody      = model.Description,
                OptionsJsonString = JsonConvert.SerializeObject(model.Options),
                TenantId          = _tenantProvider.GetTenantId()
            };
            await _pollService.AddPoll(poll);

            return(poll);
        }
Exemplo n.º 7
0
        public async Task Should_Get_UserVotedPolls()
        {
            var poll = new PolicyChangePoll
            {
                Name         = "PolicyChangePoll test",
                Active       = true,
                CreateTime   = DateTime.UtcNow.AddHours(1),
                Deadline     = DateTime.UtcNow.AddDays(1),
                QuestionBody = "PolicyChangePoll test"
            };
            var poll2 = new MultipleChoicePoll
            {
                Name = "test",
                OptionsJsonString = JsonConvert.SerializeObject(new List <string> {
                    "a", "b", "c"
                }),
                Active     = true,
                CreateTime = DateTime.UtcNow,
                Deadline   = DateTime.UtcNow.AddDays(1)
            };

            _context.Polls.Add(poll);
            _context.Polls.Add(poll2);
            _context.SaveChanges();

            _context.Votes.Add(new Vote {
                PollId = poll2.Id, VoterId = _currentUser.Id, Value = -1
            });
            _context.SaveChanges();

            var result = await _controller.GetUserNotVotedPolls();

            var actionResult    = Assert.IsType <OkObjectResult>(result);
            var actionResultObj = actionResult.Value as IList <UserNotVotedPollListViewModel>;

            Assert.NotNull(actionResultObj);
            Assert.Equal(poll.Id, actionResultObj[0].PollId);
        }