Skip to content

Commit

Permalink
Merge pull request #1041 from sul-dlss/cocina-test
Browse files Browse the repository at this point in the history
  • Loading branch information
mjgiarlo authored Nov 10, 2023
2 parents ad7a60c + a7e36e2 commit 8ed1857
Show file tree
Hide file tree
Showing 15 changed files with 818 additions and 51 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ jobs:
- name: Install dependencies
run: bin/setup
- name: Run tests
run: bundle exec rake
run: bin/rake
- name: Run tests (with cocina)
run: SETTINGS__FEATURES__COCINA=true bin/rake spec
2 changes: 1 addition & 1 deletion app/models/cocina_rights.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# The rights derived from cocina
# The file rights derived from cocina
class CocinaRights
def initialize(file_rights)
@file_rights = file_rights
Expand Down
46 changes: 38 additions & 8 deletions app/models/stacks_rights.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,59 @@ def initialize(id:, file_name:)
end

def maybe_downloadable?
rights.world_unrestricted_file?(file_name) ||
rights.stanford_only_unrestricted_file?(file_name)
if use_json?
%w[world stanford].include?(cocina_rights.download)
else
rights.world_unrestricted_file?(file_name) ||
rights.stanford_only_unrestricted_file?(file_name)
end
end

def stanford_restricted?
value, _rule = rights.stanford_only_rights_for_file file_name
if use_json?
cocina_rights.view == 'stanford'
else
value, _rule = rights.stanford_only_rights_for_file file_name

value
value
end
end

def cdl_restricted?
value, _rule = rights.cdl_rights_for_file file_name
if use_json?
cocina_rights.controlled_digital_lending?
else
value, _rule = rights.cdl_rights_for_file file_name

value
value
end
end

# Returns true if a given file has any location restrictions.
# Falls back to the object-level behavior if none at file level.
def restricted_by_location?
rights.restricted_by_location?(file_name)
if use_json?
cocina_rights.view == 'location-based' || cocina_rights.download == 'location-based'
else
rights.restricted_by_location?(file_name)
end
end

delegate :embargoed?, :embargo_release_date, to: :rights
def embargoed?
use_json? ? cocina_embargo? : rights.embargoed?
end

def embargo_release_date
use_json? ? cocina_embargo_release_date : rights.embargo_release_date
end

def cocina_embargo?
cocina_embargo_release_date && Time.parse(cocina_embargo_release_date).getlocal > Time.now.getlocal
end

def cocina_embargo_release_date
@cocina_embargo_release_date ||= public_json.dig('access', 'embargo', 'releaseDate')
end

def object_thumbnail?
use_json? ? cocina_thumbnail? : xml_thumbnail?
Expand Down
2 changes: 1 addition & 1 deletion spec/abilities/ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'rails_helper'
require 'cancan/matchers'

RSpec.describe 'Ability', type: :model do
RSpec.describe 'Ability', type: :model, unless: Settings.features.cocina do
subject(:ability) { Ability.new(user) }
let(:user) { nil }

Expand Down
23 changes: 23 additions & 0 deletions spec/controllers/file_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@
before do
allow(StacksFile).to receive(:new).and_return(file)
stub_rights_xml(world_readable_rights_xml)
allow(Purl).to receive(:public_json).and_return(public_json)
end

let(:public_json) do
{
'structural' => {
'contains' => [
{
'structural' => {
'contains' => [
{
'filename' => 'xf680rd3068_1.jp2',
'access' => {
'view' => 'world',
'download' => 'world'
}
}
]
}
}
]
}
}
end

let(:file) { StacksFile.new(id: druid, file_name: 'xf680rd3068_1.jp2') }
Expand Down
193 changes: 178 additions & 15 deletions spec/controllers/object_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
allow(Faraday).to receive(:get).with('https://purl.stanford.edu/foo.xml').and_return(
instance_double(Faraday::Response, status: 404, success?: false)
)
allow(Faraday).to receive(:get).with('https://purl.stanford.edu/foo.json').and_return(
instance_double(Faraday::Response, status: 404, success?: false)
)
get :show, params: { id: 'foo' }
expect(response.status).to eq 404
end
end

