2019-04-15 06:17:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-27 06:41:26 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-03 14:08:28 -04:00
|
|
|
RSpec.describe Snippets::NotesController do
|
2017-04-27 06:41:26 -04:00
|
|
|
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
|
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
get :index, params: { snippet_id: public_snippet }
|
2017-04-27 06:41:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns status 200" do
|
2020-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-04-27 06:41:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns not empty array of notes" do
|
2019-07-16 04:03:49 -04:00
|
|
|
expect(json_response["notes"].empty?).to be_falsey
|
2017-04-27 06:41:26 -04:00
|
|
|
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
|
2018-12-17 17:52:17 -05:00
|
|
|
get :index, params: { snippet_id: internal_snippet }
|
2017-04-27 06:41:26 -04:00
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2017-04-27 06:41:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user logged in' do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns status 200" do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :index, params: { snippet_id: internal_snippet }
|
2017-04-27 06:41:26 -04:00
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-04-27 06:41:26 -04:00
|
|
|
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
|
2018-12-17 17:52:17 -05:00
|
|
|
get :index, params: { snippet_id: private_snippet }
|
2017-04-27 06:41:26 -04:00
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2017-04-27 06:41:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user other than author logged in' do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns status 404" do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :index, params: { snippet_id: private_snippet }
|
2017-04-27 06:41:26 -04:00
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2017-04-27 06:41:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when author logged in' do
|
|
|
|
before do
|
|
|
|
note_on_private
|
|
|
|
|
|
|
|
sign_in(private_snippet.author)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns status 200" do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :index, params: { snippet_id: private_snippet }
|
2017-04-27 06:41:26 -04:00
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-04-27 06:41:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 1 note" do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :index, params: { snippet_id: private_snippet }
|
2017-04-27 06:41:26 -04:00
|
|
|
|
2019-07-16 04:03:49 -04:00
|
|
|
expect(json_response['notes'].count).to eq(1)
|
2017-04-27 06:41:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'dont show non visible notes' do
|
|
|
|
before do
|
|
|
|
note_on_public
|
|
|
|
|
|
|
|
sign_in(user)
|
|
|
|
|
2020-02-23 22:09:05 -05:00
|
|
|
expect_any_instance_of(Note).to receive(:readable_by?).and_return(false)
|
2017-04-27 06:41:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not return any note" do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :index, params: { snippet_id: public_snippet }
|
2017-04-27 06:41:26 -04:00
|
|
|
|
2019-07-16 04:03:49 -04:00
|
|
|
expect(json_response['notes'].count).to eq(0)
|
2017-04-27 06:41:26 -04:00
|
|
|
end
|
2019-05-31 12:18:09 -04:00
|
|
|
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-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
2019-05-31 12:18:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates the note' do
|
|
|
|
expect { post :create, params: request_params }.to change { Note.count }.by(1)
|
|
|
|
end
|
|
|
|
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-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
2019-05-31 12:18:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates the note' do
|
|
|
|
expect { post :create, params: request_params }.to change { Note.count }.by(1)
|
|
|
|
end
|
|
|
|
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-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2019-05-31 12:18:09 -04:00
|
|
|
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-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
2019-05-31 12:18:09 -04:00
|
|
|
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-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
2019-05-31 12:18:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates the note' do
|
|
|
|
expect { post :create, params: request_params }.to change { Note.count }.by(1)
|
|
|
|
end
|
|
|
|
end
|
2017-04-27 06:41:26 -04:00
|
|
|
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
|
2018-12-17 17:52:17 -05:00
|
|
|
delete :destroy, params: request_params
|
2017-04-27 06:41:26 -04:00
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-04-27 06:41:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes the note" do
|
2018-12-17 17:52:17 -05:00
|
|
|
expect { delete :destroy, params: request_params }.to change { Note.count }.from(1).to(0)
|
2017-04-27 06:41:26 -04:00
|
|
|
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
|
2018-12-17 17:52:17 -05:00
|
|
|
expect { delete :destroy, params: request_params }.not_to change { Note.count }
|
2017-04-27 06:41:26 -04:00
|
|
|
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
|
2018-12-17 17:52:17 -05:00
|
|
|
delete :destroy, params: request_params
|
2017-04-27 06:41:26 -04:00
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2017-04-27 06:41:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not update the note" do
|
2018-12-17 17:52:17 -05:00
|
|
|
expect { delete :destroy, params: request_params }.not_to change { Note.count }
|
2017-04-27 06:41:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST toggle_award_emoji' do
|
|
|
|
let(:note) { create(:note_on_personal_snippet, noteable: public_snippet) }
|
2019-06-17 21:44:43 -04:00
|
|
|
let(:emoji_name) { 'thumbsup'}
|
|
|
|
|
2017-04-27 06:41:26 -04:00
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
2019-06-17 21:44:43 -04:00
|
|
|
subject { post(:toggle_award_emoji, params: { snippet_id: public_snippet, id: note.id, name: emoji_name }) }
|
2017-04-27 06:41:26 -04:00
|
|
|
|
|
|
|
it "toggles the award emoji" do
|
|
|
|
expect { subject }.to change { note.award_emoji.count }.by(1)
|
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-04-27 06:41:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "removes the already awarded emoji when it exists" do
|
2019-06-17 21:44:43 -04:00
|
|
|
create(:award_emoji, awardable: note, name: emoji_name, user: user)
|
2017-04-27 06:41:26 -04:00
|
|
|
|
|
|
|
expect { subject }.to change { AwardEmoji.count }.by(-1)
|
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-04-27 06:41:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|