debian-mirror-gitlab/spec/models/event_spec.rb

378 lines
12 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe Event do
2014-09-02 18:07:02 +05:30
describe "Associations" do
2015-04-26 12:48:37 +05:30
it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:target) }
2014-09-02 18:07:02 +05:30
end
describe "Respond to" do
2015-04-26 12:48:37 +05:30
it { is_expected.to respond_to(:author_name) }
it { is_expected.to respond_to(:author_email) }
it { is_expected.to respond_to(:issue_title) }
it { is_expected.to respond_to(:merge_request_title) }
2014-09-02 18:07:02 +05:30
end
2016-06-02 11:05:42 +05:30
describe 'Callbacks' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project) }
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
describe 'after_create :reset_project_activity' do
2016-09-29 09:46:39 +05:30
it 'calls the reset_project_activity method' do
2017-08-17 22:00:37 +05:30
expect_any_instance_of(described_class).to receive(:reset_project_activity)
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
create_push_event(project, project.owner)
end
end
describe 'after_create :set_last_repository_updated_at' do
context 'with a push event' do
it 'updates the project last_repository_updated_at' do
project.update(last_repository_updated_at: 1.year.ago)
create_push_event(project, project.owner)
project.reload
expect(project.last_repository_updated_at).to be_within(1.minute).of(Time.now)
end
end
context 'without a push event' do
it 'does not update the project last_repository_updated_at' do
project.update(last_repository_updated_at: 1.year.ago)
create(:closed_issue_event, project: project, author: project.owner)
project.reload
expect(project.last_repository_updated_at).to be_within(1.minute).of(1.year.ago)
end
2016-06-02 11:05:42 +05:30
end
end
end
2014-09-02 18:07:02 +05:30
describe "Push event" do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :private) }
2016-11-03 12:29:30 +05:30
let(:user) { project.owner }
2017-08-17 22:00:37 +05:30
let(:event) { create_push_event(project, user) }
2016-11-03 12:29:30 +05:30
it do
expect(event.push?).to be_truthy
2016-11-24 13:41:30 +05:30
expect(event.visible_to_user?(user)).to be_truthy
expect(event.visible_to_user?(nil)).to be_falsey
2016-11-03 12:29:30 +05:30
expect(event.tag?).to be_falsey
expect(event.branch_name).to eq("master")
expect(event.author).to eq(user)
2014-09-02 18:07:02 +05:30
end
end
2015-11-26 14:37:03 +05:30
2017-08-17 22:00:37 +05:30
describe '#membership_changed?' do
context "created" do
subject { build(:event, :created).membership_changed? }
it { is_expected.to be_falsey }
end
context "updated" do
subject { build(:event, :updated).membership_changed? }
it { is_expected.to be_falsey }
end
context "expired" do
subject { build(:event, :expired).membership_changed? }
it { is_expected.to be_truthy }
end
context "left" do
subject { build(:event, :left).membership_changed? }
it { is_expected.to be_truthy }
end
context "joined" do
subject { build(:event, :joined).membership_changed? }
it { is_expected.to be_truthy }
end
end
2016-08-24 12:49:21 +05:30
describe '#note?' do
2017-08-17 22:00:37 +05:30
subject { described_class.new(project: target.project, target: target) }
2016-08-24 12:49:21 +05:30
context 'issue note event' do
let(:target) { create(:note_on_issue) }
it { is_expected.to be_note }
end
context 'merge request diff note event' do
let(:target) { create(:legacy_diff_note_on_merge_request) }
it { is_expected.to be_note }
end
end
2016-06-02 11:05:42 +05:30
describe '#visible_to_user?' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :public) }
2016-06-02 11:05:42 +05:30
let(:non_member) { create(:user) }
2016-11-03 12:29:30 +05:30
let(:member) { create(:user) }
let(:guest) { create(:user) }
2016-06-02 11:05:42 +05:30
let(:author) { create(:author) }
let(:assignee) { create(:user) }
let(:admin) { create(:admin) }
2017-08-17 22:00:37 +05:30
let(:issue) { create(:issue, project: project, author: author, assignees: [assignee]) }
let(:confidential_issue) { create(:issue, :confidential, project: project, author: author, assignees: [assignee]) }
2016-11-24 13:41:30 +05:30
let(:note_on_commit) { create(:note_on_commit, project: project) }
2016-06-02 11:05:42 +05:30
let(:note_on_issue) { create(:note_on_issue, noteable: issue, project: project) }
let(:note_on_confidential_issue) { create(:note_on_issue, noteable: confidential_issue, project: project) }
2017-08-17 22:00:37 +05:30
let(:event) { described_class.new(project: project, target: target, author_id: author.id) }
2016-06-02 11:05:42 +05:30
before do
2018-03-17 18:26:18 +05:30
project.add_developer(member)
project.add_guest(guest)
2016-06-02 11:05:42 +05:30
end
2016-11-24 13:41:30 +05:30
context 'commit note event' do
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
end
context 'private project' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :private) }
2016-11-24 13:41:30 +05:30
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
end
end
end
2016-06-02 11:05:42 +05:30
context 'issue event' do
context 'for non confidential issues' do
let(:target) { issue }
2016-11-03 12:29:30 +05:30
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
end
2016-06-02 11:05:42 +05:30
end
context 'for confidential issues' do
let(:target) { confidential_issue }
2016-11-03 12:29:30 +05:30
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
end
2016-06-02 11:05:42 +05:30
end
end
2016-08-24 12:49:21 +05:30
context 'issue note event' do
2016-06-02 11:05:42 +05:30
context 'on non confidential issues' do
let(:target) { note_on_issue }
2016-11-03 12:29:30 +05:30
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
end
2016-06-02 11:05:42 +05:30
end
context 'on confidential issues' do
let(:target) { note_on_confidential_issue }
2016-11-03 12:29:30 +05:30
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
end
2016-06-02 11:05:42 +05:30
end
end
2016-08-24 12:49:21 +05:30
context 'merge request diff note event' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :public) }
2016-08-24 12:49:21 +05:30
let(:merge_request) { create(:merge_request, source_project: project, author: author, assignee: assignee) }
let(:note_on_merge_request) { create(:legacy_diff_note_on_merge_request, noteable: merge_request, project: project) }
let(:target) { note_on_merge_request }
2016-11-03 12:29:30 +05:30
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
end
context 'private project' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :private) }
2016-11-03 12:29:30 +05:30
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
end
end
2016-08-24 12:49:21 +05:30
end
2016-06-02 11:05:42 +05:30
end
2015-11-26 14:37:03 +05:30
describe '.limit_recent' do
let!(:event1) { create(:closed_issue_event) }
let!(:event2) { create(:closed_issue_event) }
describe 'without an explicit limit' do
2017-08-17 22:00:37 +05:30
subject { described_class.limit_recent }
2015-11-26 14:37:03 +05:30
it { is_expected.to eq([event2, event1]) }
end
describe 'with an explicit limit' do
2017-08-17 22:00:37 +05:30
subject { described_class.limit_recent(1) }
2015-11-26 14:37:03 +05:30
it { is_expected.to eq([event2]) }
end
end
2016-06-02 11:05:42 +05:30
2016-09-29 09:46:39 +05:30
describe '#reset_project_activity' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project) }
2016-09-29 09:46:39 +05:30
context 'when a project was updated less than 1 hour ago' do
it 'does not update the project' do
project.update(last_activity_at: Time.now)
2017-09-10 17:25:29 +05:30
expect(project).not_to receive(:update_column)
.with(:last_activity_at, a_kind_of(Time))
2016-09-29 09:46:39 +05:30
2017-08-17 22:00:37 +05:30
create_push_event(project, project.owner)
2016-09-29 09:46:39 +05:30
end
end
context 'when a project was updated more than 1 hour ago' do
it 'updates the project' do
project.update(last_activity_at: 1.year.ago)
2017-08-17 22:00:37 +05:30
create_push_event(project, project.owner)
2016-09-29 09:46:39 +05:30
2016-11-03 12:29:30 +05:30
project.reload
2016-09-29 09:46:39 +05:30
2017-08-17 22:00:37 +05:30
expect(project.last_activity_at).to be_within(1.minute).of(Time.now)
2016-09-29 09:46:39 +05:30
end
end
end
2017-08-17 22:00:37 +05:30
describe '#authored_by?' do
let(:event) { build(:event) }
it 'returns true when the event author and user are the same' do
expect(event.authored_by?(event.author)).to eq(true)
end
it 'returns false when passing nil as an argument' do
expect(event.authored_by?(nil)).to eq(false)
end
it 'returns false when the given user is not the author of the event' do
user = double(:user, id: -1)
expect(event.authored_by?(user)).to eq(false)
end
end
2017-09-10 17:25:29 +05:30
describe '#body?' do
let(:push_event) do
event = build(:push_event)
allow(event).to receive(:push?).and_return(true)
event
end
it 'returns true for a push event with commits' do
allow(push_event).to receive(:push_with_commits?).and_return(true)
expect(push_event).to be_body
end
it 'returns false for a push event without a valid commit range' do
allow(push_event).to receive(:push_with_commits?).and_return(false)
expect(push_event).not_to be_body
end
it 'returns true for a Note event' do
event = build(:event)
allow(event).to receive(:note?).and_return(true)
expect(event).to be_body
end
it 'returns true if the target responds to #title' do
event = build(:event)
allow(event).to receive(:target).and_return(double(:target, title: 'foo'))
expect(event).to be_body
end
it 'returns false for a regular event without a target' do
event = build(:event)
expect(event).not_to be_body
end
end
2018-03-17 18:26:18 +05:30
describe '#target' do
it 'eager loads the author of an event target' do
create(:closed_issue_event)
events = described_class.preload(:target).all.to_a
count = ActiveRecord::QueryRecorder
.new { events.first.target.author }.count
# This expectation exists to make sure the test doesn't pass when the
# author is for some reason not loaded at all.
expect(events.first.target.author).to be_an_instance_of(User)
expect(count).to be_zero
end
end
2017-09-10 17:25:29 +05:30
def create_push_event(project, user)
event = create(:push_event, project: project, author: user)
create(:push_event_payload,
event: event,
commit_to: '1cf19a015df3523caf0a1f9d40c98a267d6a2fc2',
commit_count: 0,
ref: 'master')
event
2016-06-02 11:05:42 +05:30
end
2014-09-02 18:07:02 +05:30
end