From 94a6e147696147d4fce1e920325b36f005a92754 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 14 Sep 2015 12:57:39 +0200 Subject: [PATCH] adds tests for proposals comments --- .../{comments_spec.rb => comments/debates.rb} | 88 +++- spec/features/comments/proposals.rb | 395 ++++++++++++++++++ spec/features/votes_spec.rb | 84 +--- 3 files changed, 483 insertions(+), 84 deletions(-) rename spec/features/{comments_spec.rb => comments/debates.rb} (82%) create mode 100644 spec/features/comments/proposals.rb diff --git a/spec/features/comments_spec.rb b/spec/features/comments/debates.rb similarity index 82% rename from spec/features/comments_spec.rb rename to spec/features/comments/debates.rb index 437286bf7..4b8b23ce8 100644 --- a/spec/features/comments_spec.rb +++ b/spec/features/comments/debates.rb @@ -1,7 +1,7 @@ require 'rails_helper' include ActionView::Helpers::DateHelper -feature 'Comments' do +feature 'Commenting debates' do let(:user) { create :user } let(:debate) { create :debate } @@ -305,4 +305,90 @@ feature 'Comments' do end end + feature 'Voting comments' do + background do + @manuela = create(:user, verified_at: Time.now) + @pablo = create(:user) + @debate = create(:debate) + @comment = create(:comment, commentable: @debate) + + 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 debate_path(@debate) + + 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 debate_path(@debate) + + 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 debate_path(@debate) + + 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 debate_path(@debate) + + 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 diff --git a/spec/features/comments/proposals.rb b/spec/features/comments/proposals.rb new file mode 100644 index 000000000..9c6b99a6a --- /dev/null +++ b/spec/features/comments/proposals.rb @@ -0,0 +1,395 @@ +require 'rails_helper' +include ActionView::Helpers::DateHelper + +feature 'Commenting proposals' do + let(:user) { create :user } + let(:proposal) { create :proposal } + + scenario 'Index' do + 3.times { create(:comment, commentable: proposal) } + + visit proposal_path(proposal) + + 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 'Turns links into html links' do + create :comment, commentable: proposal, body: 'Built with http://rubyonrails.org/' + + visit proposal_path(proposal) + + 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: proposal, body: " click me http://madrid.es" + + visit proposal_path(proposal) + + within first('.comment') do + expect(page).to have_content "click me http://madrid.es" + expect(page).to have_link('http://madrid.es', href: 'http://madrid.es') + expect(page).not_to have_link('click me') + end + end + + scenario 'Paginated comments' do + per_page = 10 + (per_page + 2).times { create(:comment, commentable: proposal)} + + visit proposal_path(proposal) + + 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: proposal) + visit proposal_path(proposal) + + expect(page).to have_content 'You need to sign in or sign up to comment' + within('#comments') do + expect(page).to_not have_content 'Write a comment' + expect(page).to_not have_content 'Reply' + expect(page).to_not have_css('form') + end + end + end + + scenario 'Create', :js do + login_as(user) + visit proposal_path(proposal) + + fill_in "comment-body-proposal_#{proposal.id}", with: 'Have you thought about...?' + click_button 'Publish comment' + + within "#comments" do + expect(page).to have_content 'Have you thought about...?' + end + end + + scenario 'Errors on create', :js do + login_as(user) + visit proposal_path(proposal) + + 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: proposal, user: citizen) + + login_as(manuela) + visit proposal_path(proposal) + + 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: proposal, user: user) + + login_as(user) + visit proposal_path(proposal) + + 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: proposal) + + 7.times do + create(:comment, commentable: proposal, parent: parent) + parent = parent.children.first + end + + visit proposal_path(proposal) + expect(page).to have_css(".comment.comment.comment.comment.comment.comment.comment.comment") + end + + scenario "Flagging as inappropriate", :js do + comment = create(:comment, commentable: proposal) + + login_as(user) + visit proposal_path(proposal) + + 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: proposal) + Flag.flag(user, comment) + + login_as(user) + visit proposal_path(proposal) + + 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 + proposal = create(:proposal, title: "Should we change the world?") + comment = create(:comment, commentable: proposal) + + login_as(user) + visit proposals_path + 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 + + feature "Moderators" do + scenario "can create comment as a moderator", :js do + moderator = create(:moderator) + + login_as(moderator.user) + visit proposal_path(proposal) + + fill_in "comment-body-proposal_#{proposal.id}", with: "I am moderating!" + check "comment-as-moderator-proposal_#{proposal.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 "p.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: proposal, user: citizen) + + login_as(manuela) + visit proposal_path(proposal) + + 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 "p.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 proposal_path(proposal) + + 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 proposal_path(proposal) + + fill_in "comment-body-proposal_#{proposal.id}", with: "I am your Admin!" + check "comment-as-administrator-proposal_#{proposal.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 "p.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: proposal, user: citizen) + + login_as(manuela) + visit proposal_path(proposal) + + 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 "p.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 proposal_path(proposal) + + expect(page).to_not have_content "Comment as moderator" + end + end + + feature 'Voting comments' do + + background do + @manuela = create(:user, verified_at: Time.now) + @pablo = create(:user) + @proposal = create(:proposal) + @comment = create(:comment, commentable: @proposal) + + 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 proposal_path(@proposal) + + 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 proposal_path(@proposal) + + 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 proposal_path(@proposal) + + 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 proposal_path(@proposal) + + 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 diff --git a/spec/features/votes_spec.rb b/spec/features/votes_spec.rb index b98cf2031..84fe47dc2 100644 --- a/spec/features/votes_spec.rb +++ b/spec/features/votes_spec.rb @@ -292,88 +292,6 @@ feature 'Votes' do end end - feature 'Comments' do - background do - @debate = create(:debate) - @comment = create(:comment, commentable: @debate) - end - - scenario 'Show' do - create(:vote, voter: @manuela, votable: @comment, vote_flag: true) - create(:vote, voter: @pablo, votable: @comment, vote_flag: false) - - visit debate_path(@debate) - - 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 debate_path(@debate) - - 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 debate_path(@debate) - - 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 debate_path(@debate) - - 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 - feature 'Proposals' do xscenario "Home shows user votes on featured proposals" do pending "logged in user cannot see this page" @@ -570,4 +488,4 @@ feature 'Votes' do end xscenario "Remove support button text - tested in Update spec" -end \ No newline at end of file +end