From 64676be2462416fb80d2a831effc56dd575fc2ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= <15726+Senen@users.noreply.github.com> Date: Thu, 15 Sep 2022 18:45:28 +0200 Subject: [PATCH] Remove `token` column from `poll_voters` table As it is no longer used as originally pretended [1][2]. [1] Check consul/consul pull request 1994 [2] Check consul/consul pull request 3539 --- app/assets/javascripts/polls.js | 21 ------------------- app/controllers/polls/questions_controller.rb | 3 +-- app/controllers/polls_controller.rb | 2 -- app/helpers/polls_helper.rb | 4 ---- app/models/poll/answer.rb | 4 ++-- app/views/polls/questions/_answers.html.erb | 4 ++-- app/views/polls/questions/_question.html.erb | 2 +- app/views/polls/questions/answer.js.erb | 3 +-- app/views/polls/show.html.erb | 2 +- db/dev_seeds/polls.rb | 3 +-- ...0915154808_remove_token_from_poll_voter.rb | 5 +++++ db/schema.rb | 3 +-- spec/factories/polls.rb | 1 - spec/models/poll/answer_spec.rb | 8 +++---- spec/models/poll/voter_spec.rb | 8 +++---- spec/system/polls/voter_spec.rb | 4 ++-- 16 files changed, 24 insertions(+), 53 deletions(-) create mode 100644 db/migrate/20220915154808_remove_token_from_poll_voter.rb diff --git a/app/assets/javascripts/polls.js b/app/assets/javascripts/polls.js index 6a9f50079..fa0495e27 100644 --- a/app/assets/javascripts/polls.js +++ b/app/assets/javascripts/polls.js @@ -1,28 +1,7 @@ (function() { "use strict"; App.Polls = { - generateToken: function() { - var strings; - strings = Array.apply(null, { - length: 6 - }).map(function() { - return Math.random().toString(36).substr(2); // remove `0.` - }); - return strings.join("").substring(0, 64); - }, - replaceToken: function(token) { - $(".js-question-answer").each(function() { - var token_param; - token_param = this.search.slice(-6); - if (token_param === "token=") { - this.href = this.href + token; - } - }); - }, initialize: function() { - var token; - token = App.Polls.generateToken(); - App.Polls.replaceToken(token); $(".zoom-link").on("click", function(event) { var answer; answer = $(event.target).closest("div.answer"); diff --git a/app/controllers/polls/questions_controller.rb b/app/controllers/polls/questions_controller.rb index 3f2809b75..4f76fc672 100644 --- a/app/controllers/polls/questions_controller.rb +++ b/app/controllers/polls/questions_controller.rb @@ -6,10 +6,9 @@ class Polls::QuestionsController < ApplicationController def answer answer = @question.answers.find_or_initialize_by(author: current_user) - token = params[:token] answer.answer = params[:answer] - answer.save_and_record_voter_participation(token) + answer.save_and_record_voter_participation @answers_by_question_id = { @question.id => params[:answer] } end diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index 154961a6b..f96298914 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -1,6 +1,5 @@ class PollsController < ApplicationController include FeatureFlags - include PollsHelper feature_flag :polls @@ -20,7 +19,6 @@ class PollsController < ApplicationController def show @questions = @poll.questions.for_render.sort_for_list - @token = poll_voter_token(@poll, current_user) @poll_questions_answers = Poll::Question::Answer.where(question: @poll.questions) .with_content.order(:given_order) diff --git a/app/helpers/polls_helper.rb b/app/helpers/polls_helper.rb index 08a2d3ab2..ee4ddb375 100644 --- a/app/helpers/polls_helper.rb +++ b/app/helpers/polls_helper.rb @@ -12,10 +12,6 @@ module PollsHelper booth.name + location end - def poll_voter_token(poll, user) - Poll::Voter.find_by(poll: poll, user: user, origin: "web")&.token || "" - end - def voted_before_sign_in(question) question.answers.where(author: current_user).any? { |vote| current_user.current_sign_in_at > vote.updated_at } end diff --git a/app/models/poll/answer.rb b/app/models/poll/answer.rb index 8742b7d64..685830201 100644 --- a/app/models/poll/answer.rb +++ b/app/models/poll/answer.rb @@ -14,11 +14,11 @@ class Poll::Answer < ApplicationRecord scope :by_author, ->(author_id) { where(author_id: author_id) } scope :by_question, ->(question_id) { where(question_id: question_id) } - def save_and_record_voter_participation(token) + def save_and_record_voter_participation transaction do touch if persisted? save! - Poll::Voter.find_or_create_by!(user: author, poll: poll, origin: "web", token: token) + Poll::Voter.find_or_create_by!(user: author, poll: poll, origin: "web") end end end diff --git a/app/views/polls/questions/_answers.html.erb b/app/views/polls/questions/_answers.html.erb index 02088ab7a..4c521107f 100644 --- a/app/views/polls/questions/_answers.html.erb +++ b/app/views/polls/questions/_answers.html.erb @@ -10,11 +10,11 @@ <% else %> <%= link_to answer.title, - answer_question_path(question, answer: answer.title, token: token), + answer_question_path(question, answer: answer.title), method: :post, remote: true, title: t("poll_questions.show.vote_answer", answer: answer.title), - class: "button secondary hollow js-question-answer" %> + class: "button secondary hollow" %> <% end %> <% end %> <% elsif !user_signed_in? %> diff --git a/app/views/polls/questions/_question.html.erb b/app/views/polls/questions/_question.html.erb index 016c0fa95..ade9c5910 100644 --- a/app/views/polls/questions/_question.html.erb +++ b/app/views/polls/questions/_question.html.erb @@ -4,6 +4,6 @@
- <%= render "polls/questions/answers", question: question, token: token %> + <%= render "polls/questions/answers", question: question %>
diff --git a/app/views/polls/questions/answer.js.erb b/app/views/polls/questions/answer.js.erb index 79978e2c1..f98441d0f 100644 --- a/app/views/polls/questions/answer.js.erb +++ b/app/views/polls/questions/answer.js.erb @@ -1,2 +1 @@ -<% token = poll_voter_token(@question.poll, current_user) %> -$("#<%= dom_id(@question) %>_answers").html("<%= j render("polls/questions/answers", question: @question, token: token) %>"); +$("#<%= dom_id(@question) %>_answers").html("<%= j render("polls/questions/answers", question: @question) %>"); diff --git a/app/views/polls/show.html.erb b/app/views/polls/show.html.erb index 1aa0e89ec..ee4449124 100644 --- a/app/views/polls/show.html.erb +++ b/app/views/polls/show.html.erb @@ -32,7 +32,7 @@ <% end %> <% @questions.each do |question| %> - <%= render "polls/questions/question", question: question, token: @token %> + <%= render "polls/questions/question", question: question %> <% end %> diff --git a/db/dev_seeds/polls.rb b/db/dev_seeds/polls.rb index a4bd79fba..cf9e97fbe 100644 --- a/db/dev_seeds/polls.rb +++ b/db/dev_seeds/polls.rb @@ -146,8 +146,7 @@ section "Creating Poll Voters" do document_number: user.document_number, user: user, poll: poll, - origin: "web", - token: SecureRandom.hex(32)) + origin: "web") end def randomly_answer_questions(poll, user) diff --git a/db/migrate/20220915154808_remove_token_from_poll_voter.rb b/db/migrate/20220915154808_remove_token_from_poll_voter.rb new file mode 100644 index 000000000..068307c64 --- /dev/null +++ b/db/migrate/20220915154808_remove_token_from_poll_voter.rb @@ -0,0 +1,5 @@ +class RemoveTokenFromPollVoter < ActiveRecord::Migration[6.0] + def change + remove_column :poll_voters, :token, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index b3cac4d02..cb93e1c07 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_11_03_112944) do +ActiveRecord::Schema.define(version: 2022_09_15_154808) do # These are extensions that must be enabled in order to support this database enable_extension "pg_trgm" @@ -1210,7 +1210,6 @@ ActiveRecord::Schema.define(version: 2021_11_03_112944) do t.integer "user_id" t.string "origin" t.integer "officer_id" - t.string "token" t.index ["booth_assignment_id"], name: "index_poll_voters_on_booth_assignment_id" t.index ["document_number"], name: "index_poll_voters_on_document_number" t.index ["officer_assignment_id"], name: "index_poll_voters_on_officer_assignment_id" diff --git a/spec/factories/polls.rb b/spec/factories/polls.rb index 03ad24670..212e7998b 100644 --- a/spec/factories/polls.rb +++ b/spec/factories/polls.rb @@ -144,7 +144,6 @@ FactoryBot.define do poll { budget&.poll || association(:poll, budget: budget) } trait :from_web do origin { "web" } - token { SecureRandom.hex(32) } end trait :from_booth do diff --git a/spec/models/poll/answer_spec.rb b/spec/models/poll/answer_spec.rb index 8c0598734..d420e367e 100644 --- a/spec/models/poll/answer_spec.rb +++ b/spec/models/poll/answer_spec.rb @@ -46,7 +46,7 @@ describe Poll::Answer do answer = create(:poll_answer, question: question, author: author, answer: "Yes") expect(answer.poll.voters).to be_blank - answer.save_and_record_voter_participation("token") + answer.save_and_record_voter_participation expect(poll.reload.voters.size).to eq(1) voter = poll.voters.first @@ -57,12 +57,12 @@ describe Poll::Answer do it "updates a poll_voter with user and poll data" do answer = create(:poll_answer, question: question, author: author, answer: "Yes") - answer.save_and_record_voter_participation("token") + answer.save_and_record_voter_participation expect(poll.reload.voters.size).to eq(1) answer = create(:poll_answer, question: question, author: author, answer: "No") - answer.save_and_record_voter_participation("token") + answer.save_and_record_voter_participation expect(poll.reload.voters.size).to eq(1) @@ -76,7 +76,7 @@ describe Poll::Answer do answer = build(:poll_answer) expect do - answer.save_and_record_voter_participation("token") + answer.save_and_record_voter_participation end.to raise_error(ActiveRecord::RecordInvalid) expect(answer).not_to be_persisted diff --git a/spec/models/poll/voter_spec.rb b/spec/models/poll/voter_spec.rb index b30c7428e..e33711ff1 100644 --- a/spec/models/poll/voter_spec.rb +++ b/spec/models/poll/voter_spec.rb @@ -196,21 +196,19 @@ describe Poll::Voter do it "sets user info" do user = create(:user, document_number: "1234A", document_type: "1") - voter = build(:poll_voter, user: user, token: "1234abcd") + voter = build(:poll_voter, user: user) voter.save! expect(voter.document_number).to eq("1234A") expect(voter.document_type).to eq("1") - expect(voter.token).to eq("1234abcd") end it "sets user info with skip verification enabled" do Setting["feature.user.skip_verification"] = true user = create(:user) - voter = build(:poll_voter, user: user, token: "1234abcd") - voter.save! + voter = build(:poll_voter, user: user) - expect(voter.token).to eq("1234abcd") + expect { voter.save! }.not_to raise_exception end end end diff --git a/spec/system/polls/voter_spec.rb b/spec/system/polls/voter_spec.rb index 46305ce4a..336c2db0b 100644 --- a/spec/system/polls/voter_spec.rb +++ b/spec/system/polls/voter_spec.rb @@ -38,8 +38,8 @@ describe "Voter" do visit poll_path(poll) within("#poll_question_#{question.id}_answers") do - expect(page).not_to have_link(answer_yes.title, href: "/questions/#{question.id}/answer?answer=#{answer_yes.title}&token=") - expect(page).not_to have_link(answer_no.title, href: "/questions/#{question.id}/answer?answer=#{answer_no.title}&token=") + expect(page).not_to have_link(answer_yes.title, href: "/questions/#{question.id}/answer?answer=#{answer_yes.title}") + expect(page).not_to have_link(answer_no.title, href: "/questions/#{question.id}/answer?answer=#{answer_no.title}") end expect(page).to have_content("You must verify your account in order to answer")