summaryrefslogtreecommitdiff
path: root/spec/models/event_spec.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-03-19 12:09:33 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-03-19 12:09:33 +0000
commit652bd073731b0028641672a75355c7918b5ac116 (patch)
treee0239f98153492ac89c6fc374c5dfd1aa270d8bf /spec/models/event_spec.rb
parent2af90cef2e2e9c776eae4394a43dba3be7f33d1e (diff)
downloadgitlab-ce-652bd073731b0028641672a75355c7918b5ac116.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/models/event_spec.rb')
-rw-r--r--spec/models/event_spec.rb354
1 files changed, 218 insertions, 136 deletions
diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb
index 97ea32a120d..b2676a79b55 100644
--- a/spec/models/event_spec.rb
+++ b/spec/models/event_spec.rb
@@ -99,6 +99,39 @@ describe Event do
end
end
+ describe '#target_title' do
+ let_it_be(:project) { create(:project) }
+
+ let(:author) { project.owner }
+ let(:target) { nil }
+
+ let(:event) do
+ described_class.new(project: project,
+ target: target,
+ author_id: author.id)
+ end
+
+ context 'for an issue' do
+ let(:title) { generate(:title) }
+ let(:issue) { create(:issue, title: title, project: project) }
+ let(:target) { issue }
+
+ it 'delegates to issue title' do
+ expect(event.target_title).to eq(title)
+ end
+ end
+
+ context 'for a wiki page' do
+ let(:title) { generate(:wiki_page_title) }
+ let(:wiki_page) { create(:wiki_page, title: title, project: project) }
+ let(:event) { create(:wiki_page_event, project: project, wiki_page: wiki_page) }
+
+ it 'delegates to wiki page title' do
+ expect(event.target_title).to eq(wiki_page.title)
+ end
+ end
+ end
+
describe '#membership_changed?' do
context "created" do
subject { build(:event, :created).membership_changed? }
@@ -148,13 +181,16 @@ describe Event do
end
describe '#visible_to_user?' do
- let(:project) { create(:project, :public) }
- let(:non_member) { create(:user) }
- let(:member) { create(:user) }
- let(:guest) { create(:user) }
- let(:author) { create(:author) }
- let(:assignee) { create(:user) }
- let(:admin) { create(:admin) }
+ let_it_be(:non_member) { create(:user) }
+ let_it_be(:member) { create(:user) }
+ let_it_be(:guest) { create(:user) }
+ let_it_be(:author) { create(:author) }
+ let_it_be(:assignee) { create(:user) }
+ let_it_be(:admin) { create(:admin) }
+ let_it_be(:public_project) { create(:project, :public) }
+ let_it_be(:private_project) { create(:project, :private) }
+
+ let(:project) { public_project }
let(:issue) { create(:issue, project: project, author: author, assignees: [assignee]) }
let(:confidential_issue) { create(:issue, :confidential, project: project, author: author, assignees: [assignee]) }
let(:project_snippet) { create(:project_snippet, :public, project: project, author: author) }
@@ -165,36 +201,77 @@ describe Event do
let(:note_on_project_snippet) { create(:note_on_project_snippet, author: author, noteable: project_snippet, project: project) }
let(:note_on_personal_snippet) { create(:note_on_personal_snippet, author: author, noteable: personal_snippet, project: nil) }
let(:milestone_on_project) { create(:milestone, project: project) }
- let(:event) { described_class.new(project: project, target: target, author_id: author.id) }
+ let(:event) do
+ described_class.new(project: project,
+ target: target,
+ author_id: author.id)
+ end
before do
project.add_developer(member)
project.add_guest(guest)
end
+ def visible_to_all
+ {
+ logged_out: true,
+ non_member: true,
+ guest: true,
+ member: true,
+ admin: true
+ }
+ end
+
+ def visible_to_none
+ visible_to_all.transform_values { |_| false }
+ end
+
+ def visible_to_none_except(*roles)
+ visible_to_none.merge(roles.map { |role| [role, true] }.to_h)
+ end
+
+ def visible_to_all_except(*roles)
+ visible_to_all.merge(roles.map { |role| [role, false] }.to_h)
+ end
+
+ shared_examples 'visibility examples' do
+ it 'has the correct visibility' do
+ expect({
+ logged_out: event.visible_to_user?(nil),
+ non_member: event.visible_to_user?(non_member),
+ guest: event.visible_to_user?(guest),
+ member: event.visible_to_user?(member),
+ admin: event.visible_to_user?(admin)
+ }).to match(visibility)
+ end
+ end
+
+ shared_examples 'visible to assignee' do |visible|
+ it { expect(event.visible_to_user?(assignee)).to eq(visible) }
+ end
+
+ shared_examples 'visible to author' do |visible|
+ it { expect(event.visible_to_user?(author)).to eq(visible) }
+ end
+
+ shared_examples 'visible to assignee and author' do |visible|
+ include_examples 'visible to assignee', visible
+ include_examples 'visible to author', visible
+ end
+
context 'commit note event' do
let(:project) { create(:project, :public, :repository) }
let(:target) { note_on_commit }
- it do
- aggregate_failures do
- expect(event.visible_to_user?(non_member)).to eq true
- expect(event.visible_to_user?(member)).to eq true
- expect(event.visible_to_user?(guest)).to eq true
- expect(event.visible_to_user?(admin)).to eq true
- end
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_all }
end
context 'private project' do
let(:project) { create(:project, :private, :repository) }
- it do
- aggregate_failures do
- expect(event.visible_to_user?(non_member)).to eq false
- expect(event.visible_to_user?(member)).to eq true
- expect(event.visible_to_user?(guest)).to eq false
- expect(event.visible_to_user?(admin)).to eq true
- end
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_none_except(:member, :admin) }
end
end
end
@@ -203,27 +280,19 @@ describe Event do
context 'for non confidential issues' do
let(:target) { issue }
- it do
- expect(event.visible_to_user?(non_member)).to eq true
- expect(event.visible_to_user?(author)).to eq true
- expect(event.visible_to_user?(assignee)).to eq true
- expect(event.visible_to_user?(member)).to eq true
- expect(event.visible_to_user?(guest)).to eq true
- expect(event.visible_to_user?(admin)).to eq true
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_all }
end
+ include_examples 'visible to assignee and author', true
end
context 'for confidential issues' do
let(:target) { confidential_issue }
- it do
- expect(event.visible_to_user?(non_member)).to eq false
- expect(event.visible_to_user?(author)).to eq true
- expect(event.visible_to_user?(assignee)).to eq true
- expect(event.visible_to_user?(member)).to eq true
- expect(event.visible_to_user?(guest)).to eq false
- expect(event.visible_to_user?(admin)).to eq true
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_none_except(:member, :admin) }
end
+ include_examples 'visible to assignee and author', true
end
end
@@ -231,105 +300,99 @@ describe Event do
context 'on non confidential issues' do
let(:target) { note_on_issue }
- it do
- expect(event.visible_to_user?(non_member)).to eq true
- expect(event.visible_to_user?(author)).to eq true
- expect(event.visible_to_user?(assignee)).to eq true
- expect(event.visible_to_user?(member)).to eq true
- expect(event.visible_to_user?(guest)).to eq true
- expect(event.visible_to_user?(admin)).to eq true
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_all }
end
+ include_examples 'visible to assignee and author', true
end
context 'on confidential issues' do
let(:target) { note_on_confidential_issue }
- it do
- expect(event.visible_to_user?(non_member)).to eq false
- expect(event.visible_to_user?(author)).to eq true
- expect(event.visible_to_user?(assignee)).to eq true
- expect(event.visible_to_user?(member)).to eq true
- expect(event.visible_to_user?(guest)).to eq false
- expect(event.visible_to_user?(admin)).to eq true
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_none_except(:member, :admin) }
end
+ include_examples 'visible to assignee and author', true
end
context 'private project' do
- let(:project) { create(:project, :private) }
+ let(:project) { private_project }
let(:target) { note_on_issue }
- it do
- expect(event.visible_to_user?(non_member)).to eq false
- expect(event.visible_to_user?(author)).to eq false
- expect(event.visible_to_user?(assignee)).to eq false
- expect(event.visible_to_user?(member)).to eq true
- expect(event.visible_to_user?(guest)).to eq true
- expect(event.visible_to_user?(admin)).to eq true
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_none_except(:guest, :member, :admin) }
end
+
+ include_examples 'visible to assignee and author', false
end
end
context 'merge request diff note event' do
- let(:project) { create(:project, :public) }
let(:merge_request) { create(:merge_request, source_project: project, author: author, assignees: [assignee]) }
let(:note_on_merge_request) { create(:legacy_diff_note_on_merge_request, noteable: merge_request, project: project) }
let(:target) { note_on_merge_request }
- it do
- expect(event.visible_to_user?(non_member)).to eq true
- expect(event.visible_to_user?(author)).to eq true
- expect(event.visible_to_user?(assignee)).to eq true
- expect(event.visible_to_user?(member)).to eq true
- expect(event.visible_to_user?(guest)).to eq true
- expect(event.visible_to_user?(admin)).to eq true
+ context 'public project' do
+ let(:project) { public_project }
+
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_all }
+ end
+
+ include_examples 'visible to assignee', true
end
context 'private project' do
- let(:project) { create(:project, :private) }
+ let(:project) { private_project }
- it do
- expect(event.visible_to_user?(non_member)).to eq false
- expect(event.visible_to_user?(author)).to eq false
- expect(event.visible_to_user?(assignee)).to eq false
- expect(event.visible_to_user?(member)).to eq true
- expect(event.visible_to_user?(guest)).to eq false
- expect(event.visible_to_user?(admin)).to eq true
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_none_except(:member, :admin) }
end
+
+ include_examples 'visible to assignee', false
end
end
context 'milestone event' do
let(:target) { milestone_on_project }
- it do
- expect(event.visible_to_user?(nil)).to be_truthy
- expect(event.visible_to_user?(non_member)).to be_truthy
- expect(event.visible_to_user?(member)).to be_truthy
- expect(event.visible_to_user?(guest)).to be_truthy
- expect(event.visible_to_user?(admin)).to be_truthy
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_all }
end
context 'on public project with private issue tracker and merge requests' do
let(:project) { create(:project, :public, :issues_private, :merge_requests_private) }
- it do
- expect(event.visible_to_user?(nil)).to be_falsy
- expect(event.visible_to_user?(non_member)).to be_falsy
- expect(event.visible_to_user?(member)).to be_truthy
- expect(event.visible_to_user?(guest)).to be_truthy
- expect(event.visible_to_user?(admin)).to be_truthy
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_all_except(:logged_out, :non_member) }
end
end
context 'on private project' do
let(:project) { create(:project, :private) }
- it do
- expect(event.visible_to_user?(nil)).to be_falsy
- expect(event.visible_to_user?(non_member)).to be_falsy
- expect(event.visible_to_user?(member)).to be_truthy
- expect(event.visible_to_user?(guest)).to be_truthy
- expect(event.visible_to_user?(admin)).to be_truthy
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_all_except(:logged_out, :non_member) }
+ end
+ end
+ end
+
+ context 'wiki-page event', :aggregate_failures do
+ let(:event) { create(:wiki_page_event, project: project) }
+
+ context 'on private project', :aggregate_failures do
+ let(:project) { create(:project, :wiki_repo) }
+
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_all_except(:logged_out, :non_member) }
+ end
+ end
+
+ context 'wiki-page event on public project', :aggregate_failures do
+ let(:project) { create(:project, :public, :wiki_repo) }
+
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_all }
end
end
end
@@ -337,79 +400,98 @@ describe Event do
context 'project snippet note event' do
let(:target) { note_on_project_snippet }
- it do
- expect(event.visible_to_user?(nil)).to be_truthy
- expect(event.visible_to_user?(non_member)).to be_truthy
- expect(event.visible_to_user?(author)).to be_truthy
- expect(event.visible_to_user?(member)).to be_truthy
- expect(event.visible_to_user?(guest)).to be_truthy
- expect(event.visible_to_user?(admin)).to be_truthy
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_all }
end
context 'on public project with private snippets' do
let(:project) { create(:project, :public, :snippets_private) }
- it do
- expect(event.visible_to_user?(nil)).to be_falsy
- expect(event.visible_to_user?(non_member)).to be_falsy
-
- # Normally, we'd expect the author of a comment to be able to view it.
- # However, this doesn't seem to be the case for comments on snippets.
- expect(event.visible_to_user?(author)).to be_falsy
-
- expect(event.visible_to_user?(member)).to be_truthy
- expect(event.visible_to_user?(guest)).to be_truthy
- expect(event.visible_to_user?(admin)).to be_truthy
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_none_except(:guest, :member, :admin) }
end
+ # Normally, we'd expect the author of a comment to be able to view it.
+ # However, this doesn't seem to be the case for comments on snippets.
+ include_examples 'visible to author', false
end
context 'on private project' do
let(:project) { create(:project, :private) }
- it do
- expect(event.visible_to_user?(nil)).to be_falsy
- expect(event.visible_to_user?(non_member)).to be_falsy
-
- # Normally, we'd expect the author of a comment to be able to view it.
- # However, this doesn't seem to be the case for comments on snippets.
- expect(event.visible_to_user?(author)).to be_falsy
-
- expect(event.visible_to_user?(member)).to be_truthy
- expect(event.visible_to_user?(guest)).to be_truthy
- expect(event.visible_to_user?(admin)).to be_truthy
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_none_except(:guest, :member, :admin) }
end
+ # Normally, we'd expect the author of a comment to be able to view it.
+ # However, this doesn't seem to be the case for comments on snippets.
+ include_examples 'visible to author', false
end
end
context 'personal snippet note event' do
let(:target) { note_on_personal_snippet }
- it do
- expect(event.visible_to_user?(nil)).to be_truthy
- expect(event.visible_to_user?(non_member)).to be_truthy
- expect(event.visible_to_user?(author)).to be_truthy
- expect(event.visible_to_user?(admin)).to be_truthy
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_all }
end
+ include_examples 'visible to author', true
context 'on internal snippet' do
let(:personal_snippet) { create(:personal_snippet, :internal, author: author) }
- it do
- expect(event.visible_to_user?(nil)).to be_falsy
- expect(event.visible_to_user?(non_member)).to be_truthy
- expect(event.visible_to_user?(author)).to be_truthy
- expect(event.visible_to_user?(admin)).to be_truthy
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_all_except(:logged_out) }
end
end
context 'on private snippet' do
let(:personal_snippet) { create(:personal_snippet, :private, author: author) }
- it do
- expect(event.visible_to_user?(nil)).to be_falsy
- expect(event.visible_to_user?(non_member)).to be_falsy
- expect(event.visible_to_user?(author)).to be_truthy
- expect(event.visible_to_user?(admin)).to be_truthy
+ include_examples 'visibility examples' do
+ let(:visibility) { visible_to_none_except(:admin) }
+ end
+ include_examples 'visible to author', true
+ end
+ end
+ end
+
+ describe '.for_wiki_page' do
+ let_it_be(:events) do
+ [
+ create(:closed_issue_event),
+ create(:wiki_page_event),
+ create(:closed_issue_event),
+ create(:event, :created),
+ create(:wiki_page_event)
+ ]
+ end
+
+ it 'only contains the wiki page events' do
+ wiki_events = events.select(&:wiki_page?)
+
+ expect(described_class.for_wiki_page).to match_array(wiki_events)
+ end
+ end
+
+ describe '#wiki_page and #wiki_page?' do
+ let_it_be(:project) { create(:project, :repository) }
+
+ context 'for a wiki page event' do
+ let(:wiki_page) do
+ create(:wiki_page, :with_real_page, project: project)
+ end
+
+ subject(:event) { create(:wiki_page_event, project: project, wiki_page: wiki_page) }
+
+ it { is_expected.to have_attributes(wiki_page?: be_truthy, wiki_page: wiki_page) }
+ end
+
+ [:issue, :user, :merge_request, :snippet, :milestone, nil].each do |kind|
+ context "for a #{kind} event" do
+ it 'is nil' do
+ target = create(kind) if kind
+ event = create(:event, project: project, target: target)
+
+ expect(event).to have_attributes(wiki_page: be_nil, wiki_page?: be_falsy)
end
end
end