Esempio n. 1
0
        /// <summary>
        /// Creates the resultat model.
        /// </summary>
        /// <param name="resultat">The resultat.</param>
        /// <param name="scrutin">The scrutin.</param>
        /// <returns></returns>
        /// <exception cref="VoteIn.BL.Exceptions.VoteInException">Type de vote inconnu</exception>
        private IResultatModel CreateResultatModel(Result resultat, VotingProcess scrutin)
        {
            switch (scrutin.VotingProcessMode.Code)
            {
            case Constantes.JUG_MAJ:
                return(new ResultatMajoritaryJudgmentModel
                {
                    IndividualResults = JsonConvert.DeserializeObject <List <ResultatIndividualMajorityJudgmentModel> >(resultat.ScoreDetail)
                });

            case Constantes.VOTING_PROCESS_MAJ:
                return(new ResultatMajorityVotingProcessModel
                {
                    IndividualResults = JsonConvert.DeserializeObject <List <ResultatIndividualMajorityVotingProcessModel> >(resultat.ScoreDetail)
                });

            case Constantes.VOTE_ALTER:
                return(new AlternativeVoteResultatModel
                {
                    Stages = JsonConvert.DeserializeObject <List <AlternativeStageVote> >(resultat.ScoreDetail)
                });

            default:
                throw new VoteInException("Unknown vote type.");
            }
        }
        public void MapScrutinToScrutinModelPasDernierTour()
        {
            var scrutin = new VotingProcess();

            var model = _mapper.MapScrutinToScrutinModel(scrutin);

            Check.That(model.IsLastRound).IsFalse();
        }
Esempio n. 3
0
 /// <summary>
 /// Clores the scrutin.
 /// </summary>
 /// <param name="scrutin">The scrutin.</param>
 /// <exception cref="VoteIn.BL.Exceptions.VoteInException">Ce scrutin est déjà cloturé</exception>
 private void CloreScrutin(VotingProcess scrutin)
 {
     if (scrutin.ClosingDate.HasValue)
     {
         throw new VoteInException("Ce scrutin est déjà cloturé");
     }
     scrutin.ClosingDate = DateTime.Now;
     VotingProcessRepository.Update(scrutin);
 }
Esempio n. 4
0
        /// <summary>
        /// Puts the scrutin.
        /// </summary>
        /// <param name="scrutinId">The scrutin identifier.</param>
        /// <param name="scrutin">The scrutin.</param>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        /// <exception cref="VoteIn.BL.Exceptions.VoteInException">Impossible de cloturer, ce scrutin n'existe pas</exception>
        public VotingProcess PutScrutin(Guid scrutinId, VotingProcess scrutin, ClaimsPrincipal user)
        {
            var scrutinTuUpdte = GetScrutin(scrutinId);

            if (scrutin.IdUser == null || scrutin.IdUser != null && user.FindFirst("Id").Value == scrutin.IdUser)
            {
                VotingProcessRepository.Update(scrutin);
            }
            else
            {
                throw new VoteInException("Impossible de cloturer, ce scrutin n'existe pas");
            }
            return(scrutinTuUpdte);
        }
        public void MapScrutinToScrutinModelActeSansValeur()
        {
            var actes = new List <Act>
            {
                new Act
                {
                    IdVotingProcessOption = 3,
                    IdChoice = 20,
                }
            };
            var choice = new List <Choice>
            {
                new Choice
                {
                    Id    = 20,
                    Name  = "Choisi",
                    Value = 1
                }
            };
            var option = new Option
            {
                Id   = 10,
                Name = "Candidat 1"
            };
            var scrutin = new VotingProcess
            {
                ClosingDate             = new DateTime(2017, 11, 09),
                IdPreviousVotingProcess = 1,
                VotingProcessMode       = new VotingProcessMode
                {
                    Choice = choice
                },
                VotingProcessOption = new List <VotingProcessOption>()
                {
                    new VotingProcessOption()
                    {
                        Id     = 3,
                        Option = option,
                        Act    = actes
                    }
                }
            };

            var model = _mapper.MapScrutinToScrutinModel(scrutin);

            Check.That(model.PossibleChoices).HasSize(choice.Count);
            Check.That(model.Options.First().Suffrages.First().Value).IsEqualTo(choice.First().Value);
        }
