Using delete method while deleting Sequel models does not trigger destroy hooks, which we use for archival. With an earlier commit, we already removed all delete calls from our codebase. However, it is still possible that a delete call might be introduced in the future or an operator might call delete manually by mistake. To prevent such unintentinal delete calls, with this commit, we are prepending Sequel::Dataset and Sequel::Model classes to overwrite the delete behaviour. Now using delete method raises an error and suggests using destroy instead. If delete is really necessary in the given context, it can be still called via force flag. One ugly part of this commit is that in our version of delete, we check the call stack to see if; - delete is called from rodauth - delete is called from destroy itself to allow those uses of delete.
25 lines
907 B
Ruby
25 lines
907 B
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.describe ResourceMethods do
|
|
let(:sa) { Sshable.create_with_id(host: "test.localhost", raw_private_key_1: SshKey.generate.keypair) }
|
|
|
|
it "discourages deleting models with delete method" do
|
|
expect { sa.delete }.to raise_error(RuntimeError, /Calling delete is discouraged/)
|
|
end
|
|
|
|
it "allows deleting models with delete method if forced" do
|
|
expect { sa.delete(force: true) }.not_to raise_error
|
|
end
|
|
|
|
it "allows deleting models with destroy" do
|
|
expect { sa.destroy }.not_to raise_error
|
|
end
|
|
|
|
it "archives scrubbed version of the model when deleted" do
|
|
scrubbed_values_hash = sa.values.merge(model_name: "Sshable")
|
|
scrubbed_values_hash.delete(:raw_private_key_1)
|
|
scrubbed_values_hash.delete(:raw_private_key_2)
|
|
expect(DeletedRecord).to receive(:create).with(hash_including(model_values: scrubbed_values_hash))
|
|
sa.destroy
|
|
end
|
|
end
|