Commit 11a682a9 authored by Justin Ho's avatar Justin Ho

Refactor feature specs to use shared context

Specs that test functionality around project services
can use the shared context to DRY them up when
declaring variables and in common actions.

YouTrack spec is redundant as it's already tested in
user_activates_issue_tracker_spec.rb
parent faec4fc1
...@@ -3,16 +3,12 @@ ...@@ -3,16 +3,12 @@
require 'spec_helper' require 'spec_helper'
describe 'Disable individual triggers' do describe 'Disable individual triggers' do
let(:project) { create(:project) } include_context 'project service activation'
let(:user) { project.owner }
let(:checkbox_selector) { 'input[type=checkbox][id$=_events]' } let(:checkbox_selector) { 'input[type=checkbox][id$=_events]' }
before do before do
sign_in(user) visit_project_integration(service_name)
visit(project_settings_integrations_path(project))
click_link(service_name)
end end
context 'service has multiple supported events' do context 'service has multiple supported events' do
......
...@@ -3,26 +3,18 @@ ...@@ -3,26 +3,18 @@
require 'spec_helper' require 'spec_helper'
describe 'Prometheus external alerts', :js do describe 'Prometheus external alerts', :js do
let(:project) { create(:project) } include_context 'project service activation'
let(:user) { create(:user) }
let(:alerts_section_selector) { '.js-prometheus-alerts' } let(:alerts_section_selector) { '.js-prometheus-alerts' }
let(:alerts_section) { page.find(alerts_section_selector) } 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 context 'with manual configuration' do
before do before do
create(:prometheus_service, project: project, api_url: 'http://prometheus.example.com', manual_configuration: '1', active: true) create(:prometheus_service, project: project, api_url: 'http://prometheus.example.com', manual_configuration: '1', active: true)
end end
it 'shows the Alerts section' do 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('Alerts')
expect(alerts_section).to have_content('Receive alerts from manually configured Prometheus servers.') expect(alerts_section).to have_content('Receive alerts from manually configured Prometheus servers.')
...@@ -33,16 +25,10 @@ describe 'Prometheus external alerts', :js do ...@@ -33,16 +25,10 @@ describe 'Prometheus external alerts', :js do
context 'with no configuration' do context 'with no configuration' do
it 'does not show the Alerts section' do it 'does not show the Alerts section' do
visit_project_integration('Prometheus')
wait_for_requests wait_for_requests
expect(page).not_to have_css(alerts_section_selector) expect(page).not_to have_css(alerts_section_selector)
end end
end end
private
def visit_edit_service
visit(project_settings_integrations_path(project))
click_link('Prometheus')
end
end end
...@@ -3,29 +3,17 @@ ...@@ -3,29 +3,17 @@
require 'spec_helper' require 'spec_helper'
describe 'User activates issue tracker', :js do describe 'User activates issue tracker', :js do
let(:user) { create(:user) } include_context 'project service activation'
let(:project) { create(:project) }
let(:url) { 'http://tracker.example.com' } let(:url) { 'http://tracker.example.com' }
def fill_short_form(disabled: false) def fill_form(disable: false, skip_new_issue_url: false)
find('input[name="service[active]"] + button').click if disabled find('input[name="service[active]"] + button').click if disable
fill_in 'service_project_url', with: url fill_in 'service_project_url', with: url
fill_in 'service_issues_url', with: "#{url}/:id" 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 fill_in 'service_new_issue_url', with: url unless skip_new_issue_url
end
before do
project.add_maintainer(user)
sign_in(user)
visit project_settings_integrations_path(project)
end end
shared_examples 'external issue tracker activation' do |tracker:, skip_new_issue_url: false| shared_examples 'external issue tracker activation' do |tracker:, skip_new_issue_url: false|
...@@ -34,16 +22,10 @@ describe 'User activates issue tracker', :js do ...@@ -34,16 +22,10 @@ describe 'User activates issue tracker', :js do
before do before do
stub_request(:head, url).to_return(headers: { 'Content-Type' => 'application/json' }) stub_request(:head, url).to_return(headers: { 'Content-Type' => 'application/json' })
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') click_test_integration
wait_for_requests
end end
it 'activates the service' do it 'activates the service' do
...@@ -62,22 +44,10 @@ describe 'User activates issue tracker', :js do ...@@ -62,22 +44,10 @@ describe 'User activates issue tracker', :js do
it 'activates the service' do it 'activates the service' do
stub_request(:head, url).to_raise(Gitlab::HTTP::Error) 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 click_test_then_save_integration
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
expect(page).to have_content("#{tracker} activated.") expect(page).to have_content("#{tracker} activated.")
expect(current_path).to eq(project_settings_integrations_path(project)) expect(current_path).to eq(project_settings_integrations_path(project))
...@@ -87,13 +57,8 @@ describe 'User activates issue tracker', :js do ...@@ -87,13 +57,8 @@ describe 'User activates issue tracker', :js do
describe 'user disables the service' do describe 'user disables the service' do
before do before do
click_link(tracker) visit_project_integration(tracker)
fill_form(disable: true, skip_new_issue_url: skip_new_issue_url)
if skip_new_issue_url
fill_short_form(disabled: true)
else
fill_full_form(disabled: true)
end
click_button('Save changes') click_button('Save changes')
end end
......
...@@ -3,14 +3,13 @@ ...@@ -3,14 +3,13 @@
require 'spec_helper' require 'spec_helper'
describe 'User activates Jira', :js do describe 'User activates Jira', :js do
let(:user) { create(:user) } include_context 'project service activation'
let(:project) { create(:project) }
let(:url) { 'http://jira.example.com' } let(:url) { 'http://jira.example.com' }
let(:test_url) { 'http://jira.example.com/rest/api/2/serverInfo' } let(:test_url) { 'http://jira.example.com/rest/api/2/serverInfo' }
def fill_form(disabled: false) def fill_form(disable: false)
find('input[name="service[active]"] + button').click if disabled find('input[name="service[active]"] + button').click if disable
fill_in 'service_url', with: url fill_in 'service_url', with: url
fill_in 'service_username', with: 'username' fill_in 'service_username', with: 'username'
...@@ -18,23 +17,15 @@ describe 'User activates Jira', :js do ...@@ -18,23 +17,15 @@ describe 'User activates Jira', :js do
fill_in 'service_jira_issue_transition_id', with: '25' fill_in 'service_jira_issue_transition_id', with: '25'
end 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 describe 'user sets and activates Jira Service' do
context 'when Jira connection test succeeds' do context 'when Jira connection test succeeds' do
before do before do
server_info = { key: 'value' }.to_json 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 fill_form
click_button('Test settings and save changes') click_test_integration
wait_for_requests
end end
it 'activates the Jira service' do it 'activates the Jira service' do
...@@ -51,10 +42,10 @@ describe 'User activates Jira', :js do ...@@ -51,10 +42,10 @@ describe 'User activates Jira', :js do
context 'when Jira connection test fails' do context 'when Jira connection test fails' do
it 'shows errors when some required fields are not filled in' 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' fill_in 'service_password', with: 'password'
click_button('Test settings and save changes') click_test_integration
page.within('.service-settings') do page.within('.service-settings') do
expect(page).to have_content('This field is required.') expect(page).to have_content('This field is required.')
...@@ -62,19 +53,12 @@ describe 'User activates Jira', :js do ...@@ -62,19 +53,12 @@ describe 'User activates Jira', :js do
end end
it 'activates the Jira service' do 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'))) .to_raise(JIRA::HTTPError.new(double(message: 'message')))
click_link('Jira') visit_project_integration('Jira')
fill_form fill_form
click_button('Test settings and save changes') click_test_then_save_integration
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
expect(page).to have_content('Jira activated.') expect(page).to have_content('Jira activated.')
expect(current_path).to eq(project_settings_integrations_path(project)) expect(current_path).to eq(project_settings_integrations_path(project))
...@@ -84,8 +68,8 @@ describe 'User activates Jira', :js do ...@@ -84,8 +68,8 @@ describe 'User activates Jira', :js do
describe 'user disables the Jira Service' do describe 'user disables the Jira Service' do
before do before do
click_link('Jira') visit_project_integration('Jira')
fill_form(disabled: true) fill_form(disable: true)
click_button('Save changes') click_button('Save changes')
end end
......
...@@ -3,158 +3,158 @@ ...@@ -3,158 +3,158 @@
require 'spec_helper' require 'spec_helper'
describe 'Set up Mattermost slash commands', :js do 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 describe 'user visits the mattermost slash command config page' do
it 'shows a help message' do include_context 'project service activation'
expect(page).to have_content("This service allows users to perform common")
before do
stub_mattermost_setting(enabled: mattermost_enabled)
visit_project_integration('Mattermost slash commands')
end end
it 'shows a token placeholder' do context 'mattermost service is enabled' do
token_placeholder = find_field('service_token')['placeholder'] let(:mattermost_enabled) { true }
expect(token_placeholder).to eq('XXxxXXxxXXxxXXxxXXxxXXxx') it 'shows a help message' do
end 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 it 'shows a token placeholder' do
token = ('a'..'z').to_a.join token_placeholder = find_field('service_token')['placeholder']
fill_in 'service_token', with: token expect(token_placeholder).to eq('XXxxXXxxXXxxXXxxXXxxXXxx')
find('input[name="service[active]"] + button').click end
click_on 'Save changes'
expect(current_path).to eq(project_settings_integrations_path(project)) it 'redirects to the integrations page after saving but not activating' do
expect(page).to have_content('Mattermost slash commands settings saved, but not activated.') token = ('a'..'z').to_a.join
end
it 'redirects to the integrations page after activating' do fill_in 'service_token', with: token
token = ('a'..'z').to_a.join find('input[name="service[active]"] + button').click
click_on 'Save changes'
fill_in 'service_token', with: token expect(current_path).to eq(project_settings_integrations_path(project))
click_on 'Save changes' expect(page).to have_content('Mattermost slash commands settings saved, but not activated.')
end
expect(current_path).to eq(project_settings_integrations_path(project)) it 'redirects to the integrations page after activating' do
expect(page).to have_content('Mattermost slash commands activated.') token = ('a'..'z').to_a.join
end
it 'shows the add to mattermost button' do fill_in 'service_token', with: token
expect(page).to have_link('Add to Mattermost') click_on 'Save changes'
end
it 'shows an explanation if user is a member of no teams' do expect(current_path).to eq(project_settings_integrations_path(project))
stub_teams(count: 0) 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') it 'shows an explanation if user is a member of no teams' do
expect(page).to have_link('join a team', href: "#{Gitlab.config.mattermost.host}/select_team") stub_teams(count: 0)
end
it 'shows an explanation if user is a member of 1 team' do click_link 'Add to Mattermost'
stub_teams(count: 1)
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') it 'shows an explanation if user is a member of 1 team' do
expect(page).to have_content('This is the only available team that you are a member of.') stub_teams(count: 1)
end
it 'shows a disabled prefilled select if user is a member of 1 team' do click_link 'Add to Mattermost'
teams = stub_teams(count: 1)
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'] it 'shows a disabled prefilled select if user is a member of 1 team' do
select_element = find('#mattermost_team_id') teams = stub_teams(count: 1)
selected_option = select_element.find('option[selected]')
expect(select_element['disabled']).to eq("true") click_link 'Add to Mattermost'
expect(selected_option).to have_content(team_name.to_s)
end
it 'has a hidden input for the prefilled value if user is a member of 1 team' do team_name = teams.first['display_name']
teams = stub_teams(count: 1) 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']) it 'has a hidden input for the prefilled value if user is a member of 1 team' do
end teams = stub_teams(count: 1)
it 'shows an explanation user is a member of multiple teams' do click_link 'Add to Mattermost'
stub_teams(count: 2)
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') it 'shows an explanation user is a member of multiple teams' do
expect(page).to have_content('The list shows all available teams that you are a member of.') stub_teams(count: 2)
end
it 'shows a select with team options user is a member of multiple teams' do click_link 'Add to Mattermost'
stub_teams(count: 2)
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 click_link 'Add to Mattermost'
expect(select_element.all('option').count).to eq(3)
end
it 'shows an error alert with the error message if there is an error requesting teams' do select_element = find('#mattermost_team_id')
allow_any_instance_of(MattermostSlashCommandsService).to receive(:list_teams) { [[], 'test mattermost error message'] }
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') it 'shows an error alert with the error message if there is an error requesting teams' do
expect(page).to have_content('test mattermost error message') allow_any_instance_of(MattermostSlashCommandsService).to receive(:list_teams) { [[], 'test mattermost error message'] }
end
it 'enables the submit button if the required fields are provided', :js do click_link 'Add to Mattermost'
stub_teams(count: 1)
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") it 'enables the submit button if the required fields are provided', :js do
end stub_teams(count: 1)
it 'disables the submit button if the required fields are not provided', :js do click_link 'Add to Mattermost'
stub_teams(count: 1)
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") click_link 'Add to Mattermost'
end
def stub_teams(count: 0) fill_in('mattermost_trigger', with: '')
teams = create_teams(count)
allow_any_instance_of(MattermostSlashCommandsService).to receive(:list_teams) { [teams, nil] } expect(find('input[type="submit"]')['disabled']).to eq("true")
end
teams def stub_teams(count: 0)
end teams = create_teams(count)
def create_teams(count = 0) allow_any_instance_of(MattermostSlashCommandsService).to receive(:list_teams) { [teams, nil] }
teams = []
count.times do |i| teams
teams.push({ "id" => "x#{i}", "display_name" => "x#{i}-name" })
end 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 end
describe 'mattermost service is not enabled' do context 'mattermost service is not enabled' do
let(:mattermost_enabled) { false } let(:mattermost_enabled) { false }
it 'shows the correct trigger url' do it 'shows the correct trigger url' do
......
...@@ -3,13 +3,10 @@ ...@@ -3,13 +3,10 @@
require 'spec_helper' require 'spec_helper'
describe 'Slack slash commands' do describe 'Slack slash commands' do
let(:user) { create(:user) } include_context 'project service activation'
let(:project) { create(:project) }
before do before do
project.add_maintainer(user) visit_project_integration('Slack slash commands')
sign_in(user)
visit edit_project_service_path(project, :slack_slash_commands)
end end
it 'shows a token placeholder' do it 'shows a token placeholder' do
......
# 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
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