Skip to content

Commit

Permalink
Use webmock stubbing rather than custom method
Browse files Browse the repository at this point in the history
This helps each test show to the reader that there is a network request happening.
  • Loading branch information
jcoyne committed Nov 14, 2023
1 parent a97d669 commit eacb7ff
Show file tree
Hide file tree
Showing 28 changed files with 338 additions and 146 deletions.
3 changes: 2 additions & 1 deletion spec/components/embed/m3_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
before do
allow(request).to receive(:purl_object).and_return(object)
allow(viewer).to receive(:asset_host).and_return('http://example.com/')
allow(object).to receive(:response).and_return(image_purl)
stub_request(:get, 'https://purl.stanford.edu/12345.xml')
.to_return(status: 200, body: image_purl)
render_inline(described_class.new(viewer:))
end

Expand Down
3 changes: 2 additions & 1 deletion spec/components/embed/media_tag_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
let(:many_primary_files) { false }

before do
stub_purl_response_with_fixture(purl)
stub_request(:get, 'https://purl.stanford.edu/bc123df4567.xml')
.to_return(status: 200, body: purl)
render
end

Expand Down
3 changes: 2 additions & 1 deletion spec/features/3d_viewer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
let(:purl) { threeD_object_purl }

before do
stub_purl_response_with_fixture(purl)
stub_request(:get, 'https://purl.stanford.edu/ignored.xml')
.to_return(status: 200, body: purl)
visit_iframe_response
end

Expand Down
6 changes: 4 additions & 2 deletions spec/features/basic_functionality_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
RSpec.describe 'basic oembed functionality' do
include PurlFixtures
it 'returns a response with required parameters (xml)' do
stub_purl_response_with_fixture(file_purl)
stub_request(:get, 'https://purl.stanford.edu/abc.xml')
.to_return(status: 200, body: file_purl)
visit embed_path(url: 'http://purl.stanford.edu/abc', format: 'xml')
expect(page).to have_xpath '//oembed'
expect(page).to have_xpath '//type'
expect(page).to have_xpath '//version', text: '1.0'
end

it 'returns a response with correct parameter fields (xml)' do
stub_purl_response_with_fixture(file_purl)
stub_request(:get, 'https://purl.stanford.edu/abc.xml')
.to_return(status: 200, body: file_purl)
visit embed_path(url: 'http://purl.stanford.edu/abc', format: 'xml')
expect(page).to have_xpath '//type', text: 'rich'
expect(page).to have_xpath('//title', text: 'File Title')
Expand Down
15 changes: 10 additions & 5 deletions spec/features/download_panel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
end

it 'not shown for file viewer and leaves correctly formatted filenames alone' do
stub_purl_response_with_fixture(multi_file_purl)
stub_request(:get, 'https://purl.stanford.edu/ignored.xml')
.to_return(status: 200, body: multi_file_purl)
visit_iframe_response
expect(page).to have_css '.sul-embed-body.sul-embed-file'
expect(page).not_to have_css '.sul-embed-download-panel'
Expand All @@ -22,15 +23,17 @@
end

it 'correctly encodes a wonky filename with spaces and a special character' do
stub_purl_response_with_fixture(wonky_filename_purl)
stub_request(:get, 'https://purl.stanford.edu/ignored.xml')
.to_return(status: 200, body: wonky_filename_purl)
visit_iframe_response
link = page.find('.sul-embed-media-list a', match: :first)
expect(link['href']).to eq('https://stacks.stanford.edu/file/druid:ignored/%23Title%20of%20the%20PDF.pdf') # this file link had a # and spaces, encoding is needed
end

describe 'hide download?' do
before do
stub_purl_response_with_fixture(geo_purl_public)
stub_request(:get, 'https://purl.stanford.edu/abc123.xml')
.to_return(status: 200, body: geo_purl_public)
end

it 'when selected should hide the button' do
Expand All @@ -46,7 +49,8 @@

