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

224 lines
7.2 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
require "spec_helper"
2017-08-17 22:00:37 +05:30
describe API::Services do
2018-03-17 18:26:18 +05:30
set(:user) { create(:user) }
set(:admin) { create(:admin) }
set(:user2) { create(:user) }
set(:project) do
create(:project, creator_id: user.id, namespace: user.namespace)
end
2014-09-02 18:07:02 +05:30
2015-09-25 12:07:36 +05:30
Service.available_services_names.each do |service|
2019-10-12 21:52:04 +05:30
describe "PUT /projects/:id/services/#{service.dasherize}" do
2015-09-25 12:07:36 +05:30
include_context service
2014-09-02 18:07:02 +05:30
2016-09-13 17:45:13 +05:30
it "updates #{service} settings" do
2019-02-15 15:39:39 +05:30
put api("/projects/#{project.id}/services/#{dashed_service}", user), params: service_attrs
2014-09-02 18:07:02 +05:30
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(200)
2017-08-17 22:00:37 +05:30
current_service = project.services.first
2019-09-04 21:01:54 +05:30
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('&')
2017-08-17 22:00:37 +05:30
2019-09-04 21:01:54 +05:30
put api("/projects/#{project.id}/services/#{dashed_service}?#{query_strings}", user), params: service_attrs
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(200)
2019-09-04 21:01:54 +05:30
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-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
2015-09-25 12:07:36 +05:30
attrs = service_attrs
2015-09-11 14:41:01 +05:30
2015-09-25 12:07:36 +05:30
required_attributes = service_attrs_list.select do |attr|
service_klass.validators_on(attr).any? do |v|
v.class == ActiveRecord::Validations::PresenceValidator
end
end
2015-09-11 14:41:01 +05:30
2015-09-25 12:07:36 +05:30
if required_attributes.empty?
expected_code = 200
else
2015-12-23 02:04:40 +05:30
attrs.delete(required_attributes.sample)
2015-09-25 12:07:36 +05:30
expected_code = 400
end
2016-09-13 17:45:13 +05:30
2019-02-15 15:39:39 +05:30
put api("/projects/#{project.id}/services/#{dashed_service}", user), params: attrs
2015-09-11 14:41:01 +05:30
2015-09-25 12:07:36 +05:30
expect(response.status).to eq(expected_code)
end
2015-09-11 14:41:01 +05:30
end
2014-09-02 18:07:02 +05:30
2019-10-12 21:52:04 +05:30
describe "DELETE /projects/:id/services/#{service.dasherize}" do
2015-09-25 12:07:36 +05:30
include_context service
2014-09-02 18:07:02 +05:30
2018-03-17 18:26:18 +05:30
before do
initialize_service(service)
end
2016-09-13 17:45:13 +05:30
it "deletes #{service}" do
2015-09-25 12:07:36 +05:30
delete api("/projects/#{project.id}/services/#{dashed_service}", user)
2015-04-26 12:48:37 +05:30
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(204)
2015-11-26 14:37:03 +05:30
project.send(service_method).reload
2015-09-25 12:07:36 +05:30
expect(project.send(service_method).activated?).to be_falsey
end
2015-04-26 12:48:37 +05:30
end
2015-09-25 12:07:36 +05:30
describe "GET /projects/:id/services/#{service.dasherize}" do
include_context service
2015-04-26 12:48:37 +05:30
2015-10-24 18:46:33 +05:30
# inject some properties into the service
2019-09-30 21:07:59 +05:30
let!(:initialized_service) { initialize_service(service) }
2015-10-24 18:46:33 +05:30
2016-09-13 17:45:13 +05:30
it 'returns authentication error when unauthenticated' do
2015-10-24 18:46:33 +05:30
get api("/projects/#{project.id}/services/#{dashed_service}")
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(401)
2015-10-24 18:46:33 +05:30
end
2016-09-13 17:45:13 +05:30
it "returns all properties of service #{service} when authenticated as admin" do
2015-10-24 18:46:33 +05:30
get api("/projects/#{project.id}/services/#{dashed_service}", admin)
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(200)
expect(json_response['properties'].keys).to match_array(service_instance.api_field_names)
2015-10-24 18:46:33 +05:30
end
2016-09-13 17:45:13 +05:30
it "returns properties of service #{service} other than passwords when authenticated as project owner" do
2015-09-25 12:07:36 +05:30
get api("/projects/#{project.id}/services/#{dashed_service}", user)
2015-04-26 12:48:37 +05:30
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(200)
expect(json_response['properties'].keys).to match_array(service_instance.api_field_names)
2015-09-25 12:07:36 +05:30
end
2015-10-24 18:46:33 +05:30
2019-12-04 20:38:33 +05:30
it "returns empty hash if properties and data fields are empty" do
2019-09-30 21:07:59 +05:30
# deprecated services are not valid for update
initialized_service.update_attribute(:properties, {})
2019-12-04 20:38:33 +05:30
if initialized_service.data_fields_present?
initialized_service.data_fields.destroy
initialized_service.reload
end
2019-09-30 21:07:59 +05:30
get api("/projects/#{project.id}/services/#{dashed_service}", user)
expect(response).to have_gitlab_http_status(200)
expect(json_response['properties'].keys).to be_empty
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)
2015-10-24 18:46:33 +05:30
get api("/projects/#{project.id}/services/#{dashed_service}", user2)
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(403)
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
describe 'Mattermost Service' do
let(:service_name) { 'mattermost_slash_commands' }
context 'no service is available' do
it 'returns a not found message' do
post api("/projects/#{project.id}/services/idonotexist/trigger")
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(404)
2017-08-17 22:00:37 +05:30
expect(json_response["error"]).to eq("404 Not Found")
end
end
context 'the service exists' do
let(:params) { { token: 'token' } }
context 'the service is not active' do
before do
project.create_mattermost_slash_commands_service(
active: false,
properties: params
)
end
it 'when the service is inactive' do
2019-02-15 15:39:39 +05:30
post api("/projects/#{project.id}/services/#{service_name}/trigger"), params: params
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(404)
2017-08-17 22:00:37 +05:30
end
end
context 'the service is active' do
before do
project.create_mattermost_slash_commands_service(
active: true,
properties: params
)
end
it 'returns status 200' do
2019-02-15 15:39:39 +05:30
post api("/projects/#{project.id}/services/#{service_name}/trigger"), params: params
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(200)
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
2019-02-15 15:39:39 +05:30
post api("/projects/404/services/#{service_name}/trigger"), params: params
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(404)
2017-08-17 22:00:37 +05:30
expect(json_response["message"]).to eq("404 Service Not Found")
end
end
end
end
describe 'Slack Service' do
let(:service_name) { 'slack_slash_commands' }
before do
project.create_slack_slash_commands_service(
active: true,
properties: { token: 'token' }
)
end
it 'returns status 200' do
2019-02-15 15:39:39 +05:30
post api("/projects/#{project.id}/services/#{service_name}/trigger"), params: { token: 'token', text: 'help' }
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(200)
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
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
2019-02-15 15:39:39 +05:30
put api("/projects/#{project.id}/services/mattermost", user), params: params.merge(username: 'new_username')
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(200)
expect(json_response['properties']['username']).to eq('new_username')
end
end
2014-09-02 18:07:02 +05:30
end