2018-08-01 04:58:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-08-29 03:56:52 -04:00
|
|
|
require 'spec_helper'
|
2018-08-01 04:58:49 -04:00
|
|
|
|
|
|
|
RSpec.describe ResourceLabelEvent, type: :model do
|
2020-09-03 20:09:08 -04:00
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
|
|
|
let_it_be(:issue) { create(:issue, project: project) }
|
|
|
|
let_it_be(:merge_request) { create(:merge_request, source_project: project) }
|
|
|
|
let_it_be(:label) { create(:label, project: project) }
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2020-09-03 20:09:08 -04:00
|
|
|
subject { build(:resource_label_event, issue: issue, label: label) }
|
2018-08-01 04:58:49 -04:00
|
|
|
|
2018-12-04 05:24:21 -05:00
|
|
|
it_behaves_like 'having unique enum values'
|
|
|
|
|
2020-03-01 19:07:41 -05:00
|
|
|
it_behaves_like 'a resource event'
|
|
|
|
it_behaves_like 'a resource event for issues'
|
|
|
|
it_behaves_like 'a resource event for merge requests'
|
|
|
|
|
2018-08-01 04:58:49 -04:00
|
|
|
describe 'associations' do
|
|
|
|
it { is_expected.to belong_to(:label) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validations' do
|
|
|
|
it { is_expected.to be_valid }
|
|
|
|
|
|
|
|
describe 'Issuable validation' do
|
|
|
|
it 'is invalid if issue_id and merge_request_id are missing' do
|
|
|
|
subject.attributes = { issue: nil, merge_request: nil }
|
|
|
|
|
|
|
|
expect(subject).to be_invalid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is invalid if issue_id and merge_request_id are set' do
|
|
|
|
subject.attributes = { issue: issue, merge_request: merge_request }
|
|
|
|
|
|
|
|
expect(subject).to be_invalid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is valid if only issue_id is set' do
|
|
|
|
subject.attributes = { issue: issue, merge_request: nil }
|
|
|
|
|
|
|
|
expect(subject).to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is valid if only merge_request_id is set' do
|
|
|
|
subject.attributes = { merge_request: merge_request, issue: nil }
|
|
|
|
|
|
|
|
expect(subject).to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-09-07 09:39:20 -04:00
|
|
|
|
2020-09-28 08:10:02 -04:00
|
|
|
context 'callbacks' do
|
|
|
|
describe '#usage_metrics' do
|
|
|
|
it 'tracks changed labels' do
|
|
|
|
expect(Gitlab::UsageDataCounters::IssueActivityUniqueCounter).to receive(:track_issue_label_changed_action)
|
|
|
|
|
|
|
|
subject.save!
|
2019-11-20 01:06:16 -05:00
|
|
|
end
|
2018-09-07 09:39:20 -04:00
|
|
|
end
|
|
|
|
|
2020-09-28 08:10:02 -04:00
|
|
|
describe '#expire_etag_cache' do
|
|
|
|
def expect_expiration(issue)
|
|
|
|
expect_next_instance_of(Gitlab::EtagCaching::Store) do |instance|
|
|
|
|
expect(instance).to receive(:touch)
|
|
|
|
.with("/#{issue.project.namespace.to_param}/#{issue.project.to_param}/noteable/issue/#{issue.id}/notes")
|
|
|
|
end
|
|
|
|
end
|
2018-09-07 09:39:20 -04:00
|
|
|
|
2020-09-28 08:10:02 -04:00
|
|
|
it 'expires resource note etag cache on event save' do
|
|
|
|
expect_expiration(subject.issuable)
|
2018-09-07 09:39:20 -04:00
|
|
|
|
2020-09-28 08:10:02 -04:00
|
|
|
subject.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'expires resource note etag cache on event destroy' do
|
|
|
|
subject.save!
|
2018-09-07 09:39:20 -04:00
|
|
|
|
2020-09-28 08:10:02 -04:00
|
|
|
expect_expiration(subject.issuable)
|
2018-09-07 09:39:20 -04:00
|
|
|
|
2020-09-28 08:10:02 -04:00
|
|
|
subject.destroy!
|
|
|
|
end
|
2018-09-07 09:39:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#outdated_markdown?' do
|
|
|
|
it 'returns true if label is missing and reference is not empty' do
|
|
|
|
subject.attributes = { reference: 'ref', label_id: nil }
|
|
|
|
|
|
|
|
expect(subject.outdated_markdown?).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if reference is not set yet' do
|
|
|
|
subject.attributes = { reference: nil }
|
|
|
|
|
|
|
|
expect(subject.outdated_markdown?).to be true
|
|
|
|
end
|
|
|
|
|
2019-01-31 06:28:31 -05:00
|
|
|
it 'returns true if markdown is outdated' do
|
2019-06-05 00:59:48 -04:00
|
|
|
subject.attributes = { cached_markdown_version: ((Gitlab::MarkdownCache::CACHE_COMMONMARK_VERSION - 1) << 16) | 0 }
|
2018-09-07 09:39:20 -04:00
|
|
|
|
|
|
|
expect(subject.outdated_markdown?).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if label and reference are set' do
|
2019-06-05 00:59:48 -04:00
|
|
|
subject.attributes = { reference: 'whatever', cached_markdown_version: Gitlab::MarkdownCache::CACHE_COMMONMARK_VERSION << 16 }
|
2018-09-07 09:39:20 -04:00
|
|
|
|
|
|
|
expect(subject.outdated_markdown?).to be false
|
|
|
|
end
|
|
|
|
end
|
2020-06-10 05:08:35 -04:00
|
|
|
|
|
|
|
describe '.visible_to_user?' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:issue_project) { create(:project) }
|
|
|
|
let_it_be(:issue) { create(:issue, project: issue_project) }
|
|
|
|
|
|
|
|
subject { described_class.visible_to_user?(user, issue.resource_label_events.inc_relations) }
|
|
|
|
|
|
|
|
it 'returns events with labels accessible by user' do
|
|
|
|
label = create(:label, project: issue_project)
|
|
|
|
event = create_event(label)
|
|
|
|
issue_project.add_guest(user)
|
|
|
|
|
|
|
|
expect(subject).to eq [event]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters events with public project labels if issues and MRs are private' do
|
|
|
|
project = create(:project, :public, :issues_private, :merge_requests_private)
|
|
|
|
label = create(:label, project: project)
|
|
|
|
create_event(label)
|
|
|
|
|
|
|
|
expect(subject).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters events with project labels not accessible by user' do
|
|
|
|
project = create(:project, :private)
|
|
|
|
label = create(:label, project: project)
|
|
|
|
create_event(label)
|
|
|
|
|
|
|
|
expect(subject).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters events with group labels not accessible by user' do
|
|
|
|
group = create(:group, :private)
|
|
|
|
label = create(:group_label, group: group)
|
|
|
|
create_event(label)
|
|
|
|
|
|
|
|
expect(subject).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_event(label)
|
|
|
|
create(:resource_label_event, issue: issue, label: label)
|
|
|
|
end
|
|
|
|
end
|
2018-08-01 04:58:49 -04:00
|
|
|
end
|