debian-mirror-gitlab/spec/controllers/snippets/notes_controller_spec.rb

338 lines
8.6 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2020-06-23 00:09:42 +05:30
RSpec.describe Snippets::NotesController do
2017-08-17 22:00:37 +05:30
let(:user) { create(:user) }
let(:private_snippet) { create(:personal_snippet, :private) }
let(:internal_snippet) { create(:personal_snippet, :internal) }
let(:public_snippet) { create(:personal_snippet, :public) }
let(:note_on_private) { create(:note_on_personal_snippet, noteable: private_snippet) }
let(:note_on_internal) { create(:note_on_personal_snippet, noteable: internal_snippet) }
let(:note_on_public) { create(:note_on_personal_snippet, noteable: public_snippet) }
describe 'GET index' do
context 'when a snippet is public' do
before do
note_on_public
2019-02-15 15:39:39 +05:30
get :index, params: { snippet_id: public_snippet }
2017-08-17 22:00:37 +05:30
end
it "returns status 200" do
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
end
it "returns not empty array of notes" do
2019-09-30 21:07:59 +05:30
expect(json_response["notes"].empty?).to be_falsey
2017-08-17 22:00:37 +05:30
end
end
context 'when a snippet is internal' do
before do
note_on_internal
end
context 'when user not logged in' do
it "returns status 404" do
2019-02-15 15:39:39 +05:30
get :index, params: { snippet_id: internal_snippet }
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2017-08-17 22:00:37 +05:30
end
end
context 'when user logged in' do
before do
sign_in(user)
end
it "returns status 200" do
2019-02-15 15:39:39 +05:30
get :index, params: { snippet_id: internal_snippet }
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
end
end
end
context 'when a snippet is private' do
before do
note_on_private
end
context 'when user not logged in' do
it "returns status 404" do
2019-02-15 15:39:39 +05:30
get :index, params: { snippet_id: private_snippet }
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2017-08-17 22:00:37 +05:30
end
end
context 'when user other than author logged in' do
before do
sign_in(user)
end
it "returns status 404" do
2019-02-15 15:39:39 +05:30
get :index, params: { snippet_id: private_snippet }
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2017-08-17 22:00:37 +05:30
end
end
context 'when author logged in' do
before do
note_on_private
sign_in(private_snippet.author)
end
it "returns status 200" do
2019-02-15 15:39:39 +05:30
get :index, params: { snippet_id: private_snippet }
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
end
it "returns 1 note" do
2019-02-15 15:39:39 +05:30
get :index, params: { snippet_id: private_snippet }
2017-08-17 22:00:37 +05:30
2019-09-30 21:07:59 +05:30
expect(json_response['notes'].count).to eq(1)
2017-08-17 22:00:37 +05:30
end
end
end
context 'dont show non visible notes' do
before do
note_on_public
sign_in(user)
2020-04-08 14:13:33 +05:30
expect_any_instance_of(Note).to receive(:readable_by?).and_return(false)
2017-08-17 22:00:37 +05:30
end
it "does not return any note" do
2019-02-15 15:39:39 +05:30
get :index, params: { snippet_id: public_snippet }
2017-08-17 22:00:37 +05:30
2019-09-30 21:07:59 +05:30
expect(json_response['notes'].count).to eq(0)
2017-08-17 22:00:37 +05:30
end
2019-07-07 11:18:12 +05:30
end
end
describe 'POST create' do
context 'when a snippet is public' do
let(:request_params) do
{
note: attributes_for(:note_on_personal_snippet, noteable: public_snippet),
snippet_id: public_snippet.id
}
end
before do
sign_in user
end
it 'returns status 302' do
post :create, params: request_params
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:found)
2019-07-07 11:18:12 +05:30
end
it 'creates the note' do
expect { post :create, params: request_params }.to change { Note.count }.by(1)
end
2021-04-17 20:07:23 +05:30
2022-03-02 08:16:31 +05:30
it_behaves_like 'create notes request exceeding rate limit', :clean_gitlab_redis_cache do
let(:current_user) { user }
def request
post :create, params: request_params
end
2021-04-17 20:07:23 +05:30
end
2019-07-07 11:18:12 +05:30
end
context 'when a snippet is internal' do
let(:request_params) do
{
note: attributes_for(:note_on_personal_snippet, noteable: internal_snippet),
snippet_id: internal_snippet.id
}
end
before do
sign_in user
end
it 'returns status 302' do
post :create, params: request_params
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:found)
2019-07-07 11:18:12 +05:30
end
it 'creates the note' do
expect { post :create, params: request_params }.to change { Note.count }.by(1)
end
2021-04-17 20:07:23 +05:30
2022-03-02 08:16:31 +05:30
it_behaves_like 'create notes request exceeding rate limit', :clean_gitlab_redis_cache do
let(:current_user) { user }
def request
post :create, params: request_params
end
2021-04-17 20:07:23 +05:30
end
2019-07-07 11:18:12 +05:30
end
context 'when a snippet is private' do
let(:request_params) do
{
note: attributes_for(:note_on_personal_snippet, noteable: private_snippet),
snippet_id: private_snippet.id
}
end
before do
sign_in user
end
context 'when user is not the author' do
before do
sign_in(user)
end
it 'returns status 404' do
post :create, params: request_params
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-07-07 11:18:12 +05:30
end
it 'does not create the note' do
expect { post :create, params: request_params }.not_to change { Note.count }
end
context 'when user sends a snippet_id for a public snippet' do
let(:request_params) do
{
note: attributes_for(:note_on_personal_snippet, noteable: private_snippet),
snippet_id: public_snippet.id
}
end
it 'returns status 302' do
post :create, params: request_params
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:found)
2019-07-07 11:18:12 +05:30
end
it 'creates the note on the public snippet' do
expect { post :create, params: request_params }.to change { Note.count }.by(1)
expect(Note.last.noteable).to eq public_snippet
end
end
end
context 'when user is the author' do
before do
sign_in(private_snippet.author)
end
it 'returns status 302' do
post :create, params: request_params
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:found)
2019-07-07 11:18:12 +05:30
end
it 'creates the note' do
expect { post :create, params: request_params }.to change { Note.count }.by(1)
end
2021-04-17 20:07:23 +05:30
2022-03-02 08:16:31 +05:30
it_behaves_like 'create notes request exceeding rate limit', :clean_gitlab_redis_cache do
let(:current_user) { private_snippet.author }
def request
post :create, params: request_params
end
2021-04-17 20:07:23 +05:30
end
2019-07-07 11:18:12 +05:30
end
2017-08-17 22:00:37 +05:30
end
end
describe 'DELETE destroy' do
let(:request_params) do
{
snippet_id: public_snippet,
id: note_on_public,
format: :js
}
end
context 'when user is the author of a note' do
before do
sign_in(note_on_public.author)
end
it "returns status 200" do
2019-02-15 15:39:39 +05:30
delete :destroy, params: request_params
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
end
it "deletes the note" do
2019-02-15 15:39:39 +05:30
expect { delete :destroy, params: request_params }.to change { Note.count }.from(1).to(0)
2017-08-17 22:00:37 +05:30
end
context 'system note' do
before do
expect_any_instance_of(Note).to receive(:system?).and_return(true)
end
it "does not delete the note" do
2019-02-15 15:39:39 +05:30
expect { delete :destroy, params: request_params }.not_to change { Note.count }
2017-08-17 22:00:37 +05:30
end
end
end
context 'when user is not the author of a note' do
before do
sign_in(user)
note_on_public
end
it "returns status 404" do
2019-02-15 15:39:39 +05:30
delete :destroy, params: request_params
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2017-08-17 22:00:37 +05:30
end
it "does not update the note" do
2019-02-15 15:39:39 +05:30
expect { delete :destroy, params: request_params }.not_to change { Note.count }
2017-08-17 22:00:37 +05:30
end
end
end
describe 'POST toggle_award_emoji' do
let(:note) { create(:note_on_personal_snippet, noteable: public_snippet) }
2022-10-11 01:57:18 +05:30
let(:emoji_name) { 'thumbsup' }
2019-12-04 20:38:33 +05:30
2017-08-17 22:00:37 +05:30
before do
sign_in(user)
end
2019-12-04 20:38:33 +05:30
subject { post(:toggle_award_emoji, params: { snippet_id: public_snippet, id: note.id, name: emoji_name }) }
2017-08-17 22:00:37 +05:30
it "toggles the award emoji" do
expect { subject }.to change { note.award_emoji.count }.by(1)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
end
it "removes the already awarded emoji when it exists" do
2019-12-04 20:38:33 +05:30
create(:award_emoji, awardable: note, name: emoji_name, user: user)
2017-08-17 22:00:37 +05:30
expect { subject }.to change { AwardEmoji.count }.by(-1)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
end
end
end