From 5874d271f5b8d9453e617f16cbd20b92f718676a Mon Sep 17 00:00:00 2001 From: Adam Wead Date: Thu, 2 May 2019 14:13:10 -0400 Subject: [PATCH] Verify that emails are sent for monthly stats Rspec expectations were hiding a bug in the job parameters. This corrects the issue and tests that the email is actually queued for delivery. --- app/jobs/user_stats_notification_job.rb | 2 +- config/sample/application.yml | 6 +++--- config/travis/application_test.yml | 6 +++--- lib/tasks/scholarsphere/stats.rake | 2 +- spec/jobs/user_stats_notification_job_spec.rb | 2 +- spec/rake/scholarsphere/stats_spec.rb | 8 ++++++-- 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/jobs/user_stats_notification_job.rb b/app/jobs/user_stats_notification_job.rb index dfe3f30d6..287543846 100644 --- a/app/jobs/user_stats_notification_job.rb +++ b/app/jobs/user_stats_notification_job.rb @@ -6,7 +6,7 @@ def perform(id:, start_date:, end_date:) return unless PsuDir::LdapUser.check_ldap_exist!(user.login) UserMailer.user_stats_email( - user_email: user.email, + user: user, start_date: start_date, end_date: end_date ).deliver_now diff --git a/config/sample/application.yml b/config/sample/application.yml index 1b5889951..f6f2ca1bb 100644 --- a/config/sample/application.yml +++ b/config/sample/application.yml @@ -9,7 +9,7 @@ development: service_instance: "localhost" virtual_host: "http://localhost:3000/" stats_email: "ScholarSphere Stats " - no_reply_email: "no_repy@scholarsphere.psu.edu" + no_reply_email: "no_reply@scholarsphere.psu.edu" google_analytics_id: "test-id" read_only: "false" EZID_USER: 'ezid_user' @@ -30,7 +30,7 @@ test: service_instance: "example-test" virtual_host: "http://test.com/" stats_email: "Test email" - no_reply_email: "no_repy@scholarsphere.psu.edu" + no_reply_email: "no_reply@scholarsphere.psu.edu" google_analytics_id: "test-id" read_only: "false" RECAPTCHA_SITE_KEY: 'site-key' @@ -48,7 +48,7 @@ production: service_instance: "example-prod" virtual_host: "http://test.com/" stats_email: "Test email" - no_reply_email: "no_repy@scholarsphere.psu.edu" + no_reply_email: "no_reply@scholarsphere.psu.edu" google_analytics_id: "test-id" derivatives_path: "path" read_only: "false" diff --git a/config/travis/application_test.yml b/config/travis/application_test.yml index 967c45f92..fcf1da073 100644 --- a/config/travis/application_test.yml +++ b/config/travis/application_test.yml @@ -9,7 +9,7 @@ development: service_instance: "localhost" virtual_host: "http://localhost:3000/" stats_email: "ScholarSphere Stats " - no_reply_email: "no_repy@scholarsphere.psu.edu" + no_reply_email: "no_reply@scholarsphere.psu.edu" google_analytics_id: "test-id" read_only: "false" EZID_USER: 'ezid_user' @@ -30,7 +30,7 @@ test: service_instance: "example-test" virtual_host: "http://test.com/" stats_email: "Test email" - no_reply_email: "no_repy@scholarsphere.psu.edu" + no_reply_email: "no_reply@scholarsphere.psu.edu" google_analytics_id: "test-id" read_only: "false" RECAPTCHA_SITE_KEY: 'site-key' @@ -48,7 +48,7 @@ production: service_instance: "example-prod" virtual_host: "http://test.com/" stats_email: "Test email" - no_reply_email: "no_repy@scholarsphere.psu.edu" + no_reply_email: "no_reply@scholarsphere.psu.edu" google_analytics_id: "test-id" derivatives_path: "path" read_only: "false" diff --git a/lib/tasks/scholarsphere/stats.rake b/lib/tasks/scholarsphere/stats.rake index ce62c3c30..2380b6ea7 100644 --- a/lib/tasks/scholarsphere/stats.rake +++ b/lib/tasks/scholarsphere/stats.rake @@ -19,7 +19,7 @@ namespace :scholarsphere do start_date = Date.today.last_month.beginning_of_month end_date = Date.today.last_month.end_of_month UserStat.where(date: start_date..end_date).map(&:user_id).uniq.map do |id| - UserStatsNotificationJob.perform_later(id: id, start_date: start_date, end_date: end_date) + UserStatsNotificationJob.perform_later(id: id, start_date: start_date.to_s, end_date: end_date.to_s) end end end diff --git a/spec/jobs/user_stats_notification_job_spec.rb b/spec/jobs/user_stats_notification_job_spec.rb index 34c6e90ff..717aba7bd 100644 --- a/spec/jobs/user_stats_notification_job_spec.rb +++ b/spec/jobs/user_stats_notification_job_spec.rb @@ -16,7 +16,7 @@ it 'sends the email to the user' do expect(UserMailer).to receive(:user_stats_email) - .with(user_email: user.email, start_date: start_date, end_date: end_date) + .with(user: user, start_date: start_date, end_date: end_date) .and_return(mock_mailer) expect(mock_mailer).to receive(:deliver_now) described_class.perform_now(id: user.id, start_date: start_date, end_date: end_date) diff --git a/spec/rake/scholarsphere/stats_spec.rb b/spec/rake/scholarsphere/stats_spec.rb index 9396493dc..2e8a7fe5e 100644 --- a/spec/rake/scholarsphere/stats_spec.rb +++ b/spec/rake/scholarsphere/stats_spec.rb @@ -17,15 +17,19 @@ end context 'when a user has stats' do - let(:user) { create(:user) } + let(:user) { create(:user, email: 'nowhere@fake.com') } + let(:message) { ActionMailer::Base.deliveries.first } before do + allow(PsuDir::LdapUser).to receive(:check_ldap_exist!).with(user.login).and_return(true) UserStat.create(user_id: user.id, date: (Date.today.last_month.beginning_of_month + 10), file_downloads: 5) end it 'sends an email to the user' do - expect(UserStatsNotificationJob).to receive(:perform_later) run_task('scholarsphere:stats:notify') + expect(message.to).to contain_exactly('nowhere@fake.com') + expect(message.from).to contain_exactly(ENV['no_reply_email']) + expect(message.subject).to eq('ScholarSphere - Reporting Monthly Downloads and Views') end end end