debian-mirror-gitlab/spec/models/integrations/mattermost_slash_commands_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

135 lines
4.1 KiB
Ruby
Raw Normal View History

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
2021-09-30 23:02:18 +05:30
describe 'Mattermost API' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project) }
2021-09-30 23:02:18 +05:30
let(:integration) { project.build_mattermost_slash_commands_integration }
2017-08-17 22:00:37 +05:30
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-30 23:02:18 +05:30
allow(session).to receive(:with_session).and_yield(session)
allow(::Mattermost::Session).to receive(:new).and_return(session)
2017-08-17 22:00:37 +05:30
end
describe '#configure' do
subject do
2021-09-30 23:02:18 +05:30
integration.configure(user,
team_id: 'abc',
trigger: 'gitlab',
url: 'http://trigger.url',
icon_url: 'http://icon.url/icon.png')
2017-08-17 22:00:37 +05:30
end
2021-09-30 23:02:18 +05:30
context 'when the request succeeds' do
2017-08-17 22:00:37 +05:30
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
2021-09-30 23:02:18 +05:30
it 'saves the integration' 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
2021-09-30 23:02:18 +05:30
expect(integration.reload.token).to eq('token')
2017-08-17 22:00:37 +05:30
end
end
2021-09-30 23:02:18 +05:30
context 'when an error is received' do
2017-08-17 22:00:37 +05:30
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
2021-09-30 23:02:18 +05:30
integration.list_teams(user)
2017-08-17 22:00:37 +05:30
end
2021-09-30 23:02:18 +05:30
context 'when the request succeeds' do
2017-08-17 22:00:37 +05:30
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
2021-09-30 23:02:18 +05:30
context 'when an error is received' do
2017-08-17 22:00:37 +05:30
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