public void CastVote_WithUserThatCannotVote_IgnoresVote() { // Arrange var couchDbMock = new Mock <ICouchDBService>(); couchDbMock.Setup(c => c.FindByUsername <User>(It.IsAny <string>())).Returns(new [] { new User { Username = "******" } }); couchDbMock.Setup(c => c.FindByUsername <Vote>(It.IsAny <string>())).Returns(new[] { new Vote { Username = "******" }, new Vote { Username = "******" }, new Vote { Username = "******" } }); // Act var votingService = new VotingService(couchDbMock.Object); votingService.CastVote("*****@*****.**", 1, 2); // Assert couchDbMock.Verify(c => c.Set <Vote>(It.IsAny <Vote>()), Times.Never); }
private async void ClientVoteCompletedPage_Loaded(object sender, RoutedEventArgs e) { var election = await BlobCache.UserAccount.GetObject <Setting>("ElectionSettings"); MainGrid.Background = new ImageBrush(Util.BytesToBitmapImage(election.Logo)) { Opacity = 0.2 }; try { //make rabbitmq event here for submission of votes //submission of skipped votes await VotingService.CastVote(_votes, _voter, _skippedVotes); Text.Text = $"Good Bye {_voter.VoterName.ToUpper()}, Thank You For Voting"; } catch (Exception) { Text.Text = $"Sorry An Error Occured.\nYour Votes Were not Submitted.\n Contact the Administrators"; await VotingService.ResetVoter(_voter); } var _timer = new DispatcherTimer(); _timer.Interval = new TimeSpan(0, 0, 0, 3); _timer.Tick += _timer_Tick; _timer.Start(); }
public void CastVote_WithNonExistentUser_ThrowsException() { // Arrange var couchDbMock = new Mock <ICouchDBService>(); couchDbMock.Setup(c => c.FindByUsername <User>(It.IsAny <string>())).Returns(new List <User>()); // Act var votingService = new VotingService(couchDbMock.Object); votingService.CastVote("*****@*****.**", 1, 2); }
public void CastVote_NoCitizen_Throws() { // Arrange A.CallTo(() => _repo.GetCitizen(CitizenId)).Returns(null); // Act var exception = Record.Exception(() => _service.CastVote(CitizenId, BallotItemId, ValidOption, null)); // Assert Assert.NotNull(exception); Assert.IsType <RecordNotFoundException>(exception); Assert.Equal("Could not find that citizen", exception.Message); A.CallTo(() => _repo.GetCitizen(A <int> ._)).MustHaveHappenedOnceExactly(); A.CallTo(() => _repo.GetBallotItem(A <int> ._)).MustNotHaveHappened(); A.CallTo(() => _repo.IsCitizenEligibleToVoteOnBallotItem(A <int> ._, A <int> ._)).MustNotHaveHappened(); A.CallTo(() => _repo.GetVote(A <int> ._, A <int> ._)).MustNotHaveHappened(); A.CallTo(() => _repo.AddVote(A <int> ._, A <int> ._, A <int> ._, A <string> ._)).MustNotHaveHappened(); }
static void Vote(HttpClient client) { var votingService = new VotingService(); Console.WriteLine("You are voting now"); Console.WriteLine("UserName: "******"Password"); var password = Console.ReadLine(); Console.WriteLine("CNP: "); var cnp = Console.ReadLine(); Console.WriteLine("ID Series: "); var series = Console.ReadLine(); Console.WriteLine("ID Number: "); var number = Console.ReadLine(); var inVoterLoginDto = new InVoterLoginDTO() { UserName = username, CNP = cnp, Number = number, Series = series, Password = password }; var res = client.PostAsync("https://localhost:44355/api/auth/login-voter", new StringContent(JsonSerializer.Serialize(inVoterLoginDto, typeof(InVoterLoginDTO)), Encoding.UTF8, "application/json")) .Result; var credentials = JsonSerializer.Deserialize <OutVoterLoginDTO>(res.Content.ReadAsStringAsync().Result, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }); var res2 = client.GetStringAsync("https://localhost:44355/api/voting/candidates").Result; var candidates = JsonSerializer.Deserialize <BaseResponseDTO <List <CandidateDTO> > >(res2, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }).Result; int i = 0; foreach (var candidate in candidates) { Console.WriteLine($"Option {i}, {candidate.Name}, {candidate.Party}\n\n"); i++; } Console.WriteLine("\n\nYour option: "); var option = int.Parse(Console.ReadLine()); var candidateSelected = candidates[option]; var candidateId = candidateSelected.Id; votingService.InitialiseKeys(credentials.EncryptedPrivateKey, credentials.PublicKey, password); var userSignedVote = votingService.CastVote(credentials.UserId, candidateId, credentials.CAuthPublicKey); userSignedVote.Token = credentials.Token; userSignedVote.UserName = username; var res3 = client.PostAsync("https://localhost:44355/api/voting/cast-vote", new StringContent( JsonSerializer.Serialize(userSignedVote, typeof(UserSignedVoteDTO)), Encoding.UTF8, "application/json")) .Result; var response = res3.Content.ReadAsStringAsync().Result; Console.WriteLine(response); }