debian-mirror-gitlab/spec/requests/api/services_spec.rb

362 lines
12 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
require "spec_helper"
2020-07-28 23:09:34 +05:30
RSpec.describe API::Services do
2020-03-13 15:44:24 +05:30
let_it_be(:user) { create(:user) }
let_it_be(:user2) { create(:user) }
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
let_it_be(:project, reload: true) do
2018-03-17 18:26:18 +05:30
create(:project, creator_id: user.id, namespace: user.namespace)
end
2014-09-02 18:07:02 +05:30
2020-03-13 15:44:24 +05:30
describe "GET /projects/:id/services" do
it 'returns authentication error when unauthenticated' do
get api("/projects/#{project.id}/services")
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2020-03-13 15:44:24 +05:30
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-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:forbidden)
2020-03-13 15:44:24 +05:30
end
2021-09-30 23:02:18 +05:30
context 'with integrations' do
2021-09-04 01:27:46 +05:30
let!(:active_integration) { create(:emails_on_push_integration, project: project, active: true) }
let!(:integration) { create(:custom_issue_tracker_integration, project: project, active: false) }
2020-03-13 15:44:24 +05:30
2021-09-30 23:02:18 +05:30
it "returns a list of all active integrations" do
2020-03-13 15:44:24 +05:30
get api("/projects/#{project.id}/services", user)
2021-09-30 23:02:18 +05:30
aggregate_failures 'expect successful response with all active integrations' do
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2020-03-13 15:44:24 +05:30
expect(json_response).to be_an Array
expect(json_response.count).to eq(1)
expect(json_response.first['slug']).to eq('emails-on-push')
expect(response).to match_response_schema('public_api/v4/services')
end
end
end
end
2021-09-30 23:02:18 +05:30
Integration.available_integration_names.each do |integration|
describe "PUT /projects/:id/services/#{integration.dasherize}" do
include_context integration
2014-09-02 18:07:02 +05:30
2021-09-30 23:02:18 +05:30
it "updates #{integration} settings" do
put api("/projects/#{project.id}/services/#{dashed_integration}", user), params: integration_attrs
2014-09-02 18:07:02 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
2021-09-30 23:02:18 +05:30
current_integration = project.integrations.first
events = current_integration.event_names.empty? ? ["foo"].freeze : current_integration.event_names
2019-09-04 21:01:54 +05:30
query_strings = []
events.each do |event|
2021-09-30 23:02:18 +05:30
query_strings << "#{event}=#{!current_integration[event]}"
2019-09-04 21:01:54 +05:30
end
query_strings = query_strings.join('&')
2017-08-17 22:00:37 +05:30
2021-09-30 23:02:18 +05:30
put api("/projects/#{project.id}/services/#{dashed_integration}?#{query_strings}", user), params: integration_attrs
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2021-09-30 23:02:18 +05:30
expect(json_response['slug']).to eq(dashed_integration)
2019-09-04 21:01:54 +05:30
events.each do |event|
next if event == "foo"
2021-09-30 23:02:18 +05:30
expect(project.integrations.first[event]).not_to eq(current_integration[event]),
"expected #{!current_integration[event]} for event #{event} for service #{current_integration.title}, got #{current_integration[event]}"
2019-09-04 21:01:54 +05:30
end
2015-09-25 12:07:36 +05:30
end
2015-09-11 14:41:01 +05:30
2016-09-13 17:45:13 +05:30
it "returns if required fields missing" do
2021-09-30 23:02:18 +05:30
required_attributes = integration_attrs_list.select do |attr|
integration_klass.validators_on(attr).any? do |v|
v.instance_of?(ActiveRecord::Validations::PresenceValidator) &&
2021-02-22 17:27:13 +05:30
# exclude presence validators with conditional since those are not really required
![:if, :unless].any? { |cond| v.options.include?(cond) }
2015-09-25 12:07:36 +05:30
end
end
2015-09-11 14:41:01 +05:30
2015-09-25 12:07:36 +05:30
if required_attributes.empty?
2020-04-22 19:07:51 +05:30
expected_code = :ok
2015-09-25 12:07:36 +05:30
else
2021-09-30 23:02:18 +05:30
integration_attrs.delete(required_attributes.sample)
2020-04-22 19:07:51 +05:30
expected_code = :bad_request
2015-09-25 12:07:36 +05:30
end
2016-09-13 17:45:13 +05:30
2021-09-30 23:02:18 +05:30
put api("/projects/#{project.id}/services/#{dashed_integration}", user), params: integration_attrs
2015-09-11 14:41:01 +05:30
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(expected_code)
2015-09-25 12:07:36 +05:30
end
2015-09-11 14:41:01 +05:30
end
2014-09-02 18:07:02 +05:30
2021-09-30 23:02:18 +05:30
describe "DELETE /projects/:id/services/#{integration.dasherize}" do
include_context integration
2014-09-02 18:07:02 +05:30
2018-03-17 18:26:18 +05:30
before do
2021-09-30 23:02:18 +05:30
initialize_integration(integration)
2018-03-17 18:26:18 +05:30
end
2021-09-30 23:02:18 +05:30
it "deletes #{integration}" do
delete api("/projects/#{project.id}/services/#{dashed_integration}", user)
2015-04-26 12:48:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:no_content)
2021-09-30 23:02:18 +05:30
project.send(integration_method).reload
expect(project.send(integration_method).activated?).to be_falsey
2015-09-25 12:07:36 +05:30
end
2015-04-26 12:48:37 +05:30
end
2021-09-30 23:02:18 +05:30
describe "GET /projects/:id/services/#{integration.dasherize}" do
include_context integration
2015-04-26 12:48:37 +05:30
2021-09-30 23:02:18 +05:30
let!(:initialized_integration) { initialize_integration(integration, active: true) }
2021-06-08 01:23:25 +05:30
let_it_be(:project2) do
create(:project, creator_id: user.id, namespace: user.namespace)
end
2021-09-30 23:02:18 +05:30
def deactive_integration!
return initialized_integration.update!(active: false) unless initialized_integration.is_a?(::Integrations::Prometheus)
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
# Integrations::Prometheus sets `#active` itself within a `before_save`:
initialized_integration.manual_configuration = false
initialized_integration.save!
2021-06-08 01:23:25 +05:30
end
2015-10-24 18:46:33 +05:30
2016-09-13 17:45:13 +05:30
it 'returns authentication error when unauthenticated' do
2021-09-30 23:02:18 +05:30
get api("/projects/#{project.id}/services/#{dashed_integration}")
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2015-10-24 18:46:33 +05:30
end
2016-09-13 17:45:13 +05:30
2021-09-30 23:02:18 +05:30
it "returns all properties of active service #{integration}" do
get api("/projects/#{project.id}/services/#{dashed_integration}", user)
2015-04-26 12:48:37 +05:30
2021-09-30 23:02:18 +05:30
expect(initialized_integration).to be_active
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2021-09-30 23:02:18 +05:30
expect(json_response['properties'].keys).to match_array(integration_instance.api_field_names)
2015-09-25 12:07:36 +05:30
end
2015-10-24 18:46:33 +05:30
2021-09-30 23:02:18 +05:30
it "returns all properties of inactive integration #{integration}" do
deactive_integration!
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
get api("/projects/#{project.id}/services/#{dashed_integration}", user)
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
expect(initialized_integration).not_to be_active
2021-06-08 01:23:25 +05:30
expect(response).to have_gitlab_http_status(:ok)
2021-09-30 23:02:18 +05:30
expect(json_response['properties'].keys).to match_array(integration_instance.api_field_names)
2021-06-08 01:23:25 +05:30
end
2021-09-30 23:02:18 +05:30
it "returns not found if integration does not exist" do
get api("/projects/#{project2.id}/services/#{dashed_integration}", user)
2021-06-08 01:23:25 +05:30
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']).to eq('404 Service Not Found')
end
2021-09-30 23:02:18 +05:30
it "returns not found if service exists but is in `Project#disabled_integrations`" do
2021-06-08 01:23:25 +05:30
expect_next_found_instance_of(Project) do |project|
2021-09-30 23:02:18 +05:30
expect(project).to receive(:disabled_integrations).at_least(:once).and_return([integration])
2021-06-08 01:23:25 +05:30
end
2021-09-30 23:02:18 +05:30
get api("/projects/#{project.id}/services/#{dashed_integration}", user)
2021-06-08 01:23:25 +05:30
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']).to eq('404 Service Not Found')
end
2016-09-13 17:45:13 +05:30
it "returns error when authenticated but not a project owner" do
2018-03-17 18:26:18 +05:30
project.add_developer(user2)
2021-09-30 23:02:18 +05:30
get api("/projects/#{project.id}/services/#{dashed_integration}", user2)
2016-09-13 17:45:13 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:forbidden)
2015-10-24 18:46:33 +05:30
end
2014-09-02 18:07:02 +05:30
end
end
2017-08-17 22:00:37 +05:30
describe 'POST /projects/:id/services/:slug/trigger' do
2021-09-30 23:02:18 +05:30
describe 'Mattermost integration' do
let(:integration_name) { 'mattermost_slash_commands' }
2017-08-17 22:00:37 +05:30
2021-09-30 23:02:18 +05:30
context 'when no integration is available' do
2017-08-17 22:00:37 +05:30
it 'returns a not found message' do
post api("/projects/#{project.id}/services/idonotexist/trigger")
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2017-08-17 22:00:37 +05:30
expect(json_response["error"]).to eq("404 Not Found")
end
end
2021-09-30 23:02:18 +05:30
context 'when the integration exists' do
2017-08-17 22:00:37 +05:30
let(:params) { { token: 'token' } }
2021-09-30 23:02:18 +05:30
context 'when the integration is not active' do
2017-08-17 22:00:37 +05:30
before do
2021-09-30 23:02:18 +05:30
project.create_mattermost_slash_commands_integration(
2017-08-17 22:00:37 +05:30
active: false,
properties: params
)
end
2021-09-30 23:02:18 +05:30
it 'when the integration is inactive' do
post api("/projects/#{project.id}/services/#{integration_name}/trigger"), params: params
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2017-08-17 22:00:37 +05:30
end
end
2021-09-30 23:02:18 +05:30
context 'when the integration is active' do
2017-08-17 22:00:37 +05:30
before do
2021-09-30 23:02:18 +05:30
project.create_mattermost_slash_commands_integration(
2017-08-17 22:00:37 +05:30
active: true,
properties: params
)
end
it 'returns status 200' do
2021-09-30 23:02:18 +05:30
post api("/projects/#{project.id}/services/#{integration_name}/trigger"), params: params
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
end
end
context 'when the project can not be found' do
it 'returns a generic 404' do
2021-09-30 23:02:18 +05:30
post api("/projects/404/services/#{integration_name}/trigger"), params: params
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2017-08-17 22:00:37 +05:30
expect(json_response["message"]).to eq("404 Service Not Found")
end
end
end
end
2021-09-30 23:02:18 +05:30
describe 'Slack Integration' do
let(:integration_name) { 'slack_slash_commands' }
2017-08-17 22:00:37 +05:30
before do
2021-09-30 23:02:18 +05:30
project.create_slack_slash_commands_integration(
2017-08-17 22:00:37 +05:30
active: true,
properties: { token: 'token' }
)
end
it 'returns status 200' do
2021-09-30 23:02:18 +05:30
post api("/projects/#{project.id}/services/#{integration_name}/trigger"), params: { token: 'token', text: 'help' }
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
expect(json_response['response_type']).to eq("ephemeral")
end
end
end
2018-03-17 18:26:18 +05:30
2021-09-30 23:02:18 +05:30
describe 'Mattermost integration' do
let(:integration_name) { 'mattermost' }
2018-03-17 18:26:18 +05:30
let(:params) do
{ webhook: 'https://hook.example.com', username: 'username' }
end
before do
2021-09-30 23:02:18 +05:30
project.create_mattermost_integration(
2018-03-17 18:26:18 +05:30
active: true,
properties: params
)
end
it 'accepts a username for update' do
2021-09-30 23:02:18 +05:30
put api("/projects/#{project.id}/services/#{integration_name}", user), params: params.merge(username: 'new_username')
2018-03-17 18:26:18 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2018-03-17 18:26:18 +05:30
expect(json_response['properties']['username']).to eq('new_username')
end
end
2020-03-13 15:44:24 +05:30
2021-09-30 23:02:18 +05:30
describe 'Microsoft Teams integration' do
let(:integration_name) { 'microsoft-teams' }
2020-03-13 15:44:24 +05:30
let(:params) do
{
webhook: 'https://hook.example.com',
branches_to_be_notified: 'default',
notify_only_broken_pipelines: false
}
end
before do
2021-09-30 23:02:18 +05:30
project.create_microsoft_teams_integration(
2020-03-13 15:44:24 +05:30
active: true,
properties: params
)
end
it 'accepts branches_to_be_notified for update' do
2021-09-30 23:02:18 +05:30
put api("/projects/#{project.id}/services/#{integration_name}", user),
params: params.merge(branches_to_be_notified: 'all')
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2020-03-13 15:44:24 +05:30
expect(json_response['properties']['branches_to_be_notified']).to eq('all')
end
it 'accepts notify_only_broken_pipelines for update' do
2021-09-30 23:02:18 +05:30
put api("/projects/#{project.id}/services/#{integration_name}", user),
params: params.merge(notify_only_broken_pipelines: true)
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2020-03-13 15:44:24 +05:30
expect(json_response['properties']['notify_only_broken_pipelines']).to eq(true)
end
end
2021-01-03 14:25:43 +05:30
2021-09-30 23:02:18 +05:30
describe 'Hangouts Chat integration' do
let(:integration_name) { 'hangouts-chat' }
2021-01-03 14:25:43 +05:30
let(:params) do
{
webhook: 'https://hook.example.com',
branches_to_be_notified: 'default'
}
end
before do
2021-09-04 01:27:46 +05:30
project.create_hangouts_chat_integration(
2021-01-03 14:25:43 +05:30
active: true,
properties: params
)
end
it 'accepts branches_to_be_notified for update', :aggregate_failures do
2021-09-30 23:02:18 +05:30
put api("/projects/#{project.id}/services/#{integration_name}", user), params: params.merge(branches_to_be_notified: 'all')
2021-01-03 14:25:43 +05:30
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
2021-09-30 23:02:18 +05:30
put api("/projects/#{project.id}/services/#{integration_name}", user), params: { webhook: 'https://hook.example.com' }
2021-01-03 14:25:43 +05:30
expect(response).to have_gitlab_http_status(:ok)
end
end
2021-09-30 23:02:18 +05:30
describe 'Pipelines Email Integration' do
let(:integration_name) { 'pipelines-email' }
context 'notify_only_broken_pipelines property was saved as a string' do
before do
project.create_pipelines_email_integration(
active: false,
properties: {
"notify_only_broken_pipelines": "true",
"branches_to_be_notified": "default"
}
)
end
it 'returns boolean values for notify_only_broken_pipelines' do
get api("/projects/#{project.id}/services/#{integration_name}", user)
expect(json_response['properties']['notify_only_broken_pipelines']).to eq(true)
end
end
end
2014-09-02 18:07:02 +05:30
end