diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-23 15:08:38 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-23 15:08:38 +0300 |
commit | 5ad0cf26551baff8f08af8562a8d45e6ec14d71a (patch) | |
tree | 57f1a6bad31bcd11efacd3fdfb9cc92f88fb6a86 /spec/controllers/concerns | |
parent | f47c768fad17d4c876e96524f83f8306f071db66 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/controllers/concerns')
-rw-r--r-- | spec/controllers/concerns/page_limiter_spec.rb | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/spec/controllers/concerns/page_limiter_spec.rb b/spec/controllers/concerns/page_limiter_spec.rb new file mode 100644 index 00000000000..9ac94b7e740 --- /dev/null +++ b/spec/controllers/concerns/page_limiter_spec.rb @@ -0,0 +1,97 @@ +# frozen_string_literal: true + +require 'spec_helper' + +class PageLimiterSpecController < ApplicationController + include PageLimiter + + before_action do + limit_pages 200 + end + + def index + head :ok + end +end + +describe PageLimiter do + let(:controller_class) do + PageLimiterSpecController + end + + let(:instance) do + controller_class.new + end + + before do + allow(instance).to receive(:params) do + { + controller: "explore/projects", + action: "index" + } + end + + allow(instance).to receive(:request) do + double(:request, user_agent: "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)") + end + end + + describe "#limit_pages" do + using RSpec::Parameterized::TableSyntax + + where(:max_page, :actual_page, :result) do + 2 | 1 | nil + 2 | 2 | nil + 2 | 3 | PageLimiter::PageOutOfBoundsError + nil | 1 | PageLimiter::PageLimitNotANumberError + 0 | 1 | PageLimiter::PageLimitNotSensibleError + -1 | 1 | PageLimiter::PageLimitNotSensibleError + end + + with_them do + subject { instance.limit_pages(max_page) } + + before do + allow(instance).to receive(:params) { { page: actual_page.to_s } } + end + + it "returns the expected result" do + if result == PageLimiter::PageOutOfBoundsError + expect(instance).to receive(:record_page_limit_interception) + expect { subject }.to raise_error(result) + elsif result&.superclass == PageLimiter::PageLimiterError + expect { subject }.to raise_error(result) + else + expect(subject).to eq(result) + end + end + end + end + + describe "#default_page_out_of_bounds_response" do + subject { instance.send(:default_page_out_of_bounds_response) } + + after do + subject + end + + it "returns a bad_request header" do + expect(instance).to receive(:head).with(:bad_request) + end + end + + describe "#record_page_limit_interception" do + subject { instance.send(:record_page_limit_interception) } + + it "records a metric counter" do + expect(Gitlab::Metrics).to receive(:counter).with( + :gitlab_page_out_of_bounds, + controller: "explore/projects", + action: "index", + bot: true + ) + + subject + end + end +end |