context "with downloadable files" do
before do
allow(Faraday).to receive(:get).with('https://purl.stanford.edu/fd063dh3727.xml').and_return(
instance_double(Faraday::Response, success?: true, body: <<-EOXML)
let(:xml) do
<<-EOXML
<publicObject id="druid:fd063dh3727" published="2019-12-19T17:58:11Z" publishVersion="dor-services/8.1.1">
<contentMetadata objectId="druid:fd063dh3727" type="book">
<resource id="fd063dh3727_1" sequence="1" type="page">
Expand Down Expand Up @@ -59,8 +61,76 @@
</access>
</rightsMetadata>
</publicObject>
EOXML
)
EOXML
end
let(:json) do
{
'structural' => {
'contains' => [
{
'structural' => {
'contains' => [
{
'filename' => '36105116040556_0002.pdf',
'access' => {
'view' => 'world',
'download' => 'world'
}
},
{
'filename' => '36105116040556_0002.xml',
'access' => {
'view' => 'world',
'download' => 'world'
}
},
{
'filename' => '36105116040556_0002.jp2',
'access' => {
'view' => 'world',
'download' => 'world'
}
}
]
}
},
{
'structural' => {
'contains' => [
{
'filename' => '36105116040556_0003.pdf',
'access' => {
'view' => 'world',
'download' => 'world'
}
},
{
'filename' => '36105116040556_0003.xml',
'access' => {
'view' => 'world',
'download' => 'world'
}
},
{
'filename' => '36105116040556_0003.jp2',
'access' => {
'view' => 'world',
'download' => 'world'
}
}
]
}
}
]
}
}.to_json
end

before do
allow(Faraday).to receive(:get).with('https://purl.stanford.edu/fd063dh3727.xml')
.and_return(instance_double(Faraday::Response, success?: true, body: xml))
allow(Faraday).to receive(:get).with('https://purl.stanford.edu/fd063dh3727.json')
.and_return(instance_double(Faraday::Response, success?: true, body: json))
end

it 'creates a zip' do
Expand All @@ -71,9 +141,8 @@
end

context "with a stanford access file" do
before do
allow(Faraday).to receive(:get).with('https://purl.stanford.edu/bb142ws0723.xml').and_return(
instance_double(Faraday::Response, success?: true, body: <<-EOXML)
let(:xml) do
<<-EOXML
<publicObject id="druid:bb142ws0723" published="2020-05-09T00:44:17Z" publishVersion="dor-services/9.3.0">
<contentMetadata objectId="bb142ws0723" type="media">
<resource sequence="1" id="bb142ws0723_1" type="video">
Expand Down Expand Up @@ -107,8 +176,56 @@
</access>
</rightsMetadata>
</publicObject>
EOXML
)
EOXML
end

let(:json) do
{
'structural' => {
'contains' => [
{
'structural' => {
'contains' => [
{
'filename' => 'bb142ws0723_01_sl.mp4',
'access' => {
'view' => 'stanford',
'download' => 'stanford'
}
},
{
'filename' => 'bb142ws0723_01_thumb.jp2',
'access' => {
'view' => 'stanford',
'download' => 'stanford'
}
}
]
}
},
{
'structural' => {
'contains' => [
{
'filename' => 'bb142ws0723_program.pdf',
'access' => {
'view' => 'world',
'download' => 'world'
}
}
]
}
}
]
}
}.to_json
end

before do
allow(Faraday).to receive(:get).with('https://purl.stanford.edu/bb142ws0723.xml')
.and_return(instance_double(Faraday::Response, success?: true, body: xml))
allow(Faraday).to receive(:get).with('https://purl.stanford.edu/bb142ws0723.json')
.and_return(instance_double(Faraday::Response, success?: true, body: json))
end

it 'redirects to login' do
Expand All @@ -127,9 +244,8 @@
end

context "with a stanford access file" do
before do
allow(Faraday).to receive(:get).with('https://purl.stanford.edu/bb142ws0723.xml').and_return(
instance_double(Faraday::Response, success?: true, body: <<-EOXML)
let(:xml) do
<<-EOXML
<publicObject id="druid:bb142ws0723" published="2020-05-09T00:44:17Z" publishVersion="dor-services/9.3.0">
<contentMetadata objectId="bb142ws0723" type="media">
<resource sequence="1" id="bb142ws0723_1" type="video">
Expand Down Expand Up @@ -163,8 +279,55 @@
</access>
</rightsMetadata>
</publicObject>
EOXML
)
EOXML
end
let(:json) do
{
'structural' => {
'contains' => [
{
'structural' => {
'contains' => [
{
'filename' => 'bb142ws0723_01_sl.mp4',
'access' => {
'view' => 'stanford',
'download' => 'stanford'
}
},
{
'filename' => 'bb142ws0723_01_thumb.jp2',
'access' => {
'view' => 'stanford',
'download' => 'stanford'
}
}
]
}
},
{
'structural' => {
'contains' => [
{
'filename' => 'bb142ws0723_program.pdf',
'access' => {
'view' => 'world',
'download' => 'world'
}
}
]
}
}
]
}
}.to_json
end

before do
allow(Faraday).to receive(:get).with('https://purl.stanford.edu/bb142ws0723.xml')
.and_return(instance_double(Faraday::Response, success?: true, body: xml))
allow(Faraday).to receive(:get).with('https://purl.stanford.edu/bb142ws0723.json')
.and_return(instance_double(Faraday::Response, success?: true, body: json))
end

it 'creates a zip' do
Expand Down
Loading

0 comments on commit 8ed1857

Please sign in to comment.