|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'test_helper' |
| 4 | + |
| 5 | +module DeveloperPortal |
| 6 | + class BaseControllerTest < ActionDispatch::IntegrationTest |
| 7 | + |
| 8 | + class FilterReadOnlyParamsTest < BaseControllerTest |
| 9 | + class TestController < DeveloperPortal::BaseController |
| 10 | + skip_before_action :login_required |
| 11 | + |
| 12 | + def create |
| 13 | + render plain: filter_readonly_params(params[:user], User) |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + test 'filters out read-only fields' do |
| 18 | + account = FactoryBot.create(:simple_provider) |
| 19 | + ro_fields = FactoryBot.create_list(:fields_definition, 2, account:, read_only: true) |
| 20 | + editable_fields = FactoryBot.create_list(:fields_definition, 3, account:) |
| 21 | + |
| 22 | + ro_params = fields_to_hash(ro_fields) |
| 23 | + editable_params = fields_to_hash(editable_fields) |
| 24 | + |
| 25 | + TestController.any_instance.expects(:site_account).at_least_once.returns(account) |
| 26 | + |
| 27 | + with_test_routes do |
| 28 | + post '/test/create', params: { user: {**ro_params, **editable_params} } |
| 29 | + |
| 30 | + assert_response :success |
| 31 | + assert_equal editable_params.to_s, response.body |
| 32 | + end |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + private |
| 37 | + |
| 38 | + def fields_to_hash(fields) |
| 39 | + fields.each_with_object({}) { |fd, p| p[fd.name]=SecureRandom.hex } |
| 40 | + end |
| 41 | + |
| 42 | + def with_test_routes |
| 43 | + Rails.application.routes.draw do |
| 44 | + post '/test/create' => 'developer_portal/base_controller_test/filter_read_only_params_test/test#create' |
| 45 | + end |
| 46 | + yield |
| 47 | + ensure |
| 48 | + Rails.application.routes_reloader.reload! |
| 49 | + end |
| 50 | + end |
| 51 | +end |
0 commit comments