mattermosts_controller_spec.rb 1.74 KB
Newer Older
1
2
# frozen_string_literal: true

Z.J. van de Weg's avatar
Z.J. van de Weg committed
3
4
5
require 'spec_helper'

describe Projects::MattermostsController do
6
  let!(:project) { create(:project) }
Z.J. van de Weg's avatar
Z.J. van de Weg committed
7
8
9
  let!(:user) { create(:user) }

  before do
10
    project.add_maintainer(user)
Z.J. van de Weg's avatar
Z.J. van de Weg committed
11
12
13
14
15
    sign_in(user)
  end

  describe 'GET #new' do
    before do
16
17
18
      allow_next_instance_of(MattermostSlashCommandsService) do |instance|
        allow(instance).to receive(:list_teams).and_return([])
      end
Z.J. van de Weg's avatar
Z.J. van de Weg committed
19
    end
Z.J. van de Weg's avatar
Z.J. van de Weg committed
20

Z.J. van de Weg's avatar
Z.J. van de Weg committed
21
    it 'accepts the request' do
Z.J. van de Weg's avatar
Z.J. van de Weg committed
22
      get(:new,
blackst0ne's avatar
blackst0ne committed
23
24
25
26
          params: {
            namespace_id: project.namespace.to_param,
            project_id: project
          })
Z.J. van de Weg's avatar
Z.J. van de Weg committed
27

28
      expect(response).to have_gitlab_http_status(200)
Z.J. van de Weg's avatar
Z.J. van de Weg committed
29
30
31
32
33
34
35
36
    end
  end

  describe 'POST #create' do
    let(:mattermost_params) { { trigger: 'http://localhost:3000/trigger', team_id: 'abc' } }

    subject do
      post(:create,
blackst0ne's avatar
blackst0ne committed
37
38
39
40
41
           params: {
             namespace_id: project.namespace.to_param,
             project_id: project,
             mattermost: mattermost_params
           })
Z.J. van de Weg's avatar
Z.J. van de Weg committed
42
43
44
45
    end

    context 'no request can be made to mattermost' do
      it 'shows the error' do
46
47
48
        allow_next_instance_of(MattermostSlashCommandsService) do |instance|
          allow(instance).to receive(:configure).and_return([false, "error message"])
        end
Z.J. van de Weg's avatar
Z.J. van de Weg committed
49

50
        expect(subject).to redirect_to(new_project_mattermost_url(project))
Z.J. van de Weg's avatar
Z.J. van de Weg committed
51
52
53
54
55
      end
    end

    context 'the request is succesull' do
      before do
56
57
58
        allow_next_instance_of(Mattermost::Command) do |instance|
          allow(instance).to receive(:create).and_return('token')
        end
Z.J. van de Weg's avatar
Z.J. van de Weg committed
59
60
61
62
63
64
      end

      it 'redirects to the new page' do
        subject
        service = project.services.last

65
        expect(subject).to redirect_to(edit_project_service_url(project, service))
Z.J. van de Weg's avatar
Z.J. van de Weg committed
66
67
68
69
      end
    end
  end
end