From f102f9f753f052c4c6336ba0d7cffcec5e1d249c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baza=CC=81n?= Date: Tue, 10 Jan 2017 13:30:30 +0100 Subject: [PATCH 1/4] adds Budget::Investment to valid commentable types --- app/models/comment.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index 0bfb6a320..d4b1cc8e7 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -10,7 +10,7 @@ class Comment < ActiveRecord::Base validates :body, presence: true validates :user, presence: true - validates_inclusion_of :commentable_type, in: ["Debate", "Proposal"] + validates_inclusion_of :commentable_type, in: ["Debate", "Proposal", "Budget::Investment"] validate :validate_body_length From ad435040bdb784b2687d54bada3db47fb168703a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baza=CC=81n?= Date: Tue, 10 Jan 2017 13:54:50 +0100 Subject: [PATCH 2/4] adds helper to fix links to commentable --- app/helpers/comments_helper.rb | 8 ++++++++ app/views/comments/show.html.erb | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index 13884237b..b9e69ddf9 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -20,6 +20,14 @@ module CommentsHelper end end + def commentable_path(comment) + if comment.commentable_type == "Budget::Investment" + budget_investment_path(comment.commentable.budget_id, comment.commentable) + else + comment.commentable + end + end + def user_level_class(comment) if comment.as_administrator? "is-admin" diff --git a/app/views/comments/show.html.erb b/app/views/comments/show.html.erb index e20790181..90e49a7d3 100644 --- a/app/views/comments/show.html.erb +++ b/app/views/comments/show.html.erb @@ -1,6 +1,6 @@
- <%= link_to @comment.commentable, class: "back" do %> + <%= link_to commentable_path(@comment), class: "back" do %> <%= t("comments.show.return_to_commentable") + @comment.commentable.title %> <% end %> From b71bcdd4b56d3b9ed1a877977632651d0e3d426f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baza=CC=81n?= Date: Tue, 10 Jan 2017 14:31:02 +0100 Subject: [PATCH 3/4] adds permissions to comment Investments as admin/moderator --- app/models/abilities/administrator.rb | 2 +- app/models/abilities/moderator.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index 5590d1939..8e721b2b8 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -32,7 +32,7 @@ module Abilities can :mark_featured, Debate can :unmark_featured, Debate - can :comment_as_administrator, [Debate, Comment, Proposal] + can :comment_as_administrator, [Debate, Comment, Proposal, Budget::Investment] can [:search, :create, :index, :destroy], ::Moderator can [:search, :create, :index, :summary], ::Valuator diff --git a/app/models/abilities/moderator.rb b/app/models/abilities/moderator.rb index f6c5c5004..52d838f9c 100644 --- a/app/models/abilities/moderator.rb +++ b/app/models/abilities/moderator.rb @@ -5,7 +5,7 @@ module Abilities def initialize(user) self.merge Abilities::Moderation.new(user) - can :comment_as_moderator, [Debate, Comment, Proposal] + can :comment_as_moderator, [Debate, Comment, Proposal, Budget::Investment] end end end From 76d818dd1b86d0c1939bdebd2bb1aaab4062fcc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baza=CC=81n?= Date: Tue, 10 Jan 2017 14:31:28 +0100 Subject: [PATCH 4/4] adds Budget::Investment comments specs --- .../comments/budget_investments_spec.rb | 492 ++++++++++++++++++ 1 file changed, 492 insertions(+) create mode 100644 spec/features/comments/budget_investments_spec.rb diff --git a/spec/features/comments/budget_investments_spec.rb b/spec/features/comments/budget_investments_spec.rb new file mode 100644 index 000000000..b36f51b8f --- /dev/null +++ b/spec/features/comments/budget_investments_spec.rb @@ -0,0 +1,492 @@ +require 'rails_helper' +include ActionView::Helpers::DateHelper + +feature 'Commenting Budget::Investments' do + let(:user) { create :user } + let(:investment) { create :budget_investment } + + scenario 'Index' do + 3.times { create(:comment, commentable: investment) } + + visit budget_investment_path(investment.budget, investment) + + expect(page).to have_css('.comment', count: 3) + + comment = Comment.last + within first('.comment') do + expect(page).to have_content comment.user.name + expect(page).to have_content I18n.l(comment.created_at, format: :datetime) + expect(page).to have_content comment.body + end + end + + scenario 'Show' do + parent_comment = create(:comment, commentable: investment) + first_child = create(:comment, commentable: investment, parent: parent_comment) + second_child = create(:comment, commentable: investment, parent: parent_comment) + + visit comment_path(parent_comment) + + expect(page).to have_css(".comment", count: 3) + expect(page).to have_content parent_comment.body + expect(page).to have_content first_child.body + expect(page).to have_content second_child.body + + expect(page).to have_link "Go back to #{investment.title}", href: budget_investment_path(investment.budget, investment) + end + + scenario 'Collapsable comments', :js do + parent_comment = create(:comment, body: "Main comment", commentable: investment) + child_comment = create(:comment, body: "First subcomment", commentable: investment, parent: parent_comment) + grandchild_comment = create(:comment, body: "Last subcomment", commentable: investment, parent: child_comment) + + visit budget_investment_path(investment.budget, investment) + + expect(page).to have_css('.comment', count: 3) + + find("#comment_#{child_comment.id}_children_arrow").trigger('click') + + expect(page).to have_css('.comment', count: 2) + expect(page).to_not have_content grandchild_comment.body + + find("#comment_#{child_comment.id}_children_arrow").trigger('click') + + expect(page).to have_css('.comment', count: 3) + expect(page).to have_content grandchild_comment.body + + find("#comment_#{parent_comment.id}_children_arrow").trigger('click') + + expect(page).to have_css('.comment', count: 1) + expect(page).to_not have_content child_comment.body + expect(page).to_not have_content grandchild_comment.body + end + + scenario 'Comment order' do + c1 = create(:comment, :with_confidence_score, commentable: investment, cached_votes_up: 100, cached_votes_total: 120, created_at: Time.current - 2) + c2 = create(:comment, :with_confidence_score, commentable: investment, cached_votes_up: 10, cached_votes_total: 12, created_at: Time.current - 1) + c3 = create(:comment, :with_confidence_score, commentable: investment, cached_votes_up: 1, cached_votes_total: 2, created_at: Time.current) + + visit budget_investment_path(investment.budget, investment, order: :most_voted) + + expect(c1.body).to appear_before(c2.body) + expect(c2.body).to appear_before(c3.body) + + visit budget_investment_path(investment.budget, investment, order: :newest) + + expect(c3.body).to appear_before(c2.body) + expect(c2.body).to appear_before(c1.body) + + visit budget_investment_path(investment.budget, investment, order: :oldest) + + expect(c1.body).to appear_before(c2.body) + expect(c2.body).to appear_before(c3.body) + end + + scenario 'Creation date works differently in roots and in child comments, when sorting by confidence_score' do + old_root = create(:comment, commentable: investment, created_at: Time.current - 10) + new_root = create(:comment, commentable: investment, created_at: Time.current) + old_child = create(:comment, commentable: investment, parent_id: new_root.id, created_at: Time.current - 10) + new_child = create(:comment, commentable: investment, parent_id: new_root.id, created_at: Time.current) + + visit budget_investment_path(investment.budget, investment, order: :most_voted) + + expect(new_root.body).to appear_before(old_root.body) + expect(old_child.body).to appear_before(new_child.body) + + visit budget_investment_path(investment.budget, investment, order: :newest) + + expect(new_root.body).to appear_before(old_root.body) + expect(new_child.body).to appear_before(old_child.body) + + visit budget_investment_path(investment.budget, investment, order: :oldest) + + expect(old_root.body).to appear_before(new_root.body) + expect(old_child.body).to appear_before(new_child.body) + end + + scenario 'Turns links into html links' do + create :comment, commentable: investment, body: 'Built with http://rubyonrails.org/' + + visit budget_investment_path(investment.budget, investment) + + within first('.comment') do + expect(page).to have_content 'Built with http://rubyonrails.org/' + expect(page).to have_link('http://rubyonrails.org/', href: 'http://rubyonrails.org/') + expect(find_link('http://rubyonrails.org/')[:rel]).to eq('nofollow') + expect(find_link('http://rubyonrails.org/')[:target]).to eq('_blank') + end + end + + scenario 'Sanitizes comment body for security' do + create :comment, commentable: investment, body: " click me http://www.url.com" + + visit budget_investment_path(investment.budget, investment) + + within first('.comment') do + expect(page).to have_content "click me http://www.url.com" + expect(page).to have_link('http://www.url.com', href: 'http://www.url.com') + expect(page).not_to have_link('click me') + end + end + + scenario 'Paginated comments' do + per_page = 10 + (per_page + 2).times { create(:comment, commentable: investment)} + + visit budget_investment_path(investment.budget, investment) + + expect(page).to have_css('.comment', count: per_page) + within("ul.pagination") do + expect(page).to have_content("1") + expect(page).to have_content("2") + expect(page).to_not have_content("3") + click_link "Next", exact: false + end + + expect(page).to have_css('.comment', count: 2) + end + + feature 'Not logged user' do + scenario 'can not see comments forms' do + create(:comment, commentable: investment) + visit budget_investment_path(investment.budget, investment) + + expect(page).to have_content 'You must Sign in or Sign up to leave a comment' + within('#comments') do + expect(page).to_not have_content 'Write a comment' + expect(page).to_not have_content 'Reply' + end + end + end + + scenario 'Create', :js do + login_as(user) + visit budget_investment_path(investment.budget, investment) + + fill_in "comment-body-budget_investment_#{investment.id}", with: 'Have you thought about...?' + click_button 'Publish comment' + + within "#comments" do + expect(page).to have_content 'Have you thought about...?' + expect(page).to have_content 'Comments (1)' + end + end + + scenario 'Errors on create', :js do + login_as(user) + visit budget_investment_path(investment.budget, investment) + + click_button 'Publish comment' + + expect(page).to have_content "Can't be blank" + end + + scenario 'Reply', :js do + citizen = create(:user, username: 'Ana') + manuela = create(:user, username: 'Manuela') + comment = create(:comment, commentable: investment, user: citizen) + + login_as(manuela) + visit budget_investment_path(investment.budget, investment) + + click_link "Reply" + + within "#js-comment-form-comment_#{comment.id}" do + fill_in "comment-body-comment_#{comment.id}", with: 'It will be done next week.' + click_button 'Publish reply' + end + + within "#comment_#{comment.id}" do + expect(page).to have_content 'It will be done next week.' + end + + expect(page).to_not have_selector("#js-comment-form-comment_#{comment.id}", visible: true) + end + + scenario 'Errors on reply', :js do + comment = create(:comment, commentable: investment, user: user) + + login_as(user) + visit budget_investment_path(investment.budget, investment) + + click_link "Reply" + + within "#js-comment-form-comment_#{comment.id}" do + click_button 'Publish reply' + expect(page).to have_content "Can't be blank" + end + + end + + scenario "N replies", :js do + parent = create(:comment, commentable: investment) + + 7.times do + create(:comment, commentable: investment, parent: parent) + parent = parent.children.first + end + + visit budget_investment_path(investment.budget, investment) + expect(page).to have_css(".comment.comment.comment.comment.comment.comment.comment.comment") + end + + scenario "Flagging as inappropriate", :js do + comment = create(:comment, commentable: investment) + + login_as(user) + visit budget_investment_path(investment.budget, investment) + + within "#comment_#{comment.id}" do + page.find("#flag-expand-comment-#{comment.id}").click + page.find("#flag-comment-#{comment.id}").click + + expect(page).to have_css("#unflag-expand-comment-#{comment.id}") + end + + expect(Flag.flagged?(user, comment)).to be + end + + scenario "Undoing flagging as inappropriate", :js do + comment = create(:comment, commentable: investment) + Flag.flag(user, comment) + + login_as(user) + visit budget_investment_path(investment.budget, investment) + + within "#comment_#{comment.id}" do + page.find("#unflag-expand-comment-#{comment.id}").click + page.find("#unflag-comment-#{comment.id}").click + + expect(page).to have_css("#flag-expand-comment-#{comment.id}") + end + + expect(Flag.flagged?(user, comment)).to_not be + end + + scenario "Flagging turbolinks sanity check", :js do + investment = create(:budget_investment, title: "Should we change the world?") + comment = create(:comment, commentable: investment) + + login_as(user) + visit budget_investments_path(investment.budget) + click_link "Should we change the world?" + + within "#comment_#{comment.id}" do + page.find("#flag-expand-comment-#{comment.id}").click + expect(page).to have_selector("#flag-comment-#{comment.id}") + end + end + + scenario "Erasing a comment's author" do + investment = create(:budget_investment) + comment = create(:comment, commentable: investment, body: "this should be visible") + comment.user.erase + + visit budget_investment_path(investment.budget, investment) + within "#comment_#{comment.id}" do + expect(page).to have_content('User deleted') + expect(page).to have_content('this should be visible') + end + end + + feature "Moderators" do + scenario "can create comment as a moderator", :js do + moderator = create(:moderator) + + login_as(moderator.user) + visit budget_investment_path(investment.budget, investment) + + fill_in "comment-body-budget_investment_#{investment.id}", with: "I am moderating!" + check "comment-as-moderator-budget_investment_#{investment.id}" + click_button "Publish comment" + + within "#comments" do + expect(page).to have_content "I am moderating!" + expect(page).to have_content "Moderator ##{moderator.id}" + expect(page).to have_css "div.is-moderator" + expect(page).to have_css "img.moderator-avatar" + end + end + + scenario "can create reply as a moderator", :js do + citizen = create(:user, username: "Ana") + manuela = create(:user, username: "Manuela") + moderator = create(:moderator, user: manuela) + comment = create(:comment, commentable: investment, user: citizen) + + login_as(manuela) + visit budget_investment_path(investment.budget, investment) + + click_link "Reply" + + within "#js-comment-form-comment_#{comment.id}" do + fill_in "comment-body-comment_#{comment.id}", with: "I am moderating!" + check "comment-as-moderator-comment_#{comment.id}" + click_button 'Publish reply' + end + + within "#comment_#{comment.id}" do + expect(page).to have_content "I am moderating!" + expect(page).to have_content "Moderator ##{moderator.id}" + expect(page).to have_css "div.is-moderator" + expect(page).to have_css "img.moderator-avatar" + end + + expect(page).to_not have_selector("#js-comment-form-comment_#{comment.id}", visible: true) + end + + scenario "can not comment as an administrator" do + moderator = create(:moderator) + + login_as(moderator.user) + visit budget_investment_path(investment.budget, investment) + + expect(page).to_not have_content "Comment as administrator" + end + end + + feature "Administrators" do + scenario "can create comment as an administrator", :js do + admin = create(:administrator) + + login_as(admin.user) + visit budget_investment_path(investment.budget, investment) + + fill_in "comment-body-budget_investment_#{investment.id}", with: "I am your Admin!" + check "comment-as-administrator-budget_investment_#{investment.id}" + click_button "Publish comment" + + within "#comments" do + expect(page).to have_content "I am your Admin!" + expect(page).to have_content "Administrator ##{admin.id}" + expect(page).to have_css "div.is-admin" + expect(page).to have_css "img.admin-avatar" + end + end + + scenario "can create reply as an administrator", :js do + citizen = create(:user, username: "Ana") + manuela = create(:user, username: "Manuela") + admin = create(:administrator, user: manuela) + comment = create(:comment, commentable: investment, user: citizen) + + login_as(manuela) + visit budget_investment_path(investment.budget, investment) + + click_link "Reply" + + within "#js-comment-form-comment_#{comment.id}" do + fill_in "comment-body-comment_#{comment.id}", with: "Top of the world!" + check "comment-as-administrator-comment_#{comment.id}" + click_button 'Publish reply' + end + + within "#comment_#{comment.id}" do + expect(page).to have_content "Top of the world!" + expect(page).to have_content "Administrator ##{admin.id}" + expect(page).to have_css "div.is-admin" + expect(page).to have_css "img.admin-avatar" + end + + expect(page).to_not have_selector("#js-comment-form-comment_#{comment.id}", visible: true) + end + + scenario "can not comment as a moderator" do + admin = create(:administrator) + + login_as(admin.user) + visit budget_investment_path(investment.budget, investment) + + expect(page).to_not have_content "Comment as moderator" + end + end + + feature 'Voting comments' do + + background do + @manuela = create(:user, verified_at: Time.current) + @pablo = create(:user) + @investment = create(:budget_investment) + @comment = create(:comment, commentable: @investment) + @budget = @investment.budget + + login_as(@manuela) + end + + scenario 'Show' do + create(:vote, voter: @manuela, votable: @comment, vote_flag: true) + create(:vote, voter: @pablo, votable: @comment, vote_flag: false) + + visit budget_investment_path(@budget, @budget, @investment) + + within("#comment_#{@comment.id}_votes") do + within(".in_favor") do + expect(page).to have_content "1" + end + + within(".against") do + expect(page).to have_content "1" + end + + expect(page).to have_content "2 votes" + end + end + + scenario 'Create', :js do + visit budget_investment_path(@budget, @investment) + + within("#comment_#{@comment.id}_votes") do + find(".in_favor a").click + + within(".in_favor") do + expect(page).to have_content "1" + end + + within(".against") do + expect(page).to have_content "0" + end + + expect(page).to have_content "1 vote" + end + end + + scenario 'Update', :js do + visit budget_investment_path(@budget, @investment) + + within("#comment_#{@comment.id}_votes") do + find('.in_favor a').click + find('.against a').click + + within('.in_favor') do + expect(page).to have_content "0" + end + + within('.against') do + expect(page).to have_content "1" + end + + expect(page).to have_content "1 vote" + end + end + + scenario 'Trying to vote multiple times', :js do + visit budget_investment_path(@budget, @investment) + + within("#comment_#{@comment.id}_votes") do + find('.in_favor a').click + find('.in_favor a').click + + within('.in_favor') do + expect(page).to have_content "1" + end + + within('.against') do + expect(page).to have_content "0" + end + + expect(page).to have_content "1 vote" + end + end + end + +end \ No newline at end of file