2013-10-29 10:39:46 -04:00
|
|
|
require "spec_helper"
|
|
|
|
|
2017-04-21 16:32:02 -04:00
|
|
|
describe API::Services do
|
2017-09-05 04:04:40 -04:00
|
|
|
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
|
2013-10-29 10:39:46 -04:00
|
|
|
|
2015-08-26 19:58:49 -04:00
|
|
|
Service.available_services_names.each do |service|
|
|
|
|
describe "PUT /projects/:id/services/#{service.dasherize}" do
|
|
|
|
include_context service
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "updates #{service} settings" do
|
2015-08-26 19:58:49 -04:00
|
|
|
put api("/projects/#{project.id}/services/#{dashed_service}", user), service_attrs
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2016-12-27 07:44:24 -05:00
|
|
|
|
|
|
|
current_service = project.services.first
|
|
|
|
event = current_service.event_names.empty? ? "foo" : current_service.event_names.first
|
|
|
|
state = current_service[event] || false
|
|
|
|
|
|
|
|
put api("/projects/#{project.id}/services/#{dashed_service}?#{event}=#{!state}", user), service_attrs
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2016-12-27 07:44:24 -05:00
|
|
|
expect(project.services.first[event]).not_to eq(state) unless event == "foo"
|
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|
|
2015-08-26 19:58:49 -04:00
|
|
|
v.class == ActiveRecord::Validations::PresenceValidator
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if required_attributes.empty?
|
|
|
|
expected_code = 200
|
|
|
|
else
|
2015-12-14 21:53:52 -05:00
|
|
|
attrs.delete(required_attributes.sample)
|
2015-08-26 19:58:49 -04:00
|
|
|
expected_code = 400
|
|
|
|
end
|
2016-08-01 11:00:44 -04:00
|
|
|
|
2015-08-26 19:58:49 -04:00
|
|
|
put api("/projects/#{project.id}/services/#{dashed_service}", user), attrs
|
|
|
|
|
|
|
|
expect(response.status).to eq(expected_code)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "DELETE /projects/:id/services/#{service.dasherize}" do
|
|
|
|
include_context service
|
|
|
|
|
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)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(204)
|
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
|
|
|
|
before do
|
2016-11-16 06:46:07 -05:00
|
|
|
service_object = project.find_or_initialize_service(service)
|
2015-10-12 09:24:00 -04:00
|
|
|
service_object.properties = service_attrs
|
|
|
|
service_object.save
|
|
|
|
end
|
|
|
|
|
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}")
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(401)
|
2015-10-12 09:24:00 -04:00
|
|
|
end
|
2016-08-01 11:00:44 -04:00
|
|
|
|
|
|
|
it "returns all properties of service #{service} when authenticated as admin" do
|
2015-10-12 09:24:00 -04:00
|
|
|
get api("/projects/#{project.id}/services/#{dashed_service}", admin)
|
2016-08-01 11:00:44 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2015-10-12 09:24:00 -04:00
|
|
|
expect(json_response['properties'].keys.map(&:to_sym)).to match_array(service_attrs_list.map)
|
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns properties of service #{service} other than passwords when authenticated as project owner" do
|
2015-09-03 09:38:54 -04:00
|
|
|
get api("/projects/#{project.id}/services/#{dashed_service}", user)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2015-10-12 09:24:00 -04:00
|
|
|
expect(json_response['properties'].keys.map(&:to_sym)).to match_array(service_attrs_list_without_passwords)
|
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
|
2015-10-12 09:24:00 -04:00
|
|
|
project.team << [user2, :developer]
|
|
|
|
get api("/projects/#{project.id}/services/#{dashed_service}", user2)
|
2016-08-01 11:00:44 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(403)
|
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")
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
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
|
|
|
|
post api("/projects/#{project.id}/services/#{service_name}/trigger"), params
|
2016-11-18 04:00:40 -05:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
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
|
|
|
|
post api("/projects/#{project.id}/services/#{service_name}/trigger"), params
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
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
|
|
|
|
post api("/projects/404/services/#{service_name}/trigger"), params
|
2016-11-18 04:00:40 -05:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
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
|
|
|
|
post api("/projects/#{project.id}/services/#{service_name}/trigger"), token: 'token', text: 'help'
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
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
|
|
|
|
put api("/projects/#{project.id}/services/mattermost", user), params.merge(username: 'new_username')
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
expect(json_response['properties']['username']).to eq('new_username')
|
|
|
|
end
|
|
|
|
end
|
2013-10-29 10:39:46 -04:00
|
|
|
end
|