|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | + |
| 5 | +RSpec.describe ActiveAdminMcp::RecordUpdater do |
| 6 | + # A stand-in for an ActiveAdmin controller compiled from `permit_params`. |
| 7 | + # `permitted` is the set of fields the admin form would allow. |
| 8 | + def build_controller(param_key:, permitted:) |
| 9 | + Class.new do |
| 10 | + attr_accessor :params |
| 11 | + |
| 12 | + define_method(:permitted_params) do |
| 13 | + params.permit(param_key => permitted) |
| 14 | + end |
| 15 | + private :permitted_params |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + def build_resource(model:, actions: %i[index show new create edit update destroy], |
| 20 | + adapter:, param_key: :widget, permitted: %i[name role]) |
| 21 | + namespace = double("namespace", authorization_adapter: adapter) |
| 22 | + config = double( |
| 23 | + "config", |
| 24 | + defined_actions: actions, |
| 25 | + namespace: namespace, |
| 26 | + controller: build_controller(param_key: param_key, permitted: permitted), |
| 27 | + param_key: param_key, |
| 28 | + ) |
| 29 | + { name: "Widget", model: model, config: config } |
| 30 | + end |
| 31 | + |
| 32 | + def model_finding(record, id: 1) |
| 33 | + double("model").tap { |m| allow(m).to receive(:find_by).with(id: id).and_return(record) } |
| 34 | + end |
| 35 | + |
| 36 | + let(:permit_all) do |
| 37 | + Class.new do |
| 38 | + def initialize(*); end |
| 39 | + def authorized?(*) = true |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + let(:deny_all) do |
| 44 | + Class.new do |
| 45 | + def initialize(*); end |
| 46 | + def authorized?(*) = false |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + def update(resource, id: 1, attributes:, current_user: :admin) |
| 51 | + described_class.new(resource: resource, current_user: current_user).call(id: id, attributes: attributes) |
| 52 | + end |
| 53 | + |
| 54 | + it "writes permitted attributes and returns the updated record" do |
| 55 | + record = double("record", id: 1, as_json: { "id" => 1, "name" => "Renamed" }) |
| 56 | + allow(record).to receive(:update).and_return(true) |
| 57 | + resource = build_resource(model: model_finding(record), adapter: permit_all) |
| 58 | + |
| 59 | + result = update(resource, attributes: { "name" => "Renamed" }) |
| 60 | + |
| 61 | + expect(record).to have_received(:update).with(name: "Renamed") |
| 62 | + expect(result[:updated]).to eq([:name]) |
| 63 | + expect(result[:record]).to eq("id" => 1, "name" => "Renamed") |
| 64 | + end |
| 65 | + |
| 66 | + it "drops attributes the admin form does not permit" do |
| 67 | + record = double("record", id: 1, as_json: {}) |
| 68 | + allow(record).to receive(:update).and_return(true) |
| 69 | + resource = build_resource(model: model_finding(record), adapter: permit_all, permitted: %i[name]) |
| 70 | + |
| 71 | + update(resource, attributes: { "name" => "Renamed", "role" => "admin" }) |
| 72 | + |
| 73 | + expect(record).to have_received(:update).with(name: "Renamed") |
| 74 | + end |
| 75 | + |
| 76 | + it "refuses when ActiveAdmin does not expose the update action" do |
| 77 | + resource = build_resource(model: double("model"), adapter: permit_all, actions: %i[index show]) |
| 78 | + |
| 79 | + result = update(resource, attributes: { "name" => "Renamed" }) |
| 80 | + |
| 81 | + expect(result[:error]).to match(/not editable/i) |
| 82 | + end |
| 83 | + |
| 84 | + it "refuses when the user is not authorized to update the record" do |
| 85 | + record = double("record", id: 1) |
| 86 | + resource = build_resource(model: model_finding(record), adapter: deny_all) |
| 87 | + |
| 88 | + result = update(resource, attributes: { "name" => "Renamed" }) |
| 89 | + |
| 90 | + expect(result[:error]).to match(/not authorized/i) |
| 91 | + end |
| 92 | + |
| 93 | + it "returns an error when the record does not exist" do |
| 94 | + resource = build_resource(model: model_finding(nil, id: 999), adapter: permit_all) |
| 95 | + |
| 96 | + result = update(resource, id: 999, attributes: { "name" => "Renamed" }) |
| 97 | + |
| 98 | + expect(result[:error]).to match(/not found/i) |
| 99 | + end |
| 100 | + |
| 101 | + it "returns validation errors when the update is rejected" do |
| 102 | + record = double("record", id: 1, errors: double("errors", full_messages: ["Name can't be blank"])) |
| 103 | + allow(record).to receive(:update).and_return(false) |
| 104 | + resource = build_resource(model: model_finding(record), adapter: permit_all) |
| 105 | + |
| 106 | + result = update(resource, attributes: { "name" => "" }) |
| 107 | + |
| 108 | + expect(result[:error]).to match(/validation/i) |
| 109 | + expect(result[:details]).to eq(["Name can't be blank"]) |
| 110 | + end |
| 111 | + |
| 112 | + it "returns an error when no permitted attributes remain after filtering" do |
| 113 | + record = double("record", id: 1) |
| 114 | + resource = build_resource(model: model_finding(record), adapter: permit_all, permitted: %i[name]) |
| 115 | + |
| 116 | + result = update(resource, attributes: { "role" => "admin" }) |
| 117 | + |
| 118 | + expect(result[:error]).to match(/no permitted attributes/i) |
| 119 | + end |
| 120 | + |
| 121 | + it "updates an attribute literally named 'error' without treating it as a failure" do |
| 122 | + record = double("record", id: 1, as_json: { "id" => 1, "error" => "boom" }) |
| 123 | + allow(record).to receive(:update).and_return(true) |
| 124 | + resource = build_resource(model: model_finding(record), adapter: permit_all, permitted: %i[error]) |
| 125 | + |
| 126 | + result = update(resource, attributes: { "error" => "boom" }) |
| 127 | + |
| 128 | + expect(record).to have_received(:update).with(error: "boom") |
| 129 | + expect(result[:updated]).to eq([:error]) |
| 130 | + end |
| 131 | +end |
0 commit comments