1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
# frozen_string_literal: true
require "abstract_unit"
class PermissionsPolicyTest < ActiveSupport::TestCase
def setup
@policy = ActionDispatch::PermissionsPolicy.new
end
def test_mappings
@policy.midi :self
assert_equal "midi 'self'", @policy.build
@policy.midi :none
assert_equal "midi 'none'", @policy.build
end
def test_multiple_sources_for_a_single_directive
@policy.geolocation :self, "https://example.com"
assert_equal "geolocation 'self' https://example.com", @policy.build
end
def test_single_directive_for_multiple_directives
@policy.geolocation :self
@policy.usb :none
assert_equal "geolocation 'self'; usb 'none'", @policy.build
end
def test_multiple_directives_for_multiple_directives
@policy.geolocation :self, "https://example.com"
@policy.usb :none, "https://example.com"
assert_equal "geolocation 'self' https://example.com; usb 'none' https://example.com", @policy.build
end
def test_invalid_directive_source
exception = assert_raises(ArgumentError) do
@policy.geolocation [:non_existent]
end
assert_equal "Invalid HTTP permissions policy source: [:non_existent]", exception.message
end
end
class PermissionsPolicyMiddlewareTest < ActionDispatch::IntegrationTest
APP = ->(env) { [200, {}, []] }
POLICY = ActionDispatch::PermissionsPolicy.new do |p|
p.gyroscope :self
end
class PolicyConfigMiddleware
def initialize(app)
@app = app
end
def call(env)
env["action_dispatch.permissions_policy"] = POLICY
env["action_dispatch.show_exceptions"] = :none
@app.call(env)
end
end
test "html requests will set a policy" do
@app = build_app(->(env) { [200, { Rack::CONTENT_TYPE => "text/html" }, []] })
get "/index"
assert_equal "gyroscope 'self'", response.headers[ActionDispatch::Constants::FEATURE_POLICY]
end
test "non-html requests will set a policy" do
@app = build_app(->(env) { [200, { Rack::CONTENT_TYPE => "application/json" }, []] })
get "/index"
assert_equal "gyroscope 'self'", response.headers[ActionDispatch::Constants::FEATURE_POLICY]
end
test "existing policies will not be overwritten" do
@app = build_app(->(env) { [200, { ActionDispatch::Constants::FEATURE_POLICY => "gyroscope 'none'" }, []] })
get "/index"
assert_equal "gyroscope 'none'", response.headers[ActionDispatch::Constants::FEATURE_POLICY]
end
private
def build_app(app)
PolicyConfigMiddleware.new(
Rack::Lint.new(
ActionDispatch::PermissionsPolicy::Middleware.new(
Rack::Lint.new(app),
),
),
)
end
end
class PermissionsPolicyIntegrationTest < ActionDispatch::IntegrationTest
class PolicyController < ActionController::Base
permissions_policy only: :index do |f|
f.gyroscope :none
end
permissions_policy only: :sample_controller do |f|
f.gyroscope nil
f.usb :self
end
permissions_policy only: :multiple_directives do |f|
f.gyroscope nil
f.usb :self
f.autoplay "https://example.com"
f.payment "https://secure.example.com"
end
def index
head :ok
end
def sample_controller
head :ok
end
def multiple_directives
head :ok
end
end
ROUTES = ActionDispatch::Routing::RouteSet.new
ROUTES.draw do
scope module: "permissions_policy_integration_test" do
get "/", to: "policy#index"
get "/sample_controller", to: "policy#sample_controller"
get "/multiple_directives", to: "policy#multiple_directives"
end
end
POLICY = ActionDispatch::PermissionsPolicy.new do |p|
p.gyroscope :self
end
class PolicyConfigMiddleware
def initialize(app)
@app = app
end
def call(env)
env["action_dispatch.permissions_policy"] = POLICY
env["action_dispatch.show_exceptions"] = :none
@app.call(env)
end
end
APP = build_app(ROUTES) do |middleware|
middleware.use PolicyConfigMiddleware
middleware.use Rack::Lint
middleware.use ActionDispatch::PermissionsPolicy::Middleware
middleware.use Rack::Lint
end
def app
APP
end
def test_generates_permissions_policy_header
get "/"
assert_policy "gyroscope 'none'"
end
def test_generates_per_controller_permissions_policy_header
get "/sample_controller"
assert_policy "usb 'self'"
end
def test_generates_multiple_directives_permissions_policy_header
get "/multiple_directives"
assert_policy "usb 'self'; autoplay https://example.com; payment https://secure.example.com"
end
private
def env_config
Rails.application.env_config
end
def permissions_policy
env_config["action_dispatch.permissions_policy"]
end
def permissions_policy=(policy)
env_config["action_dispatch.permissions_policy"] = policy
end
def assert_policy(expected)
assert_response :success
assert_equal expected, response.headers["Feature-Policy"]
end
end
class PermissionsPolicyWithHelpersIntegrationTest < ActionDispatch::IntegrationTest
module ApplicationHelper
def pigs_can_fly?
false
end
end
class ApplicationController < ActionController::Base
helper_method :sky_is_blue?
def sky_is_blue?
true
end
end
class PolicyController < ApplicationController
permissions_policy do |f|
f.gyroscope :none unless helpers.pigs_can_fly?
f.usb :self if helpers.sky_is_blue?
end
def index
head :ok
end
end
ROUTES = ActionDispatch::Routing::RouteSet.new
ROUTES.draw do
scope module: "permissions_policy_with_helpers_integration_test" do
get "/", to: "policy#index"
end
end
POLICY = ActionDispatch::PermissionsPolicy.new do |p|
p.gyroscope :self
end
class PolicyConfigMiddleware
def initialize(app)
@app = app
end
def call(env)
env["action_dispatch.permissions_policy"] = POLICY
env["action_dispatch.show_exceptions"] = :none
@app.call(env)
end
end
APP = build_app(ROUTES) do |middleware|
middleware.use PolicyConfigMiddleware
middleware.use Rack::Lint
middleware.use ActionDispatch::PermissionsPolicy::Middleware
middleware.use Rack::Lint
end
def app
APP
end
def test_generates_permissions_policy_header
get "/"
assert_policy "gyroscope 'none'; usb 'self'"
end
private
def env_config
Rails.application.env_config
end
def permissions_policy
env_config["action_dispatch.permissions_policy"]
end
def permissions_policy=(policy)
env_config["action_dispatch.permissions_policy"] = policy
end
def assert_policy(expected)
assert_response :success
assert_equal expected, response.headers[ActionDispatch::Constants::FEATURE_POLICY]
end
end
|