summaryrefslogtreecommitdiff
path: root/spec/support/helpers/content_security_policy_helpers.rb
blob: 7e3de9fd2195193c18ebad5d7c6bcf590fb54db5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# frozen_string_literal: true

module ContentSecurityPolicyHelpers
  # Expecting 2 calls to current_content_security_policy by default:
  # 1. call that's being tested
  # 2. call in ApplicationController
  def setup_csp_for_controller(
    controller_class, csp = ActionDispatch::ContentSecurityPolicy.new, times: 2,
any_time: false)
    expect_next_instance_of(controller_class) do |controller|
      if any_time
        expect(controller).to receive(:current_content_security_policy).at_least(:once).and_return(csp)
      else
        expect(controller)
        .to receive(:current_content_security_policy).exactly(times).times
        .and_return(csp)
      end
    end
  end
end