Skip to content

Commit

Permalink
Add FactoryBot
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Nov 18, 2023
1 parent 7ed0d0b commit 656d2b1
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 61 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ gem 'nokogiri', '>= 1.7.1'
group :development, :test do
gem 'capybara'
gem 'debug', platforms: %i[mri]
gem 'factory_bot_rails', '~> 6.4'
gem 'high_voltage'
gem 'rspec'
gem 'rspec-rails'
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ GEM
dry-schema (>= 1.12, < 2)
zeitwerk (~> 2.6)
erubi (1.12.0)
factory_bot (6.4.0)
activesupport (>= 5.0.0)
factory_bot_rails (6.4.0)
factory_bot (~> 6.4)
railties (>= 5.0.0)
faraday (2.7.11)
base64
faraday-net_http (>= 2.0, < 3.1)
Expand Down Expand Up @@ -400,6 +405,7 @@ DEPENDENCIES
debug
dlss-capistrano
dor-rights-auth
factory_bot_rails (~> 6.4)
faraday (~> 2)
faraday-follow_redirects
filesize
Expand Down
41 changes: 41 additions & 0 deletions spec/factories/files.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

FactoryBot.define do
factory :resource_file, class: 'Embed::Purl::ResourceFile' do
trait :document do
mimetype { 'application/pdf' }
filename { 'Title of the PDF.pdf' }
size { 12_345 }
end

trait :hierarchical_file do
mimetype { 'application/pdf' }
filename { 'dir1/dir2/Title_of_2_PDF.pdf' }
size { 12_345 }
end

trait :image do
mimetype { 'image/jp2' }
filename { 'image_001.jp2' }
size { 12_345 }
end

trait :vtt do
mimetype { 'text/vtt' }
filename { 'abc_123_cap.webvtt' }
size { 176_218 }
end

trait :stanford_only do
stanford_only { true }
end

trait :world_downloadable do
world_downloadable { true }
end

trait :location_restricted do
location_restricted { true }
end
end
end
9 changes: 9 additions & 0 deletions spec/factories/resources.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

FactoryBot.define do
factory :resource do
# first_name { "John" }
# last_name { "Doe" }
# admin { false }
end
end
100 changes: 39 additions & 61 deletions spec/models/embed/purl/resource_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
RSpec.describe Embed::Purl::ResourceFile do
include PurlFixtures
describe 'attributes' do
before { stub_purl_xml_response_with_fixture(file_purl_xml) }

let(:resource_file) { Embed::Purl.new('12345').contents.first.files.first }
let(:resource_file) { build(:resource_file, :document) }

it 'gets the title from from the id attribute' do
expect(resource_file.title).to eq 'Title of the PDF.pdf'
Expand Down Expand Up @@ -58,9 +56,7 @@
end

describe '#hierarchical_title' do
before { stub_purl_xml_response_with_fixture(hierarchical_file_purl_xml) }

let(:resource_file) { Embed::Purl.new('12345').hierarchical_contents.dirs.first.dirs.first.files.first }
let(:resource_file) { build(:resource_file, :hierarchical_file) }

it 'get the hierarchical title from the id attribute' do
expect(resource_file.hierarchical_title).to eq 'Title_of_2_PDF.pdf'
Expand All @@ -76,98 +72,80 @@
end

describe 'image?' do
it 'returns true if the mimetype of the file is an image' do
stub_purl_xml_response_with_fixture(image_purl_xml)
expect(Embed::Purl.new('12345').contents.first.files.first).to be_image
context 'with an image' do
let(:resource_file) { build(:resource_file, :image) }

it 'returns true if the mimetype of the file is an image' do
expect(resource_file).to be_image
end
end

it 'returns false if the mimetype of the file is not an image' do
stub_purl_xml_response_with_fixture(file_purl_xml)
expect(Embed::Purl.new('12345').contents.first.files.first).not_to be_image
context 'with a document' do
let(:resource_file) { build(:resource_file, :document) }

it 'returns false if the mimetype of the file is not an image' do
expect(resource_file).not_to be_image
end
end
end

describe 'rights' do
subject { resource_file }

describe 'stanford_only?' do
it 'identifies stanford_only objects' do
stub_purl_xml_response_with_fixture(stanford_restricted_file_purl_xml)
expect(Embed::Purl.new('12345').contents.first.files.all?(&:stanford_only?)).to be true
end
context 'when stanford only' do
let(:resource_file) { build(:resource_file, :document, :stanford_only) }

