Commit 120e0c4d authored by blackst0ne's avatar blackst0ne Committed by Rémy Coutable

Replace the `group_hooks.feature` spinach test with an rspec analog

parent 547d2db5
---
title: 'Replace the `group_hooks.feature` spinach test with an rspec analog'
merge_request: 5515
author: '@blackst0ne'
type: other
require "spec_helper"
describe "User adds hook" do
set(:group) { create(:group) }
set(:user) { create(:user) }
set(:url) { "http://example.org" }
before do
group.add_owner(user)
sign_in(user)
visit(group_hooks_path(group))
end
it "adds new hook" do
fill_in("hook_url", with: url)
expect { click_button("Add webhook") }.to change(GroupHook, :count).by(1)
expect(current_path).to eq group_hooks_path(group)
expect(page).to have_content(url)
end
end
require "spec_helper"
describe "User tests hooks" do
set(:group) { create(:group) }
set(:hook) { create(:group_hook, group: group) }
set(:user) { create(:user) }
before do
group.add_owner(user)
sign_in(user)
visit(group_hooks_path(group))
end
context "when project is not empty" do
let!(:project) { create(:project, :repository, group: group) }
context "when URL is valid" do
before do
trigger_hook
end
it "triggers a hook" do
expect(current_path).to eq(group_hooks_path(group))
expect(page).to have_selector(".flash-notice", text: "Hook executed successfully: HTTP 200")
end
end
context "when URL is invalid" do
before do
stub_request(:post, hook.url).to_raise(SocketError.new("Failed to open"))
click_link("Test")
end
it { expect(page).to have_selector(".flash-alert", text: "Hook execution failed: Failed to open") }
end
end
context "when project is empty" do
let!(:project) { create(:project, group: group) }
before do
trigger_hook
end
it { expect(page).to have_selector('.flash-alert', text: 'Hook execution failed. Ensure the group has a project with commits.') }
end
private
def trigger_hook
stub_request(:post, hook.url).to_return(status: 200)
click_link("Test")
end
end
require "spec_helper"
describe "User views hooks" do
set(:group) { create(:group) }
set(:hook) { create(:group_hook, group: group) }
set(:user) { create(:user) }
before do
group.add_owner(user)
sign_in(user)
visit(group_hooks_path(group))
end
it { expect(page).to have_content(hook.url) }
end
Feature: Group Hooks
Background:
Given I sign in as a user
And I own group "Sourcing"
Scenario: I should see hook list
Given I own project "Shop" in group "Sourcing"
And group has hook
When I visit group hooks page
Then I should see group hook
Scenario: I add new hook
GivenI own project "Shop" in group "Sourcing"
And I visit group hooks page
When I submit new hook
Then I should see newly created hook
Scenario: I test hook
Given I own project "Shop" in group "Sourcing"
And group has hook
And I visit group hooks page
When I click test hook button
Then hook should be triggered
Scenario: I test a hook on empty project
Given I own empty project "Empty Shop" in group "Sourcing"
And group has hook
And I visit group hooks page
When I click test hook button
Then I should see hook error message
Scenario: I test a hook on down URL
Given I own project "Shop" in group "Sourcing"
And group has hook
And I visit group hooks page
When I click test hook button with invalid URL
Then I should see hook service down error message
require 'webmock'
class Spinach::Features::GroupHooks < Spinach::FeatureSteps
include SharedAuthentication
include SharedProject
include SharedPaths
include RSpec::Matchers
include RSpec::Mocks::ExampleMethods
include WebMock::API
step 'I own group "Sourcing"' do
@group = create :group, name: "Sourcing"
@group.add_owner(current_user)
end
step 'I own project "Shop" in group "Sourcing"' do
@project = create(:project, :repository, group: @group)
end
step 'I own empty project "Empty Shop" in group "Sourcing"' do
@project = create(:project, group: @group)
end
step 'group has hook' do
@hook = create(:group_hook, group: @group)
end
step 'I should see group hook' do
expect(page).to have_content @hook.url
end
step 'I submit new hook' do
@url = 'http://example.org'
fill_in "hook_url", with: @url
expect { click_button "Add webhook" }.to change(GroupHook, :count).by(1)
end
step 'I should see newly created hook' do
expect(current_path).to eq group_hooks_path(@group)
expect(page).to have_content(@url)
end
step 'I click test hook button' do
WebMock.enable!
stub_request(:post, @hook.url).to_return(status: 200)
click_link 'Test'
WebMock.disable!
end
step 'I click test hook button with invalid URL' do
stub_request(:post, @hook.url).to_raise(SocketError.new('Failed to open'))
click_link 'Test'
end
step 'hook should be triggered' do
expect(current_path).to eq group_hooks_path(@group)
expect(page).to have_selector '.flash-notice',
text: 'Hook executed successfully: HTTP 200'
end
step 'I should see hook error message' do
expect(page).to have_selector '.flash-alert',
text: 'Hook execution failed. Ensure the group has a project with commits.'
end
step 'I should see hook service down error message' do
expect(page).to have_selector '.flash-alert',
text: 'Hook execution failed: Failed to open'
end
end
...@@ -252,10 +252,6 @@ module SharedPaths ...@@ -252,10 +252,6 @@ module SharedPaths
visit project_settings_integrations_path(@project) visit project_settings_integrations_path(@project)
end end
step 'I visit group hooks page' do
visit group_hooks_path(@group)
end
step 'I visit project deploy keys page' do step 'I visit project deploy keys page' do
visit project_deploy_keys_path(@project) visit project_deploy_keys_path(@project)
end 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