2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-16 09:45:56 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Mattermost::Command do
|
2016-12-20 14:56:46 -05:00
|
|
|
let(:params) { { 'token' => 'token', team_id: 'abc' } }
|
2016-12-19 08:14:09 -05:00
|
|
|
|
2016-12-20 13:11:53 -05:00
|
|
|
before do
|
2021-05-31 23:10:06 -04:00
|
|
|
session = ::Mattermost::Session.new(nil)
|
2018-03-13 18:38:25 -04:00
|
|
|
session.base_uri = 'http://mattermost.example.com'
|
2016-12-16 09:45:56 -05:00
|
|
|
|
2021-05-31 23:10:06 -04:00
|
|
|
allow_any_instance_of(::Mattermost::Client).to receive(:with_session)
|
2018-03-13 18:38:25 -04:00
|
|
|
.and_yield(session)
|
2016-12-21 05:53:44 -05:00
|
|
|
end
|
2016-12-20 14:56:46 -05:00
|
|
|
|
2016-12-20 13:11:53 -05:00
|
|
|
describe '#create' do
|
2016-12-21 05:53:44 -05:00
|
|
|
let(:params) do
|
|
|
|
{ team_id: 'abc',
|
2017-02-22 12:44:44 -05:00
|
|
|
trigger: 'gitlab' }
|
2016-12-21 05:53:44 -05:00
|
|
|
end
|
|
|
|
|
2021-04-01 14:13:56 -04:00
|
|
|
subject { described_class.new(nil).create(params) } # rubocop:disable Rails/SaveBang
|
2016-12-21 05:53:44 -05:00
|
|
|
|
|
|
|
context 'for valid trigger word' do
|
|
|
|
before do
|
2018-05-15 11:13:49 -04:00
|
|
|
stub_request(:post, 'http://mattermost.example.com/api/v4/commands')
|
2017-06-21 09:48:12 -04:00
|
|
|
.with(body: {
|
2016-12-21 05:53:44 -05:00
|
|
|
team_id: 'abc',
|
2017-02-22 12:44:44 -05:00
|
|
|
trigger: 'gitlab'
|
2017-06-21 09:48:12 -04:00
|
|
|
}.to_json)
|
|
|
|
.to_return(
|
2018-05-15 11:13:49 -04:00
|
|
|
status: 201,
|
2016-12-21 05:53:44 -05:00
|
|
|
headers: { 'Content-Type' => 'application/json' },
|
|
|
|
body: { token: 'token' }.to_json
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a token' do
|
|
|
|
is_expected.to eq('token')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for error message' do
|
|
|
|
before do
|
2018-05-15 11:13:49 -04:00
|
|
|
stub_request(:post, 'http://mattermost.example.com/api/v4/commands')
|
2017-06-21 09:48:12 -04:00
|
|
|
.to_return(
|
2018-05-15 11:13:49 -04:00
|
|
|
status: 400,
|
2016-12-21 05:53:44 -05:00
|
|
|
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',
|
2018-05-15 11:13:49 -04:00
|
|
|
status_code: 400
|
2016-12-21 05:53:44 -05:00
|
|
|
}.to_json
|
|
|
|
)
|
|
|
|
end
|
2016-12-20 14:56:46 -05:00
|
|
|
|
2016-12-21 05:53:44 -05:00
|
|
|
it 'raises an error with message' do
|
2021-05-31 23:10:06 -04:00
|
|
|
expect { subject }.to raise_error(::Mattermost::Error, 'This trigger word is already in use. Please choose another word.')
|
2016-12-21 05:53:44 -05:00
|
|
|
end
|
2016-12-16 09:45:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|