it 'identifies stanford_only no-download objects' do
stub_purl_xml_response_with_fixture(stanford_no_download_restricted_file_purl_xml)
expect(Embed::Purl.new('12345').contents.first.files.all?(&:stanford_only?)).to be true
it { is_expected.to be_stanford_only }
end

it 'identifies world accessible objects as not stanford only' do
stub_purl_xml_response_with_fixture(file_purl_xml)
expect(Embed::Purl.new('12345').contents.first.files.all?(&:stanford_only?)).to be false
end
context 'when not stanford only' do
let(:resource_file) { build(:resource_file, :document) }

it 'identifies file-level stanford_only rights' do
stub_purl_xml_response_with_fixture(stanford_restricted_multi_file_purl_xml)
contents = Embed::Purl.new('12345').contents
first_file = contents.first.files.first
last_file = contents.last.files.first
expect(first_file).to be_stanford_only
expect(last_file).not_to be_stanford_only
it { is_expected.not_to be_stanford_only }
end
end

describe 'location_restricted?' do
it 'identifies location restricted objects' do
stub_purl_xml_response_with_fixture(single_video_purl)
expect(Embed::Purl.new('12345').contents.first.files.all?(&:location_restricted?)).to be true
end
context 'when location restricted' do
let(:resource_file) { build(:resource_file, :document, :location_restricted) }

it 'identifies world accessible objects as not stanford only' do
stub_purl_xml_response_with_fixture(file_purl_xml)
expect(Embed::Purl.new('12345').contents.first.files.all?(&:location_restricted?)).to be false
it { is_expected.to be_location_restricted }
end

it 'identifies file-level location_restricted rights' do
stub_purl_xml_response_with_fixture(video_purl)
contents = Embed::Purl.new('12345').contents
first_file = contents.first.files.first
last_file = contents.last.files.first
expect(first_file).to be_location_restricted
expect(last_file).not_to be_location_restricted
context 'when not location restricted' do
let(:resource_file) { build(:resource_file, :document) }

it { is_expected.not_to be_location_restricted }
end
end

describe 'world_downloadable?' do
it 'is false for stanford-only objects' do
stub_purl_xml_response_with_fixture(stanford_restricted_file_purl_xml)
expect(Embed::Purl.new('12345').contents.first.files.all?(&:world_downloadable?)).to be false
end
context 'when world downloadable' do
let(:resource_file) { build(:resource_file, :document, :world_downloadable) }

it 'is false for no-download objects' do
stub_purl_xml_response_with_fixture(stanford_no_download_restricted_file_purl_xml)
expect(Embed::Purl.new('12345').contents.first.files.all?(&:world_downloadable?)).to be false
it { is_expected.to be_world_downloadable }
end

it 'is true for identify world accessible objects' do
stub_purl_xml_response_with_fixture(file_purl_xml)
expect(Embed::Purl.new('12345').contents.first.files.all?(&:world_downloadable?)).to be true
context 'when not world downloadable' do
let(:resource_file) { build(:resource_file, :document) }

it { is_expected.not_to be_world_downloadable }
end
end
end

describe '#vtt?' do
subject { file.vtt? }
subject { resource_file.vtt? }

context 'when it is a vtt transcript' do
let(:file) { Embed::Purl.new('12345').contents.first.files.second }

before { stub_purl_xml_response_with_fixture(video_purl_with_vtt) }
let(:resource_file) { build(:resource_file, :vtt) }

it { is_expected.to be true }
end

context 'when it is not a vtt transcript' do
let(:file) { Embed::Purl.new('12345').contents.first.files.first }

before { stub_purl_xml_response_with_fixture(single_video_purl) }
let(:resource_file) { build(:resource_file, :document) }

it { is_expected.to be false }
end
Expand Down
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
config.include ViewComponent::TestHelpers, type: :component
config.include ViewComponent::SystemTestHelpers, type: :component
config.include Capybara::RSpecMatchers, type: :component
config.include FactoryBot::Syntax::Methods
end

def stub_purl_xml_response_with_fixture(fixture)
Expand Down

0 comments on commit 656d2b1

Please sign in to comment.