2019-04-15 06:17:05 -04:00
# frozen_string_literal: true
2015-08-12 03:40:54 -04:00
require 'spec_helper'
2020-06-03 14:08:28 -04:00
RSpec . describe Projects :: ServicesController do
2017-01-25 16:44:33 -05:00
let ( :project ) { create ( :project , :repository ) }
2015-08-12 03:40:54 -04:00
let ( :user ) { create ( :user ) }
2018-12-12 11:30:18 -05:00
let ( :service ) { create ( :jira_service , project : project ) }
let ( :service_params ) { { username : 'username' , password : 'password' , url : 'http://example.com' } }
2015-08-12 03:40:54 -04:00
before do
sign_in ( user )
2018-07-11 10:36:08 -04:00
project . add_maintainer ( user )
2019-07-16 15:49:47 -04:00
allow ( Gitlab :: UrlBlocker ) . to receive ( :validate! ) . and_return ( [ URI . parse ( 'http://example.com' ) , nil ] )
2015-08-12 03:40:54 -04:00
end
2017-05-22 06:07:12 -04:00
describe '#test' do
context 'when can_test? returns false' do
it 'renders 404' do
allow_any_instance_of ( Service ) . to receive ( :can_test? ) . and_return ( false )
2017-04-24 11:23:51 -04:00
2019-09-09 16:22:00 -04:00
put :test , params : project_params
2017-04-24 11:23:51 -04:00
2019-09-09 16:22:00 -04:00
expect ( response ) . to have_gitlab_http_status ( :not_found )
2017-04-24 11:23:51 -04:00
end
2017-05-22 06:07:12 -04:00
end
2017-04-24 11:23:51 -04:00
2018-02-23 22:18:14 -05:00
context 'when validations fail' do
2018-12-12 11:30:18 -05:00
let ( :service_params ) { { active : 'true' , url : '' } }
2018-02-23 22:18:14 -05:00
it 'returns error messages in JSON response' do
2019-09-09 16:22:00 -04:00
put :test , params : project_params ( service : service_params )
2018-02-23 22:18:14 -05:00
2019-09-09 16:22:00 -04:00
expect ( json_response [ 'message' ] ) . to eq 'Validations failed.'
2018-12-12 11:30:18 -05:00
expect ( json_response [ 'service_response' ] ) . to include " Url can't be blank "
2019-09-09 16:22:00 -04:00
expect ( response ) . to be_successful
2018-02-23 22:18:14 -05:00
end
end
2017-05-22 06:07:12 -04:00
context 'success' do
context 'with empty project' do
2017-08-02 15:55:11 -04:00
let ( :project ) { create ( :project ) }
2017-04-24 11:23:51 -04:00
2017-05-22 06:07:12 -04:00
context 'with chat notification service' do
let ( :service ) { project . create_microsoft_teams_service ( webhook : 'http://webhook.com' ) }
2017-04-24 11:23:51 -04:00
2017-05-22 06:07:12 -04:00
it 'returns success' do
allow_any_instance_of ( MicrosoftTeams :: Notifier ) . to receive ( :ping ) . and_return ( true )
2017-04-24 11:23:51 -04:00
2019-09-09 16:22:00 -04:00
put :test , params : project_params
2017-04-24 11:23:51 -04:00
2019-09-09 16:22:00 -04:00
expect ( response ) . to be_successful
2017-04-24 11:23:51 -04:00
end
end
2017-05-22 06:07:12 -04:00
it 'returns success' do
2018-12-12 11:30:18 -05:00
stub_request ( :get , 'http://example.com/rest/api/2/serverInfo' )
. to_return ( status : 200 , body : '{}' )
2017-04-24 11:23:51 -04:00
2019-09-09 16:22:00 -04:00
expect ( Gitlab :: HTTP ) . to receive ( :get ) . with ( '/rest/api/2/serverInfo' , any_args ) . and_call_original
2019-07-16 15:49:47 -04:00
2019-09-09 16:22:00 -04:00
put :test , params : project_params ( service : service_params )
2017-04-24 11:23:51 -04:00
2019-09-09 16:22:00 -04:00
expect ( response ) . to be_successful
2015-10-20 03:28:28 -04:00
end
end
2017-05-22 06:07:12 -04:00
it 'returns success' do
2018-12-12 11:30:18 -05:00
stub_request ( :get , 'http://example.com/rest/api/2/serverInfo' )
. to_return ( status : 200 , body : '{}' )
2017-04-24 11:23:51 -04:00
2019-09-09 16:22:00 -04:00
expect ( Gitlab :: HTTP ) . to receive ( :get ) . with ( '/rest/api/2/serverInfo' , any_args ) . and_call_original
2019-07-16 15:49:47 -04:00
2019-09-09 16:22:00 -04:00
put :test , params : project_params ( service : service_params )
2017-04-24 11:23:51 -04:00
2019-09-09 16:22:00 -04:00
expect ( response ) . to be_successful
2015-08-12 03:40:54 -04:00
end
2017-08-22 12:05:02 -04:00
context 'when service is configured for the first time' do
2019-09-09 16:22:00 -04:00
let ( :service_params ) do
{
'active' = > '1' ,
'push_events' = > '1' ,
'token' = > 'token' ,
'project_url' = > 'http://test.com'
}
end
2017-08-22 12:05:02 -04:00
before do
allow_any_instance_of ( ServiceHook ) . to receive ( :execute ) . and_return ( true )
end
it 'persist the object' do
do_put
2019-09-09 16:22:00 -04:00
expect ( response ) . to be_successful
2018-12-12 11:30:18 -05:00
expect ( json_response ) . to be_empty
2017-08-22 12:05:02 -04:00
expect ( BuildkiteService . first ) . to be_present
end
it 'creates the ServiceHook object' do
do_put
2019-09-09 16:22:00 -04:00
expect ( response ) . to be_successful
2018-12-12 11:30:18 -05:00
expect ( json_response ) . to be_empty
2017-08-22 12:05:02 -04:00
expect ( BuildkiteService . first . service_hook ) . to be_present
end
def do_put
2019-09-09 16:22:00 -04:00
put :test , params : project_params ( id : 'buildkite' ,
service : service_params )
2017-08-22 12:05:02 -04:00
end
end
2015-10-20 03:28:28 -04:00
end
2017-05-22 06:07:12 -04:00
context 'failure' do
2017-05-29 05:29:43 -04:00
it 'returns success status code and the error message' do
2018-12-12 11:30:18 -05:00
stub_request ( :get , 'http://example.com/rest/api/2/serverInfo' )
. to_return ( status : 404 )
2017-05-22 06:07:12 -04:00
2019-09-09 16:22:00 -04:00
put :test , params : project_params ( service : service_params )
2017-05-22 06:07:12 -04:00
2019-09-09 16:22:00 -04:00
expect ( response ) . to be_successful
2018-12-12 11:30:18 -05:00
expect ( json_response ) . to eq (
'error' = > true ,
'message' = > 'Test failed.' ,
'service_response' = > '' ,
'test_failed' = > true
)
2017-05-22 06:07:12 -04:00
end
2015-10-20 03:28:28 -04:00
end
end
2016-09-04 09:33:14 -04:00
describe 'PUT #update' do
2019-09-09 16:22:00 -04:00
describe 'as HTML' do
let ( :service_params ) { { active : true } }
2020-06-02 17:08:00 -04:00
let ( :params ) { project_params ( service : service_params ) }
let ( :message ) { 'Jira activated.' }
2020-06-17 14:09:08 -04:00
let ( :redirect_url ) { edit_project_service_path ( project , service ) }
2017-05-22 06:07:12 -04:00
2019-09-09 16:22:00 -04:00
before do
2020-06-02 17:08:00 -04:00
put :update , params : params
end
shared_examples 'service update' do
it 'redirects to the correct url with a flash message' do
expect ( response ) . to redirect_to ( redirect_url )
expect ( flash [ :notice ] ) . to eq ( message )
end
2019-09-09 16:22:00 -04:00
end
context 'when param `active` is set to true' do
2020-06-02 17:08:00 -04:00
let ( :params ) { project_params ( service : service_params , redirect_to : redirect ) }
context 'when redirect_to param is present' do
let ( :redirect ) { '/redirect_here' }
let ( :redirect_url ) { redirect }
it_behaves_like 'service update'
end
context 'when redirect_to is an external domain' do
let ( :redirect ) { 'http://examle.com' }
it_behaves_like 'service update'
end
context 'when redirect_to param is an empty string' do
let ( :redirect ) { '' }
it_behaves_like 'service update'
2019-09-09 16:22:00 -04:00
end
end
context 'when param `active` is set to false' do
let ( :service_params ) { { active : false } }
2020-06-02 17:08:00 -04:00
let ( :message ) { 'Jira settings saved, but not activated.' }
2019-09-09 16:22:00 -04:00
2020-06-02 17:08:00 -04:00
it_behaves_like 'service update'
2017-05-22 06:07:12 -04:00
end
2016-09-04 09:33:14 -04:00
end
2018-01-04 04:33:51 -05:00
2019-09-09 16:22:00 -04:00
describe 'as JSON' do
before do
put :update , params : project_params ( service : service_params , format : :json )
end
context 'when update succeeds' do
let ( :service_params ) { { url : 'http://example.com' } }
it 'returns JSON response with no errors' do
expect ( response ) . to be_successful
expect ( json_response ) . to include ( 'active' = > true , 'errors' = > { } )
end
end
2019-02-05 07:30:06 -05:00
2019-09-09 16:22:00 -04:00
context 'when update fails' do
let ( :service_params ) { { url : '' } }
2019-02-05 07:30:06 -05:00
2019-09-09 16:22:00 -04:00
it 'returns JSON response with errors' do
expect ( response ) . to have_gitlab_http_status ( :unprocessable_entity )
expect ( json_response ) . to include (
'active' = > true ,
'errors' = > { 'url' = > [ 'must be a valid URL' , %{ can't be blank } ] }
)
end
2019-02-05 07:30:06 -05:00
end
end
2020-02-10 13:09:00 -05:00
context 'Prometheus service' do
let! ( :service ) { create ( :prometheus_service , project : project ) }
let ( :service_params ) { { manual_configuration : '1' , api_url : 'http://example.com' } }
context 'feature flag :settings_operations_prometheus_service is enabled' do
before do
stub_feature_flags ( settings_operations_prometheus_service : true )
end
it 'redirects user back to edit page with alert' do
put :update , params : project_params . merge ( service : service_params )
expect ( response ) . to redirect_to ( edit_project_service_path ( project , service ) )
expected_alert = " You can now manage your Prometheus settings on the <a href= \" #{ project_settings_operations_path ( project ) } \" >Operations</a> page. Fields on this page has been deprecated. "
expect ( response ) . to set_flash . now [ :alert ] . to ( expected_alert )
end
it 'does not modify service' do
expect { put :update , params : project_params . merge ( service : service_params ) } . not_to change { project . prometheus_service . reload . attributes }
end
end
context 'feature flag :settings_operations_prometheus_service is disabled' do
before do
stub_feature_flags ( settings_operations_prometheus_service : false )
end
it 'modifies service' do
expect { put :update , params : project_params . merge ( service : service_params ) } . to change { project . prometheus_service . reload . attributes }
end
end
end
2018-01-04 04:33:51 -05:00
end
2019-09-09 16:22:00 -04:00
describe 'GET #edit' do
2020-02-10 13:09:00 -05:00
context 'Jira service' do
let ( :service_param ) { 'jira' }
before do
get :edit , params : project_params ( id : service_param )
end
context 'with approved services' do
it 'renders edit page' do
expect ( response ) . to be_successful
end
end
2018-01-04 04:33:51 -05:00
end
2020-02-10 13:09:00 -05:00
context 'Prometheus service' do
let ( :service_param ) { 'prometheus' }
context 'feature flag :settings_operations_prometheus_service is enabled' do
before do
stub_feature_flags ( settings_operations_prometheus_service : true )
get :edit , params : project_params ( id : service_param )
end
it 'renders deprecation warning notice' do
expected_alert = " You can now manage your Prometheus settings on the <a href= \" #{ project_settings_operations_path ( project ) } \" >Operations</a> page. Fields on this page has been deprecated. "
expect ( response ) . to set_flash . now [ :alert ] . to ( expected_alert )
end
end
context 'feature flag :settings_operations_prometheus_service is disabled' do
before do
stub_feature_flags ( settings_operations_prometheus_service : false )
get :edit , params : project_params ( id : service_param )
end
it 'does not render deprecation warning notice' do
expect ( response ) . not_to set_flash . now [ :alert ]
end
2018-01-04 04:33:51 -05:00
end
end
2016-09-04 09:33:14 -04:00
end
2019-09-09 16:22:00 -04:00
private
def project_params ( opts = { } )
opts . reverse_merge (
namespace_id : project . namespace ,
project_id : project ,
id : service . to_param
)
end
2015-08-12 03:40:54 -04:00
end