From 7c2d80d5cda5c74464996e6d5a6c6329a40a4cc6 Mon Sep 17 00:00:00 2001 From: Carolyn Cole Date: Wed, 22 Feb 2023 13:53:37 -0500 Subject: [PATCH] Consolidating to one way to mock S3 connections Removing stub_work_s3_requests and mock_s3_query_service --- spec/controllers/works_controller_spec.rb | 2 +- spec/models/work_spec.rb | 2 +- spec/requests/works_spec.rb | 5 +- spec/services/s3_query_service_spec.rb | 4 +- spec/support/s3_query_service_specs.rb | 27 ------ spec/support/work_s3_requests_specs.rb | 103 --------------------- spec/system/authz_super_admin_spec.rb | 6 +- spec/system/view_data_in_s3_spec.rb | 2 +- spec/system/work_edit_spec.rb | 2 +- spec/system/work_upload_s3_objects_spec.rb | 2 +- 10 files changed, 10 insertions(+), 145 deletions(-) delete mode 100644 spec/support/work_s3_requests_specs.rb diff --git a/spec/controllers/works_controller_spec.rb b/spec/controllers/works_controller_spec.rb index 026d4bfcb..44a45ff9b 100644 --- a/spec/controllers/works_controller_spec.rb +++ b/spec/controllers/works_controller_spec.rb @@ -499,7 +499,7 @@ end end - context "when the Work has been curated", mock_s3_query_service: false do + context "when the Work has been curated" do let(:work) { FactoryBot.create(:approved_work) } let(:user) do FactoryBot.create :user, collections_to_admin: [work.collection] diff --git a/spec/models/work_spec.rb b/spec/models/work_spec.rb index 344b1a147..bc5e374ca 100644 --- a/spec/models/work_spec.rb +++ b/spec/models/work_spec.rb @@ -735,7 +735,7 @@ end end - describe "#save", mock_s3_query_service: false do + describe "#save" do context "when the Work is persisted and not yet in the approved state" do let(:work) { FactoryBot.create(:draft_work) } diff --git a/spec/requests/works_spec.rb b/spec/requests/works_spec.rb index 0f9e37e94..5c599bd20 100644 --- a/spec/requests/works_spec.rb +++ b/spec/requests/works_spec.rb @@ -9,10 +9,6 @@ FactoryBot.create(:tokamak_work) end - before do - stub_work_s3_requests(work: work) - end - it "will not show a work page unless the user is logged in" do get work_url(work) expect(response.code).to eq "302" @@ -23,6 +19,7 @@ context "when authenticated" do before do sign_in(user) + stub_s3 end it "will show the work page displaying the work metadata" do diff --git a/spec/services/s3_query_service_spec.rb b/spec/services/s3_query_service_spec.rb index 62f7575bb..625853da2 100644 --- a/spec/services/s3_query_service_spec.rb +++ b/spec/services/s3_query_service_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "rails_helper" -RSpec.describe S3QueryService, mock_s3_query_service: false do +RSpec.describe S3QueryService do let(:work) { FactoryBot.create :draft_work, doi: doi } let(:subject) { described_class.new(work) } let(:s3_key1) { "10-34770/pe9w-x904/SCoData_combined_v1_2020-07_README.txt" } @@ -300,12 +300,14 @@ let(:s3_file) { s3_query_service.find_s3_file(filename: filename) } it "retrieves the S3File from the AWS Bucket" do + stub_request(:get, "https://example-bucket.s3.amazonaws.com/10.34770/pe9w-x904/#{work.id}/test.txt").to_return(status: 200, body: "test_content", headers: response_headers) expect(s3_file).not_to be nil expect(s3_file.filename).to eq("10.34770/pe9w-x904/#{work.id}/test.txt") expect(s3_file.last_modified).to be_a(Time) expect(s3_file.size).to eq(12) expect(s3_file.checksum).to eq("6805f2cfc46c0f04559748bb039d69ae") + assert_requested(:get, "https://example-bucket.s3.amazonaws.com/10.34770/pe9w-x904/#{work.id}/test.txt") end end end diff --git a/spec/support/s3_query_service_specs.rb b/spec/support/s3_query_service_specs.rb index 2b241cab1..f897113f0 100644 --- a/spec/support/s3_query_service_specs.rb +++ b/spec/support/s3_query_service_specs.rb @@ -24,30 +24,3 @@ def mock_bucket(bucket_url) stub_request(:delete, /#{bucket_url}/).to_return(status: 200) end end - -RSpec.configure do |config| - config.before(:each, mock_s3_query_service: false) do - @s3_bucket_url = "https://example-bucket.s3.amazonaws.com/" - - @s3_object_response_headers = { - 'Accept-Ranges': "bytes", - 'Content-Length': 12, - 'Content-Type': "text/plain", - 'ETag': "6805f2cfc46c0f04559748bb039d69ae", - 'Last-Modified': Time.parse("Thu, 15 Dec 2016 01:19:41 GMT") - } - - @s3_object_url = "https://example-bucket.s3.amazonaws.com/10.34770/pe9w-x904/" - stub_request(:get, /#{Regexp.escape(@s3_object_url)}/).to_return(status: 200, body: "test_content", headers: @s3_object_response_headers) - - @s3_bucket_query_url = "https://example-bucket.s3.amazonaws.com/?list-type=2&max-keys=1000&prefix=10.34770/doc-1/" - stub_request(:get, /#{Regexp.escape(@s3_bucket_query_url)}/).to_return(status: 200) - - stub_request(:get, "https://example-bucket.s3.amazonaws.com/test_key").to_return(status: 200, body: "test_content", headers: @s3_object_response_headers) - stub_request(:get, /#{Regexp.escape(@s3_bucket_url)}/).to_return(status: 200, body: "test_content", headers: @s3_object_response_headers) - - stub_request(:get, /#{Regexp.escape(@s3_bucket_url)}/).to_return(status: 200, body: "test_content", headers: @s3_object_response_headers) - - allow(S3QueryService).to receive(:new).and_call_original - end -end diff --git a/spec/support/work_s3_requests_specs.rb b/spec/support/work_s3_requests_specs.rb deleted file mode 100644 index 69e602d88..000000000 --- a/spec/support/work_s3_requests_specs.rb +++ /dev/null @@ -1,103 +0,0 @@ -# frozen_string_literal: true - -RSpec.configure do |_config| - def build_s3_list_objects_response(work:, file_name:) - @s3_list_objects_response = <<-XML - - - example-bucket - - 1 - 1000 - false - - #{work.s3_object_key}/#{file_name} - 2009-10-12T17:50:30.000Z - "fba9dede5f27731c9771645a39863328" - 434234 - STANDARD - - -XML - end - - # rubocop:disable Metrics/AbcSize - # rubocop:disable Metrics/MethodLength - def stub_work_s3_requests(works: [], work: nil, file_name: nil) - # Use the built-in AWS S3 stub - # (https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/stubbing.html) - s3 = Aws::S3::Client.new(stub_responses: true) - allow(Aws::S3::Client).to receive(:new).and_return(s3) - s3.stub_responses(:head_object, [Aws::S3::Errors::NotFound.new("1", "2"), true]) - - @works = if work.nil? - works - else - [work] - end - - @works.each do |w| - ## Implicit context: S3 Bucket exists for a pre-curation Work - # Stub the request for the S3 directory object to determine if it exists (pre-curation bucket) - stub_request(:head, "https://example-bucket.s3.amazonaws.com/#{w.s3_object_key}").to_return(status: 200) - - ## Implicit context: S3 Bucket exists for a pre-curation Work, Work is being updated into the post-curation state - # Stub the request for the S3 directory object to determine if it exists (post-curation) - stub_request(:head, "https://example-bucket-post.s3.amazonaws.com/#{w.s3_object_key}").to_return(status: 404) - # Stub the request to delete the S3 directory object to determine if it exists (pre-curation bucket) - stub_request(:delete, "https://example-bucket.s3.amazonaws.com/#{w.s3_object_key}").to_return(status: 200) - - # Build the request body for the request to query the contents of the S3 directory object - # By default, this is empty - s3_list_objects_response = [] - - unless file_name.nil? - ## Implicit context: S3 Bucket exists for a pre-curation Work, the Work has one S3 file attachment Object stored - # Stub the request for retrieving the S3 file attachment object - stub_request(:get, "https://example-bucket.s3.amazonaws.com/#{w.s3_object_key}/#{file_name}").to_return(status: 200) - # Stub the request for uploading the S3 file attachment object - stub_request(:put, "https://example-bucket.s3.amazonaws.com/#{w.s3_object_key}/#{file_name}").to_return(status: 200) - # Build the request body for the request to query the contents of the S3 directory object - s3_list_objects_response = build_s3_list_objects_response(work: w, file_name: file_name) - end - - ## Implicit context: S3 Bucket exists for a pre-curation Work, the Work has one S3 file attachment Object stored - # Stub the pre-curation bucket list - stub_request(:get, "https://example-bucket.s3.amazonaws.com/?list-type=2&max-keys=1000&prefix=#{w.s3_object_key}/").to_return( - status: 200, - body: s3_list_objects_response - ) - - ## Implicit context: S3 Bucket exists for a post-curation Work, the Work has one S3 file attachment Object stored - # Stub the post-curation bucket list - stub_request(:get, "https://example-bucket-post.s3.amazonaws.com/?list-type=2&max-keys=1000&prefix=#{w.s3_object_key}/").to_return( - status: 200, - body: s3_list_objects_response - ) - - next if file_name.nil? - # Stub the request for retrieving the S3 file attachment object - stub_request(:get, "https://example-bucket-post.s3.amazonaws.com/#{w.s3_object_key}/#{file_name}").to_return( - status: 200, - body: { - accept_ranges: "bytes", - content_length: 3191, - content_type: "image/jpeg", - etag: "\"6805f2cfc46c0f04559748bb039d69ae\"", - last_modified: Time.parse("Thu, 15 Dec 2016 01:19:41 GMT"), - metadata: { - }, - tag_count: 2, - version_id: "null" - }.to_json - ) - # Stub the request for the S3 directory object to determine if it exists (post-curation) - stub_request(:head, "https://example-bucket-post.s3.amazonaws.com/#{w.s3_object_key}/#{file_name}").to_return(status: 200) - - # Stub the request to delete the S3 file object (pre-curation bucket) - stub_request(:delete, "https://example-bucket.s3.amazonaws.com/#{w.s3_object_key}/#{file_name}").to_return(status: 200) - end - # rubocop:enable Metrics/AbcSize - # rubocop:enable Metrics/MethodLength - end -end diff --git a/spec/system/authz_super_admin_spec.rb b/spec/system/authz_super_admin_spec.rb index 588226aa9..ccee61635 100644 --- a/spec/system/authz_super_admin_spec.rb +++ b/spec/system/authz_super_admin_spec.rb @@ -65,13 +65,9 @@ it "should be able to approve a work" do stub_datacite_doi + stub_s3 work = FactoryBot.create :awaiting_approval_work - file_name = "us_covid_2019.csv" - stub_work_s3_requests(work: work, file_name: file_name) - uploaded_file = fixture_file_upload(file_name, "text/csv") - work.pre_curation_uploads.attach(uploaded_file) - work.save! work.reload allow(Work).to receive(:find).with(work.id).and_return(work) diff --git a/spec/system/view_data_in_s3_spec.rb b/spec/system/view_data_in_s3_spec.rb index 58c9a2128..5b9a55a0b 100644 --- a/spec/system/view_data_in_s3_spec.rb +++ b/spec/system/view_data_in_s3_spec.rb @@ -7,7 +7,7 @@ sign_in user end - describe "when a dataset has a DOI and its data is in S3", mock_s3_query_service: false do + describe "when a dataset has a DOI and its data is in S3" do let(:user) { FactoryBot.create :princeton_submitter } let(:work) { FactoryBot.create(:shakespeare_and_company_work, created_by_user_id: user.id) } let(:s3_query_service_double) { instance_double(S3QueryService) } diff --git a/spec/system/work_edit_spec.rb b/spec/system/work_edit_spec.rb index 14b61799e..39765ab88 100644 --- a/spec/system/work_edit_spec.rb +++ b/spec/system/work_edit_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "rails_helper" -RSpec.describe "Creating and updating works", type: :system, js: true, mock_s3_query_service: false do +RSpec.describe "Creating and updating works", type: :system, js: true do let(:user) { FactoryBot.create(:princeton_submitter) } before do diff --git a/spec/system/work_upload_s3_objects_spec.rb b/spec/system/work_upload_s3_objects_spec.rb index 1129c8417..da226b696 100644 --- a/spec/system/work_upload_s3_objects_spec.rb +++ b/spec/system/work_upload_s3_objects_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" describe "Uploading S3 Bucket Objects for new Work", mock_ezid_api: true do - context "when creating a Work", mock_s3_query_service: false do + context "when creating a Work" do let(:user) { FactoryBot.create :princeton_submitter } let(:work) { FactoryBot.create(:shakespeare_and_company_work, created_by_user_id: user.id) } let(:s3_query_service_double) { instance_double(S3QueryService, client_s3_files: s3_data) }