From 12ea724474abf4bebc41dedff4a5a3ce03e4a3b2 Mon Sep 17 00:00:00 2001 From: taitus Date: Tue, 10 May 2022 14:58:12 +0200 Subject: [PATCH] Fix evaluation comment email on system emails Currently with both seeds and dev_seeds, not only was this email not displayed from the system emails section, but it also caused an error in the application. @email_to had an empty value and in the view we tried to access @email_to.name which caused the error. We kept the same logic but added the current_user to make sure it always has a valid value. We add the current_user because the current_user is always present in this controller.. --- .../admin/system_emails_controller.rb | 2 +- spec/system/admin/system_emails_spec.rb | 43 ++++++++++++------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/app/controllers/admin/system_emails_controller.rb b/app/controllers/admin/system_emails_controller.rb index b71e1d32b..b112ac414 100644 --- a/app/controllers/admin/system_emails_controller.rb +++ b/app/controllers/admin/system_emails_controller.rb @@ -104,7 +104,7 @@ class Admin::SystemEmailsController < Admin::BaseController comment = Comment.where(commentable_type: "Budget::Investment").last if comment @email = EvaluationCommentEmail.new(comment) - @email_to = @email.to.first + @email_to = @email.to.first || current_user else redirect_to admin_system_emails_path, alert: t("admin.system_emails.alert.no_evaluation_comments") diff --git a/spec/system/admin/system_emails_spec.rb b/spec/system/admin/system_emails_spec.rb index b0717c98a..425c5b4d0 100644 --- a/spec/system/admin/system_emails_spec.rb +++ b/spec/system/admin/system_emails_spec.rb @@ -251,25 +251,36 @@ describe "System Emails" do expect(page).to have_content "Some example data is needed in order to preview the email." end - scenario "#evaluation_comment" do - admin = create(:administrator, user: create(:user, username: "Baby Doe")) - investment = create(:budget_investment, - title: "Cleaner city", - heading: heading, - author: user, - administrator: admin) - comment = create(:comment, :valuation, commentable: investment) + describe "#evaluation_comment" do + scenario "render correctly evaluaton comment mailer with valuator as a sample user" do + admin = create(:administrator, user: create(:user, username: "Baby Doe")) + investment = create(:budget_investment, + title: "Cleaner city", + heading: heading, + author: user, + administrator: admin) + comment = create(:comment, :valuation, commentable: investment) - visit admin_system_email_view_path("evaluation_comment") + visit admin_system_email_view_path("evaluation_comment") - expect(page).to have_content "New evaluation comment for Cleaner city" - expect(page).to have_content "Hi #{admin.name}" - expect(page).to have_content "There is a new evaluation comment from #{comment.user.name} "\ - "to the budget investment Cleaner city" - expect(page).to have_content comment.body + expect(page).to have_content "New evaluation comment for Cleaner city" + expect(page).to have_content "Hi #{admin.name}" + expect(page).to have_content "There is a new evaluation comment from #{comment.user.name} "\ + "to the budget investment Cleaner city" + expect(page).to have_content comment.body - expect(page).to have_link "Cleaner city", - href: admin_budget_budget_investment_url(investment.budget, investment, anchor: "comments", host: app_host) + expect(page).to have_link "Cleaner city", + href: admin_budget_budget_investment_url(investment.budget, investment, anchor: "comments", host: app_host) + end + + scenario "uses a current_user as a sample user for sample regular comments" do + create(:budget_investment_comment, body: "This is a sample comment") + + visit admin_system_email_view_path("evaluation_comment") + + expect(page).to have_content "This is a sample comment" + expect(page).to have_content admin.name + end end end