Skip to content

Commit

Permalink
add tests for json responses for create and update
Browse files Browse the repository at this point in the history
  • Loading branch information
elrayle committed Apr 29, 2022
1 parent 0209c55 commit 06dbbd5
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 14 deletions.
40 changes: 28 additions & 12 deletions spec/controllers/hyrax/dashboard/collections_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,23 +184,29 @@

context "when create fails" do
let(:collection) { Collection.new }
let(:error) { "Failed to save collection" }

before do
allow(controller).to receive(:authorize!)
allow(Collection).to receive(:new).and_return(collection)
allow(collection).to receive(:save).and_return(false)

allow(Hyrax.persister)
.to receive(:save)
.with(any_args)
.and_raise(StandardError, 'Failed to save collection')
allow(collection).to receive(:errors).and_return(error)
end

it "renders the form again" do
post :create, params: { collection: collection_attrs }

expect(response).to have_http_status(:unprocessable_entity)
expect(response).to have_http_status(:ok)
expect(response).to render_template(:new)
expect(flash[:error]).to eq error
end

it "renders json" do
post :create, params: { collection: collection_attrs, format: :json }

expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to eq "application/json"
expect(response.body).to eq error
end
end
end
Expand Down Expand Up @@ -339,29 +345,39 @@
end

context "when update fails" do
let(:collection) { FactoryBot.valkyrie_create(:hyrax_collection) }
let(:collection) { FactoryBot.create(:collection_lw) }
let(:repository) { instance_double(Blacklight::Solr::Repository, search: result) }
let(:result) { double(documents: [], total: 0) }
let(:error) { "Failed to save collection" }

before do
allow(controller).to receive(:authorize!)
allow(Collection).to receive(:find).and_return(collection)
allow(collection).to receive(:update).and_return(false)
allow(controller).to receive(:repository).and_return(repository)
allow(Hyrax.persister)
.to receive(:save)
.with(any_args)
.and_raise(StandardError, 'Failed to save collection')
allow_any_instance_of(::Collection).to receive(:save).and_return(false)
allow(collection).to receive(:errors).and_return(error)
end

it "renders the form again" do
put :update, params: {
id: collection,
collection: collection_attrs
}
expect(response).to have_http_status(:unprocessable_entity)
expect(response).to have_http_status(:ok)
expect(response).to render_template(:edit)
end

it "renders json" do
put :update, params: {
id: collection,
collection: collection_attrs,
format: :json
}
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to eq "application/json"
expect(response.body).to eq error
end
end

context "updating a collections branding metadata" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,19 @@
expect(flash[:error]).to match(/Failed to save collection/)
expect(response).to render_template(:new)
end

it "renders json" do
post :create, params: { collection: collection_attrs, format: :json }

expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to eq "application/json"

json_response = JSON.parse(response.body)
expect(json_response["code"]).to eq 422
expect(json_response["message"]).to eq "Unprocessable Entity"
expect(json_response["description"]).to eq "The resource you attempted to modify cannot be modified according to your request."
expect(json_response["errors"]).to match(/Failed to save collection/)
end
end

context "in validations" do
Expand All @@ -205,10 +218,24 @@

it "renders the form again" do
post :create, params: { collection: collection_attrs }

expect(response).to have_http_status(:unprocessable_entity)
expect(flash[:error]).to eq "Validation error"
expect(response).to render_template(:new)
end

it "renders json" do
post :create, params: { collection: collection_attrs, format: :json }

expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to eq "application/json"

json_response = JSON.parse(response.body)
expect(json_response["code"]).to eq 422
expect(json_response["message"]).to eq "Unprocessable Entity"
expect(json_response["description"]).to eq "The resource you attempted to modify cannot be modified according to your request."
expect(json_response["errors"]).to eq errmsg
end
end
end
end
Expand Down Expand Up @@ -379,12 +406,29 @@
expect(flash[:error]).to match(/Failed to save collection/)
expect(response).to render_template(:edit)
end

it "renders json" do
put :update, params: {
id: collection,
collection: collection_attrs,
format: :json
}
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to eq "application/json"

json_response = JSON.parse(response.body)
expect(json_response["code"]).to eq 422
expect(json_response["message"]).to eq "Unprocessable Entity"
expect(json_response["description"]).to eq "The resource you attempted to modify cannot be modified according to your request."
expect(json_response["errors"]).to match(/Failed to save collection/)
end
end

context "in validations" do
let(:form) { instance_double(Hyrax::Forms::PcdmCollectionForm, errors: errors) }
let(:errors) { instance_double(Reform::Contract::CustomError, messages: messages) }
let(:messages) { { "error" => "Validation error" } }
let(:messages) { { "error" => errmsg } }
let(:errmsg) { "Validation error" }
before do
allow(Hyrax::Forms::ResourceForm).to receive(:for).with(collection).and_return(form)
allow(form).to receive(:validate).with(any_args).and_return(false)
Expand All @@ -394,9 +438,25 @@
put :update, params: { id: collection, collection: collection_attrs }

expect(response).to have_http_status(:unprocessable_entity)
expect(flash[:error]).to eq "Validation error"
expect(flash[:error]).to eq errmsg
expect(response).to render_template(:edit)
end

it "renders json" do
put :update, params: {
id: collection,
collection: collection_attrs,
format: :json
}
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to eq "application/json"

json_response = JSON.parse(response.body)
expect(json_response["code"]).to eq 422
expect(json_response["message"]).to eq "Unprocessable Entity"
expect(json_response["description"]).to eq "The resource you attempted to modify cannot be modified according to your request."
expect(json_response["errors"]).to eq errmsg
end
end
end

Expand Down

0 comments on commit 06dbbd5

Please sign in to comment.