summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/services_controller_spec.rb
blob: 4fe1e8489653160711926e4a24d0da957001917d (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require 'spec_helper'

describe Projects::ServicesController do
  let(:project) { create(:project, :repository) }
  let(:user)    { create(:user) }
  let(:service) { create(:hipchat_service, project: project) }
  let(:hipchat_client) { { '#room' => double(send: true) } }
  let(:service_params) { { token: 'hipchat_token_p', room: '#room' } }

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

    controller.instance_variable_set(:@project, project)
    controller.instance_variable_set(:@service, service)
  end

  describe '#test' do
    context 'when can_test? returns false' do
      it 'renders 404' do
        allow_any_instance_of(Service).to receive(:can_test?).and_return(false)

        put :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id

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

    context 'success' do
      context 'with empty project' do
        let(:project) { create(:empty_project) }

        context 'with chat notification service' do
          let(:service) { project.create_microsoft_teams_service(webhook: 'http://webhook.com') }

          it 'returns success' do
            allow_any_instance_of(MicrosoftTeams::Notifier).to receive(:ping).and_return(true)

            put :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id

            expect(response.status).to eq(200)
          end
        end

        it 'returns success' do
          expect(HipChat::Client).to receive(:new).with('hipchat_token_p', anything).and_return(hipchat_client)

          put :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id, service: service_params

          expect(response.status).to eq(200)
        end
      end

      it 'returns success' do
        expect(HipChat::Client).to receive(:new).with('hipchat_token_p', anything).and_return(hipchat_client)

        put :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id, service: service_params

        expect(response.status).to eq(200)
      end

      def built_service
      end
    end

    context 'failure' do
      it 'returns success status code and the error message' do
        expect(HipChat::Client).to receive(:new).with('hipchat_token_p', anything).and_raise('Bad test')

        put :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id, service: service_params

        expect(response.status).to eq(200)
        expect(JSON.parse(response.body)).
          to eq('error' => true, 'message' => 'Test failed.', 'service_response' => 'Bad test')
      end
    end
  end

  describe 'PUT #update' do
    context 'when param `active` is set to true' do
      it 'activates the service and redirects to integrations paths' do
        put :update,
          namespace_id: project.namespace.id, project_id: project.id, id: service.id, service: { active: true }

        expect(response).to redirect_to(namespace_project_settings_integrations_path(project.namespace, project))
        expect(flash[:notice]).to eq 'HipChat activated.'
      end
    end

    context 'when param `active` is set to false' do
      it 'does not  activate the service but saves the settings' do
        put :update,
          namespace_id: project.namespace.id, project_id: project.id, id: service.id, service: { active: false }

        expect(flash[:notice]).to eq 'HipChat settings saved, but not activated.'
      end
    end
  end
end