Esempio n. 6
0
        /// <summary>
        /// Calculers the resultat.
        /// </summary>
        /// <param name="scrutin">The scrutin.</param>
        private void CalculerResultat(VotingProcess scrutin)
        {
            var scrutinModel  = GetScrutinModel(scrutin);
            var calculateur   = CalculatorFactory.GetCalculator(scrutinModel.Mode);
            var resultatModel = calculateur.CalculateResult(scrutinModel);

            if (resultatModel is ResultatMajorityVotingProcessModel resultatScrutinMajoritaire)
            {
                if (resultatModel.IsValidResult &&
                    !scrutinModel.IsLastRound &&
                    resultatModel.Winner == null)
                {
                    resultatModel.IdNewVotingProcess = CreateTourSuivant(scrutinModel.Id, resultatScrutinMajoritaire);
                }
            }
            SaveResultat(scrutinModel, resultatModel);
        }
 public void GivenUneListeDeScrutin(Table table)
 {
     foreach (var row in table.Rows)
     {
         var scrutin = new VotingProcess()
         {
             Id   = StepHelpers.GetNextId(),
             Name = row["VotingProcess"],
             VotingProcessMode = new VotingProcessMode
             {
                 Choice = new List <Choice>(),
                 Code   = "jugement-majoritaire"
             }
         };
         StepHelpers.AddScrutin(scrutin);
     }
 }
        public void MapScrutinToScrutinModelVodeBlanc()
        {
            var scrutin = new VotingProcess
            {
                VotingProcessOption = new List <VotingProcessOption>()
                {
                    new VotingProcessOption()
                    {
                        Id     = 3,
                        Act    = new List <Act>(),
                        Option = null
                    }
                }
            };

            var model = _mapper.MapScrutinToScrutinModel(scrutin);

            Check.That(model.Options.Single().IsBlankVote).IsTrue();
            Check.That(model.IsBlankVoteTakenIntoAccount).IsTrue();
        }
Esempio n. 9
0
        /// <summary>
        /// Maps the resultat to resultat model.
        /// </summary>
        /// <param name="scrutin">The scrutin.</param>
        /// <param name="resultat">The resultat.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">
        /// scrutin
        /// or
        /// resultat
        /// </exception>
        public IResultatModel MapResultatToResultatModel(VotingProcess scrutin, Result resultat)
        {
            if (scrutin is null)
            {
                throw new ArgumentNullException(nameof(scrutin));
            }
            if (resultat is null)
            {
                throw new ArgumentNullException(nameof(resultat));
            }

            var resultatModel = CreateResultatModel(resultat, scrutin);

            resultatModel.Id            = resultat.Id;
            resultatModel.IsValidResult = resultat.IsValid;
            resultatModel.Voters        = resultat.NbVoters;
            resultatModel.Winner        = scrutin.VotingProcessOption.Select(MapOptionScrutinToOptionModel).FirstOrDefault(o => o.Id == resultat.IdWinningOption);
            resultatModel.Options       = scrutin.VotingProcessOption.Select(MapOptionScrutinToOptionModel).ToList();

            return(resultatModel);
        }
