From f6457bbc430cd6ede565c577540634a3dbf2e572 Mon Sep 17 00:00:00 2001 From: Stanislav Kolotinskiy Date: Wed, 7 Sep 2022 12:26:41 +0300 Subject: [PATCH] Fix root request error when metrics are not initialized --- lib/travis/api/v3/router.rb | 2 +- spec/v3/router_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 spec/v3/router_spec.rb diff --git a/lib/travis/api/v3/router.rb b/lib/travis/api/v3/router.rb index d3aca33d12..1615827dc8 100644 --- a/lib/travis/api/v3/router.rb +++ b/lib/travis/api/v3/router.rb @@ -16,12 +16,12 @@ def initialize(routes = Routes) def call(env) ::Metriks.meter("api.v3.total_requests").mark + metrics = @metrics_processor.create return service_index(env) if env['PATH_INFO'.freeze] == ?/.freeze process_txt_extension!(env) - metrics = @metrics_processor.create access_control = AccessControl.new(env) env_params = params(env) factory, params = routes.factory_for(env['REQUEST_METHOD'.freeze], env['PATH_INFO'.freeze]) diff --git a/spec/v3/router_spec.rb b/spec/v3/router_spec.rb new file mode 100644 index 0000000000..f9324b4899 --- /dev/null +++ b/spec/v3/router_spec.rb @@ -0,0 +1,26 @@ +describe Travis::API::V3::Router, set_app: true do + describe '#call' do + let(:env) do + { + 'PATH_INFO' => '/', + 'rack.request.query_hash' => {}, + } + end + + context 'when user has no perms' do + before { env['HTTP_AUTHORIZATION'] = 'Token 123' } + + it 'returns a 403 error' do + expect(subject.call(env).first).to eq(403) + end + end + + context 'when user has perms' do + before { env['rack.request.query_hash']['log.token'] = '123' } + + it 'returns a 200 response' do + expect(subject.call(env).first).to eq(200) + end + end + end +end