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