2019-07-07 11:18:12 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
RSpec.describe Integrations::MattermostSlashCommands do
|
|
|
|
it_behaves_like Integrations::BaseSlashCommands
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
context 'Mattermost API' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:project) { create(:project) }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:service) { project.build_mattermost_slash_commands_service }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
2021-09-04 01:27:46 +05:30
|
|
|
session = ::Mattermost::Session.new(nil)
|
2018-03-26 14:24:53 +05:30
|
|
|
session.base_uri = 'http://mattermost.example.com'
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
allow_any_instance_of(::Mattermost::Client).to receive(:with_session)
|
2018-03-26 14:24:53 +05:30
|
|
|
.and_yield(session)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#configure' do
|
|
|
|
subject do
|
|
|
|
service.configure(user, team_id: 'abc',
|
|
|
|
trigger: 'gitlab', url: 'http://trigger.url',
|
|
|
|
icon_url: 'http://icon.url/icon.png')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'the requests succeeds' do
|
|
|
|
before do
|
2018-11-08 19:23:39 +05:30
|
|
|
stub_request(:post, 'http://mattermost.example.com/api/v4/commands')
|
2017-09-10 17:25:29 +05:30
|
|
|
.with(body: {
|
2017-08-17 22:00:37 +05:30
|
|
|
team_id: 'abc',
|
|
|
|
trigger: 'gitlab',
|
|
|
|
url: 'http://trigger.url',
|
|
|
|
icon_url: 'http://icon.url/icon.png',
|
|
|
|
auto_complete: true,
|
2018-03-27 19:54:05 +05:30
|
|
|
auto_complete_desc: "Perform common operations on: #{project.full_name}",
|
2017-08-17 22:00:37 +05:30
|
|
|
auto_complete_hint: '[help]',
|
2018-03-27 19:54:05 +05:30
|
|
|
description: "Perform common operations on: #{project.full_name}",
|
|
|
|
display_name: "GitLab / #{project.full_name}",
|
2017-08-17 22:00:37 +05:30
|
|
|
method: 'P',
|
|
|
|
username: 'GitLab'
|
2017-09-10 17:25:29 +05:30
|
|
|
}.to_json)
|
|
|
|
.to_return(
|
2017-08-17 22:00:37 +05:30
|
|
|
status: 200,
|
|
|
|
headers: { 'Content-Type' => 'application/json' },
|
|
|
|
body: { token: 'token' }.to_json
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves the service' do
|
2021-06-08 01:23:25 +05:30
|
|
|
expect { subject }.to change { project.integrations.count }.by(1)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves the token' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(service.reload.token).to eq('token')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'an error is received' do
|
|
|
|
before do
|
2018-11-08 19:23:39 +05:30
|
|
|
stub_request(:post, 'http://mattermost.example.com/api/v4/commands')
|
2017-09-10 17:25:29 +05:30
|
|
|
.to_return(
|
2017-08-17 22:00:37 +05:30
|
|
|
status: 500,
|
|
|
|
headers: { 'Content-Type' => 'application/json' },
|
|
|
|
body: {
|
|
|
|
id: 'api.command.duplicate_trigger.app_error',
|
|
|
|
message: 'This trigger word is already in use. Please choose another word.',
|
|
|
|
detailed_error: '',
|
|
|
|
request_id: 'obc374man7bx5r3dbc1q5qhf3r',
|
|
|
|
status_code: 500
|
|
|
|
}.to_json
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'shows error messages' do
|
|
|
|
succeeded, message = subject
|
|
|
|
|
|
|
|
expect(succeeded).to be(false)
|
|
|
|
expect(message).to eq('This trigger word is already in use. Please choose another word.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#list_teams' do
|
|
|
|
subject do
|
|
|
|
service.list_teams(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'the requests succeeds' do
|
|
|
|
before do
|
2018-11-08 19:23:39 +05:30
|
|
|
stub_request(:get, 'http://mattermost.example.com/api/v4/users/me/teams')
|
2017-09-10 17:25:29 +05:30
|
|
|
.to_return(
|
2017-08-17 22:00:37 +05:30
|
|
|
status: 200,
|
|
|
|
headers: { 'Content-Type' => 'application/json' },
|
2018-11-08 19:23:39 +05:30
|
|
|
body: [{ id: 'test_team_id' }].to_json
|
2017-08-17 22:00:37 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a list of teams' do
|
|
|
|
expect(subject).not_to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'an error is received' do
|
|
|
|
before do
|
2018-11-08 19:23:39 +05:30
|
|
|
stub_request(:get, 'http://mattermost.example.com/api/v4/users/me/teams')
|
2017-09-10 17:25:29 +05:30
|
|
|
.to_return(
|
2017-08-17 22:00:37 +05:30
|
|
|
status: 500,
|
|
|
|
headers: { 'Content-Type' => 'application/json' },
|
|
|
|
body: {
|
|
|
|
message: 'Failed to get team list.'
|
|
|
|
}.to_json
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'shows error messages' do
|
|
|
|
expect(subject).to eq([[], "Failed to get team list."])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
describe '#chat_responder' do
|
|
|
|
it 'returns the responder to use for Mattermost' do
|
|
|
|
expect(described_class.new.chat_responder)
|
|
|
|
.to eq(Gitlab::Chat::Responder::Mattermost)
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|