Skip to content

Commit

Permalink
fix tests for probe controller
Browse files Browse the repository at this point in the history
  • Loading branch information
peetucket committed Dec 6, 2023
1 parent 7583b28 commit da72eb4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 34 deletions.
10 changes: 4 additions & 6 deletions app/controllers/iiif/auth/v2/probe_service_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ def show
response = { '@context': 'http://iiif.io/api/auth/2/context.json', type: 'AuthProbeResult2' }

if can? :access, file
response.merge!(status: 200)
response[:status] = 200
else
# TODO: check restrictions on file object and include details in response e.g. like in MediaController#hash_for_auth_check
message = {
heading: { en: ["You can't see this"] },
note: { en: ["Sorry"] }
}
response.merge!(status: 401, message:)
response[:status] = 401
response[:heading] = { en: ["You can't see this"] }
response[:note] = { en: ["Sorry"] }
end

render json: response
Expand Down
73 changes: 45 additions & 28 deletions spec/requests/iiif/auth/v2/probe_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,37 @@
let(:id) { 'bb461xx1037' }
let(:file_name) { 'SC0193_1982-013_b06_f01_1981-09-29.pdf' }
let(:stacks_uri) { "https://stacks-uat.stanford.edu/file/druid:#{id}/#{file_name}" }
let(:user) { instance_double(User, locations: [], webauth_user: false, stanford?: false, cdl_tokens: []) }
let(:ability) { Ability.new(user) }

# TODO: figure out how to correctly mock Ability object so it doesn't actually try to hit PURL to get rights and parse

before do
get "/iiif/auth/v2/probe?id=#{stacks_uri}"
#allow(ApplicationController).to receive(:current_ability).and_return(ability)
allow(Purl).to receive(:public_json).and_return(public_json)
end

context 'when the user has access to the resource' do
let(:file) do
instance_double(
StacksFile,
id:,
file_name:,
restricted_by_location?: false,
stanford_restricted?: false,
embargoed?: false,
download: true
)
let(:public_json) do
{
'structural' => {
'contains' => [
{
'structural' => {
'contains' => [
{
'filename' => file_name,
'access' => {
'view' => 'world',
'download' => 'world'
}
}
]
}
}
]
}
}
end

before do
allow(ability).to receive(:can?).with(:access, file).and_return(false)
stub_rights_xml(world_readable_rights_xml)
get "/iiif/auth/v2/probe?id=#{stacks_uri}"
end

it 'returns a success response' do
Expand All @@ -44,20 +50,31 @@
end

context 'when the user does not have access to the resource' do
let(:file) do
instance_double(
StacksFile,
id:,
file_name:,
restricted_by_location?: false,
stanford_restricted?: true,
embargoed?: false,
download: true
)
let(:public_json) do
{
'structural' => {
'contains' => [
{
'structural' => {
'contains' => [
{
'filename' => file_name,
'access' => {
'view' => 'world',
'download' => 'stanford'
}
}
]
}
}
]
}
}
end

before do
allow(ability).to receive(:can?).with(:access, file).and_return(false)
stub_rights_xml(stanford_restricted_rights_xml)
get "/iiif/auth/v2/probe?id=#{stacks_uri}"
end

it 'returns a not authorized response' do
Expand Down

0 comments on commit da72eb4

Please sign in to comment.