BigW Consortium Gitlab

mattermost_slash_commands_service_spec.rb 3.8 KB
Newer Older
1 2
require 'spec_helper'

3 4
describe MattermostSlashCommandsService, :models do
  it_behaves_like "chat slash commands service"
5

Kamil Trzcinski committed
6
  context 'Mattermost API' do
7 8
    let(:project) { create(:empty_project) }
    let(:service) { project.build_mattermost_slash_commands_service }
9
    let(:user) { create(:user) }
10

Kamil Trzcinski committed
11 12 13
    before do
      Mattermost::Session.base_uri("http://mattermost.example.com")

14 15
      allow_any_instance_of(Mattermost::Client).to receive(:with_session).
        and_yield(Mattermost::Session.new(nil))
16 17
    end

Kamil Trzcinski committed
18 19 20 21 22
    describe '#configure' do
      subject do
        service.configure(user, team_id: 'abc',
                                trigger: 'gitlab', url: 'http://trigger.url',
                                icon_url: 'http://icon.url/icon.png')
23 24
      end

Kamil Trzcinski committed
25 26
      context 'the requests succeeds' do
        before do
27 28
          stub_request(:post, 'http://mattermost.example.com/api/v3/teams/abc/commands/create').
            with(body: {
Kamil Trzcinski committed
29 30 31 32 33 34 35 36 37 38
              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.name_with_namespace}",
              auto_complete_hint: '[help]',
              description: "Perform common operations on: #{project.name_with_namespace}",
              display_name: "GitLab / #{project.name_with_namespace}",
              method: 'P',
39
              username: 'GitLab'
40 41
            }.to_json).
            to_return(
Kamil Trzcinski committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
              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
57 58
      end

Kamil Trzcinski committed
59 60
      context 'an error is received' do
        before do
61 62
          stub_request(:post, 'http://mattermost.example.com/api/v3/teams/abc/commands/create').
            to_return(
Kamil Trzcinski committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76
              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
77

Kamil Trzcinski committed
78 79 80
          expect(succeeded).to be(false)
          expect(message).to eq('This trigger word is already in use. Please choose another word.')
        end
81 82 83
      end
    end

Kamil Trzcinski committed
84 85 86 87 88 89 90
    describe '#list_teams' do
      subject do
        service.list_teams(user)
      end

      context 'the requests succeeds' do
        before do
91 92
          stub_request(:get, 'http://mattermost.example.com/api/v3/teams/all').
            to_return(
Kamil Trzcinski committed
93 94
              status: 200,
              headers: { 'Content-Type' => 'application/json' },
95
              body: { 'list' => true }.to_json
Kamil Trzcinski committed
96 97 98 99 100 101 102 103 104 105
            )
        end

        it 'returns a list of teams' do
          expect(subject).not_to be_empty
        end
      end

      context 'an error is received' do
        before do
106 107
          stub_request(:get, 'http://mattermost.example.com/api/v3/teams/all').
            to_return(
Kamil Trzcinski committed
108 109 110 111 112 113 114 115 116
              status: 500,
              headers: { 'Content-Type' => 'application/json' },
              body: {
                message: 'Failed to get team list.'
              }.to_json
            )
        end

        it 'shows error messages' do
117
          expect(subject).to eq([[], "Failed to get team list."])
Kamil Trzcinski committed
118
        end
119 120 121
      end
    end
  end
122
end