Esempio n. 10
0
        /// <summary>
        /// Maps the scrutin to scrutin model.
        /// </summary>
        /// <param name="scrutin">The scrutin.</param>
        /// <returns></returns>
        /// <exception cref="VoteIn.BL.Exceptions.VoteInException">Aucun scrutin à mapper</exception>
        public VotingProcessModel MapScrutinToScrutinModel(VotingProcess scrutin)
        {
            if (scrutin == null)
            {
                throw new VoteInException("Aucun scrutin à mapper");
            }

            var model = new VotingProcessModel
            {
                Id          = scrutin.Id,
                ClosingDate = scrutin.ClosingDate,
                IsLastRound = scrutin.IdPreviousVotingProcess.HasValue,
                Mode        = scrutin.VotingProcessMode?.Code
            };

            if (scrutin.VotingProcessMode != null)
            {
                var choiceModels = MapChoiceToChoiceModel(scrutin.VotingProcessMode.Choice.ToList());
                model.PossibleChoices.AddRange(choiceModels);
            }

            if (scrutin.VotingProcessOption != null)
            {
                var optionsModels = MapOptionToOptionsModels(scrutin.VotingProcessOption.ToList(), model.PossibleChoices);
                model.Options.AddRange(optionsModels);
            }

            if (scrutin.Suffrage != null)
            {
                var bulletinModels = MapSuffrageToBulletin(scrutin.Suffrage.ToList());
                model.Ballots.AddRange(bulletinModels);
            }

            model.IsBlankVoteTakenIntoAccount = model.Options.Any(o => o.IsBlankVote);

            return(model);
        }
Esempio n. 11
0
 public static VotingProcess AddScrutin(VotingProcess scrutin) => ScrutinsParNom[scrutin.Name] = scrutin;
Esempio n. 12
0
 /// <summary>
 /// Gets the scrutin model.
 /// </summary>
 /// <param name="scrutin">The scrutin.</param>
 /// <returns></returns>
 private VotingProcessModel GetScrutinModel(VotingProcess scrutin)
 {
     return(MapperService.MapScrutinToScrutinModel(scrutin));
 }
Esempio n. 13
0
        public void MapResultatToResultatScrutinMajoritaireModel()
        {
            var optionVainqueur = new OptionsModel
            {
                Id   = 28,
                Name = "Winner"
            };
            var optionPerdant = new OptionsModel
            {
                Id   = 32,
                Name = "Loser"
            };
            var resultatGagnant = new ResultatIndividualMajorityVotingProcessModel
            {
                Option     = optionVainqueur,
                Votes      = 3,
                Percentage = 75m
            };
            var resultatPerdant = new ResultatIndividualMajorityVotingProcessModel
            {
                Option     = optionPerdant,
                Votes      = 1,
                Percentage = 25m
            };

            var scrutin = new VotingProcess
            {
                VotingProcessOption = new List <VotingProcessOption>
                {
                    new VotingProcessOption {
                        Option = new Option {
                            Id = optionVainqueur.Id, Name = optionVainqueur.Name
                        }
                    },
                    new VotingProcessOption {
                        Option = new Option {
                            Id = optionPerdant.Id, Name = optionPerdant.Name
                        }
                    }
                },
                VotingProcessMode = new VotingProcessMode {
                    Code = "scrutin-majoritaire"
                }
            };
            var resultat = new Result
            {
                Id = 3,
                IdWinningOption = optionVainqueur.Id,
                IsValid         = true,
                NbVoters        = 3,
                ScoreDetail     = JsonConvert.SerializeObject(new List <ResultatIndividualMajorityVotingProcessModel> {
                    resultatGagnant, resultatPerdant
                })
            };

            var resultatModel = _mapper.MapResultatToResultatModel(scrutin, resultat) as ResultatMajorityVotingProcessModel;

            Check.That(resultatModel.Id).IsEqualTo(resultat.Id);
            Check.That(resultatModel.IsValidResult).IsEqualTo(resultat.IsValid);
            Check.That(resultatModel.Voters).IsEqualTo(resultat.NbVoters);
            Check.That(resultatModel.Winner.Id).IsEqualTo(resultat.IdWinningOption);
            Check.That(resultatModel.Winner.Name).IsEqualTo(optionVainqueur.Name);

            var resultatsIndividuels = resultatModel.IndividualResults;

            Check.That(resultatsIndividuels[0].Votes).IsEqualTo(resultatGagnant.Votes);
            Check.That(resultatsIndividuels[0].Percentage).IsEqualTo(resultatGagnant.Percentage);
            Check.That(resultatsIndividuels[0].Option.Id).IsEqualTo(resultatGagnant.Option.Id);

            Check.That(resultatsIndividuels[1].Votes).IsEqualTo(resultatPerdant.Votes);
            Check.That(resultatsIndividuels[1].Percentage).IsEqualTo(resultatPerdant.Percentage);
            Check.That(resultatsIndividuels[1].Option.Id).IsEqualTo(resultatPerdant.Option.Id);
        }
Esempio n. 14
0
        public void MapScrutinToScrutinModel()
        {
            var actes = new List <Act>
            {
                new Act
                {
                    IdSuffrage            = 15,
                    IdVotingProcessOption = 3,
                    IdChoice = 20,
                    Value    = 1
                }
            };
            var choice = new List <Choice>
            {
                new Choice
                {
                    Id    = 20,
                    Name  = "Choisi",
                    Value = 1
                }
            };
            var option = new Option
            {
                Id   = 10,
                Name = "Candidat 1"
            };
            var scrutin = new VotingProcess
            {
                Id                      = 3,
                ClosingDate             = new DateTime(2017, 11, 09),
                IdPreviousVotingProcess = 1,
                VotingProcessMode       = new VotingProcessMode
                {
                    Choice = choice,
                    Code   = "scrutin-majoritaire"
                },
                VotingProcessOption = new List <VotingProcessOption>()
                {
                    new VotingProcessOption()
                    {
                        Id     = 3,
                        Option = option,
                        Act    = actes
                    }
                }
            };

            var model = _mapper.MapScrutinToScrutinModel(scrutin);

            Check.That(model.Id).IsEqualTo(scrutin.Id);
            Check.That(model.ClosingDate).IsEqualTo(scrutin.ClosingDate);
            Check.That(model.IsLastRound).IsTrue();
            Check.That(model.IsBlankVoteTakenIntoAccount).IsFalse();

            //Choice
            Check.That(model.PossibleChoices).HasSize(choice.Count);
            var choiceModel = model.PossibleChoices.First();
            var choiceBdd   = choice.First();

            Check.That(choiceModel.Id).IsEqualTo(choiceBdd.Id);
            Check.That(choiceModel.Name).IsEqualTo(choiceBdd.Name);
            Check.That(choiceModel.Value).IsEqualTo(choiceBdd.Value);

            //Option
            Check.That(model.Options).HasSize(1);
            var optionModel = model.Options.First();

            Check.That(optionModel.Id).IsEqualTo(option.Id);
            Check.That(optionModel.Name).IsEqualTo(option.Name);
            Check.That(optionModel.IsBlankVote).IsFalse();

            //Suffrage
            var suffragesModel = model.Options.SelectMany(o => o.Suffrages).ToList();

            Check.That(suffragesModel).HasSize(actes.Count);
            Check.That(suffragesModel.First().Value).IsEqualTo(actes.First().Value);
        }
Esempio n. 15
0
        /// <summary>
        /// Gets the options qualifiees.
        /// </summary>
        /// <param name="resultatModel">The resultat model.</param>
        /// <param name="ancienScrutin">The ancien scrutin.</param>
        /// <returns></returns>
        private List <VotingProcessOption> GetOptionsQualifiees(ResultatMajorityVotingProcessModel resultatModel, VotingProcess ancienScrutin)
        {
            Option GetOptionAncienScrutin(OptionsModel o) => ancienScrutin.VotingProcessOption.First(os => os.Option.Id == o.Id).Option;

            var resultatsQualifies = resultatModel.IndividualResults.Take(2);

            return(resultatsQualifies.Select(o => new VotingProcessOption {
                Option = GetOptionAncienScrutin(o.Option)
            }).ToList());
        }
