Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #411 from alphagov/switch-global-export-to-s3
Upload exported CSV file to S3 rather than attaching it
- Loading branch information
Showing
7 changed files
with
88 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
class GlobalExportNotification < ApplicationMailer | ||
def notification_email(notification_email, filename, csv_contents) | ||
attachments[filename] = csv_contents | ||
|
||
def notification_email(notification_email, url) | ||
@url = url | ||
mail( | ||
to: notification_email, | ||
subject: "[GOV.UK Feedback Explorer] Your global export is attached", | ||
subject: "[GOV.UK Feedback Explorer] Your global export is ready", | ||
) | ||
end | ||
end |
4 changes: 3 additions & 1 deletion
4
app/views/global_export_notification/notification_email.text.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
Hi, | ||
|
||
Your CSV file is attached. | ||
Use the link below to download your CSV file from Feedback Explorer. | ||
|
||
<%= @url %> | ||
|
||
- Feedex |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class S3FileUploader | ||
def self.save_file_to_s3(filename, csv) | ||
connection = Fog::Storage.new( | ||
provider: "AWS", | ||
region: ENV["AWS_REGION"], | ||
aws_access_key_id: ENV["AWS_ACCESS_KEY_ID"], | ||
aws_secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"], | ||
) | ||
|
||
directory = connection.directories.get(ENV["AWS_S3_BUCKET_NAME"]) | ||
|
||
directory.files.create( | ||
key: filename, | ||
body: csv, | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe GlobalExportNotification, type: :mailer do | ||
describe "notification_email" do | ||
subject(:mail) { GlobalExportNotification.notification_email("foo@example.com", "http://www.example.com/foo.csv") } | ||
|
||
it "is sent from the no reply address" do | ||
expect(mail.from).to eq ["inside-government@digital.cabinet-office.gov.uk"] | ||
end | ||
|
||
it "is sent to the correct recipient" do | ||
expect(mail.to).to eq ["foo@example.com"] | ||
end | ||
|
||
it "contains the URL" do | ||
expect(mail.body).to include "http://www.example.com/foo.csv" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require "rails_helper" | ||
require "date" | ||
|
||
describe GenerateGlobalExportCsvWorker, type: :worker do | ||
subject(:worker) { described_class.new } | ||
before do | ||
Fog.mock! | ||
ENV["AWS_REGION"] = "eu-west-1" | ||
ENV["AWS_ACCESS_KEY_ID"] = "test" | ||
ENV["AWS_SECRET_ACCESS_KEY"] = "test" | ||
ENV["AWS_S3_BUCKET_NAME"] = "test-bucket" | ||
|
||
# Create an S3 bucket so the code being tested can find it | ||
connection = Fog::Storage.new( | ||
provider: "AWS", | ||
region: ENV["AWS_REGION"], | ||
aws_access_key_id: ENV["AWS_ACCESS_KEY_ID"], | ||
aws_secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"], | ||
) | ||
@directory = connection.directories.get(ENV["AWS_S3_BUCKET_NAME"]) || connection.directories.create(key: ENV["AWS_S3_BUCKET_NAME"]) | ||
end | ||
|
||
it "has the expected filename" do | ||
from_date = "2019-01-01" | ||
to_date = "2019-12-31" | ||
described_class.new.perform( | ||
"from_date" => from_date, | ||
"to_date" => to_date, | ||
"notification_email" => "inside-government@digital.cabinet-office.gov.uk", | ||
) | ||
|
||
file = @directory.files.get("feedex_#{from_date}T00:00:00Z_#{to_date}T23:59:59Z.csv") | ||
expect(file).not_to be nil | ||
rows = file.body.split("\n") | ||
expect(rows.count).to eq 1 | ||
expect(rows.first).to eq "date,report_count" | ||
end | ||
end |