gitlab-org--gitlab-foss/spec/models/project_services/mattermost_slash_commands_s...

126 lines
3.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-11-18 09:00:40 +00:00
require 'spec_helper'
describe MattermostSlashCommandsService do
2016-12-19 14:40:06 +00:00
it_behaves_like "chat slash commands service"
2016-12-20 11:02:37 +00:00
2016-12-21 10:53:44 +00:00
context 'Mattermost API' do
let(:project) { create(:project) }
2016-12-20 11:02:37 +00:00
let(:service) { project.build_mattermost_slash_commands_service }
let(:user) { create(:user) }
2016-12-20 11:02:37 +00:00
2016-12-21 10:53:44 +00:00
before do
session = Mattermost::Session.new(nil)
session.base_uri = 'http://mattermost.example.com'
2016-12-21 10:53:44 +00:00
2017-06-21 13:48:12 +00:00
allow_any_instance_of(Mattermost::Client).to receive(:with_session)
.and_yield(session)
2016-12-20 11:02:37 +00:00
end
2016-12-21 10:53:44 +00:00
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
2016-12-21 10:53:44 +00:00
context 'the requests succeeds' do
before do
stub_request(:post, 'http://mattermost.example.com/api/v4/commands')
2017-06-21 13:48:12 +00:00
.with(body: {
2016-12-21 10:53:44 +00:00
team_id: 'abc',
trigger: 'gitlab',
url: 'http://trigger.url',
icon_url: 'http://icon.url/icon.png',
auto_complete: true,
auto_complete_desc: "Perform common operations on: #{project.full_name}",
2016-12-21 10:53:44 +00:00
auto_complete_hint: '[help]',
description: "Perform common operations on: #{project.full_name}",
display_name: "GitLab / #{project.full_name}",
2016-12-21 10:53:44 +00:00
method: 'P',
2017-02-22 17:44:44 +00:00
username: 'GitLab'
2017-06-21 13:48:12 +00:00
}.to_json)
.to_return(
2016-12-21 10:53:44 +00:00
status: 200,
headers: { 'Content-Type' => 'application/json' },
body: { token: 'token' }.to_json
)
end
it 'saves the service' do
expect { subject }.to change { project.services.count }.by(1)
end
it 'saves the token' do
subject
expect(service.reload.token).to eq('token')
end
2016-12-20 11:02:37 +00:00
end
2016-12-21 10:53:44 +00:00
context 'an error is received' do
before do
stub_request(:post, 'http://mattermost.example.com/api/v4/commands')
2017-06-21 13:48:12 +00:00
.to_return(
2016-12-21 10:53:44 +00:00
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
2016-12-20 11:02:37 +00:00
2016-12-21 10:53:44 +00:00
expect(succeeded).to be(false)
expect(message).to eq('This trigger word is already in use. Please choose another word.')
end
2016-12-20 11:02:37 +00:00
end
end
2016-12-21 10:53:44 +00:00
describe '#list_teams' do
subject do
service.list_teams(user)
end
context 'the requests succeeds' do
before do
stub_request(:get, 'http://mattermost.example.com/api/v4/users/me/teams')
2017-06-21 13:48:12 +00:00
.to_return(
2016-12-21 10:53:44 +00:00
status: 200,
headers: { 'Content-Type' => 'application/json' },
body: [{ id: 'test_team_id' }].to_json
2016-12-21 10:53:44 +00:00
)
end
it 'returns a list of teams' do
expect(subject).not_to be_empty
end
end
context 'an error is received' do
before do
stub_request(:get, 'http://mattermost.example.com/api/v4/users/me/teams')
2017-06-21 13:48:12 +00:00
.to_return(
2016-12-21 10:53:44 +00:00
status: 500,
headers: { 'Content-Type' => 'application/json' },
body: {
message: 'Failed to get team list.'
}.to_json
)
end
it 'shows error messages' do
2017-01-26 08:16:07 +00:00
expect(subject).to eq([[], "Failed to get team list."])
2016-12-21 10:53:44 +00:00
end
2016-12-20 11:02:37 +00:00
end
end
end
2016-11-18 09:00:40 +00:00
end