Skip to content

Commit

Permalink
Consolidating to one way to mock S3 connections
Browse files Browse the repository at this point in the history
Removing stub_work_s3_requests and mock_s3_query_service
  • Loading branch information
carolyncole committed Feb 22, 2023
1 parent 2b3cd4b commit 7c2d80d
Show file tree
Hide file tree
Showing 10 changed files with 10 additions and 145 deletions.
2 changes: 1 addition & 1 deletion spec/controllers/works_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion spec/models/work_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down
5 changes: 1 addition & 4 deletions spec/requests/works_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion spec/services/s3_query_service_spec.rb
Original file line number Diff line number Diff line change
@@ -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" }
Expand Down Expand Up @@ -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
27 changes: 0 additions & 27 deletions spec/support/s3_query_service_specs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
103 changes: 0 additions & 103 deletions spec/support/work_s3_requests_specs.rb

This file was deleted.

6 changes: 1 addition & 5 deletions spec/system/authz_super_admin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion spec/system/view_data_in_s3_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
2 changes: 1 addition & 1 deletion spec/system/work_edit_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/system/work_upload_s3_objects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down

0 comments on commit 7c2d80d

Please sign in to comment.