Commit 46e58819 authored by Eugenia Grieff's avatar Eugenia Grieff

Use let_it_be where possible

parent 9605f38e
......@@ -5,13 +5,13 @@ require 'spec_helper'
RSpec.describe Notes::BuildService do
include AdminModeHelper
let_it_be(:note) { create(:discussion_note_on_issue) }
let(:project) { note.project }
let(:author) { note.author }
let(:user) { author }
let(:merge_request) { create(:merge_request, source_project: project) }
let(:mr_note) { create(:discussion_note_on_merge_request, noteable: merge_request, project: project, author: note.author) }
let_it_be(:project) { create(:project, :repository) }
let_it_be(:note) { create(:discussion_note_on_issue, project: project) }
let_it_be(:author) { note.author }
let_it_be(:user) { author }
let_it_be(:noteable_author) { create(:user) }
let_it_be(:other_user) { create(:user) }
let_it_be(:external) { create(:user, :external) }
let(:base_params) { { note: 'Test' } }
let(:params) { {} }
......@@ -30,11 +30,10 @@ RSpec.describe Notes::BuildService do
end
context 'when discussion is resolved' do
let(:params) { { in_reply_to_discussion_id: mr_note.discussion_id } }
let_it_be(:merge_request) { create(:merge_request, source_project: project) }
let_it_be(:mr_note) { create(:discussion_note_on_merge_request, :resolved, noteable: merge_request, project: project, author: author) }
before do
mr_note.resolve!(author)
end
let(:params) { { in_reply_to_discussion_id: mr_note.discussion_id } }
it 'resolves the note' do
expect(new_note).to be_valid
......@@ -59,7 +58,7 @@ RSpec.describe Notes::BuildService do
end
context 'when user has no access to discussion' do
let(:user) { create(:user) }
let(:user) { other_user }
it 'sets an error' do
expect(new_note.errors[:base]).to include('Discussion to reply to cannot be found')
......@@ -67,16 +66,14 @@ RSpec.describe Notes::BuildService do
end
context 'personal snippet note' do
def reply(note, user = nil)
user ||= create(:user)
def reply(note, user = other_user)
described_class.new(nil,
user,
note: 'Test',
in_reply_to_discussion_id: note.discussion_id).execute
end
let(:snippet_author) { create(:user) }
let_it_be(:snippet_author) { noteable_author }
context 'when a snippet is public' do
it 'creates a reply note' do
......@@ -91,8 +88,8 @@ RSpec.describe Notes::BuildService do
end
context 'when a snippet is private' do
let(:snippet) { create(:personal_snippet, :private, author: snippet_author) }
let(:note) { create(:discussion_note_on_personal_snippet, noteable: snippet) }
let_it_be(:snippet) { create(:personal_snippet, :private, author: snippet_author) }
let_it_be(:note) { create(:discussion_note_on_personal_snippet, noteable: snippet) }
it 'creates a reply note when the author replies' do
new_note = reply(note, snippet_author)
......@@ -109,8 +106,8 @@ RSpec.describe Notes::BuildService do
end
context 'when a snippet is internal' do
let(:snippet) { create(:personal_snippet, :internal, author: snippet_author) }
let(:note) { create(:discussion_note_on_personal_snippet, noteable: snippet) }
let_it_be(:snippet) { create(:personal_snippet, :internal, author: snippet_author) }
let_it_be(:note) { create(:discussion_note_on_personal_snippet, noteable: snippet) }
it 'creates a reply note when the author replies' do
new_note = reply(note, snippet_author)
......@@ -127,7 +124,7 @@ RSpec.describe Notes::BuildService do
end
it 'sets an error when an external user replies' do
new_note = reply(note, create(:user, :external))
new_note = reply(note, external)
expect(new_note.errors[:base]).to include('Discussion to reply to cannot be found')
end
......@@ -136,7 +133,8 @@ RSpec.describe Notes::BuildService do
end
context 'when replying to individual note' do
let(:note) { create(:note_on_issue) }
let_it_be(:note) { create(:note_on_issue, project: project) }
let(:params) { { in_reply_to_discussion_id: note.discussion_id } }
it 'sets the note up to be in reply to that note' do
......@@ -146,7 +144,7 @@ RSpec.describe Notes::BuildService do
end
context 'when noteable does not support replies' do
let(:note) { create(:note_on_commit) }
let_it_be(:note) { create(:note_on_commit, project: project) }
it 'builds another individual note' do
expect(new_note).to be_valid
......@@ -157,177 +155,140 @@ RSpec.describe Notes::BuildService do
end
context 'confidential comments' do
let_it_be(:assignee) { create(:user) }
let_it_be(:project) { create(:project, :public) }
let_it_be(:guest) { create(:user) }
let_it_be(:external) { create(:user) }
let_it_be(:reporter) { create(:user) }
let_it_be(:admin) { create(:admin) }
let_it_be(:issuable_assignee) { other_user }
let_it_be(:issue) do
create(:issue, project: project, author: noteable_author, assignees: [issuable_assignee])
end
before do
project.update_attribute(:visibility_level, Project::PUBLIC)
project.add_guest(guest)
project.add_reporter(reporter)
end
context 'when replying to a confidential comment' do
let(:note) { create(:note_on_issue, confidential: true) }
let(:params) { { in_reply_to_discussion_id: note.discussion_id, confidential: false } }
context 'when creating a new confidential comment' do
let(:params) { { confidential: true, noteable: issue } }
shared_examples 'returns `Discussion to reply to cannot be found` error' do
it 'returns `Discussion to reply to cannot be found` error' do
expect(new_note.errors.added?(:base, "Discussion to reply to cannot be found")).to be true
end
shared_examples 'user allowed to set comment as confidential' do
it { expect(new_note.confidential).to be_truthy }
end
shared_examples 'confidential set to `true`' do
it '`confidential` param is ignored' do
expect(new_note.confidential).to be_truthy
end
shared_examples 'user not allowed to set comment as confidential' do
it { expect(new_note.confidential).to be_falsey }
end
context 'with reporter access' do
before do
project.add_reporter(author)
end
context 'reporter' do
let(:user) { reporter }
it_behaves_like 'confidential set to `true`'
it_behaves_like 'user allowed to set comment as confidential'
end
context 'with admin access' do
let(:user) { admin }
context 'issuable author' do
let(:user) { noteable_author }
before do
enable_admin_mode!(admin)
it_behaves_like 'user allowed to set comment as confidential'
end
it_behaves_like 'confidential set to `true`'
end
context 'issuable assignee' do
let(:user) { issuable_assignee }
context 'with noteable author' do
let(:user) { note.noteable.author }
it_behaves_like 'confidential set to `true`'
it_behaves_like 'user allowed to set comment as confidential'
end
context 'with noteable assignee' do
let(:user) { assignee }
context 'admin' do
before do
project
note.noteable.assignees = [assignee]
end
it_behaves_like 'confidential set to `true`'
enable_admin_mode!(admin)
end
context 'with noteable assignee' do
let(:user) { assignee }
let(:user) { admin }
before do
project
note.noteable.assignees = [assignee]
it_behaves_like 'user allowed to set comment as confidential'
end
it_behaves_like 'confidential set to `true`'
context 'external' do
let(:user) { external }
it_behaves_like 'user not allowed to set comment as confidential'
end
context 'with guest access' do
context 'guest' do
let(:user) { guest }
before do
project.add_guest(guest)
it_behaves_like 'user not allowed to set comment as confidential'
end
it_behaves_like 'returns `Discussion to reply to cannot be found` error'
end
context 'with external user' do
let(:user) { external }
it_behaves_like 'returns `Discussion to reply to cannot be found` error'
end
end
context 'when replying to a confidential comment' do
let_it_be(:note) { create(:note_on_issue, confidential: true, noteable: issue, project: project) }
context 'when replying to a public comment' do
let(:note) { create(:note_on_issue, confidential: false) }
let(:params) { { in_reply_to_discussion_id: note.discussion_id, confidential: true } }
let(:params) { { in_reply_to_discussion_id: note.discussion_id, confidential: false } }
it '`confidential` param is ignored and set to `false`' do
expect(new_note.confidential).to be_falsey
end
shared_examples 'returns `Discussion to reply to cannot be found` error' do
it do
expect(new_note.errors.added?(:base, "Discussion to reply to cannot be found")).to be true
end
context 'when creating a new comment' do
context 'when the `confidential` note flag is set to `true`' do
shared_examples 'sets confidentiality as `true`' do
it { expect(new_note.confidential).to be_truthy }
end
shared_examples 'sets confidentiality as `false`' do
it { expect(new_note.confidential).to be_falsey }
shared_examples 'confidential set to `true`' do
it '`confidential` param is ignored to match the parent note confidentiality' do
expect(new_note.confidential).to be_truthy
end
context 'when the user is allowed (reporter)' do
let(:params) { { confidential: true, noteable: merge_request } }
it_behaves_like 'sets confidentiality as `true`'
end
context 'when the user is allowed (issuable author)' do
let(:user) { external }
let(:issue) { create(:issue, author: user) }
let(:params) { { confidential: true, noteable: issue } }
context 'with reporter access' do
let(:user) { reporter }
it_behaves_like 'sets confidentiality as `true`'
it_behaves_like 'confidential set to `true`'
end
context 'when the user is allowed (issuable assignee)' do
let(:user) { assignee }
let(:params) { { confidential: true, noteable: merge_request } }
context 'with admin access' do
let(:user) { admin }
before do
merge_request.assignees = [assignee]
end
it_behaves_like 'sets confidentiality as `true`'
enable_admin_mode!(admin)
end
context 'when the user is allowed (admin)' do
before do
enable_admin_mode!(admin)
it_behaves_like 'confidential set to `true`'
end
let(:user) { admin }
let(:params) { { confidential: true, noteable: merge_request } }
context 'with noteable author' do
let(:user) { note.noteable.author }
it_behaves_like 'sets confidentiality as `true`'
it_behaves_like 'confidential set to `true`'
end
context 'when the user is not allowed (external)' do
let(:user) { external }
let(:params) { { confidential: true, noteable: merge_request } }
context 'with noteable assignee' do
let(:user) { issuable_assignee }
it_behaves_like 'sets confidentiality as `false`'
it_behaves_like 'confidential set to `true`'
end
context 'when the user is not allowed (guest)' do
context 'with guest access' do
let(:user) { guest }
let(:params) { { confidential: true, noteable: merge_request } }
before do
project.add_guest(guest)
it_behaves_like 'returns `Discussion to reply to cannot be found` error'
end
it_behaves_like 'sets confidentiality as `false`'
context 'with external user' do
let(:user) { external }
it_behaves_like 'returns `Discussion to reply to cannot be found` error'
end
end
context 'when the `confidential` note flag is set to `false`' do
let(:params) { { confidential: false, noteable: merge_request } }
context 'when replying to a public comment' do
let_it_be(:note) { create(:note_on_issue, confidential: false, noteable: issue, project: project) }
let(:params) { { in_reply_to_discussion_id: note.discussion_id, confidential: true } }
it 'note `confidential` flag is set to `false`' do
it '`confidential` param is ignored and set to `false`' do
expect(new_note.confidential).to be_falsey
end
end
end
end
context 'when noteable is not set' do
let(:params) { { noteable_type: note.noteable_type, noteable_id: note.noteable_id } }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment