require 'spec_helper' describe SlashCommands::InterpretService, services: true do let(:project) { create(:project) } let(:user) { create(:user) } let(:issue) { create(:issue, project: project) } let(:milestone) { create(:milestone, project: project, title: '9.10') } let(:inprogress) { create(:label, project: project, title: 'In Progress') } let(:bug) { create(:label, project: project, title: 'Bug') } before do project.team << [user, :developer] end describe '#execute' do let(:service) { described_class.new(project, user) } let(:merge_request) { create(:merge_request, source_project: project) } shared_examples 'open command' do it 'returns state_event: "open" if content contains /open' do issuable.close! _, updates = service.execute(content, issuable) expect(updates).to eq(state_event: 'reopen') end end shared_examples 'close command' do it 'returns state_event: "close" if content contains /open' do _, updates = service.execute(content, issuable) expect(updates).to eq(state_event: 'close') end end shared_examples 'title command' do it 'populates title: "A brand new title" if content contains /title A brand new title' do _, updates = service.execute(content, issuable) expect(updates).to eq(title: 'A brand new title') end end shared_examples 'assign command' do it 'fetches assignee and populates assignee_id if content contains /assign' do _, updates = service.execute(content, issuable) expect(updates).to eq(assignee_id: user.id) end end shared_examples 'unassign command' do it 'populates assignee_id: nil if content contains /unassign' do issuable.update(assignee_id: user.id) _, updates = service.execute(content, issuable) expect(updates).to eq(assignee_id: nil) end end shared_examples 'milestone command' do it 'fetches milestone and populates milestone_id if content contains /milestone' do milestone # populate the milestone _, updates = service.execute(content, issuable) expect(updates).to eq(milestone_id: milestone.id) end end shared_examples 'clear_milestone command' do it 'populates milestone_id: nil if content contains /clear_milestone' do issuable.update(milestone_id: milestone.id) _, updates = service.execute(content, issuable) expect(updates).to eq(milestone_id: nil) end end shared_examples 'label command' do it 'fetches label ids and populates add_label_ids if content contains /label' do bug # populate the label inprogress # populate the label _, updates = service.execute(content, issuable) expect(updates).to eq(add_label_ids: [bug.id, inprogress.id]) end end shared_examples 'unlabel command' do it 'fetches label ids and populates remove_label_ids if content contains /unlabel' do issuable.update(label_ids: [inprogress.id]) # populate the label _, updates = service.execute(content, issuable) expect(updates).to eq(remove_label_ids: [inprogress.id]) end end shared_examples 'clear_labels command' do it 'populates label_ids: [] if content contains /clear_labels' do issuable.update(label_ids: [inprogress.id]) # populate the label _, updates = service.execute(content, issuable) expect(updates).to eq(label_ids: []) end end shared_examples 'todo command' do it 'populates todo_event: "add" if content contains /todo' do _, updates = service.execute(content, issuable) expect(updates).to eq(todo_event: 'add') end end shared_examples 'done command' do it 'populates todo_event: "done" if content contains /done' do TodoService.new.mark_todo(issuable, user) _, updates = service.execute(content, issuable) expect(updates).to eq(todo_event: 'done') end end shared_examples 'subscribe command' do it 'populates subscription_event: "subscribe" if content contains /subscribe' do _, updates = service.execute(content, issuable) expect(updates).to eq(subscription_event: 'subscribe') end end shared_examples 'unsubscribe command' do it 'populates subscription_event: "unsubscribe" if content contains /unsubscribe' do issuable.subscribe(user) _, updates = service.execute(content, issuable) expect(updates).to eq(subscription_event: 'unsubscribe') end end shared_examples 'due_date command' do it 'populates due_date: Date.new(2016, 8, 28) if content contains /due_date 2016-08-28' do _, updates = service.execute(content, issuable) expect(updates).to eq(due_date: defined?(expected_date) ? expected_date : Date.new(2016, 8, 28)) end end shared_examples 'clear_due_date command' do it 'populates due_date: nil if content contains /clear_due_date' do issuable.update(due_date: Date.today) _, updates = service.execute(content, issuable) expect(updates).to eq(due_date: nil) end end shared_examples 'empty command' do it 'populates {} if content contains an unsupported command' do _, updates = service.execute(content, issuable) expect(updates).to be_empty end end it_behaves_like 'open command' do let(:content) { '/open' } let(:issuable) { issue } end it_behaves_like 'open command' do let(:content) { '/open' } let(:issuable) { merge_request } end it_behaves_like 'open command' do let(:content) { '/reopen' } let(:issuable) { issue } end it_behaves_like 'close command' do let(:content) { '/close' } let(:issuable) { issue } end it_behaves_like 'close command' do let(:content) { '/close' } let(:issuable) { merge_request } end it_behaves_like 'title command' do let(:content) { '/title A brand new title' } let(:issuable) { issue } end it_behaves_like 'title command' do let(:content) { '/title A brand new title' } let(:issuable) { merge_request } end it_behaves_like 'empty command' do let(:content) { '/title' } let(:issuable) { issue } end it_behaves_like 'assign command' do let(:content) { "/assign @#{user.username}" } let(:issuable) { issue } end it_behaves_like 'assign command' do let(:content) { "/assign @#{user.username}" } let(:issuable) { merge_request } end it_behaves_like 'empty command' do let(:content) { '/assign @abcd1234' } let(:issuable) { issue } end it_behaves_like 'empty command' do let(:content) { '/assign' } let(:issuable) { issue } end it_behaves_like 'unassign command' do let(:content) { '/unassign' } let(:issuable) { issue } end it_behaves_like 'unassign command' do let(:content) { '/unassign' } let(:issuable) { merge_request } end it_behaves_like 'unassign command' do let(:content) { '/remove_assignee' } let(:issuable) { issue } end it_behaves_like 'milestone command' do let(:content) { "/milestone %#{milestone.title}" } let(:issuable) { issue } end it_behaves_like 'milestone command' do let(:content) { "/milestone %#{milestone.title}" } let(:issuable) { merge_request } end it_behaves_like 'clear_milestone command' do let(:content) { '/clear_milestone' } let(:issuable) { issue } end it_behaves_like 'clear_milestone command' do let(:content) { '/clear_milestone' } let(:issuable) { merge_request } end it_behaves_like 'clear_milestone command' do let(:content) { '/remove_milestone' } let(:issuable) { issue } end it_behaves_like 'label command' do let(:content) { %(/label ~"#{inprogress.title}" ~#{bug.title} ~unknown) } let(:issuable) { issue } end it_behaves_like 'label command' do let(:content) { %(/label ~"#{inprogress.title}" ~#{bug.title} ~unknown) } let(:issuable) { merge_request } end it_behaves_like 'label command' do let(:content) { %(/labels ~"#{inprogress.title}" ~#{bug.title} ~unknown) } let(:issuable) { issue } end it_behaves_like 'unlabel command' do let(:content) { %(/unlabel ~"#{inprogress.title}") } let(:issuable) { issue } end it_behaves_like 'unlabel command' do let(:content) { %(/unlabel ~"#{inprogress.title}") } let(:issuable) { merge_request } end it_behaves_like 'unlabel command' do let(:content) { %(/remove_labels ~"#{inprogress.title}") } let(:issuable) { issue } end it_behaves_like 'unlabel command' do let(:content) { %(/remove_label ~"#{inprogress.title}") } let(:issuable) { issue } end it_behaves_like 'clear_labels command' do let(:content) { '/clear_labels' } let(:issuable) { issue } end it_behaves_like 'clear_labels command' do let(:content) { '/clear_labels' } let(:issuable) { merge_request } end it_behaves_like 'clear_labels command' do let(:content) { '/clear_label' } let(:issuable) { issue } end it_behaves_like 'todo command' do let(:content) { '/todo' } let(:issuable) { issue } end it_behaves_like 'todo command' do let(:content) { '/todo' } let(:issuable) { merge_request } end it_behaves_like 'done command' do let(:content) { '/done' } let(:issuable) { issue } end it_behaves_like 'done command' do let(:content) { '/done' } let(:issuable) { merge_request } end it_behaves_like 'subscribe command' do let(:content) { '/subscribe' } let(:issuable) { issue } end it_behaves_like 'subscribe command' do let(:content) { '/subscribe' } let(:issuable) { merge_request } end it_behaves_like 'unsubscribe command' do let(:content) { '/unsubscribe' } let(:issuable) { issue } end it_behaves_like 'unsubscribe command' do let(:content) { '/unsubscribe' } let(:issuable) { merge_request } end it_behaves_like 'due_date command' do let(:content) { '/due_date 2016-08-28' } let(:issuable) { issue } end it_behaves_like 'due_date command' do let(:content) { '/due tomorrow' } let(:issuable) { issue } let(:expected_date) { Date.tomorrow } end it_behaves_like 'due_date command' do let(:content) { '/due 5 days from now' } let(:issuable) { issue } let(:expected_date) { 5.days.from_now.to_date } end it_behaves_like 'due_date command' do let(:content) { '/due in 2 days' } let(:issuable) { issue } let(:expected_date) { 2.days.from_now.to_date } end it_behaves_like 'empty command' do let(:content) { '/due_date foo bar' } let(:issuable) { issue } end it_behaves_like 'empty command' do let(:content) { '/due_date 2016-08-28' } let(:issuable) { merge_request } end it_behaves_like 'clear_due_date command' do let(:content) { '/clear_due_date' } let(:issuable) { issue } end it_behaves_like 'empty command' do let(:content) { '/clear_due_date' } let(:issuable) { merge_request } end end end