예제 #1
0
        public async Task <IHttpActionResult> CastVote(CastVoteModel vote)
        {
            if (vote == null)
            {
                return(BadRequest("Please provide valid inputs!"));
            }

            if (vote.ElectorID == 0)
            {
                return(BadRequest("Please provide valid elector ID!"));
            }

            if (vote.CandidateID == 0)
            {
                return(BadRequest("Please provide valid candidate ID!"));
            }

            if (string.IsNullOrEmpty(vote.Location))
            {
                return(BadRequest("Please provide valid location!"));
            }

            if (await AuthService.ValidateUserAndToken(vote.Token, vote.UserID, vote.Email, vote.Location))
            {
                if (await ElectorService.ElectorHasVoted(vote))
                {
                    return(BadRequest("Elector has already voted!"));
                }
                else
                {
                    BallotModel newVote = new BallotModel()
                    {
                        CandidateID = vote.CandidateID,
                        DistrictID  = vote.DistrictID,
                        CenterID    = vote.CenterID,
                        Location    = vote.Location
                    };

                    if (await BallotService.AddNewBallot(newVote))
                    {
                        if (await ElectorService.ElectorVoted(vote))
                        {
                            return(Ok("Vote Casted Successfully!"));
                        }
                        else
                        {
                            return(BadRequest("Error In Casting The Vote!"));
                        }
                    }
                    else
                    {
                        return(BadRequest("Error In Casting The Vote!"));
                    }
                }
            }
            else
            {
                return(Unauthorized());
            }
        }
예제 #2
0
        public async void CreateTest()
        {
            // Arrange
            var mockSaver = new Mock <IRepository <Ballot> >();
            var election  = new Election
            {
                Id             = 1,
                CreatedDate    = DateTime.Now,
                ElectionQr     = Guid.NewGuid(),
                ExpirationDate = DateTime.Now.AddDays(5)
            };

            var ballot = GetSampleBallot(election);

            mockSaver.Setup(bs => bs.AddAsync(It.IsAny <Ballot>()))
            .ReturnsAsync((Ballot b) => b)
            .Callback <Ballot>(b =>
            {
                b.Id = 1;
            });

            IBallotService service = new BallotService(mockSaver.Object);

            // Act
            ballot = await service.AddAsync(ballot);

            // Assert
            Assert.True(ballot.Id.Equals(1));
        }
예제 #3
0
        public async void DeleteTest()
        {
            // Arrange
            var mockSaver = new Mock <IRepository <Ballot> >();

            mockSaver.Setup(saver => saver.RemoveAsync(It.IsAny <Ballot>())).Returns(Task.CompletedTask);

            // Assert setup
            mockSaver.Verify(saver => saver.RemoveAsync(It.IsAny <Ballot>()), Times.AtMostOnce);

            var service = new BallotService(mockSaver.Object);

            // Act
            await service.RemoveAsync(new Ballot
            {
                Name = "Speaker 1"
            });
        }
예제 #4
0
        public async void UpdateTest()
        {
            // Arrange
            var mockSaver = new Mock <IRepository <Ballot> >();


            mockSaver.Setup(bs => bs.UpdateAsync(It.IsAny <Ballot>()))
            .ReturnsAsync((Ballot b) => b);

            mockSaver.Verify(saver => saver.UpdateAsync(It.IsAny <Ballot>()), Times.AtMostOnce);

            var service = new BallotService(mockSaver.Object);

            // Act
            var ballotUpdate = await service.UpdateAsync(GetSampleBallot(null));

            // Assert
            Assert.NotNull(ballotUpdate);
        }
예제 #5
0
        public async Task <IHttpActionResult> GetBallots(BallotModel ballot)
        {
            if (await AuthService.ValidateUserAndToken(ballot.Token, ballot.UserID, ballot.Email, ballot.Location))
            {
                var ballots = await BallotService.GetBallots(ballot);

                if (ballots.Count > 0)
                {
                    return(Ok(ballots));
                }
                else
                {
                    return(BadRequest("No Ballots Exists!"));
                }
            }
            else
            {
                return(Unauthorized());
            }
        }
예제 #6
0
        public async Task <IHttpActionResult> GetTotalNoOfVotes(BallotModel vote)
        {
            if (await AuthService.ValidateUserAndToken(vote.Token, vote.UserID, vote.Email, vote.Location))
            {
                var votes = await BallotService.GetTotalVotes(vote);

                if (votes.Count > 0)
                {
                    return(Ok(votes));
                }
                else
                {
                    return(BadRequest("No Votes Exists!"));
                }
            }
            else
            {
                return(Unauthorized());
            }
        }
예제 #7
0
        public async Task <IHttpActionResult> UpdateBallot(BallotModel ballot)
        {
            if (ballot == null)
            {
                return(BadRequest("Please provide valid inputs!"));
            }

            if (ballot.ID == 0)
            {
                return(BadRequest("Please provide valid ballot ID!"));
            }

            if (string.IsNullOrEmpty(ballot.Location))
            {
                return(BadRequest("Please provide valid location!"));
            }

            if (await AuthService.ValidateUserAndToken(ballot.Token, ballot.UserID, ballot.Email, ballot.Location))
            {
                if (await BallotService.BallotExists(ballot))
                {
                    if (await BallotService.UpdateBallot(ballot))
                    {
                        return(Ok("Ballot Updated Successfully!"));
                    }
                    else
                    {
                        return(BadRequest("Failed To Update Ballot!"));
                    }
                }
                else
                {
                    return(BadRequest("No Such Ballot Exists!"));
                }
            }
            else
            {
                return(Unauthorized());
            }
        }
예제 #8
0
        public async Task <IHttpActionResult> AddNewBallot(BallotModel ballot)
        {
            if (ballot == null)
            {
                return(BadRequest("Please provide valid inputs!"));
            }

            if (ballot.CandidateID == 0)
            {
                return(BadRequest("Please provide valid candidate ID!"));
            }

            if (string.IsNullOrEmpty(ballot.Location))
            {
                return(BadRequest("Please provide valid location!"));
            }

            if (await AuthService.ValidateUserAndToken(ballot.Token, ballot.UserID, ballot.Email, ballot.Location))
            {
                if (await BallotService.BallotExists(ballot))
                {
                    return(BadRequest("Ballot Already Exists"));
                }
                else
                {
                    if (await BallotService.AddNewBallot(ballot))
                    {
                        return(Ok("Ballot Added Successfully!"));
                    }
                    else
                    {
                        return(BadRequest("Ballot Adding Failed!"));
                    }
                }
            }
            else
            {
                return(Unauthorized());
            }
        }
예제 #9
0
        public async void GetByIdTest()
        {
            // Arrange
            var mockBuilder = new Mock <IRepository <Ballot> >();

            mockBuilder.Setup(bb => bb.GetByIdAsync(It.IsAny <int>()))
            .ReturnsAsync((int i) => new Ballot
            {
                Name = "Speakers",
                Id   = i
            });

            mockBuilder.Verify(saver => saver.GetByIdAsync(It.IsAny <int>()), Times.AtMostOnce);

            var service = new BallotService(mockBuilder.Object);

            // Act
            var ballot = await service.GetByIdAsync(1);

            // Assert
            Assert.True(ballot.Id == 1);
        }