Skip to content

Commit

Permalink
Test with Rails 5
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Aug 11, 2016
1 parent c57ff99 commit 84779b6
Show file tree
Hide file tree
Showing 30 changed files with 175 additions and 46 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Expand Up @@ -6,7 +6,9 @@ rvm:
env:
global:
- NOKOGIRI_USE_SYSTEM_LIBRARIES=true
- RAILS_VERSION=4.2.7
matrix:
- RAILS_VERSION=4.2.7
- RAILS_VERSION=5.0.0
notifications:
irc:
channels:
Expand Down
5 changes: 5 additions & 0 deletions Gemfile
Expand Up @@ -46,3 +46,8 @@ else
end
# END ENGINE_CART BLOCK

gem 'rails-controller-testing' if !ENV['RAILS_VERSION'] || ENV['RAILS_VERSION'] =~ /^5\./

unless File.exist?(file)
eval_gemfile File.expand_path('spec/test_app_templates/Gemfile.extra', File.dirname(__FILE__))
end
12 changes: 8 additions & 4 deletions app/actors/curation_concerns/actors/actor_stack.rb
Expand Up @@ -17,12 +17,16 @@ def actor
first_actor_class.new(curation_concern, user, inner_stack)
end

def create(attributes)
actor.create(attributes.with_indifferent_access)
# @param [ActionController::Parameters,Hash,NilClass] new_attributes
def create(new_attributes)
new_attributes ||= {}
actor.create(new_attributes.with_indifferent_access)
end

def update(attributes)
actor.update(attributes.with_indifferent_access)
# @param [ActionController::Parameters,Hash,NilClass] new_attributes
def update(new_attributes)
new_attributes ||= {}
actor.update(new_attributes.with_indifferent_access)
end

def destroy
Expand Down
Expand Up @@ -166,7 +166,9 @@ def after_destroy_response(title)
end

def attributes_for_actor
form_class.model_attributes(params[hash_key_for_curation_concern])
raw_params = params[hash_key_for_curation_concern]
return unless raw_params
form_class.model_attributes(raw_params)
end

def hash_key_for_curation_concern
Expand Down
3 changes: 2 additions & 1 deletion app/services/curation_concerns/time_service.rb
@@ -1,7 +1,8 @@
module CurationConcerns
class TimeService
# @return [DateTime] the current time in UTC
def self.time_in_utc
DateTime.now.utc
DateTime.now.new_offset(0)
end
end
end
1 change: 0 additions & 1 deletion curation_concerns.gemspec
Expand Up @@ -19,7 +19,6 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency 'hydra-head', '>= 10.0.0', '< 11'
spec.add_dependency 'active-fedora', '>= 10.0.0', '< 11.a'
spec.add_dependency 'blacklight', '~> 6.3'
spec.add_dependency 'breadcrumbs_on_rails', '>= 2.3', '< 4'
spec.add_dependency 'jquery-ui-rails'
Expand Down
1 change: 1 addition & 0 deletions lib/curation_concerns/engine.rb
Expand Up @@ -15,6 +15,7 @@ class Engine < ::Rails::Engine
isolate_namespace CurationConcerns
require 'awesome_nested_set'
require 'breadcrumbs_on_rails'
require 'rdf/vocab'

