Commit b8dc935e authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '213950-refactor-feature-specs-around-project-services' into 'master'

Refactor feature specs around project services

See merge request gitlab-org/gitlab!29470
parents f8036bfc 6b6b779a
......@@ -30,7 +30,7 @@ class Projects::MattermostsController < Projects::ApplicationController
def configure_params
params.require(:mattermost).permit(:trigger, :team_id).merge(
url: service_trigger_url(@service),
icon_url: asset_url('slash-command-logo.png'))
icon_url: asset_url('slash-command-logo.png', skip_pipeline: true))
end
def teams
......
......@@ -57,7 +57,7 @@
.form-group
= label_tag nil, _('Customize icon'), class: 'col-12 col-form-label label-bold'
.col-12
= image_tag(asset_url('slash-command-logo.png'), width: 36, height: 36, class: 'mr-3')
= image_tag(asset_url('slash-command-logo.png', skip_pipeline: true), width: 36, height: 36, class: 'mr-3')
= link_to(_('Download image'), asset_url('gitlab_logo.png'), class: 'btn btn-sm', target: '_blank', rel: 'noopener noreferrer')
.form-group
......
......@@ -5,8 +5,8 @@ require 'spec_helper'
describe 'Prometheus custom metrics', :js do
include PrometheusHelpers
let(:project) { create(:project) }
let(:user) { create(:user) }
include_context 'project service activation'
let!(:prometheus_metric) { create(:prometheus_metric, project: project) }
around do |example|
......@@ -14,30 +14,18 @@ describe 'Prometheus custom metrics', :js do
end
before do
project.add_maintainer(user)
sign_in(user)
visit(project_settings_integrations_path(project))
click_link('Prometheus')
stub_request(:get, prometheus_query_with_time_url('avg(metric)', Time.now.utc))
create(:prometheus_service, project: project, api_url: 'http://prometheus.example.com', manual_configuration: '1', active: true)
click_link('Prometheus')
visit_project_integration('Prometheus')
end
it 'Deletes a custom metric' do
wait_for_requests
it 'deletes a custom metric' do
first('.custom-metric-link-bold').click
click_button('Delete')
click_button('Delete metric')
wait_for_requests
expect(all('.custom-metric-link-bold').count).to eq(0)
end
end
......@@ -3,16 +3,12 @@
require 'spec_helper'
describe 'Disable individual triggers' do
let(:project) { create(:project) }
let(:user) { project.owner }
include_context 'project service activation'
let(:checkbox_selector) { 'input[type=checkbox][id$=_events]' }
before do
sign_in(user)
visit(project_settings_integrations_path(project))
click_link(service_name)
visit_project_integration(service_name)
end
context 'service has multiple supported events' do
......
......@@ -3,26 +3,18 @@
require 'spec_helper'
describe 'Prometheus external alerts', :js do
let(:project) { create(:project) }
let(:user) { create(:user) }
include_context 'project service activation'
let(:alerts_section_selector) { '.js-prometheus-alerts' }
let(:alerts_section) { page.find(alerts_section_selector) }
before do
sign_in(user)
project.add_maintainer(user)
visit_edit_service
end
context 'with manual configuration' do
before do
create(:prometheus_service, project: project, api_url: 'http://prometheus.example.com', manual_configuration: '1', active: true)
end
it 'shows the Alerts section' do
visit_edit_service
visit_project_integration('Prometheus')
expect(alerts_section).to have_content('Alerts')
expect(alerts_section).to have_content('Receive alerts from manually configured Prometheus servers.')
......@@ -33,16 +25,10 @@ describe 'Prometheus external alerts', :js do
context 'with no configuration' do
it 'does not show the Alerts section' do
visit_project_integration('Prometheus')
wait_for_requests
expect(page).not_to have_css(alerts_section_selector)
end
end
private
def visit_edit_service
visit(project_settings_integrations_path(project))
click_link('Prometheus')
end
end
......@@ -3,29 +3,17 @@
require 'spec_helper'
describe 'User activates issue tracker', :js do
let(:user) { create(:user) }
let(:project) { create(:project) }
include_context 'project service activation'
let(:url) { 'http://tracker.example.com' }
def fill_short_form(disabled: false)
find('input[name="service[active]"] + button').click if disabled
def fill_form(disable: false, skip_new_issue_url: false)
click_active_toggle if disable
fill_in 'service_project_url', with: url
fill_in 'service_issues_url', with: "#{url}/:id"
end
def fill_full_form(disabled: false)
fill_short_form(disabled: disabled)
fill_in 'service_new_issue_url', with: url
end
before do
project.add_maintainer(user)
sign_in(user)
visit project_settings_integrations_path(project)
fill_in 'service_new_issue_url', with: url unless skip_new_issue_url
end
shared_examples 'external issue tracker activation' do |tracker:, skip_new_issue_url: false|
......@@ -34,16 +22,10 @@ describe 'User activates issue tracker', :js do
before do
stub_request(:head, url).to_return(headers: { 'Content-Type' => 'application/json' })
click_link(tracker)
if skip_new_issue_url
fill_short_form
else
fill_full_form
end
visit_project_integration(tracker)
fill_form(skip_new_issue_url: skip_new_issue_url)
click_button('Test settings and save changes')
wait_for_requests
click_test_integration
end
it 'activates the service' do
......@@ -62,22 +44,10 @@ describe 'User activates issue tracker', :js do
it 'activates the service' do
stub_request(:head, url).to_raise(Gitlab::HTTP::Error)
click_link(tracker)
visit_project_integration(tracker)
fill_form(skip_new_issue_url: skip_new_issue_url)
if skip_new_issue_url
fill_short_form
else
fill_full_form
end
click_button('Test settings and save changes')
wait_for_requests
expect(find('.flash-container-page')).to have_content 'Test failed.'
expect(find('.flash-container-page')).to have_content 'Save anyway'
find('.flash-alert .flash-action').click
wait_for_requests
click_test_then_save_integration
expect(page).to have_content("#{tracker} activated.")
expect(current_path).to eq(project_settings_integrations_path(project))
......@@ -87,13 +57,8 @@ describe 'User activates issue tracker', :js do
describe 'user disables the service' do
before do
click_link(tracker)
if skip_new_issue_url
fill_short_form(disabled: true)
else
fill_full_form(disabled: true)
end
visit_project_integration(tracker)
fill_form(disable: true, skip_new_issue_url: skip_new_issue_url)
click_button('Save changes')
end
......
......@@ -3,14 +3,13 @@
require 'spec_helper'
describe 'User activates Jira', :js do
let(:user) { create(:user) }
let(:project) { create(:project) }
include_context 'project service activation'
let(:url) { 'http://jira.example.com' }
let(:test_url) { 'http://jira.example.com/rest/api/2/serverInfo' }
def fill_form(disabled: false)
find('input[name="service[active]"] + button').click if disabled
def fill_form(disable: false)
click_active_toggle if disable
fill_in 'service_url', with: url
fill_in 'service_username', with: 'username'
......@@ -18,23 +17,15 @@ describe 'User activates Jira', :js do
fill_in 'service_jira_issue_transition_id', with: '25'
end
before do
project.add_maintainer(user)
sign_in(user)
visit project_settings_integrations_path(project)
end
describe 'user sets and activates Jira Service' do
context 'when Jira connection test succeeds' do
before do
server_info = { key: 'value' }.to_json
WebMock.stub_request(:get, test_url).with(basic_auth: %w(username password)).to_return(body: server_info)
stub_request(:get, test_url).with(basic_auth: %w(username password)).to_return(body: server_info)
click_link('Jira')
visit_project_integration('Jira')
fill_form
click_button('Test settings and save changes')
wait_for_requests
click_test_integration
end
it 'activates the Jira service' do
......@@ -51,10 +42,10 @@ describe 'User activates Jira', :js do
context 'when Jira connection test fails' do
it 'shows errors when some required fields are not filled in' do
click_link('Jira')
visit_project_integration('Jira')
fill_in 'service_password', with: 'password'
click_button('Test settings and save changes')
click_test_integration
page.within('.service-settings') do
expect(page).to have_content('This field is required.')
......@@ -62,19 +53,12 @@ describe 'User activates Jira', :js do
end
it 'activates the Jira service' do
WebMock.stub_request(:get, test_url).with(basic_auth: %w(username password))
stub_request(:get, test_url).with(basic_auth: %w(username password))
.to_raise(JIRA::HTTPError.new(double(message: 'message')))
click_link('Jira')
visit_project_integration('Jira')
fill_form
click_button('Test settings and save changes')
wait_for_requests
expect(find('.flash-container-page')).to have_content 'Test failed. message'
expect(find('.flash-container-page')).to have_content 'Save anyway'
find('.flash-alert .flash-action').click
wait_for_requests
click_test_then_save_integration
expect(page).to have_content('Jira activated.')
expect(current_path).to eq(project_settings_integrations_path(project))
......@@ -84,8 +68,8 @@ describe 'User activates Jira', :js do
describe 'user disables the Jira Service' do
before do
click_link('Jira')
fill_form(disabled: true)
visit_project_integration('Jira')
fill_form(disable: true)
click_button('Save changes')
end
......
......@@ -3,158 +3,158 @@
require 'spec_helper'
describe 'Set up Mattermost slash commands', :js do
let(:user) { create(:user) }
let(:project) { create(:project) }
let(:mattermost_enabled) { true }
before do
stub_mattermost_setting(enabled: mattermost_enabled)
project.add_maintainer(user)
sign_in(user)
visit edit_project_service_path(project, :mattermost_slash_commands)
end
describe 'user visits the mattermost slash command config page' do
it 'shows a help message' do
expect(page).to have_content("This service allows users to perform common")
include_context 'project service activation'
before do
stub_mattermost_setting(enabled: mattermost_enabled)
visit_project_integration('Mattermost slash commands')
end
it 'shows a token placeholder' do
token_placeholder = find_field('service_token')['placeholder']
context 'mattermost service is enabled' do
let(:mattermost_enabled) { true }
expect(token_placeholder).to eq('XXxxXXxxXXxxXXxxXXxxXXxx')
end
it 'shows a help message' do
expect(page).to have_content("This service allows users to perform common")
end
it 'redirects to the integrations page after saving but not activating' do
token = ('a'..'z').to_a.join
it 'shows a token placeholder' do
token_placeholder = find_field('service_token')['placeholder']
fill_in 'service_token', with: token
find('input[name="service[active]"] + button').click
click_on 'Save changes'
expect(token_placeholder).to eq('XXxxXXxxXXxxXXxxXXxxXXxx')
end
expect(current_path).to eq(project_settings_integrations_path(project))
expect(page).to have_content('Mattermost slash commands settings saved, but not activated.')
end
it 'redirects to the integrations page after saving but not activating' do
token = ('a'..'z').to_a.join
it 'redirects to the integrations page after activating' do
token = ('a'..'z').to_a.join
fill_in 'service_token', with: token
click_active_toggle
click_on 'Save changes'
fill_in 'service_token', with: token
click_on 'Save changes'
expect(current_path).to eq(project_settings_integrations_path(project))
expect(page).to have_content('Mattermost slash commands settings saved, but not activated.')
end
expect(current_path).to eq(project_settings_integrations_path(project))
expect(page).to have_content('Mattermost slash commands activated.')
end
it 'redirects to the integrations page after activating' do
token = ('a'..'z').to_a.join
it 'shows the add to mattermost button' do
expect(page).to have_link('Add to Mattermost')
end
fill_in 'service_token', with: token
click_on 'Save changes'
it 'shows an explanation if user is a member of no teams' do
stub_teams(count: 0)
expect(current_path).to eq(project_settings_integrations_path(project))
expect(page).to have_content('Mattermost slash commands activated.')
end
click_link 'Add to Mattermost'
it 'shows the add to mattermost button' do
expect(page).to have_link('Add to Mattermost')
end
expect(page).to have_content('You aren’t a member of any team on the Mattermost instance')
expect(page).to have_link('join a team', href: "#{Gitlab.config.mattermost.host}/select_team")
end
it 'shows an explanation if user is a member of no teams' do
stub_teams(count: 0)
it 'shows an explanation if user is a member of 1 team' do
stub_teams(count: 1)
click_link 'Add to Mattermost'
click_link 'Add to Mattermost'
expect(page).to have_content('You aren’t a member of any team on the Mattermost instance')
expect(page).to have_link('join a team', href: "#{Gitlab.config.mattermost.host}/select_team")
end
expect(page).to have_content('The team where the slash commands will be used in')
expect(page).to have_content('This is the only available team that you are a member of.')
end
it 'shows an explanation if user is a member of 1 team' do
stub_teams(count: 1)
it 'shows a disabled prefilled select if user is a member of 1 team' do
teams = stub_teams(count: 1)
click_link 'Add to Mattermost'
click_link 'Add to Mattermost'
expect(page).to have_content('The team where the slash commands will be used in')
expect(page).to have_content('This is the only available team that you are a member of.')
end
team_name = teams.first['display_name']
select_element = find('#mattermost_team_id')
selected_option = select_element.find('option[selected]')
it 'shows a disabled prefilled select if user is a member of 1 team' do
teams = stub_teams(count: 1)
expect(select_element['disabled']).to eq("true")
expect(selected_option).to have_content(team_name.to_s)
end
click_link 'Add to Mattermost'
it 'has a hidden input for the prefilled value if user is a member of 1 team' do
teams = stub_teams(count: 1)
team_name = teams.first['display_name']
select_element = find('#mattermost_team_id')
selected_option = select_element.find('option[selected]')
click_link 'Add to Mattermost'
expect(select_element['disabled']).to eq("true")
expect(selected_option).to have_content(team_name.to_s)
end
expect(find('input#mattermost_team_id', visible: false).value).to eq(teams.first['id'])
end
it 'has a hidden input for the prefilled value if user is a member of 1 team' do
teams = stub_teams(count: 1)
it 'shows an explanation user is a member of multiple teams' do
stub_teams(count: 2)
click_link 'Add to Mattermost'
click_link 'Add to Mattermost'
expect(find('input#mattermost_team_id', visible: false).value).to eq(teams.first['id'])
end
expect(page).to have_content('Select the team where the slash commands will be used in')
expect(page).to have_content('The list shows all available teams that you are a member of.')
end
it 'shows an explanation user is a member of multiple teams' do
stub_teams(count: 2)
it 'shows a select with team options user is a member of multiple teams' do
stub_teams(count: 2)
click_link 'Add to Mattermost'
click_link 'Add to Mattermost'
expect(page).to have_content('Select the team where the slash commands will be used in')
expect(page).to have_content('The list shows all available teams that you are a member of.')
end
select_element = find('#mattermost_team_id')
it 'shows a select with team options user is a member of multiple teams' do
stub_teams(count: 2)
expect(select_element['disabled']).to be_falsey
expect(select_element.all('option').count).to eq(3)
end
click_link 'Add to Mattermost'
it 'shows an error alert with the error message if there is an error requesting teams' do
allow_any_instance_of(MattermostSlashCommandsService).to receive(:list_teams) { [[], 'test mattermost error message'] }
select_element = find('#mattermost_team_id')
click_link 'Add to Mattermost'
expect(select_element['disabled']).to be_falsey
expect(select_element.all('option').count).to eq(3)
end
expect(page).to have_selector('.alert')
expect(page).to have_content('test mattermost error message')
end
it 'shows an error alert with the error message if there is an error requesting teams' do
allow_any_instance_of(MattermostSlashCommandsService).to receive(:list_teams) { [[], 'test mattermost error message'] }
it 'enables the submit button if the required fields are provided', :js do
stub_teams(count: 1)
click_link 'Add to Mattermost'
click_link 'Add to Mattermost'
expect(page).to have_selector('.alert')
expect(page).to have_content('test mattermost error message')
end
expect(find('input[type="submit"]')['disabled']).not_to eq("true")
end
it 'enables the submit button if the required fields are provided', :js do
stub_teams(count: 1)
it 'disables the submit button if the required fields are not provided', :js do
stub_teams(count: 1)
click_link 'Add to Mattermost'
click_link 'Add to Mattermost'
expect(find('input[type="submit"]')['disabled']).not_to eq("true")
end
fill_in('mattermost_trigger', with: '')
it 'disables the submit button if the required fields are not provided', :js do
stub_teams(count: 1)
expect(find('input[type="submit"]')['disabled']).to eq("true")
end
click_link 'Add to Mattermost'
def stub_teams(count: 0)
teams = create_teams(count)
fill_in('mattermost_trigger', with: '')
allow_any_instance_of(MattermostSlashCommandsService).to receive(:list_teams) { [teams, nil] }
expect(find('input[type="submit"]')['disabled']).to eq("true")
end
teams
end
def stub_teams(count: 0)
teams = create_teams(count)
def create_teams(count = 0)
teams = []
allow_any_instance_of(MattermostSlashCommandsService).to receive(:list_teams) { [teams, nil] }
count.times do |i|
teams.push({ "id" => "x#{i}", "display_name" => "x#{i}-name" })
teams
end
teams
def create_teams(count = 0)
teams = []
count.times do |i|
teams.push({ "id" => "x#{i}", "display_name" => "x#{i}-name" })
end
teams
end
end
describe 'mattermost service is not enabled' do
context 'mattermost service is not enabled' do
let(:mattermost_enabled) { false }
it 'shows the correct trigger url' do
......
......@@ -3,13 +3,10 @@
require 'spec_helper'
describe 'Slack slash commands' do
let(:user) { create(:user) }
let(:project) { create(:project) }
include_context 'project service activation'
before do
project.add_maintainer(user)
sign_in(user)
visit edit_project_service_path(project, :slack_slash_commands)
visit_project_integration('Slack slash commands')
end
it 'shows a token placeholder' do
......@@ -24,7 +21,7 @@ describe 'Slack slash commands' do
it 'redirects to the integrations page after saving but not activating', :js do
fill_in 'service_token', with: 'token'
find('input[name="service[active]"] + button').click
click_active_toggle
click_on 'Save'
expect(current_path).to eq(project_settings_integrations_path(project))
......
# frozen_string_literal: true
require 'spec_helper'
describe 'User activates issue tracker', :js do
let(:user) { create(:user) }
let(:project) { create(:project) }
let(:url) { 'http://tracker.example.com' }
def fill_form(disabled: false)
find('input[name="service[active]"] + button').click if disabled
fill_in 'service_project_url', with: url
fill_in 'service_issues_url', with: "#{url}/:id"
end
before do
project.add_maintainer(user)
sign_in(user)
visit project_settings_integrations_path(project)
end
shared_examples 'external issue tracker activation' do |tracker:|
describe 'user sets and activates the Service' do
context 'when the connection test succeeds' do
before do
stub_request(:head, url).to_return(headers: { 'Content-Type' => 'application/json' })
click_link(tracker)
fill_form
click_button('Test settings and save changes')
wait_for_requests
end
it 'activates the service' do
expect(page).to have_content("#{tracker} activated.")
expect(current_path).to eq(project_settings_integrations_path(project))
end
it 'shows the link in the menu' do
page.within('.nav-sidebar') do
expect(page).to have_link(tracker, href: url)
end
end
end
context 'when the connection test fails' do
it 'activates the service' do
stub_request(:head, url).to_raise(Gitlab::HTTP::Error)
click_link(tracker)
fill_form
click_button('Test settings and save changes')
wait_for_requests
expect(find('.flash-container-page')).to have_content 'Test failed.'
expect(find('.flash-container-page')).to have_content 'Save anyway'
find('.flash-alert .flash-action').click
wait_for_requests
expect(page).to have_content("#{tracker} activated.")
expect(current_path).to eq(project_settings_integrations_path(project))
end
end
end
describe 'user disables the service' do
before do
click_link(tracker)
fill_form(disabled: true)
click_button('Save changes')
end
it 'saves but does not activate the service' do
expect(page).to have_content("#{tracker} settings saved, but not activated.")
expect(current_path).to eq(project_settings_integrations_path(project))
end
it 'does not show the external tracker link in the menu' do
page.within('.nav-sidebar') do
expect(page).not_to have_link(tracker, href: url)
end
end
end
end
it_behaves_like 'external issue tracker activation', tracker: 'YouTrack'
end
......@@ -18,6 +18,10 @@ shared_context 'project service activation' do
click_link(name)
end
def click_active_toggle
find('input[name="service[active]"] + button').click
end
def click_test_integration
click_button('Test settings and save changes')
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment