2019-10-28 20:06:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-10-29 10:39:46 -04:00
|
|
|
require "spec_helper"
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe API::Services do
|
2020-02-17 10:09:01 -05:00
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:user2) { create(:user) }
|
2017-09-05 04:04:40 -04:00
|
|
|
|
2020-02-17 10:09:01 -05:00
|
|
|
let_it_be(:project, reload: true) do
|
2017-09-05 04:04:40 -04:00
|
|
|
create(:project, creator_id: user.id, namespace: user.namespace)
|
|
|
|
end
|
2013-10-29 10:39:46 -04:00
|
|
|
|
2020-01-07 10:07:34 -05:00
|
|
|
describe "GET /projects/:id/services" do
|
|
|
|
it 'returns authentication error when unauthenticated' do
|
|
|
|
get api("/projects/#{project.id}/services")
|
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2020-01-07 10:07:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns error when authenticated but user is not a project owner" do
|
|
|
|
project.add_developer(user2)
|
|
|
|
get api("/projects/#{project.id}/services", user2)
|
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2020-01-07 10:07:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'project with services' do
|
|
|
|
let!(:active_service) { create(:emails_on_push_service, project: project, active: true) }
|
|
|
|
let!(:service) { create(:custom_issue_tracker_service, project: project, active: false) }
|
|
|
|
|
|
|
|
it "returns a list of all active services" do
|
|
|
|
get api("/projects/#{project.id}/services", user)
|
|
|
|
|
|
|
|
aggregate_failures 'expect successful response with all active services' do
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2020-01-07 10:07:34 -05:00
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(json_response.count).to eq(1)
|
2020-01-15 13:08:34 -05:00
|
|
|
expect(json_response.first['slug']).to eq('emails-on-push')
|
2020-01-07 10:07:34 -05:00
|
|
|
expect(response).to match_response_schema('public_api/v4/services')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-26 19:58:49 -04:00
|
|
|
Service.available_services_names.each do |service|
|
2019-08-01 02:20:35 -04:00
|
|
|
describe "PUT /projects/:id/services/#{service.dasherize}" do
|
2015-08-26 19:58:49 -04:00
|
|
|
include_context service
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "updates #{service} settings" do
|
2018-12-17 17:52:17 -05:00
|
|
|
put api("/projects/#{project.id}/services/#{dashed_service}", user), params: service_attrs
|
2015-08-26 19:58:49 -04:00
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2016-12-27 07:44:24 -05:00
|
|
|
|
|
|
|
current_service = project.services.first
|
2019-06-16 11:57:38 -04:00
|
|
|
events = current_service.event_names.empty? ? ["foo"].freeze : current_service.event_names
|
|
|
|
query_strings = []
|
|
|
|
events.each do |event|
|
|
|
|
query_strings << "#{event}=#{!current_service[event]}"
|
|
|
|
end
|
|
|
|
query_strings = query_strings.join('&')
|
2016-12-27 07:44:24 -05:00
|
|
|
|
2019-06-16 11:57:38 -04:00
|
|
|
put api("/projects/#{project.id}/services/#{dashed_service}?#{query_strings}", user), params: service_attrs
|
2016-12-27 07:44:24 -05:00
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2020-01-15 13:08:34 -05:00
|
|
|
expect(json_response['slug']).to eq(dashed_service)
|
2019-06-16 11:57:38 -04:00
|
|
|
events.each do |event|
|
|
|
|
next if event == "foo"
|
|
|
|
|
|
|
|
expect(project.services.first[event]).not_to eq(current_service[event]),
|
|
|
|
"expected #{!current_service[event]} for event #{event} for service #{current_service.title}, got #{current_service[event]}"
|
|
|
|
end
|
2015-08-26 19:58:49 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns if required fields missing" do
|
2015-08-26 19:58:49 -04:00
|
|
|
attrs = service_attrs
|
2015-09-16 05:43:16 -04:00
|
|
|
|
2015-08-26 19:58:49 -04:00
|
|
|
required_attributes = service_attrs_list.select do |attr|
|
2015-09-16 05:43:16 -04:00
|
|
|
service_klass.validators_on(attr).any? do |v|
|
2020-11-26 16:09:22 -05:00
|
|
|
v.class == ActiveRecord::Validations::PresenceValidator &&
|
|
|
|
# exclude presence validators with conditional since those are not really required
|
|
|
|
![:if, :unless].any? { |cond| v.options.include?(cond) }
|
2015-08-26 19:58:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if required_attributes.empty?
|
2020-03-31 17:08:05 -04:00
|
|
|
expected_code = :ok
|
2015-08-26 19:58:49 -04:00
|
|
|
else
|
2015-12-14 21:53:52 -05:00
|
|
|
attrs.delete(required_attributes.sample)
|
2020-03-31 17:08:05 -04:00
|
|
|
expected_code = :bad_request
|
2015-08-26 19:58:49 -04:00
|
|
|
end
|
2016-08-01 11:00:44 -04:00
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
put api("/projects/#{project.id}/services/#{dashed_service}", user), params: attrs
|
2015-08-26 19:58:49 -04:00
|
|
|
|
2020-03-31 17:08:05 -04:00
|
|
|
expect(response).to have_gitlab_http_status(expected_code)
|
2015-08-26 19:58:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-01 02:20:35 -04:00
|
|
|
describe "DELETE /projects/:id/services/#{service.dasherize}" do
|
2015-08-26 19:58:49 -04:00
|
|
|
include_context service
|
|
|
|
|
2018-01-04 04:33:51 -05:00
|
|
|
before do
|
|
|
|
initialize_service(service)
|
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "deletes #{service}" do
|
2015-08-26 19:58:49 -04:00
|
|
|
delete api("/projects/#{project.id}/services/#{dashed_service}", user)
|
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
2015-11-09 10:48:03 -05:00
|
|
|
project.send(service_method).reload
|
2015-08-26 19:58:49 -04:00
|
|
|
expect(project.send(service_method).activated?).to be_falsey
|
|
|
|
end
|
2014-10-14 13:07:34 -04:00
|
|
|
end
|
2015-09-03 09:38:54 -04:00
|
|
|
|
|
|
|
describe "GET /projects/:id/services/#{service.dasherize}" do
|
|
|
|
include_context service
|
|
|
|
|
2015-10-12 09:24:00 -04:00
|
|
|
# inject some properties into the service
|
2019-07-11 05:53:08 -04:00
|
|
|
let!(:initialized_service) { initialize_service(service) }
|
2015-10-12 09:24:00 -04:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'returns authentication error when unauthenticated' do
|
2015-10-12 09:24:00 -04:00
|
|
|
get api("/projects/#{project.id}/services/#{dashed_service}")
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2015-10-12 09:24:00 -04:00
|
|
|
end
|
2016-08-01 11:00:44 -04:00
|
|
|
|
2019-12-06 16:07:54 -05:00
|
|
|
it "returns all properties of service #{service}" do
|
2015-09-03 09:38:54 -04:00
|
|
|
get api("/projects/#{project.id}/services/#{dashed_service}", user)
|
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-01-06 01:18:13 -05:00
|
|
|
expect(json_response['properties'].keys).to match_array(service_instance.api_field_names)
|
2015-09-03 09:38:54 -04:00
|
|
|
end
|
2015-10-12 09:24:00 -04:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns error when authenticated but not a project owner" do
|
2017-12-22 03:18:28 -05:00
|
|
|
project.add_developer(user2)
|
2015-10-12 09:24:00 -04:00
|
|
|
get api("/projects/#{project.id}/services/#{dashed_service}", user2)
|
2016-08-01 11:00:44 -04:00
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2015-10-12 09:24:00 -04:00
|
|
|
end
|
2015-09-03 09:38:54 -04:00
|
|
|
end
|
2014-10-14 13:07:34 -04:00
|
|
|
end
|
2016-11-18 04:00:40 -05:00
|
|
|
|
|
|
|
describe 'POST /projects/:id/services/:slug/trigger' do
|
2016-12-29 04:48:18 -05:00
|
|
|
describe 'Mattermost Service' do
|
|
|
|
let(:service_name) { 'mattermost_slash_commands' }
|
2016-11-18 04:00:40 -05:00
|
|
|
|
2016-12-29 04:48:18 -05:00
|
|
|
context 'no service is available' do
|
|
|
|
it 'returns a not found message' do
|
|
|
|
post api("/projects/#{project.id}/services/idonotexist/trigger")
|
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2016-12-29 04:48:18 -05:00
|
|
|
expect(json_response["error"]).to eq("404 Not Found")
|
|
|
|
end
|
2016-11-18 04:00:40 -05:00
|
|
|
end
|
|
|
|
|
2016-12-29 04:48:18 -05:00
|
|
|
context 'the service exists' do
|
|
|
|
let(:params) { { token: 'token' } }
|
2016-11-18 05:38:54 -05:00
|
|
|
|
2016-12-29 04:48:18 -05:00
|
|
|
context 'the service is not active' do
|
|
|
|
before do
|
|
|
|
project.create_mattermost_slash_commands_service(
|
|
|
|
active: false,
|
|
|
|
properties: params
|
|
|
|
)
|
|
|
|
end
|
2016-11-18 04:00:40 -05:00
|
|
|
|
2016-12-29 04:48:18 -05:00
|
|
|
it 'when the service is inactive' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/projects/#{project.id}/services/#{service_name}/trigger"), params: params
|
2016-11-18 04:00:40 -05:00
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2016-12-29 04:48:18 -05:00
|
|
|
end
|
2016-11-18 04:00:40 -05:00
|
|
|
end
|
|
|
|
|
2016-12-29 04:48:18 -05:00
|
|
|
context 'the service is active' do
|
|
|
|
before do
|
|
|
|
project.create_mattermost_slash_commands_service(
|
|
|
|
active: true,
|
|
|
|
properties: params
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns status 200' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/projects/#{project.id}/services/#{service_name}/trigger"), params: params
|
2016-12-29 04:48:18 -05:00
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2016-12-29 04:48:18 -05:00
|
|
|
end
|
2016-11-18 04:00:40 -05:00
|
|
|
end
|
|
|
|
|
2016-12-29 04:48:18 -05:00
|
|
|
context 'when the project can not be found' do
|
|
|
|
it 'returns a generic 404' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/projects/404/services/#{service_name}/trigger"), params: params
|
2016-11-18 04:00:40 -05:00
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2016-12-29 04:48:18 -05:00
|
|
|
expect(json_response["message"]).to eq("404 Service Not Found")
|
|
|
|
end
|
2016-11-18 04:00:40 -05:00
|
|
|
end
|
|
|
|
end
|
2016-12-29 04:48:18 -05:00
|
|
|
end
|
2016-11-18 05:38:54 -05:00
|
|
|
|
2016-12-29 04:48:18 -05:00
|
|
|
describe 'Slack Service' do
|
|
|
|
let(:service_name) { 'slack_slash_commands' }
|
2016-11-18 05:38:54 -05:00
|
|
|
|
2016-12-29 04:48:18 -05:00
|
|
|
before do
|
|
|
|
project.create_slack_slash_commands_service(
|
|
|
|
active: true,
|
|
|
|
properties: { token: 'token' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns status 200' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/projects/#{project.id}/services/#{service_name}/trigger"), params: { token: 'token', text: 'help' }
|
2016-12-29 04:48:18 -05:00
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2016-12-29 04:48:18 -05:00
|
|
|
expect(json_response['response_type']).to eq("ephemeral")
|
2016-11-18 05:38:54 -05:00
|
|
|
end
|
2016-11-18 04:00:40 -05:00
|
|
|
end
|
|
|
|
end
|
2017-11-08 05:22:24 -05:00
|
|
|
|
|
|
|
describe 'Mattermost service' do
|
|
|
|
let(:service_name) { 'mattermost' }
|
|
|
|
let(:params) do
|
|
|
|
{ webhook: 'https://hook.example.com', username: 'username' }
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.create_mattermost_service(
|
|
|
|
active: true,
|
|
|
|
properties: params
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts a username for update' do
|
2020-01-26 01:08:38 -05:00
|
|
|
put api("/projects/#{project.id}/services/#{service_name}", user), params: params.merge(username: 'new_username')
|
2017-11-08 05:22:24 -05:00
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-11-08 05:22:24 -05:00
|
|
|
expect(json_response['properties']['username']).to eq('new_username')
|
|
|
|
end
|
|
|
|
end
|
2020-01-26 01:08:38 -05:00
|
|
|
|
|
|
|
describe 'Microsoft Teams service' do
|
|
|
|
let(:service_name) { 'microsoft-teams' }
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
webhook: 'https://hook.example.com',
|
|
|
|
branches_to_be_notified: 'default',
|
|
|
|
notify_only_broken_pipelines: false
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.create_microsoft_teams_service(
|
|
|
|
active: true,
|
|
|
|
properties: params
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts branches_to_be_notified for update' do
|
|
|
|
put api("/projects/#{project.id}/services/#{service_name}", user), params: params.merge(branches_to_be_notified: 'all')
|
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2020-01-26 01:08:38 -05:00
|
|
|
expect(json_response['properties']['branches_to_be_notified']).to eq('all')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts notify_only_broken_pipelines for update' do
|
|
|
|
put api("/projects/#{project.id}/services/#{service_name}", user), params: params.merge(notify_only_broken_pipelines: true)
|
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2020-01-26 01:08:38 -05:00
|
|
|
expect(json_response['properties']['notify_only_broken_pipelines']).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2020-09-25 08:10:00 -04:00
|
|
|
|
|
|
|
describe 'Hangouts Chat service' do
|
|
|
|
let(:service_name) { 'hangouts-chat' }
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
webhook: 'https://hook.example.com',
|
|
|
|
branches_to_be_notified: 'default'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.create_hangouts_chat_service(
|
|
|
|
active: true,
|
|
|
|
properties: params
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts branches_to_be_notified for update', :aggregate_failures do
|
|
|
|
put api("/projects/#{project.id}/services/#{service_name}", user), params: params.merge(branches_to_be_notified: 'all')
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response['properties']['branches_to_be_notified']).to eq('all')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'only requires the webhook param' do
|
|
|
|
put api("/projects/#{project.id}/services/#{service_name}", user), params: { webhook: 'https://hook.example.com' }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
|
|
|
end
|
2013-10-29 10:39:46 -04:00
|
|
|
end
|