From 2bdc2c27d7e1a8b462edcccbc0d03e62348e6bf1 Mon Sep 17 00:00:00 2001 From: Angel Perez Date: Mon, 13 Nov 2017 16:05:10 -0400 Subject: [PATCH] Add email specs for Poll --- .../admin/poll/polls_controller.rb | 1 + app/helpers/mailer_helper.rb | 1 + app/models/comment_notifier.rb | 1 - config/locales/en/activerecord.yml | 3 + config/locales/es/activerecord.yml | 3 + spec/features/comments/polls_spec.rb | 2 +- spec/features/emails_spec.rb | 58 ++++++++++++------- spec/support/common_actions.rb | 2 + 8 files changed, 48 insertions(+), 23 deletions(-) diff --git a/app/controllers/admin/poll/polls_controller.rb b/app/controllers/admin/poll/polls_controller.rb index 90a31192c..32e38eb84 100644 --- a/app/controllers/admin/poll/polls_controller.rb +++ b/app/controllers/admin/poll/polls_controller.rb @@ -17,6 +17,7 @@ class Admin::Poll::PollsController < Admin::Poll::BaseController end def create + @poll = Poll.new(poll_params.merge(author: current_user)) if @poll.save redirect_to [:admin, @poll], notice: t("flash.actions.create.poll") else diff --git a/app/helpers/mailer_helper.rb b/app/helpers/mailer_helper.rb index e0ab9c71b..c3bda48b9 100644 --- a/app/helpers/mailer_helper.rb +++ b/app/helpers/mailer_helper.rb @@ -1,6 +1,7 @@ module MailerHelper def commentable_url(commentable) + return poll_url(commentable) if commentable.is_a?(Poll) return debate_url(commentable) if commentable.is_a?(Debate) return proposal_url(commentable) if commentable.is_a?(Proposal) return community_topic_url(commentable.community_id, commentable) if commentable.is_a?(Topic) diff --git a/app/models/comment_notifier.rb b/app/models/comment_notifier.rb index 600009a23..68b350e6b 100644 --- a/app/models/comment_notifier.rb +++ b/app/models/comment_notifier.rb @@ -22,7 +22,6 @@ class CommentNotifier end def email_on_comment? - return false if @comment.commentable.is_a?(Poll) commentable_author = @comment.commentable.author commentable_author != @author && commentable_author.email_on_comment? end diff --git a/config/locales/en/activerecord.yml b/config/locales/en/activerecord.yml index 89e46b1a8..f5f06122e 100644 --- a/config/locales/en/activerecord.yml +++ b/config/locales/en/activerecord.yml @@ -91,6 +91,9 @@ en: topic: one: "Topic" other: "Topics" + poll: + one: "Poll" + other: "Polls" attributes: budget: name: "Name" diff --git a/config/locales/es/activerecord.yml b/config/locales/es/activerecord.yml index d08b1c7ff..50499193d 100644 --- a/config/locales/es/activerecord.yml +++ b/config/locales/es/activerecord.yml @@ -91,6 +91,9 @@ es: topic: one: "Tema" other: "Temas" + poll: + one: "Votación" + other: "Votaciones" attributes: budget: name: "Nombre" diff --git a/spec/features/comments/polls_spec.rb b/spec/features/comments/polls_spec.rb index 0fe01cf06..05df65ad0 100644 --- a/spec/features/comments/polls_spec.rb +++ b/spec/features/comments/polls_spec.rb @@ -3,7 +3,7 @@ include ActionView::Helpers::DateHelper feature 'Commenting polls' do let(:user) { create :user } - let(:poll) { create :poll } + let(:poll) { create(:poll, author: create(:user)) } scenario 'Index' do 3.times { create(:comment, commentable: poll) } diff --git a/spec/features/emails_spec.rb b/spec/features/emails_spec.rb index 769e6db38..8ba88e844 100644 --- a/spec/features/emails_spec.rb +++ b/spec/features/emails_spec.rb @@ -150,6 +150,37 @@ feature 'Emails' do end end + context 'Poll comments' do + scenario 'Send email on poll comment', :js do + user = create(:user, email_on_comment: true) + poll = create(:poll, author: user) + comment_on(poll) + + email = open_last_email + expect(email).to have_subject('Someone has commented on your poll') + expect(email).to deliver_to(poll.author) + expect(email).to have_body_text(poll_path(poll)) + expect(email).to have_body_text(I18n.t('mailers.config.manage_email_subscriptions')) + expect(email).to have_body_text(account_path) + end + + scenario 'Do not send email about own poll comments', :js do + user = create(:user, email_on_comment: true) + poll = create(:poll, author: user) + comment_on(poll, user) + + expect { open_last_email }.to raise_error 'No email has been sent!' + end + + scenario 'Do not send email about poll question comment unless set in preferences', :js do + user = create(:user, email_on_comment: false) + poll = create(:poll, author: user) + comment_on(poll) + + expect { open_last_email }.to raise_error 'No email has been sent!' + end + end + context 'Comment replies' do scenario "Send email on comment reply", :js do user = create(:user, email_on_comment_reply: true) @@ -439,40 +470,25 @@ feature 'Emails' do context "Polls" do - scenario "Do not send email on poll comment", :js do - user1 = create(:user, email_on_comment: true) - user2 = create(:user) - - poll = create(:poll, author: user1) - reset_mailer - - login_as(user2) - visit poll_path(poll) - - fill_in "comment-body-poll_#{poll.id}", with: 'Have you thought about...?' - click_button 'Publish comment' - - expect(page).to have_content 'Have you thought about...?' - - expect { open_last_email }.to raise_error "No email has been sent!" - end - scenario "Send email on poll comment reply", :js do user1 = create(:user, email_on_comment_reply: true) user2 = create(:user) - - poll = create(:poll) + poll = create(:poll, author: create(:user)) comment = create(:comment, commentable: poll, author: user1) login_as(user2) visit poll_path(poll) 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 - expect(page).to have_content 'It will be done next week.' + + within "#comment_#{comment.id}" do + expect(page).to have_content 'It will be done next week.' + end email = open_last_email expect(email).to have_subject('Someone has responded to your comment') diff --git a/spec/support/common_actions.rb b/spec/support/common_actions.rb index 438b6c163..4b2134a02 100644 --- a/spec/support/common_actions.rb +++ b/spec/support/common_actions.rb @@ -83,6 +83,8 @@ module CommonActions debate_path(commentable) elsif commentable.is_a?(Topic) community_topic_path(commentable, community_id: commentable.community_id) + elsif commentable.is_a?(Poll) + poll_path(commentable) else budget_investment_path(commentable, budget_id: commentable.budget_id) end