Browse Source

Fix download mailer tests

pull/851/head
Sam Collard 3 years ago
parent
commit
9d4c641eff
  1. 2
      app/controllers/organisations_controller.rb
  2. 2
      app/jobs/email_csv_job.rb
  3. 6
      app/mailers/csv_download_mailer.rb
  4. 2
      spec/mailers/csv_download_mailer_spec.rb
  5. 50
      spec/requests/organisations_controller_spec.rb

2
app/controllers/organisations_controller.rb

@ -127,7 +127,7 @@ private
end end
def authenticate_scope! def authenticate_scope!
if %w[create new logs download_csv].include? action_name if %w[create new logs download_csv email_csv].include? action_name
head :unauthorized and return unless current_user.support? head :unauthorized and return unless current_user.support?
elsif current_user.organisation != @organisation && !current_user.support? elsif current_user.organisation != @organisation && !current_user.support?
render_not_found render_not_found

2
app/jobs/email_csv_job.rb

@ -16,6 +16,6 @@ class EmailCsvJob < ApplicationJob
url = storage_service.get_presigned_url(filename, EXPIRATION_TIME) url = storage_service.get_presigned_url(filename, EXPIRATION_TIME)
CsvDownloadMailer.new.send_email(user, url, duration) CsvDownloadMailer.new.send_email(user, url, EXPIRATION_TIME)
end end
end end

6
app/mailers/csv_download_mailer.rb

@ -3,9 +3,9 @@ class CsvDownloadMailer < NotifyMailer
def send_csv_download_mail(user, link, duration) def send_csv_download_mail(user, link, duration)
send_email( send_email(
email_address: user.email, user.email,
template_id: CSV_DOWNLOAD_TEMPLATE_ID, CSV_DOWNLOAD_TEMPLATE_ID,
personalisation: { name: user.name, link:, duration: ActiveSupport::Duration.build(duration).inspect }, { name: user.name, link:, duration: ActiveSupport::Duration.build(duration).inspect },
) )
end end
end end

2
spec/mailers/csv_download_mailer_spec.rb

@ -24,7 +24,7 @@ RSpec.describe CsvDownloadMailer do
}, },
) )
described_class.new.send_email(user, link, duration) described_class.new.send_csv_download_mail(user, link, duration)
end end
end end
end end

50
spec/requests/organisations_controller_spec.rb

@ -352,26 +352,30 @@ RSpec.describe OrganisationsController, type: :request do
end end
context "when viewing logs for other organisation" do context "when viewing logs for other organisation" do
before do it "does not display the logs" do
get "/organisations/#{unauthorised_organisation.id}/logs", headers:, params: {} get "/organisations/#{unauthorised_organisation.id}/logs", headers:, params: {}
expect(response).to have_http_status(:unauthorized)
end end
it "returns not found 404 from org details route" do it "prevents CSV download" do
expect(response).to have_http_status(:not_found) expect {
end post "/organisations/#{unauthorised_organisation.id}/logs/email-csv", headers:, params: {}
}.not_to enqueue_job(EmailCsvJob)
it "shows the 404 view" do expect(response).to have_http_status(:unauthorized)
expect(page).to have_content("Page not found")
end end
end end
context "when viewing logs for your organisation" do context "when viewing logs for your organisation" do
before do it "does not display the logs" do
get "/organisations/#{organisation.id}/logs", headers:, params: {} get "/organisations/#{organisation.id}/logs", headers:, params: {}
expect(response).to have_http_status(:unauthorized)
end end
it "redirects to /logs page" do it "prevents CSV download" do
expect(response).to redirect_to("/logs") expect {
post "/organisations/#{organisation.id}/logs/email-csv", headers:, params: {}
}.not_to enqueue_job(EmailCsvJob)
expect(response).to have_http_status(:unauthorized)
end end
end end
@ -495,26 +499,30 @@ RSpec.describe OrganisationsController, type: :request do
end end
context "when viewing logs for other organisation" do context "when viewing logs for other organisation" do
before do it "does not display the logs" do
get "/organisations/#{unauthorised_organisation.id}/logs", headers:, params: {} get "/organisations/#{unauthorised_organisation.id}/logs", headers:, params: {}
expect(response).to have_http_status(:unauthorized)
end end
it "returns not found 404 from org details route" do it "prevents CSV download" do
expect(response).to have_http_status(:not_found) expect {
end post "/organisations/#{unauthorised_organisation.id}/logs/email-csv", headers:, params: {}
}.not_to enqueue_job(EmailCsvJob)
it "shows the 404 view" do expect(response).to have_http_status(:unauthorized)
expect(page).to have_content("Page not found")
end end
end end
context "when viewing logs for your organisation" do context "when viewing logs for your organisation" do
before do it "does not display the logs" do
get "/organisations/#{organisation.id}/logs", headers:, params: {} get "/organisations/#{organisation.id}/logs", headers:, params: {}
expect(response).to have_http_status(:unauthorized)
end end
it "redirects to /logs page" do it "prevents CSV download" do
expect(response).to redirect_to("/logs") expect {
post "/organisations/#{organisation.id}/logs/email-csv", headers:, params: {}
}.not_to enqueue_job(EmailCsvJob)
expect(response).to have_http_status(:unauthorized)
end end
end end
end end
@ -1035,7 +1043,7 @@ RSpec.describe OrganisationsController, type: :request do
end end
it "has a CSV download button with the correct path" do it "has a CSV download button with the correct path" do
expect(page).to have_link("Download (CSV)", href: "/organisations/#{organisation.id}/logs/csv-download?search=") expect(page).to have_link("Download (CSV)", href: "/organisations/#{organisation.id}/logs/csv-download")
end end
context "when you download the CSV" do context "when you download the CSV" do

Loading…
Cancel
Save