public void Submission_Up() { string userName = USERNAMES.User50CCP; var user = TestHelper.SetPrincipal(userName, null); using (var db = new Voat.Data.Repository(user)) { var x = db.GetSubmission(context.SubmissionID); var ups = x.UpCount; var downs = x.DownCount; var response = db.VoteSubmission(context.SubmissionID, 1, IpHash.CreateHash("127.0.0.1")); var expectedUpCount = ups + 1; var expectedDownCount = downs; Assert.AreEqual(expectedUpCount, response.Response.UpCount, "Response UpCount is off"); Assert.AreEqual(expectedDownCount, response.Response.DownCount, "Response DownCount is off"); //pull fresh data and compare x = db.GetSubmission(context.SubmissionID); Assert.AreEqual(expectedUpCount, x.UpCount, "Database UpCount is off"); Assert.AreEqual(expectedDownCount, x.DownCount, "Database DownCount is off"); TestDirectVoteAccess(userName, Domain.Models.ContentType.Submission, context.SubmissionID, 1); } }
public void Submission_Reset_NoRevoke() { var user = TestHelper.SetPrincipal(USERNAMES.User100CCP, null); using (var db = new Voat.Data.Repository(user)) { var x = db.GetSubmission(context.SubmissionID); var ups = x.UpCount; var downs = x.DownCount; var response = db.VoteSubmission(context.SubmissionID, 1, IpHash.CreateHash("127.0.0.1")); Assert.AreEqual(Status.Success, response.Status, "Vote was not successfull"); Assert.AreEqual(1, response.RecordedValue, "Recorded value off"); var expectedUpCount = ups + 1; var expectedDownCount = downs; Assert.AreEqual(expectedUpCount, response.Response.UpCount, "Response UpCount is off"); Assert.AreEqual(expectedDownCount, response.Response.DownCount, "Response DownCount is off"); //pull fresh data and compare x = db.GetSubmission(context.SubmissionID); Assert.AreEqual(expectedUpCount, x.UpCount, "Database UpCount is off"); Assert.AreEqual(expectedDownCount, x.DownCount, "Database DownCount is off"); //try to re-up vote response = db.VoteSubmission(context.SubmissionID, 1, IpHash.CreateHash("127.0.0.1"), false); Assert.IsTrue(response.Status == Status.Ignored); //setting tells voting to ignore if submitted vote is current vote Assert.IsTrue(response.RecordedValue == 1); //should still be an upvote } }
public void DownvoteComment() { TestHelper.SetPrincipal("User500CCP"); bool voteEventReceived = false; EventNotification.Instance.OnVoteReceived += (s, e) => { voteEventReceived = e.TargetUserName == "unit" && e.SendingUserName == "User500CCP" && e.ChangeValue == -1 && e.ReferenceType == Domain.Models.ContentType.Comment && e.ReferenceID == 1; }; var cmd = new CommentVoteCommand(1, -1, IpHash.CreateHash("127.0.0.1")); var c = cmd.Execute().Result; Assert.IsTrue(c.Success); Assert.IsNotNull(c.Response); //verify in db using (var db = new Voat.Data.Repository()) { var comment = db.GetComment(1); Assert.IsNotNull(comment, "Couldn't find comment in db"); Assert.AreEqual(comment.UpCount, c.Response.UpCount); Assert.AreEqual(comment.DownCount, c.Response.DownCount); } Assert.IsTrue(voteEventReceived, "VoteEvent not have the expected values"); }
public void Submission_Down() { string userName = USERNAMES.User500CCP; var user = TestHelper.SetPrincipal(userName, null); //This user has one comment with 101 likes using (var db = new Voat.Data.Repository(user)) { var x = db.GetSubmission(context.SubmissionID); var ups = x.UpCount; var downs = x.DownCount; var response = db.VoteSubmission(context.SubmissionID, -1, IpHash.CreateHash("127.0.0.1")); Assert.AreEqual(Status.Success, response.Status, response.Message); Assert.AreEqual(-1, response.RecordedValue, "Recorded value off"); var expectedUpCount = ups; var expectedDownCount = downs + 1; Assert.AreEqual(expectedUpCount, response.Response.UpCount, "Response UpCount is off"); Assert.AreEqual(expectedDownCount, response.Response.DownCount, "Response DownCount is off"); //pull fresh data and compare x = db.GetSubmission(context.SubmissionID); Assert.AreEqual(expectedUpCount, x.UpCount, "Database UpCount is off"); Assert.AreEqual(expectedDownCount, x.DownCount, "Database DownCount is off"); TestDirectVoteAccess(userName, Domain.Models.ContentType.Submission, context.SubmissionID, -1); } }
public void Comment_Reset_Default() { using (var db = new Voat.Data.Repository()) { var x = db.GetComment(context.CommentID); var ups = x.UpCount; var downs = x.DownCount; TestHelper.SetPrincipal("User100CCP", null); var response = db.VoteComment(context.CommentID, 1, IpHash.CreateHash("127.0.0.1")); //refresh comment x = db.GetComment(context.CommentID); Assert.IsTrue(response.Success, response.ToString()); Assert.AreEqual(1, response.RecordedValue, "Vote value incorrect"); Assert.AreEqual((ups + 1), x.UpCount, "UpCount Off"); //try to re-up vote, by default should revoke vote response = db.VoteComment(context.CommentID, 1, IpHash.CreateHash("127.0.0.1")); Assert.AreEqual(Status.Success, response.Status); Assert.AreEqual(0, response.RecordedValue, "Vote value incorrect"); ////try to reset vote //response = repository.VoteComment(context.CommentID, 0); //Assert.IsTrue(response.Successfull); //refresh comment x = db.GetComment(context.CommentID); Assert.AreEqual(ups, x.UpCount, "Final Up Count off"); Assert.AreEqual(downs, x.DownCount, "Final Down Count off"); } }
public async Task Comment_Down() { string userName = USERNAMES.User500CCP; var user = TestHelper.SetPrincipal(userName, null); //This user has one comment with 101 likes using (var db = new Voat.Data.Repository(user)) { var x = await db.GetComment(context.CommentID); var ups = x.UpCount; var downs = x.DownCount; var response = db.VoteComment(context.CommentID, -1, IpHash.CreateHash("127.0.0.1")); Assert.IsTrue(response.Success, response.Message); Assert.AreEqual(-1, response.RecordedValue, "Vote was not successfull"); //refresh comment x = await db.GetComment(context.CommentID); Assert.AreEqual(ups, x.UpCount); Assert.AreEqual((downs + 1), x.DownCount); TestDirectVoteAccess(userName, Domain.Models.ContentType.Comment, context.CommentID, -1); } }
public void EnsureInvalidVoteValueThrowsException_Sub() { using (var db = new Voat.Data.Repository()) { db.VoteSubmission(1, 21, IpHash.CreateHash("127.0.0.1")); } }
public async Task Comment_Up() { string userName = USERNAMES.User50CCP; var user = TestHelper.SetPrincipal(userName, null); using (var db = new Voat.Data.Repository(user)) { var x = await db.GetComment(context.CommentID); var ups = x.UpCount; var downs = x.DownCount; var response = db.VoteComment(context.CommentID, 1, IpHash.CreateHash("127.0.0.1")); //refresh comment x = await db.GetComment(context.CommentID); Assert.IsTrue(response.Success, response.ToString()); Assert.AreEqual(1, response.RecordedValue, "Vote value incorrect"); Assert.AreEqual((ups + 1), x.UpCount, "UpCount Off"); Assert.AreEqual(downs, x.DownCount, "DownCount Off"); TestDirectVoteAccess(userName, Domain.Models.ContentType.Comment, context.CommentID, 1); } }
public void EnsureInvalidVoteValueThrowsException_Com() { using (var db = new Voat.Data.Repository()) { db.VoteComment(121, -2, IpHash.CreateHash("127.0.0.1")); } }
public async Task <JsonResult> Vote(int submissionID, int typeOfVote) { var cmd = new SubmissionVoteCommand(submissionID, typeOfVote, IpHash.CreateHash(UserHelper.UserIpAddress(this.Request))); var result = await cmd.Execute(); return(Json(result)); }
public async Task <JsonResult> VoteComment(int commentId, int typeOfVote) { var cmd = new CommentVoteCommand(commentId, typeOfVote, IpHash.CreateHash(UserHelper.UserIpAddress(this.Request))); var result = await cmd.Execute(); return(Json(result)); }
public async Task Comment_Reset_NoRevoke() { var user = TestHelper.SetPrincipal(USERNAMES.User100CCP, null); using (var db = new Voat.Data.Repository(user)) { var x = await db.GetComment(context.CommentID); var ups = x.UpCount; var downs = x.DownCount; var response = db.VoteComment(context.CommentID, 1, IpHash.CreateHash("127.0.0.1")); //refresh comment x = await db.GetComment(context.CommentID); Assert.IsTrue(response.Success, response.ToString()); Assert.AreEqual(1, response.RecordedValue, "Vote value incorrect"); Assert.AreEqual((ups + 1), x.UpCount, "UpCount Off"); //try to re-up vote, by default should revoke vote response = db.VoteComment(context.CommentID, 1, IpHash.CreateHash("127.0.0.1"), false); Assert.AreEqual(Status.Ignored, response.Status); Assert.AreEqual(1, response.RecordedValue, "Vote value incorrect"); } }
public void Submission_Reset_Default() { var user = TestHelper.SetPrincipal(USERNAMES.User100CCP, null); using (var db = new Voat.Data.Repository(user)) { var x = db.GetSubmission(context.SubmissionID); var ups = x.UpCount; var downs = x.DownCount; var response = db.VoteSubmission(context.SubmissionID, 1, IpHash.CreateHash("127.0.0.1")); Assert.AreEqual(Status.Success, response.Status, "Vote was not successfull"); Assert.AreEqual(1, response.RecordedValue, "Recorded value off"); var expectedUpCount = ups + 1; var expectedDownCount = downs; Assert.AreEqual(expectedUpCount, response.Response.UpCount, "Response UpCount is off"); Assert.AreEqual(expectedDownCount, response.Response.DownCount, "Response DownCount is off"); //pull fresh data and compare x = db.GetSubmission(context.SubmissionID); Assert.AreEqual(expectedUpCount, x.UpCount, "Database UpCount is off"); Assert.AreEqual(expectedDownCount, x.DownCount, "Database DownCount is off"); //try to re-up vote response = db.VoteSubmission(context.SubmissionID, 1, IpHash.CreateHash("127.0.0.1")); Assert.IsTrue(response.Status == Status.Success); Assert.IsTrue(response.RecordedValue == 0); } }
public void InvalidVoteValue_Submission_Low() { TestHelper.SetPrincipal("unit"); var cmd = new CommentVoteCommand(1, -2, IpHash.CreateHash("127.0.0.1")); var c = cmd.Execute().Result; }
public void InvalidVoteValue_Comment_Low() { VoatAssert.Throws<ArgumentOutOfRangeException>(() => { var user = TestHelper.SetPrincipal(USERNAMES.Unit); var cmd = new CommentVoteCommand(1, -2, IpHash.CreateHash("127.0.0.1")).SetUserContext(user); var c = cmd.Execute().Result; }); }
public void DownvoteComment_MinCCP() { var user = TestHelper.SetPrincipal(USERNAMES.User0CCP); var cmd = new CommentVoteCommand(5, -1, IpHash.CreateHash("127.0.0.1")).SetUserContext(user); //SubmissionID: 3 is in MinCCP sub var c = cmd.Execute().Result; Assert.IsFalse(c.Success); Assert.IsNull(c.Response); }
public void Submission_Down_NoCCP() { var user = TestHelper.SetPrincipal("TestUser03", null); //Random User has no CCP using (var db = new Voat.Data.Repository(user)) { var response = db.VoteSubmission(context.SubmissionID, -1, IpHash.CreateHash("127.0.0.1")); Assert.AreEqual(Status.Denied, response.Status); } }
public void Comment_Down_NoCCP() { using (var db = new Voat.Data.Repository()) { TestHelper.SetPrincipal("TestUser3", null); //Random User has no CCP var response = db.VoteComment(context.CommentID, -1, IpHash.CreateHash("127.0.0.1")); Assert.AreEqual(Status.Denied, response.Status); } }
public void DownvoteSubmission_MinCCP() { TestHelper.SetPrincipal("User0CCP"); var cmd = new SubmissionVoteCommand(3, -1, IpHash.CreateHash("127.0.0.1")); //SubmissionID: 3 is in MinCCP sub var c = cmd.Execute().Result; Assert.IsFalse(c.Success); Assert.IsNull(c.Response); }
public void EnsureInvalidVoteValueThrowsException_Sub() { string userName = USERNAMES.User500CCP; var user = TestHelper.SetPrincipal(userName, null); //This user has one comment with 101 likes VoatAssert.Throws <ArgumentOutOfRangeException>(() => { using (var db = new Voat.Data.Repository(user)) { db.VoteSubmission(1, 21, IpHash.CreateHash("127.0.0.1")); } }); }
public void DownVoat_Comment_Denied_MinCCPInSubverse() { TestHelper.SetPrincipal("User100CCP"); var context = new VoatRuleContext(); context.PropertyBag.CommentID = 5;//A minCCP of 5000 is required in this comment sub context.PropertyBag.AddressHash = IpHash.CreateHash("127.0.0.1"); var outcome = UnitTestRulesEngine.Instance.EvaluateRuleSet(context, RuleScope.DownVoteComment, RuleScope.DownVote, RuleScope.Vote); Assert.AreEqual(RuleResult.Denied, outcome.Result); Assert.AreEqual("2.4", outcome.RuleNumber); }
public void Bug_Trap_Spam_Votes_VoteCommand() { int submissionID = 1; Submission beforesubmission = GetSubmission(); int exCount = 0; Func <Task <VoteResponse> > vote1 = new Func <Task <VoteResponse> >(async() => { var principle = new System.Security.Principal.GenericPrincipal(new System.Security.Principal.GenericIdentity(USERNAMES.User500CCP, "Bearer"), null); System.Threading.Thread.CurrentPrincipal = principle; var cmd = new SubmissionVoteCommand(submissionID, 1, IpHash.CreateHash("127.0.0.1")); Interlocked.Increment(ref exCount); return(await cmd.Execute());//.Result; }); Func <Task <VoteResponse> > vote2 = new Func <Task <VoteResponse> >(async() => { var principle = new System.Security.Principal.GenericPrincipal(new System.Security.Principal.GenericIdentity(USERNAMES.User100CCP, "Bearer"), null); System.Threading.Thread.CurrentPrincipal = principle; var cmd = new SubmissionVoteCommand(submissionID, 1, IpHash.CreateHash("127.0.0.1")); Interlocked.Increment(ref exCount); return(await cmd.Execute());//.Result; }); //exCount = -2; //var x = vote1().Result; //var y = vote2().Result; var tasks = new List <Task <VoteResponse> >(); for (int i = 0; i < count; i++) { if (i % 2 == 0) { tasks.Add(Task.Run(vote1)); } else { tasks.Add(Task.Run(vote2)); } } Task.WaitAll(tasks.ToArray()); Submission aftersubmission = GetSubmission(); Assert.AreEqual(count, exCount, "Execution count is off"); AssertData(beforesubmission, aftersubmission); }
public void UpvoteSubmission_DeniesSameIP() { var user = TestHelper.SetPrincipal("UnitTestUser45"); var cmd = new SubmissionVoteCommand(1, 1, IpHash.CreateHash("1.1.1.1")).SetUserContext(user); var c = cmd.Execute().Result; VoatAssert.IsValid(c); user = TestHelper.SetPrincipal("UnitTestUser46"); cmd = new SubmissionVoteCommand(1, 1, IpHash.CreateHash("1.1.1.1")).SetUserContext(user); c = cmd.Execute().Result; Assert.IsNotNull(c, "Response is null"); Assert.IsFalse(c.Success, c.Message); Assert.IsNull(c.Response); }
public async Task UpvoteComment() { var user = TestHelper.SetPrincipal(USERNAMES.User50CCP); var cmd = new CommentVoteCommand(1, 1, IpHash.CreateHash("127.0.0.2")).SetUserContext(user); var c = cmd.Execute().Result; VoatAssert.IsValid(c); //verify in db using (var db = new Voat.Data.Repository(user)) { var comment = await db.GetComment(1); Assert.IsNotNull(comment, "Couldn't find comment in db"); Assert.AreEqual(comment.UpCount, c.Response.UpCount); Assert.AreEqual(comment.DownCount, c.Response.DownCount); } }
public void Bug_Trap_Spam_Votes_Repository() { Submission beforesubmission = GetSubmission(); int exCount = 0; Func <VoteResponse> vote1 = new Func <VoteResponse>(() => { var principal = new System.Security.Principal.GenericPrincipal(new System.Security.Principal.GenericIdentity(USERNAMES.User500CCP, "Bearer"), null); System.Threading.Thread.CurrentPrincipal = principal; Interlocked.Increment(ref exCount); using (var repo = new Voat.Data.Repository(principal)) { return(repo.VoteSubmission(submissionID, 1, IpHash.CreateHash("127.0.0.1"))); } }); Func <VoteResponse> vote2 = new Func <VoteResponse>(() => { var principal = new System.Security.Principal.GenericPrincipal(new System.Security.Principal.GenericIdentity(USERNAMES.User100CCP, "Bearer"), null); System.Threading.Thread.CurrentPrincipal = principal; Interlocked.Increment(ref exCount); using (var repo = new Voat.Data.Repository(principal)) { return(repo.VoteSubmission(submissionID, 1, IpHash.CreateHash("127.0.0.1"))); } }); var tasks = new List <Task <VoteResponse> >(); for (int i = 0; i < count; i++) { if (i % 2 == 0) { tasks.Add(Task.Run(vote1)); } else { tasks.Add(Task.Run(vote2)); } } Task.WaitAll(tasks.ToArray()); Submission aftersubmission = GetSubmission(); Assert.AreEqual(count, exCount, "Execution count is off"); AssertData(beforesubmission, aftersubmission); }
public void DownvoteSubmission() { var submissionUser = "******"; var newSubmission = TestHelper.ContentCreation.CreateSubmission(submissionUser, new Domain.Models.UserSubmission() { Title = "This is what I think about you guys", Subverse = SUBVERSES.Unit }); var userName = USERNAMES.User500CCP; var user = TestHelper.SetPrincipal(userName); bool voteEventReceived = false; EventNotification.Instance.OnVoteReceived += (s, e) => { voteEventReceived = e.TargetUserName == submissionUser && e.SendingUserName == userName && e.ChangeValue == -1 && e.ReferenceType == Domain.Models.ContentType.Submission && e.ReferenceID == newSubmission.ID; }; var cmd = new SubmissionVoteCommand(newSubmission.ID, -1, IpHash.CreateHash("127.0.0.100")).SetUserContext(user); var c = cmd.Execute().Result; VoatAssert.IsValid(c); //verify in db using (var db = new Voat.Data.Repository(user)) { var submissionFromRepo = db.GetSubmission(newSubmission.ID); Assert.IsNotNull(submissionFromRepo, "Couldn't find comment in db"); Assert.AreEqual(submissionFromRepo.UpCount, c.Response.UpCount); Assert.AreEqual(submissionFromRepo.DownCount, c.Response.DownCount); } Assert.IsTrue(voteEventReceived, "VoteEvent not have the expected values"); //Verify Submission pull has correct vote value recorded in output for current user var q = new QuerySubmission(newSubmission.ID, true).SetUserContext(user); var submission = q.Execute(); Assert.IsNotNull(submission); Assert.AreEqual(c.RecordedValue, submission.Vote); //Verify non-logged in user has correct vote value TestHelper.SetPrincipal(null); q = new QuerySubmission(1, true); submission = q.Execute(); Assert.IsNotNull(submission); Assert.AreEqual(null, submission.Vote); }
public JsonResult VoteComment(int commentId, int typeOfVote) { lock (_locker) { int dailyVotingQuota = Settings.DailyVotingQuota; var loggedInUser = User.Identity.Name; var userCcp = Karma.CommentKarma(loggedInUser); var scaledDailyVotingQuota = Math.Max(dailyVotingQuota, userCcp / 2); var totalVotesUsedInPast24Hours = UserHelper.TotalVotesUsedInPast24Hours(User.Identity.Name); switch (typeOfVote) { case 1: if (userCcp >= 20) { if (totalVotesUsedInPast24Hours < scaledDailyVotingQuota) { // perform upvoting or resetting VotingComments.UpvoteComment(commentId, loggedInUser, IpHash.CreateHash(UserHelper.UserIpAddress(Request))); } } else if (totalVotesUsedInPast24Hours < 11) { // perform upvoting or resetting even if user has no CCP but only allow 10 votes per 24 hours VotingComments.UpvoteComment(commentId, loggedInUser, IpHash.CreateHash(UserHelper.UserIpAddress(Request))); } break; case -1: if (userCcp >= 100) { if (totalVotesUsedInPast24Hours < scaledDailyVotingQuota) { // perform downvoting or resetting VotingComments.DownvoteComment(commentId, loggedInUser, IpHash.CreateHash(UserHelper.UserIpAddress(Request))); } } break; } Response.StatusCode = 200; return(Json("Voting ok", JsonRequestBehavior.AllowGet)); } }
public void Bug_Trap_Spam_Votes_Repository_2() { Submission beforesubmission = GetSubmission(); int exCount = 0; Func <VoteResponse> vote1 = new Func <VoteResponse>(() => { var user = TestHelper.SetPrincipal(USERNAMES.User500CCP); Interlocked.Increment(ref exCount); using (var repo = new Voat.Data.Repository(user)) { return(repo.VoteSubmission(submissionID, 1, IpHash.CreateHash("127.0.0.1"))); } }); Func <VoteResponse> vote2 = new Func <VoteResponse>(() => { var user = TestHelper.SetPrincipal(USERNAMES.User100CCP); Interlocked.Increment(ref exCount); using (var repo = new Voat.Data.Repository(user)) { return(repo.VoteSubmission(submissionID, 1, IpHash.CreateHash("127.0.0.1"))); } }); var tasks = new List <Task <VoteResponse> >(); for (int i = 0; i < count; i++) { if (i % 2 == 0) { tasks.Add(Task.Run(vote1)); } else { tasks.Add(Task.Run(vote2)); } } Task.WaitAll(tasks.ToArray()); Submission aftersubmission = GetSubmission(); Assert.AreEqual(count, exCount, "Execution count is off"); AssertData(beforesubmission, aftersubmission); }
public void Bug_Trap_Spam_Votes_VoteCommand_2() { int submissionID = 1; Submission beforesubmission = GetSubmission(); int exCount = 0; Func <Task <VoteResponse> > vote1 = new Func <Task <VoteResponse> >(async() => { var user = TestHelper.SetPrincipal(USERNAMES.User500CCP); var cmd = new SubmissionVoteCommand(submissionID, 1, IpHash.CreateHash("127.0.0.1")).SetUserContext(user); Interlocked.Increment(ref exCount); return(await cmd.Execute());//.Result; }); Func <Task <VoteResponse> > vote2 = new Func <Task <VoteResponse> >(async() => { var user = TestHelper.SetPrincipal(USERNAMES.User100CCP); var cmd = new SubmissionVoteCommand(submissionID, 1, IpHash.CreateHash("127.0.0.1")).SetUserContext(user);; Interlocked.Increment(ref exCount); return(await cmd.Execute());//.Result; }); var tasks = new List <Task <VoteResponse> >(); for (int i = 0; i < count; i++) { if (i % 2 == 0) { tasks.Add(Task.Run(vote1)); } else { tasks.Add(Task.Run(vote2)); } } Task.WaitAll(tasks.ToArray()); Submission aftersubmission = GetSubmission(); Assert.AreEqual(count, exCount, "Execution count is off"); AssertData(beforesubmission, aftersubmission); }
public async Task <JsonResult> Vote(Domain.Models.ContentType contentType, int id, int voteStatus) { VoteResponse result = null; switch (contentType) { case Domain.Models.ContentType.Submission: var cmdV = new SubmissionVoteCommand(id, voteStatus, IpHash.CreateHash(Request.RemoteAddress())).SetUserContext(User); result = await cmdV.Execute(); break; case Domain.Models.ContentType.Comment: var cmdC = new CommentVoteCommand(id, voteStatus, IpHash.CreateHash(Request.RemoteAddress())).SetUserContext(User); result = await cmdC.Execute(); break; } return(Json(result)); }