summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZ.J. van de Weg <git@zjvandeweg.nl>2016-11-17 15:30:04 +0100
committerZ.J. van de Weg <git@zjvandeweg.nl>2016-11-17 21:34:24 +0100
commit1607efa40081702488e22e560db2c1e30cd80093 (patch)
treee43388424e05c45eee6cf2ddef9f7e538327019b
parent6737ada0c8d980ed1bd8f425e885fa1b89930616 (diff)
downloadgitlab-ce-1607efa40081702488e22e560db2c1e30cd80093.tar.gz
Add tests for increased converage
-rw-r--r--lib/gitlab/chat_commands/base_command.rb8
-rw-r--r--lib/gitlab/chat_commands/command.rb16
-rw-r--r--lib/gitlab/chat_commands/issue_create.rb6
-rw-r--r--lib/gitlab/chat_commands/issue_show.rb2
-rw-r--r--lib/mattermost/presenter.rb5
-rw-r--r--spec/lib/gitlab/chat_commands/command_spec.rb22
-rw-r--r--spec/lib/gitlab/chat_commands/issue_create_spec.rb15
-rw-r--r--spec/lib/gitlab/chat_commands/issue_show_spec.rb8
8 files changed, 45 insertions, 37 deletions
diff --git a/lib/gitlab/chat_commands/base_command.rb b/lib/gitlab/chat_commands/base_command.rb
index b5d58af3588..81b15bd1f7a 100644
--- a/lib/gitlab/chat_commands/base_command.rb
+++ b/lib/gitlab/chat_commands/base_command.rb
@@ -35,14 +35,6 @@ module Gitlab
Ability.allowed?(object, action, subject)
end
- def present(resource)
- Mattermost::Presenter.present(resource)
- end
-
- def help(messages)
- Mattermost::Presenter.help(messages)
- end
-
def find_by_iid(iid)
resource = collection.find_by(iid: iid)
diff --git a/lib/gitlab/chat_commands/command.rb b/lib/gitlab/chat_commands/command.rb
index 0ed51d9b8fc..43144975901 100644
--- a/lib/gitlab/chat_commands/command.rb
+++ b/lib/gitlab/chat_commands/command.rb
@@ -9,9 +9,11 @@ module Gitlab
def execute
klass, match = fetch_klass
- return help(help_messages, params[:command]) unless klass.try(:available?, project)
-
- klass.new(project, current_user, params).execute(match)
+ if klass
+ present klass.new(project, current_user, params).execute(match)
+ else
+ help(help_messages)
+ end
end
private
@@ -40,6 +42,14 @@ module Gitlab
def command
params[:text]
end
+
+ def present(resource)
+ Mattermost::Presenter.present(resource)
+ end
+
+ def help(messages)
+ Mattermost::Presenter.help(messages, params[:command])
+ end
end
end
end
diff --git a/lib/gitlab/chat_commands/issue_create.rb b/lib/gitlab/chat_commands/issue_create.rb
index 0e2b4c0e9cd..1e311e09771 100644
--- a/lib/gitlab/chat_commands/issue_create.rb
+++ b/lib/gitlab/chat_commands/issue_create.rb
@@ -1,6 +1,6 @@
module Gitlab
module ChatCommands
- class IssueCreate < IssueCommand
+ class IssueCreate < IssueCommand
def self.match(text)
/\Aissue\s+create\s+(?<title>[^\n]*)\n*(?<description>.*)\z/.match(text)
end
@@ -10,12 +10,12 @@ module Gitlab
end
def execute(match)
- present nil unless can?(current_user, :create_issue, project)
+ return nil unless can?(current_user, :create_issue, project)
title = match[:title]
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
diff --git a/lib/gitlab/chat_commands/issue_show.rb b/lib/gitlab/chat_commands/issue_show.rb
index e5530df31cc..f5bceb038e5 100644
--- a/lib/gitlab/chat_commands/issue_show.rb
+++ b/lib/gitlab/chat_commands/issue_show.rb
@@ -10,7 +10,7 @@ module Gitlab
end
def execute(match)
- present find_by_iid(match[:iid])
+ find_by_iid(match[:iid])
end
end
end
diff --git a/lib/mattermost/presenter.rb b/lib/mattermost/presenter.rb
index b3d6c025109..84b7b8edd9e 100644
--- a/lib/mattermost/presenter.rb
+++ b/lib/mattermost/presenter.rb
@@ -4,19 +4,20 @@ module Mattermost
include Rails.application.routes.url_helpers
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)
end
def help(messages, command)
+ return ephemeral_response("No commands configured") unless messages.count > 1
message = ["Available commands:"]
messages.each do |messsage|
message << "- #{command} #{message}"
end
- ephemeral_response(messages.join("\n"))
+ ephemeral_response(message.join("\n"))
end
def not_found
diff --git a/spec/lib/gitlab/chat_commands/command_spec.rb b/spec/lib/gitlab/chat_commands/command_spec.rb
index bbd47f45761..328187b5048 100644
--- a/spec/lib/gitlab/chat_commands/command_spec.rb
+++ b/spec/lib/gitlab/chat_commands/command_spec.rb
@@ -1,19 +1,19 @@
require 'spec_helper'
describe Gitlab::ChatCommands::Command, service: true do
- let(:project) { create(:project) }
- let(:user) { create(:user) }
- let(:params) { { text: 'issue show 1' } }
+ let(:project) { create(:project) }
+ let(:user) { create(:user) }
subject { described_class.new(project, user, params).execute }
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) }
it 'displays the help message' do
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
@@ -25,5 +25,17 @@ describe Gitlab::ChatCommands::Command, service: true do
expect(subject[:text]).to start_with('Available commands')
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
diff --git a/spec/lib/gitlab/chat_commands/issue_create_spec.rb b/spec/lib/gitlab/chat_commands/issue_create_spec.rb
index 5f5cc706c96..4831f24efed 100644
--- a/spec/lib/gitlab/chat_commands/issue_create_spec.rb
+++ b/spec/lib/gitlab/chat_commands/issue_create_spec.rb
@@ -2,8 +2,8 @@ require 'spec_helper'
describe Gitlab::ChatCommands::IssueCreate, service: true do
describe '#execute' do
- let(:project) { create(:empty_project) }
- let(:user) { create(:user) }
+ let(:project) { create(:empty_project) }
+ let(:user) { create(:user) }
let(:regex_match) { described_class.match("issue create bird is the word") }
before do
@@ -16,12 +16,9 @@ describe Gitlab::ChatCommands::IssueCreate, service: true do
context 'without description' do
it 'creates the issue' do
- expect do
- subject # this trigger the execution
- end.to change { project.issues.count }.by(1)
+ expect { subject }.to change { project.issues.count }.by(1)
- expect(subject[:response_type]).to be :in_channel
- expect(subject[:text]).to match('bird is the word')
+ expect(subject.title).to eq('bird is the word')
end
end
@@ -29,11 +26,9 @@ describe Gitlab::ChatCommands::IssueCreate, service: true do
let(:description) { "Surfin bird" }
let(:regex_match) { described_class.match("issue create bird is the word\n#{description}") }
- before do
+ it 'creates the issue with description' do
subject
- end
- it 'creates the issue with description' do
expect(Issue.last.description).to eq(description)
end
end
diff --git a/spec/lib/gitlab/chat_commands/issue_show_spec.rb b/spec/lib/gitlab/chat_commands/issue_show_spec.rb
index d7824dd6bf5..331a4604e9b 100644
--- a/spec/lib/gitlab/chat_commands/issue_show_spec.rb
+++ b/spec/lib/gitlab/chat_commands/issue_show_spec.rb
@@ -17,17 +17,15 @@ describe Gitlab::ChatCommands::IssueShow, service: true do
context 'the issue exists' do
it 'returns the issue' do
- expect(subject[:response_type]).to be(:in_channel)
- expect(subject[:text]).to match(issue.title)
+ expect(subject.iid).to be issue.iid
end
end
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
- expect(subject[:response_type]).to be(:ephemeral)
- expect(subject[:text]).to start_with('404 not found!')
+ expect(subject).to be_nil
end
end
end