Commit 1607efa4 authored by Z.J. van de Weg's avatar Z.J. van de Weg

Add tests for increased converage

parent 6737ada0
...@@ -35,14 +35,6 @@ module Gitlab ...@@ -35,14 +35,6 @@ module Gitlab
Ability.allowed?(object, action, subject) Ability.allowed?(object, action, subject)
end end
def present(resource)
Mattermost::Presenter.present(resource)
end
def help(messages)
Mattermost::Presenter.help(messages)
end
def find_by_iid(iid) def find_by_iid(iid)
resource = collection.find_by(iid: iid) resource = collection.find_by(iid: iid)
......
...@@ -9,9 +9,11 @@ module Gitlab ...@@ -9,9 +9,11 @@ module Gitlab
def execute def execute
klass, match = fetch_klass klass, match = fetch_klass
return help(help_messages, params[:command]) unless klass.try(:available?, project) if klass
present klass.new(project, current_user, params).execute(match)
klass.new(project, current_user, params).execute(match) else
help(help_messages)
end
end end
private private
...@@ -40,6 +42,14 @@ module Gitlab ...@@ -40,6 +42,14 @@ module Gitlab
def command def command
params[:text] params[:text]
end end
def present(resource)
Mattermost::Presenter.present(resource)
end
def help(messages)
Mattermost::Presenter.help(messages, params[:command])
end
end end
end end
end end
module Gitlab module Gitlab
module ChatCommands module ChatCommands
class IssueCreate < IssueCommand class IssueCreate < IssueCommand
def self.match(text) def self.match(text)
/\Aissue\s+create\s+(?<title>[^\n]*)\n*(?<description>.*)\z/.match(text) /\Aissue\s+create\s+(?<title>[^\n]*)\n*(?<description>.*)\z/.match(text)
end end
...@@ -10,12 +10,12 @@ module Gitlab ...@@ -10,12 +10,12 @@ module Gitlab
end end
def execute(match) def execute(match)
present nil unless can?(current_user, :create_issue, project) return nil unless can?(current_user, :create_issue, project)
title = match[:title] title = match[:title]
description = match[:description] description = match[:description]
present Issues::CreateService.new(project, current_user, title: title, description: description).execute Issues::CreateService.new(project, current_user, title: title, description: description).execute
end end
end end
end end
......
...@@ -10,7 +10,7 @@ module Gitlab ...@@ -10,7 +10,7 @@ module Gitlab
end end
def execute(match) def execute(match)
present find_by_iid(match[:iid]) find_by_iid(match[:iid])
end end
end end
end end
......
...@@ -4,19 +4,20 @@ module Mattermost ...@@ -4,19 +4,20 @@ module Mattermost
include Rails.application.routes.url_helpers include Rails.application.routes.url_helpers
def authorize_chat_name(url) def authorize_chat_name(url)
message = "Hi there! We've yet to get acquainted! Please [introduce yourself](#{url})!" message = "Hi there! We've yet to get acquainted! Please introduce yourself by [connection your GitLab profile](#{url})!"
ephemeral_response(message) ephemeral_response(message)
end end
def help(messages, command) def help(messages, command)
return ephemeral_response("No commands configured") unless messages.count > 1
message = ["Available commands:"] message = ["Available commands:"]
messages.each do |messsage| messages.each do |messsage|
message << "- #{command} #{message}" message << "- #{command} #{message}"
end end
ephemeral_response(messages.join("\n")) ephemeral_response(message.join("\n"))
end end
def not_found def not_found
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::ChatCommands::Command, service: true do describe Gitlab::ChatCommands::Command, service: true do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:params) { { text: 'issue show 1' } }
subject { described_class.new(project, user, params).execute } subject { described_class.new(project, user, params).execute }
describe '#execute' do describe '#execute' do
context 'when the command is not available' do context 'when no command is not available' do
let(:params) { { text: 'issue show 1' } }
let(:project) { create(:project, has_external_issue_tracker: true) } let(:project) { create(:project, has_external_issue_tracker: true) }
it 'displays the help message' do it 'displays the help message' do
expect(subject[:response_type]).to be(:ephemeral) expect(subject[:response_type]).to be(:ephemeral)
expect(subject[:text]).to start_with('Available commands') expect(subject[:text]).to start_with('404 not found')
end end
end end
...@@ -25,5 +25,17 @@ describe Gitlab::ChatCommands::Command, service: true do ...@@ -25,5 +25,17 @@ describe Gitlab::ChatCommands::Command, service: true do
expect(subject[:text]).to start_with('Available commands') expect(subject[:text]).to start_with('Available commands')
end end
end end
context 'issue is succesfully created' do
let(:params) { { text: "issue create my new issue" } }
before do
project.team << [user, :master]
end
it 'presents the issue' do
expect(subject[:text]).to match("my new issue")
end
end
end end
end end
...@@ -2,8 +2,8 @@ require 'spec_helper' ...@@ -2,8 +2,8 @@ require 'spec_helper'
describe Gitlab::ChatCommands::IssueCreate, service: true do describe Gitlab::ChatCommands::IssueCreate, service: true do
describe '#execute' do describe '#execute' do
let(:project) { create(:empty_project) } let(:project) { create(:empty_project) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:regex_match) { described_class.match("issue create bird is the word") } let(:regex_match) { described_class.match("issue create bird is the word") }
before do before do
...@@ -16,12 +16,9 @@ describe Gitlab::ChatCommands::IssueCreate, service: true do ...@@ -16,12 +16,9 @@ describe Gitlab::ChatCommands::IssueCreate, service: true do
context 'without description' do context 'without description' do
it 'creates the issue' do it 'creates the issue' do
expect do expect { subject }.to change { project.issues.count }.by(1)
subject # this trigger the execution
end.to change { project.issues.count }.by(1)
expect(subject[:response_type]).to be :in_channel expect(subject.title).to eq('bird is the word')
expect(subject[:text]).to match('bird is the word')
end end
end end
...@@ -29,11 +26,9 @@ describe Gitlab::ChatCommands::IssueCreate, service: true do ...@@ -29,11 +26,9 @@ describe Gitlab::ChatCommands::IssueCreate, service: true do
let(:description) { "Surfin bird" } let(:description) { "Surfin bird" }
let(:regex_match) { described_class.match("issue create bird is the word\n#{description}") } let(:regex_match) { described_class.match("issue create bird is the word\n#{description}") }
before do it 'creates the issue with description' do
subject subject
end
it 'creates the issue with description' do
expect(Issue.last.description).to eq(description) expect(Issue.last.description).to eq(description)
end end
end end
......
...@@ -17,17 +17,15 @@ describe Gitlab::ChatCommands::IssueShow, service: true do ...@@ -17,17 +17,15 @@ describe Gitlab::ChatCommands::IssueShow, service: true do
context 'the issue exists' do context 'the issue exists' do
it 'returns the issue' do it 'returns the issue' do
expect(subject[:response_type]).to be(:in_channel) expect(subject.iid).to be issue.iid
expect(subject[:text]).to match(issue.title)
end end
end end
context 'the issue does not exist' do context 'the issue does not exist' do
let(:regex_match) { described_class.match("issue show 1234") } let(:regex_match) { described_class.match("issue show 2343242") }
it "returns nil" do it "returns nil" do
expect(subject[:response_type]).to be(:ephemeral) expect(subject).to be_nil
expect(subject[:text]).to start_with('404 not found!')
end end
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