Esempio n. 16
0
        /// <summary>
        /// Counts the vote.
        /// </summary>
        /// <param name="votingProcess">The scrutin.</param>
        public void CountVote(VotingProcess votingProcess)
        {
            var votes      = new List <JObject>();
            var enveloppes = GetAll(filter: e => e.IdVotingProcess == votingProcess.Id);

            //Décoder la clé AES avec la clé privée RSA
            using (TextReader sr = new StringReader(votingProcess.GetPEMString()))
            {
                var pr        = new PemReader(sr);
                var KeyPair   = (AsymmetricCipherKeyPair)pr.ReadObject();
                var rsaParams = DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters)KeyPair.Private);
                using (var rsa = new RSACryptoServiceProvider())
                {
                    rsa.ImportParameters(rsaParams);
                    foreach (var enveloppe in enveloppes)
                    {
                        try
                        {
                            var aesKey = Convert.FromBase64String(Encoding.UTF8.GetString(rsa.Decrypt(Convert.FromBase64String(enveloppe.Key), false)));

                            var counter = new byte[16];
                            for (var i = 0; i < 15; i++)
                            {
                                counter[i] = 0;
                            }
                            counter[15] = 1;

                            //Décoder le contenu avec la clé AES
                            var jsonBytes  = CTREncryptBytes(Convert.FromBase64String(enveloppe.Content), aesKey, counter);
                            var jsonString = Encoding.UTF8.GetString(jsonBytes);

                            votes.Add(JObject.Parse(jsonString));
                            //TODO Delete(enveloppe);
                        }
                        catch
                        {
                            //TODO : Traiter le cas où l'on rencontre une enveloppe invalide.
                        }
                    }
                }
            }
            Save();
            foreach (var vote in votes)
            {
                switch (votingProcess.VotingProcessMode.Code)
                {
                case Constantes.VOTE_ALTER:
                    suffrageRepository.AddVoteAlternatif(vote.ToObject <VoteAlternative>());
                    break;

                case Constantes.VOTING_PROCESS_MAJ:
                    suffrageRepository.AddVoteScrutinMajoritaire(vote.ToObject <VoteMajoritaryVotingProcess>());
                    break;

                case Constantes.JUG_MAJ:
                    suffrageRepository.AddVoteJugementMajoritaire(vote.ToObject <VoteMajoritaryJudgment>());
                    break;

                case Constantes.CONDOR_RANDOM:
                    suffrageRepository.AddVoteAlternatif(vote.ToObject <VoteAlternative>());
                    break;
                }
            }
        }
Esempio n. 17
0
        public async Task CreateScrutin_ScrutinMajoritaire_Success()
        {
            SetupDatabase();

            var modeScrutins = await SendJsonResponseAsync <IEnumerable <VotingProcessMode> >(HttpMethod.Get, $"/api/ModeScrutin", null);

            var scrutinMajoritaire = modeScrutins.First(m => m.Code == "scrutin-majoritaire");

            var votingProcess = new VotingProcess()
            {
                Name                = "VotingProcess Majoritaire",
                Description         = "test scrutin majoritaire",
                Public              = true,
                OpeningDate         = DateTime.Now,
                Author              = "test",
                AuthorMail          = "*****@*****.**",
                IdVotingProcessMode = scrutinMajoritaire.Id,
                VotingProcessOption = new Collection <VotingProcessOption>()
                {
                    new VotingProcessOption()
                    {
                        Option = new Option()
                        {
                            Name = "a"
                        }
                    },
                    new VotingProcessOption()
                    {
                        Option = new Option()
                        {
                            Name = "b"
                        }
                    },
                    new VotingProcessOption()
                    {
                        Option = new Option()
                        {
                            Name = "c"
                        }
                    }
                }
            };

            votingProcess = await SendJsonResponseAsync <VotingProcess>(HttpMethod.Post, $"/api/VotingProcess", votingProcess);

            var listChoice = await SendJsonResponseAsync <IEnumerable <Choice> >(HttpMethod.Get, $"/api/Choice", null);

            var choice = listChoice.First(c => c.IdVotingProcessMode == votingProcess.IdVotingProcessMode && c.Value == 1);

            //Vote
            var votes  = new int[100];
            var random = new Random();

            for (var i = 0; i < votes.Length; i++)
            {
                votes[i] = random.Next(0, votes.Length);
            }

            foreach (var value in votes)
            {
                var candidat = 0;

                if (value > 66)
                {
                    candidat = 2;
                }
                else if (value > 33)
                {
                    candidat = 1;
                }

                var vote = new
                {
                    IdScrutin = votingProcess.Id,
                    IdOption  = votingProcess.VotingProcessOption.ElementAt(candidat).IdOption,
                    _type     = "VoteMajoritaryVotingProcess"
                };

                await SendNoResponseAsync(HttpMethod.Post, $"/api/suffrage/sendVote", vote);
            }

            //Closing
            await SendNoResponseAsync(HttpMethod.Post, $"/api/scrutin/{votingProcess.Id}/clore", null);
        }
Esempio n. 18
0
        public async Task CreateScrutin_JugementMajoritaire_Success()
        {
            SetupDatabase();

            var scrutin = new VotingProcess()
            {
                Name                = "Jugement Majoritaire",
                Description         = "test jugement majoritaire",
                Public              = true,
                OpeningDate         = DateTime.Now,
                Author              = "Julien",
                AuthorMail          = "*****@*****.**",
                IdVotingProcessMode = 2,
                VotingProcessOption = new Collection <VotingProcessOption>()
                {
                    new VotingProcessOption()
                    {
                        Option = new Option()
                        {
                            Name = "Emmanuel Macron", Color = "#DDDDDD"
                        }
                    },
                    new VotingProcessOption()
                    {
                        Option = new Option()
                        {
                            Name = "Marine Le Pen", Color = "#000055"
                        }
                    },
                    new VotingProcessOption()
                    {
                        Option = new Option()
                        {
                            Name = "Philippe Douste-Blazy", Color = "#FF851B"
                        }
                    }
                }
            };

            scrutin = await SendJsonResponseAsync <VotingProcess>(HttpMethod.Post, $"/api/VotingProcess", scrutin);

            var choice = await SendJsonResponseAsync <IEnumerable <Choice> >(HttpMethod.Get, $"/api/Choice", null);

            choice = choice.Where(c => c.IdVotingProcessMode == scrutin.IdVotingProcessMode).ToList();

            //Vote
            var random = new Random();

            for (var i = 0; i < 100; i++)
            {
                var vote = new
                {
                    IdScrutin    = scrutin.Id,
                    OptionChoice = new[]
                    {
                        new {
                            IdOption = scrutin.VotingProcessOption.ElementAt(0).IdOption,
                            IdChoice = choice.ElementAt(random.Next(0, 7)).Id,
                        },
                        new {
                            IdOption = scrutin.VotingProcessOption.ElementAt(1).IdOption,
                            IdChoice = choice.ElementAt(random.Next(0, 7)).Id,
                        },
                        new {
                            IdOption = scrutin.VotingProcessOption.ElementAt(2).IdOption,
                            IdChoice = choice.ElementAt(random.Next(0, 7)).Id,
                        },
                    },
                    _type = "VoteMajoritaryJudgment"
                };

                await SendNoResponseAsync(HttpMethod.Post, $"/api/suffrage/sendVote", vote);
            }

            //Closing
            await SendNoResponseAsync(HttpMethod.Post, $"/api/scrutin/{scrutin.Id}/clore", null);
        }