2012-02-28 08:09:23 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-09 04:50:51 -05:00
|
|
|
describe Event, models: true do
|
2012-02-28 09:01:14 -05:00
|
|
|
describe "Associations" do
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to belong_to(:project) }
|
|
|
|
it { is_expected.to belong_to(:target) }
|
2012-02-28 09:01:14 -05:00
|
|
|
end
|
|
|
|
|
2012-03-28 15:53:45 -04:00
|
|
|
describe "Respond to" do
|
2015-02-12 13:17:35 -05:00
|
|
|
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) }
|
|
|
|
it { is_expected.to respond_to(:commits) }
|
2012-03-28 15:53:45 -04:00
|
|
|
end
|
|
|
|
|
2016-04-21 03:42:08 -04:00
|
|
|
describe 'Callbacks' do
|
2017-05-03 21:44:39 -04:00
|
|
|
let(:project) { create(:empty_project) }
|
2016-04-21 03:42:08 -04:00
|
|
|
|
2017-05-03 21:44:39 -04:00
|
|
|
describe 'after_create :reset_project_activity' do
|
2016-09-16 10:05:12 -04:00
|
|
|
it 'calls the reset_project_activity method' do
|
2017-01-29 17:37:24 -05:00
|
|
|
expect_any_instance_of(described_class).to receive(:reset_project_activity)
|
2016-04-21 03:42:08 -04:00
|
|
|
|
2017-05-03 21:44:39 -04:00
|
|
|
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-04-21 03:42:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-14 18:00:59 -04:00
|
|
|
describe "Push event" do
|
2017-01-26 17:44:58 -05:00
|
|
|
let(:project) { create(:empty_project, :private) }
|
2016-10-12 02:16:29 -04:00
|
|
|
let(:user) { project.owner }
|
2017-05-03 21:44:39 -04:00
|
|
|
let(:event) { create_push_event(project, user) }
|
2016-10-12 02:16:29 -04:00
|
|
|
|
|
|
|
it do
|
|
|
|
expect(event.push?).to be_truthy
|
2016-11-04 10:15:43 -04:00
|
|
|
expect(event.visible_to_user?(user)).to be_truthy
|
|
|
|
expect(event.visible_to_user?(nil)).to be_falsey
|
2016-10-12 02:16:29 -04:00
|
|
|
expect(event.tag?).to be_falsey
|
|
|
|
expect(event.branch_name).to eq("master")
|
|
|
|
expect(event.author).to eq(user)
|
2012-03-28 15:53:45 -04:00
|
|
|
end
|
|
|
|
end
|
2015-11-11 11:14:47 -05:00
|
|
|
|
2016-10-06 11:19:27 -04:00
|
|
|
describe '#membership_changed?' do
|
|
|
|
context "created" do
|
2017-01-29 17:37:24 -05:00
|
|
|
subject { build(:event, :created).membership_changed? }
|
2016-10-06 11:19:27 -04:00
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "updated" do
|
2017-01-29 17:37:24 -05:00
|
|
|
subject { build(:event, :updated).membership_changed? }
|
2016-10-06 11:19:27 -04:00
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "expired" do
|
2017-01-29 17:37:24 -05:00
|
|
|
subject { build(:event, :expired).membership_changed? }
|
2016-10-06 11:19:27 -04:00
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "left" do
|
2017-01-29 17:37:24 -05:00
|
|
|
subject { build(:event, :left).membership_changed? }
|
2016-10-06 11:19:27 -04:00
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "joined" do
|
2017-01-29 17:37:24 -05:00
|
|
|
subject { build(:event, :joined).membership_changed? }
|
2016-10-06 11:19:27 -04:00
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-04 10:29:28 -04:00
|
|
|
describe '#note?' do
|
2017-01-29 17:37:24 -05:00
|
|
|
subject { described_class.new(project: target.project, target: target) }
|
2016-07-04 10:29:28 -04:00
|
|
|
|
|
|
|
context 'issue note event' do
|
|
|
|
let(:target) { create(:note_on_issue) }
|
|
|
|
|
|
|
|
it { is_expected.to be_note }
|
|
|
|
end
|
|
|
|
|
2016-07-06 04:08:42 -04:00
|
|
|
context 'merge request diff note event' do
|
2016-07-07 15:57:38 -04:00
|
|
|
let(:target) { create(:legacy_diff_note_on_merge_request) }
|
2016-07-04 10:29:28 -04:00
|
|
|
|
|
|
|
it { is_expected.to be_note }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-24 13:24:22 -04:00
|
|
|
describe '#visible_to_user?' do
|
2016-03-24 13:20:47 -04:00
|
|
|
let(:project) { create(:empty_project, :public) }
|
|
|
|
let(:non_member) { create(:user) }
|
2016-10-12 02:16:29 -04:00
|
|
|
let(:member) { create(:user) }
|
|
|
|
let(:guest) { create(:user) }
|
2016-03-24 13:20:47 -04:00
|
|
|
let(:author) { create(:author) }
|
|
|
|
let(:assignee) { create(:user) }
|
|
|
|
let(:admin) { create(:admin) }
|
2017-05-04 08:11:15 -04:00
|
|
|
let(:issue) { create(:issue, project: project, author: author, assignees: [assignee]) }
|
|
|
|
let(:confidential_issue) { create(:issue, :confidential, project: project, author: author, assignees: [assignee]) }
|
2016-11-16 09:17:09 -05:00
|
|
|
let(:note_on_commit) { create(:note_on_commit, project: project) }
|
2016-03-24 13:20:47 -04:00
|
|
|
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-01-29 17:37:24 -05:00
|
|
|
let(:event) { described_class.new(project: project, target: target, author_id: author.id) }
|
2016-03-24 13:20:47 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
project.team << [member, :developer]
|
2016-06-06 15:13:31 -04:00
|
|
|
project.team << [guest, :guest]
|
2016-03-24 13:20:47 -04:00
|
|
|
end
|
2016-03-17 17:03:10 -04:00
|
|
|
|
2016-11-16 09:17:09 -05:00
|
|
|
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
|
|
|
|
let(:project) { create(:empty_project, :private) }
|
|
|
|
|
|
|
|
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-03-24 13:20:47 -04:00
|
|
|
context 'issue event' do
|
2016-03-17 17:03:10 -04:00
|
|
|
context 'for non confidential issues' do
|
2016-03-24 13:20:47 -04:00
|
|
|
let(:target) { issue }
|
2016-03-17 17:03:10 -04:00
|
|
|
|
2016-10-12 02:16:29 -04:00
|
|
|
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-03-17 17:03:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for confidential issues' do
|
2016-03-24 13:20:47 -04:00
|
|
|
let(:target) { confidential_issue }
|
|
|
|
|
2016-10-12 02:16:29 -04:00
|
|
|
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-03-24 13:20:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-04 10:29:28 -04:00
|
|
|
context 'issue note event' do
|
2016-03-24 13:20:47 -04:00
|
|
|
context 'on non confidential issues' do
|
|
|
|
let(:target) { note_on_issue }
|
|
|
|
|
2016-10-12 02:16:29 -04:00
|
|
|
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-03-24 13:20:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'on confidential issues' do
|
|
|
|
let(:target) { note_on_confidential_issue }
|
2016-03-17 17:03:10 -04:00
|
|
|
|
2016-10-12 02:16:29 -04:00
|
|
|
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-03-17 17:03:10 -04:00
|
|
|
end
|
|
|
|
end
|
2016-07-04 10:29:28 -04:00
|
|
|
|
2016-07-06 04:08:42 -04:00
|
|
|
context 'merge request diff note event' do
|
2017-01-26 17:44:58 -05:00
|
|
|
let(:project) { create(:empty_project, :public) }
|
2016-07-04 10:29:28 -04:00
|
|
|
let(:merge_request) { create(:merge_request, source_project: project, author: author, assignee: assignee) }
|
2016-07-07 15:57:38 -04:00
|
|
|
let(:note_on_merge_request) { create(:legacy_diff_note_on_merge_request, noteable: merge_request, project: project) }
|
2016-07-04 10:29:28 -04:00
|
|
|
let(:target) { note_on_merge_request }
|
|
|
|
|
2016-10-12 02:16:29 -04:00
|
|
|
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-10-04 08:52:08 -04:00
|
|
|
|
|
|
|
context 'private project' do
|
2017-01-26 17:44:58 -05:00
|
|
|
let(:project) { create(:empty_project, :private) }
|
2016-10-04 08:52:08 -04:00
|
|
|
|
2016-10-12 02:16:29 -04:00
|
|
|
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-10-04 08:52:08 -04:00
|
|
|
end
|
2016-07-04 10:29:28 -04:00
|
|
|
end
|
2016-03-17 17:03:10 -04:00
|
|
|
end
|
|
|
|
|
2015-11-18 06:25:37 -05:00
|
|
|
describe '.limit_recent' do
|
|
|
|
let!(:event1) { create(:closed_issue_event) }
|
|
|
|
let!(:event2) { create(:closed_issue_event) }
|
|
|
|
|
|
|
|
describe 'without an explicit limit' do
|
2017-01-29 17:37:24 -05:00
|
|
|
subject { described_class.limit_recent }
|
2015-11-18 06:25:37 -05:00
|
|
|
|
|
|
|
it { is_expected.to eq([event2, event1]) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with an explicit limit' do
|
2017-01-29 17:37:24 -05:00
|
|
|
subject { described_class.limit_recent(1) }
|
2015-11-18 06:25:37 -05:00
|
|
|
|
|
|
|
it { is_expected.to eq([event2]) }
|
|
|
|
end
|
|
|
|
end
|
2016-04-21 03:42:08 -04:00
|
|
|
|
2016-09-16 10:05:12 -04:00
|
|
|
describe '#reset_project_activity' do
|
|
|
|
let(:project) { create(:empty_project) }
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
expect(project).not_to receive(:update_column).
|
|
|
|
with(:last_activity_at, a_kind_of(Time))
|
|
|
|
|
2017-05-03 21:44:39 -04:00
|
|
|
create_push_event(project, project.owner)
|
2016-09-16 10:05:12 -04:00
|
|
|
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-05-03 21:44:39 -04:00
|
|
|
create_push_event(project, project.owner)
|
2016-09-16 10:05:12 -04:00
|
|
|
|
2016-10-04 11:22:58 -04:00
|
|
|
project.reload
|
2016-09-16 10:05:12 -04:00
|
|
|
|
2017-05-03 21:44:39 -04:00
|
|
|
expect(project.last_activity_at).to be_within(1.minute).of(Time.now)
|
2016-09-16 10:05:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-24 05:25:23 -05:00
|
|
|
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-05-03 21:44:39 -04:00
|
|
|
def create_push_event(project, user, attrs = {})
|
2016-04-21 03:42:08 -04:00
|
|
|
data = {
|
|
|
|
before: Gitlab::Git::BLANK_SHA,
|
|
|
|
after: "0220c11b9a3e6c69dc8fd35321254ca9a7b98f7e",
|
|
|
|
ref: "refs/heads/master",
|
|
|
|
user_id: user.id,
|
|
|
|
user_name: user.name,
|
|
|
|
repository: {
|
|
|
|
name: project.name,
|
|
|
|
url: "localhost/rubinius",
|
|
|
|
description: "",
|
|
|
|
homepage: "localhost/rubinius",
|
|
|
|
private: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 17:37:24 -05:00
|
|
|
described_class.create({
|
2016-04-21 03:42:08 -04:00
|
|
|
project: project,
|
2017-01-29 17:37:24 -05:00
|
|
|
action: described_class::PUSHED,
|
2016-04-21 03:42:08 -04:00
|
|
|
data: data,
|
|
|
|
author_id: user.id
|
2016-10-12 02:16:29 -04:00
|
|
|
}.merge!(attrs))
|
2016-04-21 03:42:08 -04:00
|
|
|
end
|
2012-02-28 08:09:23 -05:00
|
|
|
end
|