Don’t allow comments on questions for unverified users and when the phase is closed
This commit is contained in:
@@ -1,6 +1,10 @@
|
|||||||
class CommentsController < ApplicationController
|
class CommentsController < ApplicationController
|
||||||
|
include CustomUrlsHelper
|
||||||
|
|
||||||
before_action :authenticate_user!, only: :create
|
before_action :authenticate_user!, only: :create
|
||||||
before_action :load_commentable, only: :create
|
before_action :load_commentable, only: :create
|
||||||
|
before_action :verify_resident_for_commentable!, only: :create
|
||||||
|
before_action :verify_comments_open!, only: [:create, :vote]
|
||||||
before_action :build_comment, only: :create
|
before_action :build_comment, only: :create
|
||||||
|
|
||||||
load_and_authorize_resource
|
load_and_authorize_resource
|
||||||
@@ -77,4 +81,20 @@ class CommentsController < ApplicationController
|
|||||||
Notification.add(notifiable.author_id, notifiable) unless comment.author_id == notifiable.author_id
|
Notification.add(notifiable.author_id, notifiable) unless comment.author_id == notifiable.author_id
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def verify_resident_for_commentable!
|
||||||
|
return if current_user.administrator? || current_user.moderator?
|
||||||
|
|
||||||
|
if @commentable.respond_to?(:comments_for_verified_residents_only?) && @commentable.comments_for_verified_residents_only?
|
||||||
|
verify_resident!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def verify_comments_open!
|
||||||
|
return if current_user.administrator? || current_user.moderator?
|
||||||
|
|
||||||
|
if @commentable.respond_to?(:comments_closed?) && @commentable.comments_closed?
|
||||||
|
redirect_to @commentable, alert: t('comments.comments_closed')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
@@ -75,4 +75,22 @@ module CommentsHelper
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def require_verified_resident_for_commentable?(commentable, current_user)
|
||||||
|
return false if current_user.administrator? || current_user.moderator?
|
||||||
|
|
||||||
|
commentable.respond_to?(:comments_for_verified_residents_only?) && commentable.comments_for_verified_residents_only? && !current_user.residence_verified?
|
||||||
|
end
|
||||||
|
|
||||||
|
def comments_closed_for_commentable?(commentable)
|
||||||
|
commentable.respond_to?(:comments_closed?) && commentable.comments_closed?
|
||||||
|
end
|
||||||
|
|
||||||
|
def comments_closed_text(commentable)
|
||||||
|
if commentable.class == Legislation::Question
|
||||||
|
t("legislation.questions.comments.comments_closed")
|
||||||
|
else
|
||||||
|
t("comments.comments_closed")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -27,4 +27,16 @@ class Legislation::Question < ActiveRecord::Base
|
|||||||
def answer_for_user(user)
|
def answer_for_user(user)
|
||||||
answers.where(user: user).first
|
answers.where(user: user).first
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def comments_for_verified_residents_only?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def comments_closed?
|
||||||
|
!comments_open?
|
||||||
|
end
|
||||||
|
|
||||||
|
def comments_open?
|
||||||
|
process.open_phase?(:debate)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -81,7 +81,7 @@
|
|||||||
<%= t("comments.comment.responses", count: 0) %>
|
<%= t("comments.comment.responses", count: 0) %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<% if user_signed_in? %>
|
<% if user_signed_in? && !comments_closed_for_commentable?(comment.commentable) && !require_verified_resident_for_commentable?(comment.commentable, current_user) %>
|
||||||
<span class="divider"> | </span>
|
<span class="divider"> | </span>
|
||||||
<%= link_to(comment_link_text(comment), "",
|
<%= link_to(comment_link_text(comment), "",
|
||||||
class: "js-add-comment-link", data: {'id': dom_id(comment)}) %>
|
class: "js-add-comment-link", data: {'id': dom_id(comment)}) %>
|
||||||
|
|||||||
@@ -13,10 +13,21 @@
|
|||||||
<%= render 'shared/wide_order_selector', i18n_namespace: "comments" %>
|
<%= render 'shared/wide_order_selector', i18n_namespace: "comments" %>
|
||||||
|
|
||||||
<% if user_signed_in? %>
|
<% if user_signed_in? %>
|
||||||
|
<% if comments_closed_for_commentable?(commentable) %>
|
||||||
|
<br>
|
||||||
|
<div data-alert class="callout primary">
|
||||||
|
<%= comments_closed_text(commentable) %>
|
||||||
|
</div>
|
||||||
|
<% elsif require_verified_resident_for_commentable?(commentable, current_user) %>
|
||||||
|
<br>
|
||||||
|
<div data-alert class="callout primary">
|
||||||
|
<%= t("comments.verified_only", verify_account: link_to(t("comments.verify_account"), verification_path )).html_safe %>
|
||||||
|
</div>
|
||||||
|
<% else %>
|
||||||
<%= render 'comments/form', {commentable: commentable, parent_id: nil, toggeable: false} %>
|
<%= render 'comments/form', {commentable: commentable, parent_id: nil, toggeable: false} %>
|
||||||
|
<% end %>
|
||||||
<% else %>
|
<% else %>
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
<div data-alert class="callout primary">
|
<div data-alert class="callout primary">
|
||||||
<%= t("debates.show.login_to_comment",
|
<%= t("debates.show.login_to_comment",
|
||||||
signin: link_to(t("votes.signin"), new_user_session_path),
|
signin: link_to(t("votes.signin"), new_user_session_path),
|
||||||
|
|||||||
@@ -34,6 +34,9 @@ en:
|
|||||||
close: Close
|
close: Close
|
||||||
menu: Menu
|
menu: Menu
|
||||||
comments:
|
comments:
|
||||||
|
comments_closed: Comments are closed
|
||||||
|
verified_only: To participate %{verify_account}
|
||||||
|
verify_account: verify your account
|
||||||
comment:
|
comment:
|
||||||
admin: Administrator
|
admin: Administrator
|
||||||
author: Author
|
author: Author
|
||||||
|
|||||||
@@ -34,6 +34,9 @@ es:
|
|||||||
close: Cerrar
|
close: Cerrar
|
||||||
menu: Menú
|
menu: Menú
|
||||||
comments:
|
comments:
|
||||||
|
comments_closed: Los comentarios están cerrados
|
||||||
|
verified_only: Para participar %{verify_account}
|
||||||
|
verify_account: verifica tu cuenta
|
||||||
comment:
|
comment:
|
||||||
admin: Administrador
|
admin: Administrador
|
||||||
author: Autor
|
author: Autor
|
||||||
|
|||||||
@@ -84,6 +84,7 @@ en:
|
|||||||
comments:
|
comments:
|
||||||
comment_button: Publish answer
|
comment_button: Publish answer
|
||||||
comments_title: Open answers
|
comments_title: Open answers
|
||||||
|
comments_closed: Closed phase
|
||||||
form:
|
form:
|
||||||
leave_comment: Leave your answer
|
leave_comment: Leave your answer
|
||||||
question:
|
question:
|
||||||
|
|||||||
@@ -84,6 +84,7 @@ es:
|
|||||||
comments:
|
comments:
|
||||||
comment_button: Publicar respuesta
|
comment_button: Publicar respuesta
|
||||||
comments_title: Respuestas abiertas
|
comments_title: Respuestas abiertas
|
||||||
|
comments_closed: Fase cerrada
|
||||||
form:
|
form:
|
||||||
leave_comment: Deja tu respuesta
|
leave_comment: Deja tu respuesta
|
||||||
question:
|
question:
|
||||||
|
|||||||
38
spec/controllers/comments_controller_spec.rb
Normal file
38
spec/controllers/comments_controller_spec.rb
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe CommentsController do
|
||||||
|
|
||||||
|
describe 'POST create' do
|
||||||
|
before(:each) do
|
||||||
|
@process = create(:legislation_process, debate_start_date: Date.current - 3.day, debate_end_date: Date.current + 2.days)
|
||||||
|
@question = create(:legislation_question, process: @process, title: "Question 1")
|
||||||
|
@user = create(:user, :level_two)
|
||||||
|
@unverified_user = create(:user)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should create an comment if the comments are open' do
|
||||||
|
sign_in @user
|
||||||
|
|
||||||
|
expect do
|
||||||
|
xhr :post, :create, comment: {commentable_id: @question.id, commentable_type: "Legislation::Question", body: "a comment"}
|
||||||
|
end.to change { @question.reload.comments_count }.by(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should not create a comment if the comments are closed' do
|
||||||
|
sign_in @user
|
||||||
|
@process.update_attribute(:debate_end_date, Date.current - 1.day)
|
||||||
|
|
||||||
|
expect do
|
||||||
|
xhr :post, :create, comment: {commentable_id: @question.id, commentable_type: "Legislation::Question", body: "a comment"}
|
||||||
|
end.to_not change { @question.reload.comments_count }
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should not create a comment for unverified users when the commentable requires it' do
|
||||||
|
sign_in @unverified_user
|
||||||
|
|
||||||
|
expect do
|
||||||
|
xhr :post, :create, comment: {commentable_id: @question.id, commentable_type: "Legislation::Question", body: "a comment"}
|
||||||
|
end.to_not change { @question.reload.comments_count }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -486,6 +486,17 @@ FactoryGirl.define do
|
|||||||
allegations_end_date Date.current - 4.days
|
allegations_end_date Date.current - 4.days
|
||||||
final_publication_date Date.current - 2.days
|
final_publication_date Date.current - 2.days
|
||||||
end
|
end
|
||||||
|
|
||||||
|
trait :in_debate_phase do
|
||||||
|
start_date Date.current - 5.days
|
||||||
|
end_date Date.current + 5.days
|
||||||
|
debate_start_date Date.current - 5.days
|
||||||
|
debate_end_date Date.current + 1.days
|
||||||
|
draft_publication_date Date.current + 1.day
|
||||||
|
allegations_start_date Date.current + 2.days
|
||||||
|
allegations_end_date Date.current + 3.days
|
||||||
|
final_publication_date Date.current + 5.days
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
factory :legislation_draft_version, class: 'Legislation::DraftVersion' do
|
factory :legislation_draft_version, class: 'Legislation::DraftVersion' do
|
||||||
|
|||||||
@@ -2,8 +2,9 @@ require 'rails_helper'
|
|||||||
include ActionView::Helpers::DateHelper
|
include ActionView::Helpers::DateHelper
|
||||||
|
|
||||||
feature 'Commenting legislation questions' do
|
feature 'Commenting legislation questions' do
|
||||||
let(:user) { create :user }
|
let(:user) { create :user, :level_two }
|
||||||
let(:legislation_question) { create :legislation_question }
|
let(:process) { create :legislation_process, :in_debate_phase }
|
||||||
|
let(:legislation_question) { create :legislation_question, process: process }
|
||||||
|
|
||||||
scenario 'Index' do
|
scenario 'Index' do
|
||||||
3.times { create(:comment, commentable: legislation_question) }
|
3.times { create(:comment, commentable: legislation_question) }
|
||||||
@@ -181,9 +182,27 @@ feature 'Commenting legislation questions' do
|
|||||||
expect(page).to have_content "Can't be blank"
|
expect(page).to have_content "Can't be blank"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
scenario "Unverified user can't create comments", :js do
|
||||||
|
unverified_user = create :user
|
||||||
|
login_as(unverified_user)
|
||||||
|
|
||||||
|
visit legislation_process_question_path(legislation_question.process, legislation_question)
|
||||||
|
|
||||||
|
expect(page).to have_content "To participate verify your account"
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario "Can't create comments if debate phase is not open", :js do
|
||||||
|
process.update_attributes(debate_start_date: Date.current - 2.days, debate_end_date: Date.current - 1.days)
|
||||||
|
login_as(user)
|
||||||
|
|
||||||
|
visit legislation_process_question_path(legislation_question.process, legislation_question)
|
||||||
|
|
||||||
|
expect(page).to have_content "Closed phase"
|
||||||
|
end
|
||||||
|
|
||||||
scenario 'Reply', :js do
|
scenario 'Reply', :js do
|
||||||
citizen = create(:user, username: 'Ana')
|
citizen = create(:user, username: 'Ana')
|
||||||
manuela = create(:user, username: 'Manuela')
|
manuela = create(:user, :level_two, username: 'Manuela')
|
||||||
comment = create(:comment, commentable: legislation_question, user: citizen)
|
comment = create(:comment, commentable: legislation_question, user: citizen)
|
||||||
|
|
||||||
login_as(manuela)
|
login_as(manuela)
|
||||||
@@ -264,7 +283,7 @@ feature 'Commenting legislation questions' do
|
|||||||
end
|
end
|
||||||
|
|
||||||
scenario "Flagging turbolinks sanity check", :js do
|
scenario "Flagging turbolinks sanity check", :js do
|
||||||
legislation_question = create(:legislation_question, title: "Should we change the world?")
|
legislation_question = create(:legislation_question, process: process, title: "Should we change the world?")
|
||||||
comment = create(:comment, commentable: legislation_question)
|
comment = create(:comment, commentable: legislation_question)
|
||||||
|
|
||||||
login_as(user)
|
login_as(user)
|
||||||
@@ -278,7 +297,6 @@ feature 'Commenting legislation questions' do
|
|||||||
end
|
end
|
||||||
|
|
||||||
scenario "Erasing a comment's author" do
|
scenario "Erasing a comment's author" do
|
||||||
legislation_question = create(:legislation_question)
|
|
||||||
comment = create(:comment, commentable: legislation_question, body: 'this should be visible')
|
comment = create(:comment, commentable: legislation_question, body: 'this should be visible')
|
||||||
comment.user.erase
|
comment.user.erase
|
||||||
|
|
||||||
@@ -290,7 +308,6 @@ feature 'Commenting legislation questions' do
|
|||||||
end
|
end
|
||||||
|
|
||||||
scenario 'Submit button is disabled after clicking', :js do
|
scenario 'Submit button is disabled after clicking', :js do
|
||||||
legislation_question = create(:legislation_question)
|
|
||||||
login_as(user)
|
login_as(user)
|
||||||
visit legislation_process_question_path(legislation_question.process, legislation_question)
|
visit legislation_process_question_path(legislation_question.process, legislation_question)
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ feature "Notifications" do
|
|||||||
let(:user) { create :user }
|
let(:user) { create :user }
|
||||||
let(:debate) { create :debate, author: author }
|
let(:debate) { create :debate, author: author }
|
||||||
let(:proposal) { create :proposal, author: author }
|
let(:proposal) { create :proposal, author: author }
|
||||||
let(:legislation_question) { create(:legislation_question, author: administrator) }
|
let(:process) { create :legislation_process, :in_debate_phase }
|
||||||
|
let(:legislation_question) { create(:legislation_question, process: process, author: administrator) }
|
||||||
let(:legislation_annotation) { create(:legislation_annotation, author: author) }
|
let(:legislation_annotation) { create(:legislation_annotation, author: author) }
|
||||||
|
|
||||||
scenario "User commented on my debate", :js do
|
scenario "User commented on my debate", :js do
|
||||||
@@ -36,7 +37,8 @@ feature "Notifications" do
|
|||||||
end
|
end
|
||||||
|
|
||||||
scenario "User commented on my legislation question", :js do
|
scenario "User commented on my legislation question", :js do
|
||||||
login_as user
|
verified_user = create(:user, :level_two)
|
||||||
|
login_as verified_user
|
||||||
visit legislation_process_question_path legislation_question.process, legislation_question
|
visit legislation_process_question_path legislation_question.process, legislation_question
|
||||||
|
|
||||||
fill_in "comment-body-legislation_question_#{legislation_question.id}", with: "I answered your question"
|
fill_in "comment-body-legislation_question_#{legislation_question.id}", with: "I answered your question"
|
||||||
|
|||||||
Reference in New Issue
Block a user