Browse Source

More rebase updates

pull/2673/head
Kat 2 years ago
parent
commit
0143aa6ecc
  1. 8
      app/services/collection_resources_service.rb
  2. 7
      app/services/storage/s3_service.rb
  3. 2
      spec/features/collection_resources_spec.rb
  4. 12
      spec/helpers/collection_resources_helper_spec.rb
  5. 19
      spec/requests/collection_resources_controller_spec.rb

8
app/services/collection_resources_service.rb

@ -20,13 +20,7 @@ class CollectionResourcesService
end end
def file_exists_on_s3?(file) def file_exists_on_s3?(file)
url = "https://#{@storage_service.configuration.bucket_name}.s3.amazonaws.com/#{file}" @storage_service.file_exists?(file)
uri = URI.parse(url)
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
http.request_head(uri)
end
response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPRedirection)
end end
def upload_collection_resource(filename, file) def upload_collection_resource(filename, file)

7
app/services/storage/s3_service.rb

@ -43,6 +43,13 @@ module Storage
@client.head_object(bucket: @configuration.bucket_name, key: file_name) @client.head_object(bucket: @configuration.bucket_name, key: file_name)
end end
def file_exists?(file_name)
@client.head_object(bucket: @configuration.bucket_name, key: file_name)
true
rescue Aws::S3::Errors::NotFound
false
end
private private
def create_configuration def create_configuration

2
spec/features/collection_resources_spec.rb

