summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/mattermosts_controller_spec.rb
blob: 12e413db9025bd5eee7110ef634bd09f6d3cd92f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require 'spec_helper'

describe Projects::MattermostsController do
  let!(:project) { create(:empty_project) }
  let!(:user) { create(:user) }

  before do
    project.team << [user, :master]
    sign_in(user)
  end

  describe 'GET #new' do
    before do
      allow_any_instance_of(MattermostSlashCommandsService)
        .to receive(:list_teams).and_return([])
    end

    it 'accepts the request' do
      get(:new,
          namespace_id: project.namespace.to_param,
          project_id: project)

      expect(response).to have_http_status(200)
    end
  end

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

    subject do
      post(:create,
           namespace_id: project.namespace.to_param,
           project_id: project,
           mattermost: mattermost_params)
    end

    context 'no request can be made to mattermost' do
      it 'shows the error' do
        allow_any_instance_of(MattermostSlashCommandsService).to receive(:configure).and_return([false, "error message"])

        expect(subject).to redirect_to(new_project_mattermost_url(project))
      end
    end

    context 'the request is succesull' do
      before do
        allow_any_instance_of(Mattermost::Command).to receive(:create).and_return('token')
      end

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

        expect(subject).to redirect_to(edit_project_service_url(project, service))
      end
    end
  end
end