Merge branch 'bw-move-readonly-logic' into 'master'
Refactor rspec matchers in read_only_spec.rb See merge request gitlab-org/gitlab-ce!20414
This commit is contained in:
commit
270580f97c
3 changed files with 27 additions and 33 deletions
|
@ -69,6 +69,7 @@ module Gitlab
|
||||||
@route_hash ||= Rails.application.routes.recognize_path(request.url, { method: request.request_method }) rescue {}
|
@route_hash ||= Rails.application.routes.recognize_path(request.url, { method: request.request_method }) rescue {}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Overridden in EE module
|
||||||
def whitelisted_routes
|
def whitelisted_routes
|
||||||
grack_route || ReadOnly.internal_routes.any? { |path| request.path.include?(path) } || lfs_route || sidekiq_route
|
grack_route || ReadOnly.internal_routes.any? { |path| request.path.include?(path) } || lfs_route || sidekiq_route
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,28 +4,6 @@ describe Gitlab::Middleware::ReadOnly do
|
||||||
include Rack::Test::Methods
|
include Rack::Test::Methods
|
||||||
using RSpec::Parameterized::TableSyntax
|
using RSpec::Parameterized::TableSyntax
|
||||||
|
|
||||||
RSpec::Matchers.define :be_a_redirect do
|
|
||||||
match do |response|
|
|
||||||
response.status == 301
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
RSpec::Matchers.define :disallow_request do
|
|
||||||
match do |middleware|
|
|
||||||
alert = middleware.env['rack.session'].to_hash
|
|
||||||
.dig('flash', 'flashes', 'alert')
|
|
||||||
|
|
||||||
alert&.include?('You cannot perform write operations')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
RSpec::Matchers.define :disallow_request_in_json do
|
|
||||||
match do |response|
|
|
||||||
json_response = JSON.parse(response.body)
|
|
||||||
response.body.include?('You cannot perform write operations') && json_response.key?('message')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:rack_stack) do
|
let(:rack_stack) do
|
||||||
rack = Rack::Builder.new do
|
rack = Rack::Builder.new do
|
||||||
use ActionDispatch::Session::CacheStore
|
use ActionDispatch::Session::CacheStore
|
||||||
|
@ -66,38 +44,38 @@ describe Gitlab::Middleware::ReadOnly do
|
||||||
it 'expects PATCH requests to be disallowed' do
|
it 'expects PATCH requests to be disallowed' do
|
||||||
response = request.patch('/test_request')
|
response = request.patch('/test_request')
|
||||||
|
|
||||||
expect(response).to be_a_redirect
|
expect(response).to be_redirect
|
||||||
expect(subject).to disallow_request
|
expect(subject).to disallow_request
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'expects PUT requests to be disallowed' do
|
it 'expects PUT requests to be disallowed' do
|
||||||
response = request.put('/test_request')
|
response = request.put('/test_request')
|
||||||
|
|
||||||
expect(response).to be_a_redirect
|
expect(response).to be_redirect
|
||||||
expect(subject).to disallow_request
|
expect(subject).to disallow_request
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'expects POST requests to be disallowed' do
|
it 'expects POST requests to be disallowed' do
|
||||||
response = request.post('/test_request')
|
response = request.post('/test_request')
|
||||||
|
|
||||||
expect(response).to be_a_redirect
|
expect(response).to be_redirect
|
||||||
expect(subject).to disallow_request
|
expect(subject).to disallow_request
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'expects a internal POST request to be allowed after a disallowed request' do
|
it 'expects a internal POST request to be allowed after a disallowed request' do
|
||||||
response = request.post('/test_request')
|
response = request.post('/test_request')
|
||||||
|
|
||||||
expect(response).to be_a_redirect
|
expect(response).to be_redirect
|
||||||
|
|
||||||
response = request.post("/api/#{API::API.version}/internal")
|
response = request.post("/api/#{API::API.version}/internal")
|
||||||
|
|
||||||
expect(response).not_to be_a_redirect
|
expect(response).not_to be_redirect
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'expects DELETE requests to be disallowed' do
|
it 'expects DELETE requests to be disallowed' do
|
||||||
response = request.delete('/test_request')
|
response = request.delete('/test_request')
|
||||||
|
|
||||||
expect(response).to be_a_redirect
|
expect(response).to be_redirect
|
||||||
expect(subject).to disallow_request
|
expect(subject).to disallow_request
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -105,7 +83,7 @@ describe Gitlab::Middleware::ReadOnly do
|
||||||
expect(Rails.application.routes).to receive(:recognize_path).and_call_original
|
expect(Rails.application.routes).to receive(:recognize_path).and_call_original
|
||||||
response = request.post('/root/gitlab-ce/new/master/app/info/lfs/objects/batch')
|
response = request.post('/root/gitlab-ce/new/master/app/info/lfs/objects/batch')
|
||||||
|
|
||||||
expect(response).to be_a_redirect
|
expect(response).to be_redirect
|
||||||
expect(subject).to disallow_request
|
expect(subject).to disallow_request
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -120,19 +98,19 @@ describe Gitlab::Middleware::ReadOnly do
|
||||||
expect(Rails.application.routes).not_to receive(:recognize_path)
|
expect(Rails.application.routes).not_to receive(:recognize_path)
|
||||||
response = request.post("/api/#{API::API.version}/internal")
|
response = request.post("/api/#{API::API.version}/internal")
|
||||||
|
|
||||||
expect(response).not_to be_a_redirect
|
expect(response).not_to be_redirect
|
||||||
expect(subject).not_to disallow_request
|
expect(subject).not_to disallow_request
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'expects requests to sidekiq admin to be allowed' do
|
it 'expects requests to sidekiq admin to be allowed' do
|
||||||
response = request.post('/admin/sidekiq')
|
response = request.post('/admin/sidekiq')
|
||||||
|
|
||||||
expect(response).not_to be_a_redirect
|
expect(response).not_to be_redirect
|
||||||
expect(subject).not_to disallow_request
|
expect(subject).not_to disallow_request
|
||||||
|
|
||||||
response = request.get('/admin/sidekiq')
|
response = request.get('/admin/sidekiq')
|
||||||
|
|
||||||
expect(response).not_to be_a_redirect
|
expect(response).not_to be_redirect
|
||||||
expect(subject).not_to disallow_request
|
expect(subject).not_to disallow_request
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -150,7 +128,7 @@ describe Gitlab::Middleware::ReadOnly do
|
||||||
expect(Rails.application.routes).to receive(:recognize_path).and_call_original
|
expect(Rails.application.routes).to receive(:recognize_path).and_call_original
|
||||||
response = request.post(path)
|
response = request.post(path)
|
||||||
|
|
||||||
expect(response).not_to be_a_redirect
|
expect(response).not_to be_redirect
|
||||||
expect(subject).not_to disallow_request
|
expect(subject).not_to disallow_request
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
15
spec/support/matchers/disallow_request_matchers.rb
Normal file
15
spec/support/matchers/disallow_request_matchers.rb
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
RSpec::Matchers.define :disallow_request do
|
||||||
|
match do |middleware|
|
||||||
|
alert = middleware.env['rack.session'].to_hash
|
||||||
|
.dig('flash', 'flashes', 'alert')
|
||||||
|
|
||||||
|
alert&.include?('You cannot perform write operations')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
RSpec::Matchers.define :disallow_request_in_json do
|
||||||
|
match do |response|
|
||||||
|
json_response = JSON.parse(response.body)
|
||||||
|
response.body.include?('You cannot perform write operations') && json_response.key?('message')
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue