2013-10-29 14:39:46 +00:00
|
|
|
require "spec_helper"
|
|
|
|
|
2014-04-11 19:45:56 +00:00
|
|
|
describe API::API, api: true do
|
2013-10-29 14:39:46 +00:00
|
|
|
include ApiHelpers
|
|
|
|
let(:user) { create(:user) }
|
2015-10-12 13:24:00 +00:00
|
|
|
let(:admin) { create(:admin) }
|
|
|
|
let(:user2) { create(:user) }
|
2014-01-22 19:03:52 +00:00
|
|
|
let(:project) {create(:project, creator_id: user.id, namespace: user.namespace) }
|
2013-10-29 14:39:46 +00:00
|
|
|
|
2015-08-26 23:58:49 +00:00
|
|
|
Service.available_services_names.each do |service|
|
|
|
|
describe "PUT /projects/:id/services/#{service.dasherize}" do
|
|
|
|
include_context service
|
|
|
|
|
2016-08-01 15:00:44 +00:00
|
|
|
it "updates #{service} settings" do
|
2015-08-26 23:58:49 +00:00
|
|
|
put api("/projects/#{project.id}/services/#{dashed_service}", user), service_attrs
|
|
|
|
|
2016-06-27 18:10:42 +00:00
|
|
|
expect(response).to have_http_status(200)
|
2015-08-26 23:58:49 +00:00
|
|
|
end
|
|
|
|
|
2016-08-01 15:00:44 +00:00
|
|
|
it "returns if required fields missing" do
|
2015-08-26 23:58:49 +00:00
|
|
|
attrs = service_attrs
|
2015-09-16 09:43:16 +00:00
|
|
|
|
2015-08-26 23:58:49 +00:00
|
|
|
required_attributes = service_attrs_list.select do |attr|
|
2015-09-16 09:43:16 +00:00
|
|
|
service_klass.validators_on(attr).any? do |v|
|
2015-08-26 23:58:49 +00:00
|
|
|
v.class == ActiveRecord::Validations::PresenceValidator
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if required_attributes.empty?
|
|
|
|
expected_code = 200
|
|
|
|
else
|
2015-12-15 02:53:52 +00:00
|
|
|
attrs.delete(required_attributes.sample)
|
2015-08-26 23:58:49 +00:00
|
|
|
expected_code = 400
|
|
|
|
end
|
2016-08-01 15:00:44 +00:00
|
|
|
|
2015-08-26 23:58:49 +00: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 15:00:44 +00:00
|
|
|
it "deletes #{service}" do
|
2015-08-26 23:58:49 +00:00
|
|
|
delete api("/projects/#{project.id}/services/#{dashed_service}", user)
|
|
|
|
|
2016-06-27 18:10:42 +00:00
|
|
|
expect(response).to have_http_status(200)
|
2015-11-09 15:48:03 +00:00
|
|
|
project.send(service_method).reload
|
2015-08-26 23:58:49 +00:00
|
|
|
expect(project.send(service_method).activated?).to be_falsey
|
|
|
|
end
|
2014-10-14 17:07:34 +00:00
|
|
|
end
|
2015-09-03 13:38:54 +00:00
|
|
|
|
|
|
|
describe "GET /projects/:id/services/#{service.dasherize}" do
|
|
|
|
include_context service
|
|
|
|
|
2015-10-12 13:24:00 +00:00
|
|
|
# inject some properties into the service
|
|
|
|
before do
|
2016-11-16 11:46:07 +00:00
|
|
|
service_object = project.find_or_initialize_service(service)
|
2015-10-12 13:24:00 +00:00
|
|
|
service_object.properties = service_attrs
|
|
|
|
service_object.save
|
|
|
|
end
|
|
|
|
|
2016-08-01 15:00:44 +00:00
|
|
|
it 'returns authentication error when unauthenticated' do
|
2015-10-12 13:24:00 +00:00
|
|
|
get api("/projects/#{project.id}/services/#{dashed_service}")
|
2016-06-27 18:10:42 +00:00
|
|
|
expect(response).to have_http_status(401)
|
2015-10-12 13:24:00 +00:00
|
|
|
end
|
2016-08-01 15:00:44 +00:00
|
|
|
|
|
|
|
it "returns all properties of service #{service} when authenticated as admin" do
|
2015-10-12 13:24:00 +00:00
|
|
|
get api("/projects/#{project.id}/services/#{dashed_service}", admin)
|
2016-08-01 15:00:44 +00:00
|
|
|
|
2016-06-27 18:10:42 +00:00
|
|
|
expect(response).to have_http_status(200)
|
2015-10-12 13:24:00 +00:00
|
|
|
expect(json_response['properties'].keys.map(&:to_sym)).to match_array(service_attrs_list.map)
|
|
|
|
end
|
|
|
|
|
2016-08-01 15:00:44 +00:00
|
|
|
it "returns properties of service #{service} other than passwords when authenticated as project owner" do
|
2015-09-03 13:38:54 +00:00
|
|
|
get api("/projects/#{project.id}/services/#{dashed_service}", user)
|
|
|
|
|
2016-06-27 18:10:42 +00:00
|
|
|
expect(response).to have_http_status(200)
|
2015-10-12 13:24:00 +00:00
|
|
|
expect(json_response['properties'].keys.map(&:to_sym)).to match_array(service_attrs_list_without_passwords)
|
2015-09-03 13:38:54 +00:00
|
|
|
end
|
2015-10-12 13:24:00 +00:00
|
|
|
|
2016-08-01 15:00:44 +00:00
|
|
|
it "returns error when authenticated but not a project owner" do
|
2015-10-12 13:24:00 +00:00
|
|
|
project.team << [user2, :developer]
|
|
|
|
get api("/projects/#{project.id}/services/#{dashed_service}", user2)
|
2016-08-01 15:00:44 +00:00
|
|
|
|
2016-06-27 18:10:42 +00:00
|
|
|
expect(response).to have_http_status(403)
|
2015-10-12 13:24:00 +00:00
|
|
|
end
|
2015-09-03 13:38:54 +00:00
|
|
|
end
|
2014-10-14 17:07:34 +00:00
|
|
|
end
|
2016-11-18 09:00:40 +00:00
|
|
|
|
|
|
|
describe 'POST /projects/:id/services/:slug/trigger' do
|
|
|
|
let!(:project) { create(:empty_project) }
|
2016-11-18 15:16:45 +00:00
|
|
|
let(:service_name) { 'mattermost_slash_commands' }
|
2016-11-18 09:00:40 +00:00
|
|
|
|
|
|
|
context 'no service is available' do
|
|
|
|
it 'returns a not found message' do
|
2016-11-18 11:08:30 +00:00
|
|
|
post api("/projects/#{project.id}/services/idonotexist/trigger")
|
2016-11-18 09:00:40 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(404)
|
2016-11-18 11:08:30 +00:00
|
|
|
expect(json_response["message"]).to eq("404 Service Not Found")
|
2016-11-18 09:00:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'the service exists' do
|
2016-11-18 10:38:54 +00:00
|
|
|
let(:params) { { token: 'token' } }
|
|
|
|
|
2016-11-18 09:00:40 +00:00
|
|
|
context 'the service is not active' do
|
|
|
|
let!(:inactive_service) do
|
2016-11-18 15:16:45 +00:00
|
|
|
project.create_mattermost_slash_commands_service(
|
2016-11-18 09:00:40 +00:00
|
|
|
active: false,
|
|
|
|
properties: { token: 'token' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'when the service is inactive' do
|
2016-11-18 15:16:45 +00:00
|
|
|
post api("/projects/#{project.id}/services/mattermost_slash_commands/trigger")
|
2016-11-18 09:00:40 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'the service is active' do
|
|
|
|
let!(:active_service) do
|
2016-11-18 15:16:45 +00:00
|
|
|
project.create_mattermost_slash_commands_service(
|
2016-11-18 09:00:40 +00:00
|
|
|
active: true,
|
|
|
|
properties: { token: 'token' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retusn status 200' do
|
2016-11-18 15:16:45 +00:00
|
|
|
post api("/projects/#{project.id}/services/mattermost_slash_commands/trigger"), params
|
2016-11-18 09:00:40 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
|
|
|
end
|
2016-11-18 10:38:54 +00:00
|
|
|
|
|
|
|
context 'when the project can not be found' do
|
|
|
|
it 'returns a generic 404' do
|
2016-11-18 15:16:45 +00:00
|
|
|
post api("/projects/404/services/mattermost_slash_commands/trigger"), params
|
2016-11-18 10:38:54 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(404)
|
2016-11-18 11:08:30 +00:00
|
|
|
expect(json_response["message"]).to eq("404 Service Not Found")
|
2016-11-18 10:38:54 +00:00
|
|
|
end
|
|
|
|
end
|
2016-11-18 09:00:40 +00:00
|
|
|
end
|
|
|
|
end
|
2013-10-29 14:39:46 +00:00
|
|
|
end
|