Commit 3ebdab56 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Check permissions to create a note slack command

Every Slack Slash command should have a `self.allowed?` method
defined to check the permissions to perform an action
parent ee77d3df
...@@ -11,6 +11,10 @@ module Gitlab ...@@ -11,6 +11,10 @@ module Gitlab
'issue comment <id> *`⇧ Shift`*+*`↵ Enter`* <comment>' 'issue comment <id> *`⇧ Shift`*+*`↵ Enter`* <comment>'
end end
def self.allowed?(issue, user)
can?(user, :create_note, issue)
end
def execute(match) def execute(match)
note_body = match[:note_body].to_s.strip note_body = match[:note_body].to_s.strip
issue = find_by_iid(match[:iid]) issue = find_by_iid(match[:iid])
......
...@@ -33,7 +33,7 @@ module Gitlab ...@@ -33,7 +33,7 @@ module Gitlab
def fields def fields
[ [
{ {
title: "Comment", title: 'Comment',
value: resource.note value: resource.note
} }
] ]
......
...@@ -115,5 +115,10 @@ describe Gitlab::SlashCommands::Command do ...@@ -115,5 +115,10 @@ describe Gitlab::SlashCommands::Command do
let(:params) { { text: 'issue move #78291 to gitlab/gitlab-ci' } } let(:params) { { text: 'issue move #78291 to gitlab/gitlab-ci' } }
it { is_expected.to eq(Gitlab::SlashCommands::IssueMove) } it { is_expected.to eq(Gitlab::SlashCommands::IssueMove) }
end end
context 'IssueComment is triggered' do
let(:params) { { text: "issue comment #503\ncomment body" } }
it { is_expected.to eq(Gitlab::SlashCommands::IssueComment) }
end
end end
end end
...@@ -3,17 +3,18 @@ ...@@ -3,17 +3,18 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::SlashCommands::IssueComment do describe Gitlab::SlashCommands::IssueComment do
let_it_be(:project) { create(:project) }
let_it_be(:issue) { create(:issue, project: project) }
let(:user) { issue.author }
describe '#execute' do describe '#execute' do
let_it_be(:project) { create(:project) }
let_it_be(:issue) { create(:issue, project: project) }
let(:user) { issue.author }
let(:chat_name) { double(:chat_name, user: user) } let(:chat_name) { double(:chat_name, user: user) }
let(:regex_match) { described_class.match("issue comment #{issue.iid}\nComment body") } let(:regex_match) { described_class.match("issue comment #{issue.iid}\nComment body") }
subject { described_class.new(project, chat_name).execute(regex_match) } subject { described_class.new(project, chat_name).execute(regex_match) }
context 'when the issue exists' do context 'when the issue exists' do
context 'when the user does not have premission' do context 'when the user does not have permission' do
let(:chat_name) { double(:chat_name, user: create(:user)) } let(:chat_name) { double(:chat_name, user: create(:user)) }
it 'does not allow the user to comment' do it 'does not allow the user to comment' do
...@@ -54,7 +55,7 @@ describe Gitlab::SlashCommands::IssueComment do ...@@ -54,7 +55,7 @@ describe Gitlab::SlashCommands::IssueComment do
context 'the issue does not exist' do context 'the issue does not exist' do
let(:regex_match) { described_class.match("issue comment 2343242\nComment body") } let(:regex_match) { described_class.match("issue comment 2343242\nComment body") }
it "returns not found" do it 'returns not found' do
expect(subject[:response_type]).to be(:ephemeral) expect(subject[:response_type]).to be(:ephemeral)
expect(subject[:text]).to match('not found') expect(subject[:text]).to match('not found')
end end
...@@ -94,4 +95,24 @@ describe Gitlab::SlashCommands::IssueComment do ...@@ -94,4 +95,24 @@ describe Gitlab::SlashCommands::IssueComment do
end end
end end
end end
describe '.allowed?' do
subject { described_class.allowed?(issue, user) }
before do
allow(Ability).to receive(:allowed?).with(user, :create_note, issue).and_return(is_allowed)
end
context 'when the user can create a note' do
let(:is_allowed) { true }
it { is_expected.to be_truthy }
end
context 'when the user cannot create a note' do
let(:is_allowed) { false }
it { is_expected.to be_falsey }
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment