Skip to content

Commit

Permalink
Add tests for WorksControllerBehavior#show, #create, & #new
Browse files Browse the repository at this point in the history
Adds module level tests for `WorksControllerBehavior#show` using a
controller registered for a Valkyrie model. A minor changes to `GraphExporter`
is needed to make some of these tests pass (the changed branch is otherwise
untested).

The outline of tests for `#create` and `#new` are also put in place, but these
require more work (on Actors and Forms, respectively) to pass. Accordingly, they
are left as pending tests.
  • Loading branch information
Tom Johnson committed Jan 3, 2020
1 parent b5e9604 commit a6c86d5
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 23 deletions.
14 changes: 0 additions & 14 deletions app/models/concerns/hyrax/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,19 +231,5 @@ def can_review_submissions?
.where('sipity_workflow_roles.role_id' => approving_role.id).any?
end
end

##
# @api private
#
# Overwrite extract subjects to map permissions
#
# @note this shims in support for using existing abilities when passing
# Valkyrie::Resources. We'll need to support valkyrie resources natively
# as well.
def extract_subjects(subject)
subject = subject.alternate_ids&.first&.id if subject.is_a?(Hyrax::Resource)

super
end
end
end
2 changes: 1 addition & 1 deletion app/services/hyrax/graph_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def subject_replacer(klass, resource_id, anchor = nil)
route_key = if Hyrax.config.curation_concerns.include?(klass)
klass.model_name.singular_route_key
else
SolrDocument.model_name.singular_route_key
::SolrDocument.model_name.singular_route_key
end
routes = Rails.application.routes.url_helpers
builder = ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder
Expand Down
139 changes: 131 additions & 8 deletions spec/controllers/concerns/hyrax/works_controller_behavior_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
# frozen_string_literal: true

RSpec.describe Hyrax::WorksControllerBehavior, type: :controller do
RSpec.describe Hyrax::WorksControllerBehavior, :clean_repo, type: :controller do
let(:paths) { Rails.application.routes.url_helpers }
let(:title) { ['Comet in Moominland'] }
let(:work) { FactoryBot.valkyrie_create(:hyrax_work, alternate_ids: [id], title: title) }
let(:id) { '123' }

before(:context) { Hyrax.config.register_curation_concern(Hyrax::Test::SimpleWork) }

after(:context) do
config = Hyrax.config
types = config.registered_curation_concern_types - ["Hyrax::Test::SimpleWork"]

Hyrax.config.instance_variable_set(:@registered_concerns, types)
end

controller(ApplicationController) do
include Hyrax::WorksControllerBehavior

self.curation_concern_type = Hyrax::Test::BookResource
self.curation_concern_type = Hyrax::Test::SimpleWork
self.search_builder_class = Hyrax::Test::SimpleWorkSearchBuilder
end

shared_context 'with a logged in user' do
Expand All @@ -15,19 +28,32 @@
before { sign_in user }
end

describe '#edit' do
let(:work) { FactoryBot.valkyrie_create(:hyrax_work, :public, alternate_ids: [id]) }
let(:id) { '123' }
describe '#create' do
it 'redirects to new user login' do
get :create, params: {}

before { Hyrax.persister.save(resource: work) }
expect(response).to redirect_to paths.new_user_session_path(locale: :en)
end

xcontext 'with a logged in user' do
include_context 'with a logged in user'

it 'is successful' do
get :create, params: {}

expect(response).to be_successful
end
end
end

describe '#edit' do
it 'gives a 404 for a missing object' do
expect { get :edit, params: { id: 'missing_id' } }
.to raise_error Hyrax::ObjectNotFoundError
end

it 'redirects to new user login' do
get :edit, params: { id: id }
get :edit, params: { id: work.id }

expect(response).to redirect_to paths.new_user_session_path(locale: :en)
end
Expand All @@ -36,10 +62,107 @@
include_context 'with a logged in user'

it 'gives unauthorized' do
get :edit, params: { id: id }
get :edit, params: { id: work.id }

expect(response.status).to eq 401
end
end
end

describe '#new' do
it 'redirect to user login' do
get :new
expect(response).to redirect_to paths.new_user_session_path(locale: :en)
end

xcontext 'with a logged in user' do
include_context 'with a logged in user'

it 'is successful' do
get :new

expect(response).to be_successful
end

it 'renders a form' do
get :new

expect(assigns[:form]).to be_kind_of Hyrax::WorkForm
end
end
end

describe '#show' do
shared_examples 'allows show access' do
it 'allows access' do
get :show, params: { id: work.id }

expect(response.status).to eq 200
end

it 'resolves ntriples' do
get :show, params: { id: work.id }, format: :nt

expect(RDF::Reader.for(:ntriples).new(response.body).objects)
.to include(RDF::Literal(title.first))
end

it 'resolves turtle' do
get :show, params: { id: work.id }, format: :ttl

expect(RDF::Reader.for(:ttl).new(response.body).objects)
.to include(RDF::Literal(title.first))
end

it 'resolves jsonld' do
get :show, params: { id: work.id }, format: :jsonld

expect(RDF::Reader.for(:jsonld).new(response.body).objects)
.to include(RDF::Literal(title.first))
end

xit 'resolves json' do
get :show, params: { id: work.id }, format: :json

expect(response.body).to include(title.first)
end
end

it 'gives a 404 for a missing object' do
expect { get :show, params: { id: 'missing_id' } }
.to raise_error Blacklight::Exceptions::RecordNotFound
end

it 'redirects to new user login' do
get :show, params: { id: work.id }

expect(response).to redirect_to paths.new_user_session_path(locale: :en)
end

context 'when indexed as public' do
let(:index_document) do
Wings::ActiveFedoraConverter.convert(resource: work).to_solr.tap do |doc|
doc[Hydra.config.permissions.read.group] = 'public'
end
end

before { ActiveFedora::SolrService.add(index_document, softCommit: true) }

it_behaves_like 'allows show access'
end

context 'when the user has read access' do
include_context 'with a logged in user'

let(:index_document) do
Wings::ActiveFedoraConverter.convert(resource: work).to_solr.tap do |doc|
doc[Hydra.config.permissions.read.individual] = [user.user_key]
end
end

before { ActiveFedora::SolrService.add(index_document, softCommit: true) }

it_behaves_like 'allows show access'
end
end
end
6 changes: 6 additions & 0 deletions spec/support/simple_work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class SimpleWorkLegacy < ActiveFedora::Base
include WorkBehavior
include CoreMetadata
end

class SimpleWorkSearchBuilder < Hyrax::WorkSearchBuilder
def work_types
[Hyrax::Test::SimpleWorkLegacy]
end
end
end
end

Expand Down

0 comments on commit a6c86d5

Please sign in to comment.