describe 'download file count shows within download button' do
it 'has the file count for multiple media files in the download panel' do
stub_purl_response_with_fixture(multi_media_purl)
stub_request(:get, 'https://purl.stanford.edu/ignored.xml')
.to_return(status: 200, body: multi_media_purl)
visit_iframe_response
expect(page).to have_css '.sul-embed-body.sul-embed-media' # so shows download count
within '.sul-i-download-3' do
Expand All @@ -55,7 +59,8 @@
end

it 'only counts downloadable files' do
stub_purl_response_with_fixture(world_restricted_download_purl)
stub_request(:get, 'https://purl.stanford.edu/ignored.xml')
.to_return(status: 200, body: world_restricted_download_purl)
visit_iframe_response
expect(page).to have_css '.sul-embed-body.sul-embed-media' # so shows download count
within '.sul-i-download-3' do
Expand Down
3 changes: 2 additions & 1 deletion spec/features/embed_this_panel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
let(:iframe_options) { {} }

before do
stub_purl_response_with_fixture(spec_fixture)
stub_request(:get, 'https://purl.stanford.edu/ab123cd4567.xml')
.to_return(status: 200, body: spec_fixture)
visit_iframe_response('ab123cd4567', **iframe_options)
end

Expand Down
12 changes: 8 additions & 4 deletions spec/features/feature_testing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
include PurlFixtures
describe 'basic functionality' do
it 'makes purl embed request and embed' do
stub_purl_response_with_fixture(file_purl)
stub_request(:get, 'https://purl.stanford.edu/ignored.xml')
.to_return(status: 200, body: file_purl)
visit_iframe_response
expect(page).to have_css('.sul-embed-container')
expect(page).to have_css('.sul-embed-header')
Expand All @@ -16,15 +17,17 @@
end

it 'hides the title when requested' do
stub_purl_response_with_fixture(file_purl)
stub_request(:get, 'https://purl.stanford.edu/abc123.xml')
.to_return(status: 200, body: file_purl)
visit_iframe_response('abc123', hide_title: true)
expect(page).not_to have_css('.sul-embed-header-title')
end
end

describe 'file viewer' do
it 'contains the file list' do
stub_purl_response_with_fixture(file_purl)
stub_request(:get, 'https://purl.stanford.edu/ignored.xml')
.to_return(status: 200, body: file_purl)
visit_iframe_response
expect(page).to have_css('.sul-embed-file-list')
expect(page).to have_css('.sul-embed-media-list')
Expand All @@ -39,7 +42,8 @@
end

it 'contains 4 files in file list' do
stub_purl_response_with_fixture(multi_resource_multi_type_purl)
stub_request(:get, 'https://purl.stanford.edu/ignored.xml')
.to_return(status: 200, body: multi_resource_multi_type_purl)
visit_iframe_response
expect(page).to have_css('.sul-embed-count', count: 4)
end
Expand Down
3 changes: 2 additions & 1 deletion spec/features/file_hierarchy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
RSpec.describe 'file viewer with hierarchy', :js do
include PurlFixtures
it 'renders hierarchy' do
stub_purl_response_with_fixture(hierarchical_file_purl)
stub_request(:get, 'https://purl.stanford.edu/ignored.xml')
.to_return(status: 200, body: hierarchical_file_purl)
visit_iframe_response
expect(page).to have_content('2 items')
# There are 2 files
Expand Down
3 changes: 2 additions & 1 deletion spec/features/file_list_accessibility_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
RSpec.describe 'file list accessibility', :js do
include PurlFixtures
it 'page has relevant sr-only classes' do
stub_purl_response_with_fixture(hybrid_object_purl)
stub_request(:get, 'https://purl.stanford.edu/ignored.xml')
.to_return(status: 200, body: hybrid_object_purl)
visit_iframe_response
expect(page).to have_css 'img.sul-embed-square-image[alt]', visible: :all
expect(page).to have_content 'Download item 1'
Expand Down
10 changes: 6 additions & 4 deletions spec/features/file_preview_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