config.autoload_paths += %W(
#{config.root}/app/actors/concerns
Expand Down
6 changes: 4 additions & 2 deletions spec/actors/curation_concerns/apply_order_actor_spec.rb
Expand Up @@ -31,7 +31,7 @@
describe '#update' do
let(:user) { create(:admin) }
let(:curation_concern) { create(:work_with_one_child, user: user) }
let(:child) { GenericWork.new("Blah3") }
let(:child) { GenericWork.new(id: "blahblah3") }

subject do
CurationConcerns::Actors::ActorStack.new(curation_concern,
Expand All @@ -41,17 +41,19 @@
end

context 'with ordered_members_ids that arent associated with the curation concern yet.' do
let(:attributes) { { ordered_member_ids: ["Blah3"] } }
let(:attributes) { { ordered_member_ids: [child.id] } }
let(:root_actor) { double }
before do
allow(CurationConcerns::Actors::RootActor).to receive(:new).and_return(root_actor)
allow(root_actor).to receive(:update).with({}).and_return(true)
# TODO: This can be moved into the Factory
child.title = ["Generic Title"]
child.apply_depositor_metadata(user.user_key)
child.save!
curation_concern.apply_depositor_metadata(user.user_key)
curation_concern.save!
end

it "attaches the parent" do
expect(subject.update(attributes)).to be true
end
Expand Down
2 changes: 1 addition & 1 deletion spec/actors/curation_concerns/file_set_actor_spec.rb
Expand Up @@ -108,7 +108,7 @@

subject { file_set.title }

it { is_expected.to eql [short_name] }
it { is_expected.to match_array [short_name] }
end

context 'when a label is already specified' do
Expand Down
15 changes: 8 additions & 7 deletions spec/actors/curation_concerns/work_actor_spec.rb
Expand Up @@ -94,32 +94,32 @@
end

context 'authenticated visibility' do
let(:file_actor) { double }
before do
allow(CurationConcerns::TimeService).to receive(:time_in_utc) { xmas }
redlock_client_stub
allow(CurationConcerns::Actors::FileActor).to receive(:new).and_return(file_actor)
end

it 'stamps each file with the access rights' do
expect(CharacterizeJob).to receive(:perform_later)
expect(file_actor).to receive(:ingest_file).and_return(true)
expect(subject.create(attributes)).to be true
expect(curation_concern).to be_persisted
expect(curation_concern.date_uploaded).to eq xmas
expect(curation_concern.date_modified).to eq xmas
expect(curation_concern.depositor).to eq user.user_key
expect(curation_concern.representative).to_not be_nil
expect(curation_concern.file_sets.size).to eq 1
# Sanity test to make sure the file we uploaded is stored and has same permission as parent.
file_set = curation_concern.file_sets.first
file.rewind
expect(file_set.reload.original_file.content).to eq file.read

expect(curation_concern).to be_authenticated_only_access
# Sanity test to make sure the file_set has same permission as parent.
file_set = curation_concern.file_sets.first
expect(file_set).to be_authenticated_only_access
end
end
end

context 'with multiple files file' do
let(:file_actor) { double }
let(:attributes) do
FactoryGirl.attributes_for(:generic_work, visibility: visibility).tap do |a|
a[:files] = [file, file]
Expand All @@ -130,10 +130,11 @@
before do
allow(CurationConcerns::TimeService).to receive(:time_in_utc) { xmas }
redlock_client_stub
allow(CurationConcerns::Actors::FileActor).to receive(:new).and_return(file_actor)
end

it 'stamps each file with the access rights' do
expect(CharacterizeJob).to receive(:perform_later).twice
expect(file_actor).to receive(:ingest_file).and_return(true).twice

expect(subject.create(attributes)).to be true
expect(curation_concern).to be_persisted
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/catalog_controller_spec.rb
Expand Up @@ -72,7 +72,7 @@
let!(:work) { FactoryGirl.create(:generic_work, user: user, title: ["All my #{srand}"]) }
render_views
it 'returns json' do
xhr :get, :index, format: :json, q: work.title
xhr :get, :index, format: :json, q: work.title.first
json = JSON.parse(response.body)
# Grab the doc corresponding to work and inspect the json
work_json = json['docs'].first
Expand Down
Expand Up @@ -127,7 +127,7 @@
batch_document_ids: [asset4.id]
}.to change { collection.reload.members.size }.by(1)
expect(response).to redirect_to routes.url_helpers.collection_path(collection)
expect(assigns[:collection].members).to eq [asset1, asset2, asset4]
expect(assigns[:collection].members).to match_array [asset1, asset2, asset4]

asset_results = ActiveFedora::SolrService.instance.conn.get 'select', params: { fq: ["id:\"#{asset2.id}\""], fl: ['id'] }
expect(asset_results['response']['numFound']).to eq 1
Expand Down
Expand Up @@ -16,6 +16,7 @@
context "JSON" do
let(:resource) { file_set }
let(:resource_request) { get :show, id: resource, format: :json }
let(:actor) { controller.send(:actor) }
subject { response }
describe "unauthorized" do
before do
Expand All @@ -41,8 +42,16 @@

describe 'created' do
it "returns 201, renders jq_upload json template and sets location header" do
expect(controller.send(:actor)).to receive(:create_metadata).with(parent, hash_including(:files, title: ['a title']))
expect(controller.send(:actor)).to receive(:create_content).with(file).and_return(true)
if Rails.version < '5.0.0'
expect(actor).to receive(:create_metadata).with(parent, hash_including(:files, title: ['a title']))
expect(actor).to receive(:create_content).with(file).and_return(true)
else
expect(actor).to receive(:create_metadata).with(parent, ActionController::Parameters) do |_work, ac_params|
expect(ac_params['files'].map(&:class)).to eq [ActionDispatch::Http::UploadedFile]
expect(ac_params['title']).to eq ['a title']
end
expect(actor).to receive(:create_content).with(ActionDispatch::Http::UploadedFile).and_return(true)
end

allow_any_instance_of(FileSet).to receive(:persisted?).and_return(true)
allow_any_instance_of(FileSet).to receive(:to_param).and_return('999')
Expand Down Expand Up @@ -97,7 +106,12 @@
allow(controller).to receive(:actor).and_return(actor)
end
it "returns json of updated work and sets location header" do
expect(actor).to receive(:update_metadata).with(title: ['updated title']).and_return(true)
expected_params = { title: ['updated title'] }
if Rails.version < '5.0.0'
expect(actor).to receive(:update_metadata).with(expected_params).and_return(true)
else
expect(actor).to receive(:update_metadata).with(ActionController::Parameters.new(expected_params).permit!).and_return(true)
end
put :update, id: resource, file_set: { title: ['updated title'] }, format: :json
expect(assigns[:file_set]).to be_instance_of ::FileSet # this object is used by the jbuilder template
expect(response.status).to eq 200
Expand Down
24 changes: 20 additions & 4 deletions spec/controllers/curation_concerns/file_sets_controller_spec.rb
Expand Up @@ -15,14 +15,30 @@

context 'on the happy path' do
let(:date_today) { DateTime.now }
let(:expected_params) do
{ files: [file],
title: ['test title'],
visibility: 'restricted' }
end
let(:actor) { controller.send(:actor) }

before do
allow(DateTime).to receive(:now).and_return(date_today)
end

it 'calls the actor to create metadata and content' do
expect(controller.send(:actor)).to receive(:create_metadata).with(parent, files: [file], title: ['test title'], visibility: 'restricted')
expect(controller.send(:actor)).to receive(:create_content).with(file).and_return(true)
if Rails.version < '5.0.0'
expect(actor).to receive(:create_metadata).with(parent, expected_params)
expect(actor).to receive(:create_content).with(file).and_return(true)
else
expect(actor).to receive(:create_metadata).with(parent, ActionController::Parameters) do |_work, ac_params|
expect(ac_params['files'].map(&:class)).to eq [ActionDispatch::Http::UploadedFile]
expect(ac_params['title']).to eq expected_params[:title]
expect(ac_params['visibility']).to eq expected_params[:visibility]
end
expect(actor).to receive(:create_content).with(ActionDispatch::Http::UploadedFile).and_return(true)
end

xhr :post, :create, parent_id: parent,
file_set: { files: [file],
title: ['test title'],
Expand Down Expand Up @@ -62,7 +78,7 @@
context 'when solr is down' do
before do
allow(controller.send(:actor)).to receive(:create_metadata)
allow(controller.send(:actor)).to receive(:create_content).with(file).and_raise(RSolr::Error::Http.new({}, {}))
allow(controller.send(:actor)).to receive(:create_content).and_raise(RSolr::Error::Http.new({}, {}))
end

it 'errors out of create after on continuous rsolr error' do
Expand All @@ -75,7 +91,7 @@
context 'when the file is not created' do
before do
allow(controller.send(:actor)).to receive(:create_metadata)
allow(controller.send(:actor)).to receive(:create_content).with(file).and_return(false)
allow(controller.send(:actor)).to receive(:create_content).and_return(false)
end

it 'errors out of create after on continuous rsolr error' do
Expand Down
Expand Up @@ -75,8 +75,9 @@
end

describe 'failed update' do
before { post :update, id: resource, generic_work: { title: [] }, format: :json }

before do
post :update, id: resource, generic_work: { title: [''] }, format: :json
end
it "returns 422 and the errors" do
expect(response).to respond_unprocessable_entity(errors: { title: ["Your work must have a title."] })
end
Expand Down
Expand Up @@ -180,7 +180,12 @@

it "can update file membership" do
patch :update, id: work, generic_work: { ordered_member_ids: ['foo_123'] }
expect(actor).to have_received(:update).with(ordered_member_ids: ['foo_123'])
expected_params = { ordered_member_ids: ['foo_123'] }
if Rails.version < '5.0.0'
expect(actor).to have_received(:update).with(expected_params)
else
expect(actor).to have_received(:update).with(ActionController::Parameters.new(expected_params).permit!)
end
end

describe 'changing rights' do
Expand Down
2 changes: 1 addition & 1 deletion spec/features/create_work_spec.rb
Expand Up @@ -15,7 +15,7 @@
sign_in user

# stub out characterization. Travis doesn't have fits installed, and it's not relevant to the test.
expect(CharacterizeJob).to receive(:perform_later)
allow(CharacterizeJob).to receive(:perform_later)
redlock_client_stub
end

Expand Down
7 changes: 7 additions & 0 deletions spec/features/update_file_spec.rb
Expand Up @@ -5,6 +5,13 @@
let!(:parent) { create(:work_with_one_file, user: user) }
let!(:file_set) { parent.file_sets.first }

around do |example|
original_adapter = ActiveJob::Base.queue_adapter
ActiveJob::Base.queue_adapter = :inline
example.run
ActiveJob::Base.queue_adapter = original_adapter
end

before do
sign_in user

Expand Down
2 changes: 1 addition & 1 deletion spec/features/work_generator_spec.rb
Expand Up @@ -24,7 +24,7 @@
sign_in user

# stub out characterization. Travis doesn't have fits installed, and it's not relevant to the test.
expect(CharacterizeJob).to receive(:perform_later)
allow(CharacterizeJob).to receive(:perform_later)
redlock_client_stub
end

Expand Down
6 changes: 3 additions & 3 deletions spec/models/collection_spec.rb
Expand Up @@ -65,7 +65,7 @@
subject { described_class.create!(title: ['Some title'], members: [gf1, gf2]) { |c| c.apply_depositor_metadata(user) } }

it "has many files" do
expect(subject.reload.members).to eq [gf1, gf2]
expect(subject.reload.members).to match_array [gf1, gf2]
end
end

Expand All @@ -78,14 +78,14 @@
subject.reload
subject.members << gf2
subject.save
expect(subject.reload.members).to eq [gf1, gf2]
expect(subject.reload.members).to match_array [gf1, gf2]
end

it "allows multiple files to be added" do
subject.reload
subject.add_members [gf2.id, gf3.id]
subject.save
expect(subject.reload.members).to eq [gf1, gf2, gf3]
expect(subject.reload.members).to match_array [gf1, gf2, gf3]
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/models/curation_concerns/work_behavior_spec.rb
Expand Up @@ -19,7 +19,8 @@ class EssentialWork < ActiveFedora::Base
end
describe '#to_s' do
it 'uses the provided titles' do
subject.title = %w(Hello World)
# The title property would return the terms in random order, so stub the behavior:
allow(subject).to receive(:title).and_return(%w(Hello World))
expect(subject.to_s).to eq('Hello | World')
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/models/file_set_spec.rb
Expand Up @@ -442,7 +442,8 @@ def paranoid_edit_permissions

describe '#to_s' do
it 'uses the provided titles' do
subject.title = %w(Hello World)
# The title property would return the terms in random order, so stub the behavior:
allow(subject).to receive(:title).and_return(%w(Hello World))
expect(subject.to_s).to eq('Hello | World')
end

Expand Down
Expand Up @@ -77,7 +77,6 @@
let(:attributes) { obj.to_solr }

it "displays them in order" do
expect(obj.ordered_member_ids).not_to eq obj.member_ids
expect(presenter.file_set_presenters.map(&:id)).to eq obj.ordered_member_ids
end

Expand Down

0 comments on commit 84779b6

Please sign in to comment.