Skip to content

Commit

Permalink
Merge 975fadf into d5f8742
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartin-sul committed Aug 21, 2019
2 parents d5f8742 + 975fadf commit 9cfba04
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
19 changes: 18 additions & 1 deletion lib/dor/rights_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ def embargoed?
@embargoed
end

def check_index_elements_calculated!
unless index_elements.size > 0
raise "primary access rights not calculated. instantiate by calling '.parse(xml, forindex = true)'."
end
end

# this is just a convenience method for asking whether an object's rights would
# classify it as 'dark'. that info is mostly used for object-level indexing/faceting.
# thus, we currently only calculate it when parsing object rights for indexing.
# to keep from having to refactor or duplicate code right now, we'll just leverage
# what we've got, and throw an error if the object wasn't instantiated in a way
# this method can use.
def dark?
check_index_elements_calculated!
index_elements[:primary] == 'dark'
end

# Returns true if the object is world readable AND has no rule attribute
# @return [Boolean]
def world_unrestricted?
Expand Down Expand Up @@ -277,7 +294,7 @@ def self.validate_lite(doc)
def self.extract_index_terms(doc)
terms = []
machine = doc.at_xpath("//rightsMetadata/access[@type='read' and not(file)]/machine")
terms.push 'none_discover' if doc.at_xpath("//rightsMetadata/access[@type='discover']/machine/none")
terms.push 'none_discover' if doc.at_xpath("//rightsMetadata/access[@type='discover']/machine/none") || doc.at_xpath("//rightsMetadata/access[@type='discover']/machine[not(*)]")
terms.push 'world_discover' if doc.at_xpath("//rightsMetadata/access[@type='discover']/machine/world[not(@rule)]")
return terms if machine.nil?

Expand Down
14 changes: 11 additions & 3 deletions spec/dor_services_examples_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'spec_helper'

describe Dor::RightsAuth do
it 'handles fixture druid:oo201oo0001' do
xml = <<-EOXML
<?xml version="1.0"?>
let(:xml) do
<<-EOXML
<?xml version="1.0"?>
<rightsMetadata>
<copyright>
<human type="copyright">This work is in the Public Domain.</human>
Expand All @@ -24,10 +24,18 @@
</use>
</rightsMetadata>
EOXML
end

it 'answers the rights queries as expected for fixture druid:oo201oo0001' do
r = Dor::RightsAuth.parse(xml, true)
expect(r).to be_stanford_only_unrestricted
expect(r).not_to be_public_unrestricted
expect(r.index_elements).to include :primary => 'access_restricted'
expect(r.dark?).to be false
end

it 'raises a helpful error when dark? is called, if the object was not instantiated appropriately' do
r = Dor::RightsAuth.parse(xml)
expect { r.dark? }.to raise_error(RuntimeError, /instantiate by calling '.parse\(xml, forindex = true\)'/)
end
end
18 changes: 18 additions & 0 deletions spec/rights_auth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,24 @@
expect(r).not_to be_readable
expect(r).not_to be_public_unrestricted_file('file.doc')
expect(r).not_to be_public_unrestricted
expect { r.dark? }.to raise_error(RuntimeError)
end
end

describe 'parse for indexing' do
it 'correctly' do
r = Dor::RightsAuth.parse rights, true

world, rule1 = r.world_rights
stan, rule2 = r.stanford_only_rights
expect(world).not_to be
expect(stan ).not_to be
expect(rule1).to be_nil
expect(rule2).to be_nil
expect(r).not_to be_readable
expect(r).not_to be_public_unrestricted_file('file.doc')
expect(r).not_to be_public_unrestricted
expect(r.dark?).to be true
end
end
end
Expand Down

0 comments on commit 9cfba04

Please sign in to comment.