RSpec.describe 'file preview', :js do
include PurlFixtures
before do
stub_request(:get, 'https://purl.stanford.edu/ab123cd4567.xml')
.to_return(status: 200, body: hybrid_object_purl)
end

it 'displays a toggle-able section image preview' do
stub_purl_response_with_fixture(hybrid_object_purl)
visit_iframe_response
visit_iframe_response('ab123cd4567')
expect(page).not_to have_css('.sul-embed-preview', visible: :visible)
expect(page).to have_css('.sul-embed-preview-toggle', count: 1, text: 'Preview')
click_link('Preview')
Expand All @@ -16,15 +20,13 @@
end

it 'displays sul-embed-square-image for file list icon' do
stub_purl_response_with_fixture(hybrid_object_purl)
visit_iframe_response('ab123cd4567')
click_link('Preview')
expect(page).to have_css('.sul-embed-square-image', visible: :all)
expect(page).to have_css('img[src="https://stacks.stanford.edu/image/iiif/ab123cd4567%2Ftn629pk3948_img_1/square/100,100/0/default.jpg"]', visible: :all)
end

it 'has (hidden) thumb image' do
stub_purl_response_with_fixture(hybrid_object_purl)
visit_iframe_response('ab123cd4567')
click_link('Preview')
expect(page).to have_css('.sul-embed-preview')
Expand Down
12 changes: 8 additions & 4 deletions spec/features/file_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

context 'when text is entered with a file list' do
it 'limits shown files' do
stub_purl_response_with_fixture(file_purl)
stub_request(:get, 'https://purl.stanford.edu/abc123.xml')
.to_return(status: 200, body: file_purl)
visit_iframe_response('abc123', min_files_to_search: 1)
expect(page).to have_css('.sul-embed-count', count: 1)
expect(page).to have_css '.sul-embed-item-count', text: '1 item'
Expand All @@ -21,7 +22,8 @@

context 'when text is entered with a hierarchical file list' do
it 'hides empty directories' do
stub_purl_response_with_fixture(hierarchical_file_purl)
stub_request(:get, 'https://purl.stanford.edu/abc123.xml')
.to_return(status: 200, body: hierarchical_file_purl)
visit_iframe_response('abc123', min_files_to_search: 1)
expect(page).to have_css '.sul-embed-item-count', text: '2 items'
expect(page).to have_content('dir1')
Expand All @@ -41,13 +43,15 @@

context 'when the number of files are beneath the threshold' do
it 'does not display the search box' do
stub_purl_response_with_fixture(file_purl)
stub_request(:get, 'https://purl.stanford.edu/ignored.xml')
.to_return(status: 200, body: file_purl)
visit_iframe_response
expect(page).not_to have_css('.sul-embed-search-input')
end

it 'hides the search box when requested' do
stub_purl_response_with_fixture(file_purl)
stub_request(:get, 'https://purl.stanford.edu/abc123.xml')
.to_return(status: 200, body: file_purl)
visit_iframe_response('abc123', hide_search: true)
expect(page).not_to have_css('.sul-embed-search')
end
Expand Down
9 changes: 6 additions & 3 deletions spec/features/geo_viewer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
include PurlFixtures

before do
stub_purl_response_with_fixture(geo_purl_public)
stub_request(:get, 'https://purl.stanford.edu/cz128vq0535.xml')
.to_return(status: 200, body: geo_purl_public)
visit_iframe_response('cz128vq0535')
end

Expand Down Expand Up @@ -60,7 +61,8 @@
include PurlFixtures

before do
stub_purl_response_with_fixture(geo_purl_restricted)
stub_request(:get, 'https://purl.stanford.edu/ignored.xml')
.to_return(status: 200, body: geo_purl_restricted)
visit_iframe_response
end

Expand All @@ -82,7 +84,8 @@
include PurlFixtures

before do
stub_purl_response_with_fixture(geo_purl_index_map)
stub_request(:get, 'https://purl.stanford.edu/ts545zc6250.xml')
.to_return(status: 200, body: geo_purl_index_map)
visit_iframe_response 'ts545zc6250'
end

