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
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)
......
......@@ -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
......@@ -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
......
......@@ -10,7 +10,7 @@ module Gitlab
end
def execute(match)
present find_by_iid(match[:iid])
find_by_iid(match[:iid])
end
end
end
......
......@@ -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
......
......@@ -3,17 +3,17 @@ require 'spec_helper'
describe Gitlab::ChatCommands::Command, service: true do
let(:project) { create(:project) }
let(:user) { create(:user) }
let(:params) { { text: 'issue show 1' } }
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
......@@ -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
......
......@@ -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
......
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