diff --git a/spec/controllers/hyrax/dashboard/collections_controller_spec.rb b/spec/controllers/hyrax/dashboard/collections_controller_spec.rb index 90eed650fe..ab4307d0d6 100644 --- a/spec/controllers/hyrax/dashboard/collections_controller_spec.rb +++ b/spec/controllers/hyrax/dashboard/collections_controller_spec.rb @@ -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 @@ -339,19 +345,18 @@ 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 @@ -359,9 +364,20 @@ 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 diff --git a/spec/controllers/hyrax/dashboard/collections_controller_with_resource_spec.rb b/spec/controllers/hyrax/dashboard/collections_controller_with_resource_spec.rb index 7aad8662ce..0e1d2f4148 100644 --- a/spec/controllers/hyrax/dashboard/collections_controller_with_resource_spec.rb +++ b/spec/controllers/hyrax/dashboard/collections_controller_with_resource_spec.rb @@ -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 @@ -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 @@ -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) @@ -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