services_controller_spec.rb 5.38 KB
Newer Older
1
2
3
require 'spec_helper'

describe Projects::ServicesController do
4
  let(:project) { create(:project, :repository) }
5
  let(:user)    { create(:user) }
6
7
8
  let(:service) { create(:hipchat_service, project: project) }
  let(:hipchat_client) { { '#room' => double(send: true) } }
  let(:service_params) { { token: 'hipchat_token_p', room: '#room' } }
9
10
11

  before do
    sign_in(user)
12
    project.add_maintainer(user)
13
14
  end

15
16
17
18
  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)
19

20
        put :test, namespace_id: project.namespace, project_id: project, id: service.to_param
21

22
        expect(response).to have_gitlab_http_status(404)
23
      end
24
    end
25

26
27
28
29
30
31
32
33
34
35
36
37
    context 'when validations fail' do
      let(:service_params) { { active: 'true', token: '' } }

      it 'returns error messages in JSON response' do
        put :test, namespace_id: project.namespace, project_id: project, id: :hipchat, service: service_params

        expect(json_response['message']).to eq "Validations failed."
        expect(json_response['service_response']).to eq "Token can't be blank"
        expect(response).to have_gitlab_http_status(200)
      end
    end

38
39
    context 'success' do
      context 'with empty project' do
40
        let(:project) { create(:project) }
41

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

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

48
            put :test, namespace_id: project.namespace, project_id: project, id: service.to_param
49

50
            expect(response.status).to eq(200)
51
52
53
          end
        end

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

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

59
          expect(response.status).to eq(200)
60
61
62
        end
      end

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

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

68
        expect(response.status).to eq(200)
69
      end
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

      context 'when service is configured for the first time' do
        before do
          allow_any_instance_of(ServiceHook).to receive(:execute).and_return(true)
        end

        it 'persist the object' do
          do_put

          expect(BuildkiteService.first).to be_present
        end

        it 'creates the ServiceHook object' do
          do_put

          expect(BuildkiteService.first.service_hook).to be_present
        end

        def do_put
          put :test, namespace_id: project.namespace,
                     project_id: project,
                     id: 'buildkite',
                     service: { 'active' => '1', 'push_events' => '1', token: 'token', 'project_url' => 'http://test.com' }
        end
      end
95
96
    end

97
    context 'failure' do
Jarka Kadlecova's avatar
Jarka Kadlecova committed
98
      it 'returns success status code and the error message' do
99
100
        expect(HipChat::Client).to receive(:new).with('hipchat_token_p', anything).and_raise('Bad test')

101
        put :test, namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params
102
103

        expect(response.status).to eq(200)
104
        expect(JSON.parse(response.body))
105
          .to eq('error' => true, 'message' => 'Test failed.', 'service_response' => 'Bad test', 'test_failed' => true)
106
      end
107
108
    end
  end
109
110

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

116
        expect(response).to redirect_to(project_settings_integrations_path(project))
117
118
119
        expect(flash[:notice]).to eq 'HipChat activated.'
      end
    end
120

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

126
        expect(flash[:notice]).to eq 'HipChat settings saved, but not activated.'
127
128
      end
    end
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

    context 'with a deprecated service' do
      let(:service) { create(:kubernetes_service, project: project) }

      before do
        put :update,
          namespace_id: project.namespace, project_id: project, id: service.to_param, service: { namespace: 'updated_namespace' }
      end

      it 'should not update the service' do
        service.reload
        expect(service.namespace).not_to eq('updated_namespace')
      end
    end
  end

  describe "GET #edit" do
    before do
      get :edit, namespace_id: project.namespace, project_id: project, id: service_id
    end

    context 'with approved services' do
      let(:service_id) { 'jira' }

      it 'should render edit page' do
        expect(response).to be_success
      end
    end

    context 'with a deprecated service' do
      let(:service_id) { 'kubernetes' }

      it 'should render edit page' do
        expect(response).to be_success
      end
    end
165
  end
166
end