Expand Down
3 changes: 2 additions & 1 deletion spec/features/legacy_media_viewer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
before do
allow(Settings.streaming).to receive(:auth_url).and_return('/test_auth_jsonp_endpoint')
stub_auth
stub_purl_response_with_fixture(purl)
stub_request(:get, 'https://purl.stanford.edu/ignored.xml')
.to_return(status: 200, body: purl)
visit_iframe_response
end

Expand Down
3 changes: 2 additions & 1 deletion spec/features/media_viewer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
before do
allow(Settings.enabled_features).to receive(:new_component).and_return(true)
stub_auth
stub_purl_response_with_fixture(purl)
stub_request(:get, 'https://purl.stanford.edu/ignored.xml')
.to_return(status: 200, body: purl)
visit_iframe_response
end

Expand Down
7 changes: 5 additions & 2 deletions spec/features/metadata_panel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
)
end

before do
stub_request(:get, 'https://purl.stanford.edu/ignored.xml')
.to_return(status: 200, body: file_purl)
end

it 'is present after a user clicks the button' do
stub_purl_response_with_fixture(file_purl)
visit_iframe_response
expect(page).to have_css('.sul-embed-metadata-panel', visible: :hidden)
page.find('[data-sul-embed-toggle="sul-embed-metadata-panel"]', match: :first).click
Expand All @@ -23,7 +27,6 @@
end

it 'has purl link, use and reproduction, and license text' do
stub_purl_response_with_fixture(file_purl)
visit_iframe_response
page.find('[data-sul-embed-toggle="sul-embed-metadata-panel"]', match: :first).click
expect(page).to have_css('dt', text: 'Citation URL', visible: :all)
Expand Down
3 changes: 2 additions & 1 deletion spec/features/pdf_viewer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
let(:purl) { pdf_document_purl }

before do
stub_purl_response_with_fixture(purl)
stub_request(:get, 'https://purl.stanford.edu/ignored.xml')
.to_return(status: 200, body: purl)
visit_iframe_response
end

Expand Down
3 changes: 2 additions & 1 deletion spec/features/sandbox_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
include PurlFixtures

before do
stub_purl_response_with_fixture(file_purl)
stub_request(:get, 'https://purl.stanford.edu/ab123cd4567.xml')
.to_return(status: 200, body: file_purl)
end

it 'returns the iframe output from the embed endpoint' do
Expand Down
3 changes: 2 additions & 1 deletion spec/features/was_seed_viewer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
before do
allow_any_instance_of(Embed::WasTimeMap).to receive(:redirectable_connection).and_return(fake_connection)
expect(fake_connection).to receive(:get).once
stub_purl_response_with_fixture(was_seed_purl)
stub_request(:get, 'https://purl.stanford.edu/ignored.xml')
.to_return(status: 200, body: was_seed_purl)
visit_iframe_response
end

Expand Down
6 changes: 5 additions & 1 deletion spec/lib/embed/viewer/common_viewer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
let(:media_viewer) { Embed::Viewer::Media.new(request) }
let(:was_seed_viewer) { Embed::Viewer::WasSeed.new(request) }

before do
stub_request(:get, 'https://purl.stanford.edu/12345.xml')
.to_return(status: 200, body: multi_file_purl)
end

describe 'height/width' do
it 'sets a default height and default width to nil (which can be overridden at the viewer level)' do
stub_purl_request(request)
Expand All @@ -22,7 +27,6 @@
expect(request).to receive(:maxheight).at_least(:once).and_return(100)
expect(request).to receive(:maxwidth).at_least(:once).and_return(200)
stub_purl_request(request)
stub_purl_response_with_fixture(multi_file_purl)
expect(file_viewer.height).to eq 100
expect(file_viewer.width).to eq 200
end
Expand Down
Loading

0 comments on commit eacb7ff

Please sign in to comment.