@ -7,7 +7,7 @@ RSpec.describe "Collection resources" do
before do before do
allow(CollectionResourcesService).to receive(:new).and_return(collection_resources_service) allow(CollectionResourcesService).to receive(:new).and_return(collection_resources_service)
allow(collection_resources_service).to receive(:upload_collection_resource) allow(collection_resources_service).to receive(:upload_collection_resource)
allow(collection_resources_service).to receive(:get_file_metadata).and_return({ "Content-Type" => "application/pdf", "Content-Length" => 1000 }) allow(collection_resources_service).to receive(:get_file_metadata).and_return({ "content_type" => "application/pdf", "content_length" => 1000 })
allow(user).to receive(:need_two_factor_authentication?).and_return(false) allow(user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in user sign_in user
end end

12
spec/helpers/collection_resources_helper_spec.rb

@ -115,10 +115,8 @@ RSpec.describe CollectionResourcesHelper do
end end
before do before do
WebMock.stub_request(:head, /https:\/\/core-test-collection-resources\.s3\.amazonaws\.com\/2023_24_lettings_paper_form.pdf/) allow(storage_service).to receive(:get_file_metadata).with("2023_24_lettings_paper_form.pdf").and_return("content_length" => 292_864, "content_type" => "application/pdf")
.to_return(status: 200, body: "", headers: { "Content-Length" => 292_864, "Content-Type" => "application/pdf" }) allow(storage_service).to receive(:get_file_metadata).with("2023_24_lettings_bulk_upload_template.xlsx").and_return("content_length" => 19_456, "content_type" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
WebMock.stub_request(:head, /https:\/\/core-test-collection-resources\.s3\.amazonaws\.com\/2023_24_lettings_bulk_upload_template.xlsx/)
.to_return(status: 200, body: "", headers: { "Content-Length" => 19_456, "Content-Type" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" })
end end
it "returns component items" do it "returns component items" do
@ -146,10 +144,8 @@ RSpec.describe CollectionResourcesHelper do
end end
before do before do
WebMock.stub_request(:head, /https:\/\/core-test-collection-resources\.s3\.amazonaws\.com\/2023_24_lettings_paper_form.pdf/) allow(storage_service).to receive(:get_file_metadata).with("2023_24_lettings_paper_form.pdf").and_return("content_length" => 292_864, "content_type" => "application/pdf")
.to_return(status: 200, body: "", headers: { "Content-Length" => 292_864, "Content-Type" => "application/pdf" }) allow(storage_service).to receive(:get_file_metadata).with("2023_24_lettings_bulk_upload_template.xlsx").and_return("content_length" => 19_456, "content_type" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
WebMock.stub_request(:head, /https:\/\/core-test-collection-resources\.s3\.amazonaws\.com\/2023_24_lettings_bulk_upload_template.xlsx/)
.to_return(status: 200, body: "", headers: { "Content-Length" => 19_456, "Content-Type" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" })
end end
it "returns component items" do it "returns component items" do

19
spec/requests/collection_resources_controller_spec.rb

@ -2,7 +2,7 @@ require "rails_helper"
RSpec.describe CollectionResourcesController, type: :request do RSpec.describe CollectionResourcesController, type: :request do
let(:page) { Capybara::Node::Simple.new(response.body) } let(:page) { Capybara::Node::Simple.new(response.body) }
let(:storage_service) { instance_double(Storage::S3Service) } let(:storage_service) { instance_double(Storage::S3Service, get_file_metadata: nil) }
before do before do
allow(Storage::S3Service).to receive(:new).and_return(storage_service) allow(Storage::S3Service).to receive(:new).and_return(storage_service)
@ -49,6 +49,7 @@ RSpec.describe CollectionResourcesController, type: :request do
before do before do
allow(Time.zone).to receive(:today).and_return(Time.zone.local(2025, 1, 8)) allow(Time.zone).to receive(:today).and_return(Time.zone.local(2025, 1, 8))
allow(user).to receive(:need_two_factor_authentication?).and_return(false) allow(user).to receive(:need_two_factor_authentication?).and_return(false)
allow(storage_service).to receive(:file_exists?).and_return(true)
sign_in user sign_in user
end end
@ -71,6 +72,7 @@ RSpec.describe CollectionResourcesController, type: :request do
context "when files are on S3" do context "when files are on S3" do
before do before do
allow(storage_service).to receive(:file_exists?).and_return(true)
get collection_resources_path get collection_resources_path
end end
@ -110,8 +112,7 @@ RSpec.describe CollectionResourcesController, type: :request do
context "when files are not on S3" do context "when files are not on S3" do
before do before do
WebMock.stub_request(:head, /https:\/\/core-test-collection-resources\.s3\.amazonaws\.com/) allow(storage_service).to receive(:file_exists?).and_return(false)
.to_return(status: 404, body: "", headers: {})
get collection_resources_path get collection_resources_path
end end
@ -141,8 +142,7 @@ RSpec.describe CollectionResourcesController, type: :request do
context "when the file exists on S3" do context "when the file exists on S3" do
before do before do
WebMock.stub_request(:get, /https:\/\/core-test-collection-resources\.s3\.amazonaws\.com/) allow(storage_service).to receive(:get_file_io).and_return("file")
.to_return(status: 200, body: "file", headers: { "Content-Type" => "application/pdf", "Content-Length" => 1000 })
get download_mandatory_collection_resource_path(log_type: "lettings", year: 2025, resource_type: "paper_form") get download_mandatory_collection_resource_path(log_type: "lettings", year: 2025, resource_type: "paper_form")
end end
@ -153,8 +153,7 @@ RSpec.describe CollectionResourcesController, type: :request do
context "when the file does not exist on S3" do context "when the file does not exist on S3" do
before do before do
WebMock.stub_request(:get, /https:\/\/core-test-collection-resources\.s3\.amazonaws\.com/) allow(storage_service).to receive(:get_file_io).and_return(nil)
.to_return(status: 404, body: "", headers: {})
get download_mandatory_collection_resource_path(log_type: "lettings", year: 2024, resource_type: "paper_form") get download_mandatory_collection_resource_path(log_type: "lettings", year: 2024, resource_type: "paper_form")
end end
@ -189,8 +188,7 @@ RSpec.describe CollectionResourcesController, type: :request do
context "when year is in editable_collection_resource_years but not in displayed_collection_resource_years" do context "when year is in editable_collection_resource_years but not in displayed_collection_resource_years" do
before do before do
WebMock.stub_request(:get, /https:\/\/core-test-collection-resources\.s3\.amazonaws\.com/) allow(storage_service).to receive(:get_file_io).and_return("file")
.to_return(status: 200, body: "file", headers: { "Content-Type" => "application/pdf", "Content-Length" => 1000 })
get download_mandatory_collection_resource_path(log_type: "lettings", year: 2026, resource_type: "paper_form") get download_mandatory_collection_resource_path(log_type: "lettings", year: 2026, resource_type: "paper_form")
end end
@ -262,9 +260,10 @@ RSpec.describe CollectionResourcesController, type: :request do
describe "PATCH #update_mandatory_collection_resource" do describe "PATCH #update_mandatory_collection_resource" do
let(:some_file) { File.open(file_fixture("blank_bulk_upload_sales.csv")) } let(:some_file) { File.open(file_fixture("blank_bulk_upload_sales.csv")) }
let(:params) { { collection_resource: { year: 2024, log_type: "sales", resource_type: "bulk_upload_template", file: some_file } } } let(:params) { { collection_resource: { year: 2024, log_type: "sales", resource_type: "bulk_upload_template", file: some_file } } }
let(:collection_resource_service) { instance_double(CollectionResourcesService) }
before do before do
allow(UploadCollectionResourcesService).to receive(:upload_collection_resource) allow(CollectionResourcesService).to receive(:new).and_return(collection_resource_service)
end end
context "when user is not signed in" do context "when user is not signed in" do

Loading…
Cancel
Save