diff options
Diffstat (limited to 'spec/controllers')
-rw-r--r-- | spec/controllers/projects/notes_controller_spec.rb | 70 | ||||
-rw-r--r-- | spec/controllers/snippets/notes_controller_spec.rb | 55 |
2 files changed, 102 insertions, 23 deletions
diff --git a/spec/controllers/projects/notes_controller_spec.rb b/spec/controllers/projects/notes_controller_spec.rb index 2241a7cf590..4a5283f1127 100644 --- a/spec/controllers/projects/notes_controller_spec.rb +++ b/spec/controllers/projects/notes_controller_spec.rb @@ -775,32 +775,40 @@ RSpec.describe Projects::NotesController, type: :controller, feature_category: : end describe 'PUT update' do + let(:note_params) { { note: "New comment" } } + let(:request_params) do { namespace_id: project.namespace, project_id: project, id: note, format: :json, - note: { - note: "New comment" - } + note: note_params } end - specify { expect(put(:update, params: request_params)).to have_request_urgency(:low) } + subject(:update_note) { put :update, params: request_params } - context "should update the note with a valid issue" do - before do - sign_in(note.author) - project.add_developer(note.author) - end + before do + sign_in(note.author) + project.add_developer(note.author) + end + + specify { expect(update_note).to have_request_urgency(:low) } + context "when the note is valid" do it "updates the note" do - expect { put :update, params: request_params }.to change { note.reload.note } + expect { update_note }.to change { note.reload.note } + end + + it "returns status 200" do + update_note + + expect(response).to have_gitlab_http_status(:ok) end end - context "doesnt update the note" do + context "when the issue is confidential and the user has guest permissions" do let(:issue) { create(:issue, :confidential, project: project) } let(:note) { create(:note, noteable: issue, project: project) } @@ -809,20 +817,38 @@ RSpec.describe Projects::NotesController, type: :controller, feature_category: : project.add_guest(user) end - it "disallows edits when the issue is confidential and the user has guest permissions" do - request_params = { - namespace_id: project.namespace, - project_id: project, - id: note, - format: :json, - note: { - note: "New comment" - } - } - expect { put :update, params: request_params }.not_to change { note.reload.note } + it "disallows edits" do + expect { update_note }.not_to change { note.reload.note } + end + + it "returns status 404" do + update_note + expect(response).to have_gitlab_http_status(:not_found) end end + + context "when there are ActiveRecord validation errors" do + before do + allow(note).to receive_message_chain(:errors, :full_messages) + .and_return(['Error 1', 'Error 2']) + + allow_next_instance_of(Notes::UpdateService) do |service| + allow(service).to receive(:execute).and_return(note) + end + end + + it "does not update the note" do + expect { update_note }.not_to change { note.reload.note } + end + + it "returns status 422", :aggregate_failures do + update_note + + expect(response).to have_gitlab_http_status(:unprocessable_entity) + expect(response.body).to eq('{"errors":"Error 1 and Error 2"}') + end + end end describe 'DELETE destroy' do diff --git a/spec/controllers/snippets/notes_controller_spec.rb b/spec/controllers/snippets/notes_controller_spec.rb index 00d99b46d0b..578973d5b3d 100644 --- a/spec/controllers/snippets/notes_controller_spec.rb +++ b/spec/controllers/snippets/notes_controller_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Snippets::NotesController do +RSpec.describe Snippets::NotesController, feature_category: :team_planning do let(:user) { create(:user) } let(:private_snippet) { create(:personal_snippet, :private) } @@ -256,6 +256,59 @@ RSpec.describe Snippets::NotesController do end end + describe 'PUT update' do + let(:note_params) { { note: "New comment" } } + + let(:request_params) do + { + snippet_id: public_snippet, + id: note_on_public, + format: :json, + note: note_params + } + end + + before do + sign_in(note_on_public.author) + end + + subject(:update_note) { put :update, params: request_params } + + context "when the note is valid" do + it "updates the note" do + expect { update_note }.to change { note_on_public.reload.note } + end + + it "returns status 200" do + post :create, params: request_params + + expect(response).to have_gitlab_http_status(:ok) + end + end + + context "when there are ActiveRecord validation errors" do + before do + allow(note_on_public).to receive_message_chain(:errors, :full_messages) + .and_return(['Error 1', 'Error 2']) + + allow_next_instance_of(Notes::UpdateService) do |service| + allow(service).to receive(:execute).and_return(note_on_public) + end + end + + it "does not update the note" do + expect { update_note }.not_to change { note_on_public.reload.note } + end + + it "returns status 422", :aggregate_failures do + update_note + + expect(response).to have_gitlab_http_status(:unprocessable_entity) + expect(response.body).to eq('{"errors":"Error 1 and Error 2"}') + end + end + end + describe 'DELETE destroy' do let(:request_params) do { |