BigW Consortium Gitlab

command_spec.rb 1.75 KB
Newer Older
1 2 3
require 'spec_helper'

describe Mattermost::Command do
4
  let(:params) { { 'token' => 'token', team_id: 'abc' } }
5

Kamil Trzcinski committed
6
  before do
Kamil Trzcinski committed
7
    Mattermost::Session.base_uri('http://mattermost.example.com')
8

9 10
    allow_any_instance_of(Mattermost::Client).to receive(:with_session)
      .and_yield(Mattermost::Session.new(nil))
Kamil Trzcinski committed
11
  end
12

Kamil Trzcinski committed
13
  describe '#create' do
Kamil Trzcinski committed
14 15
    let(:params) do
      { team_id: 'abc',
16
        trigger: 'gitlab' }
Kamil Trzcinski committed
17 18 19 20 21 22
    end

    subject { described_class.new(nil).create(params) }

    context 'for valid trigger word' do
      before do
23 24
        stub_request(:post, 'http://mattermost.example.com/api/v3/teams/abc/commands/create')
          .with(body: {
Kamil Trzcinski committed
25
            team_id: 'abc',
26
            trigger: 'gitlab'
27 28
          }.to_json)
          .to_return(
Kamil Trzcinski committed
29 30 31 32 33 34 35 36 37 38 39 40 41
            status: 200,
            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
42 43
        stub_request(:post, 'http://mattermost.example.com/api/v3/teams/abc/commands/create')
          .to_return(
Kamil Trzcinski committed
44 45 46 47 48 49 50 51 52 53 54
            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
55

Kamil Trzcinski committed
56 57 58
      it 'raises an error with message' do
        expect { subject }.to raise_error(Mattermost::Error, 'This trigger word is already in use. Please choose another word.')
      end
59 60 61
    end
  end
end