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
|
# frozen_string_literal: true
require "cases/encryption/helper"
require "models/book_encrypted"
class ActiveRecord::Encryption::ExtendedDeterministicQueriesTest < ActiveRecord::EncryptionTestCase
setup do
ActiveRecord::Encryption.config.support_unencrypted_data = true
end
test "Finds records when data is unencrypted" do
UnencryptedBook.create!(name: "Dune")
assert EncryptedBook.find_by(name: "Dune") # core
assert EncryptedBook.where("id > 0").find_by(name: "Dune") # relation
end
test "Finds records when data is encrypted" do
EncryptedBook.create!(name: "Dune")
assert EncryptedBook.find_by(name: "Dune") # core
assert EncryptedBook.where("id > 0").find_by(name: "Dune") # relation
end
test "Works well with downcased attributes" do
EncryptedBookWithDowncaseName.create! name: "Dune"
assert EncryptedBookWithDowncaseName.find_by(name: "DUNE")
end
test "Works well with string attribute names" do
UnencryptedBook.create! "name" => "Dune"
assert EncryptedBook.find_by("name" => "Dune")
end
test "find_or_create_by works" do
EncryptedBook.find_or_create_by!(name: "Dune")
assert EncryptedBook.find_by(name: "Dune")
EncryptedBook.find_or_create_by!(name: "Dune")
assert EncryptedBook.find_by(name: "Dune")
end
test "does not mutate arguments" do
props = { name: "Dune" }
assert_equal "Dune", EncryptedBook.find_or_initialize_by(props).name
assert_equal "Dune", props[:name]
end
test "where(...).first_or_create works" do
EncryptedBook.where(name: "Dune").first_or_create
assert EncryptedBook.exists?(name: "Dune")
end
test "exists?(...) works" do
EncryptedBook.create! name: "Dune"
assert EncryptedBook.exists?(name: "Dune")
end
test "If support_unencrypted_data is opted out at the attribute level, cannot find unencrypted data" do
UnencryptedBook.create! name: "Dune"
assert_nil EncryptedBookWithUnencryptedDataOptedOut.find_by(name: "Dune") # core
assert_nil EncryptedBookWithUnencryptedDataOptedOut.where("id > 0").find_by(name: "Dune") # relation
end
test "If support_unencrypted_data is opted out at the attribute level, can find encrypted data" do
EncryptedBook.create! name: "Dune"
assert EncryptedBookWithUnencryptedDataOptedOut.find_by(name: "Dune") # core
assert EncryptedBookWithUnencryptedDataOptedOut.where("id > 0").find_by(name: "Dune") # relation
end
test "If support_unencrypted_data is opted in at the attribute level, can find unencrypted data" do
UnencryptedBook.create! name: "Dune"
assert EncryptedBookWithUnencryptedDataOptedIn.find_by(name: "Dune") # core
assert EncryptedBookWithUnencryptedDataOptedIn.where("id > 0").find_by(name: "Dune") # relation
end
test "If support_unencrypted_data is opted in at the attribute level, can find encrypted data" do
EncryptedBook.create! name: "Dune"
assert EncryptedBookWithUnencryptedDataOptedIn.find_by(name: "Dune") # core
assert EncryptedBookWithUnencryptedDataOptedIn.where("id > 0").find_by(name: "Dune") # relation
end
end
|