2020-05-22 08:08:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe ResourceEvents::ChangeStateService do
|
2020-05-22 08:08:15 -04:00
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
|
|
|
let(:issue) { create(:issue, project: project) }
|
|
|
|
let(:merge_request) { create(:merge_request, source_project: project) }
|
2020-07-15 08:09:26 -04:00
|
|
|
let(:source_commit) { create(:commit, project: project) }
|
|
|
|
let(:source_merge_request) { create(:merge_request, source_project: project, target_project: project, target_branch: 'foo') }
|
2020-05-22 08:08:15 -04:00
|
|
|
|
2020-07-15 08:09:26 -04:00
|
|
|
shared_examples 'a state event' do
|
|
|
|
%w[opened reopened closed locked].each do |state|
|
|
|
|
it "creates the expected event if resource has #{state} state" do
|
|
|
|
described_class.new(user: user, resource: resource).execute(status: state, mentionable_source: source)
|
|
|
|
|
|
|
|
event = resource.resource_state_events.last
|
2020-05-22 08:08:15 -04:00
|
|
|
|
2020-07-15 08:09:26 -04:00
|
|
|
if resource.is_a?(Issue)
|
|
|
|
expect(event.issue).to eq(resource)
|
2020-05-22 08:08:15 -04:00
|
|
|
expect(event.merge_request).to be_nil
|
2020-07-15 08:09:26 -04:00
|
|
|
elsif resource.is_a?(MergeRequest)
|
|
|
|
expect(event.issue).to be_nil
|
|
|
|
expect(event.merge_request).to eq(resource)
|
2020-05-22 08:08:15 -04:00
|
|
|
end
|
2020-07-15 08:09:26 -04:00
|
|
|
|
|
|
|
expect(event.state).to eq(state)
|
|
|
|
|
|
|
|
expect_event_source(event, source)
|
2020-05-22 08:08:15 -04:00
|
|
|
end
|
2021-01-12 16:10:47 -05:00
|
|
|
|
|
|
|
it "sets the created_at timestamp from the system_note_timestamp" do
|
|
|
|
resource.system_note_timestamp = Time.at(43).utc
|
|
|
|
|
|
|
|
described_class.new(user: user, resource: resource).execute(status: state, mentionable_source: source)
|
|
|
|
event = resource.resource_state_events.last
|
|
|
|
|
|
|
|
expect(event.created_at).to eq(Time.at(43).utc)
|
|
|
|
end
|
2020-05-22 08:08:15 -04:00
|
|
|
end
|
2020-07-15 08:09:26 -04:00
|
|
|
end
|
2020-05-22 08:08:15 -04:00
|
|
|
|
2020-07-15 08:09:26 -04:00
|
|
|
describe '#execute' do
|
|
|
|
context 'when resource is an Issue' do
|
|
|
|
context 'when no source is given' do
|
|
|
|
it_behaves_like 'a state event' do
|
|
|
|
let(:resource) { issue }
|
|
|
|
let(:source) { nil }
|
|
|
|
end
|
|
|
|
end
|
2020-05-22 08:08:15 -04:00
|
|
|
|
2020-07-15 08:09:26 -04:00
|
|
|
context 'when source commit is given' do
|
|
|
|
it_behaves_like 'a state event' do
|
|
|
|
let(:resource) { issue }
|
|
|
|
let(:source) { source_commit }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when source merge request is given' do
|
|
|
|
it_behaves_like 'a state event' do
|
|
|
|
let(:resource) { issue }
|
|
|
|
let(:source) { source_merge_request }
|
2020-05-22 08:08:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-07-15 08:09:26 -04:00
|
|
|
|
|
|
|
context 'when resource is a MergeRequest' do
|
|
|
|
context 'when no source is given' do
|
|
|
|
it_behaves_like 'a state event' do
|
|
|
|
let(:resource) { merge_request }
|
|
|
|
let(:source) { nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when source commit is given' do
|
|
|
|
it_behaves_like 'a state event' do
|
|
|
|
let(:resource) { merge_request }
|
|
|
|
let(:source) { source_commit }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when source merge request is given' do
|
|
|
|
it_behaves_like 'a state event' do
|
|
|
|
let(:resource) { merge_request }
|
|
|
|
let(:source) { source_merge_request }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def expect_event_source(event, source)
|
|
|
|
if source.is_a?(MergeRequest)
|
|
|
|
expect(event.source_commit).to be_nil
|
|
|
|
expect(event.source_merge_request).to eq(source)
|
|
|
|
elsif source.is_a?(Commit)
|
|
|
|
expect(event.source_commit).to eq(source.id)
|
|
|
|
expect(event.source_merge_request).to be_nil
|
|
|
|
else
|
|
|
|
expect(event.source_merge_request).to be_nil
|
|
|
|
expect(event.source_commit).to be_nil
|
|
|
|
end
|
2020-05-22 08:08:15 -04:00
|
|
|
end
|
|
|
|
end
|