Skip to content

Commit

Permalink
Add more tests for find_signed/! methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanvlviv committed Oct 30, 2020
1 parent f5d68da commit 21889ff
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion activerecord/test/cases/signed_id_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SignedIdTest < ActiveRecord::TestCase
assert_equal Company.first, Company.find_signed(Company.first.signed_id)
end

test "raise UnknownPrimaryKey when model have no primary key" do
test "find signed record raises UnknownPrimaryKey when a model has no primary key" do
error = assert_raises(ActiveRecord::UnknownPrimaryKey) do
Matey.find_signed("this will not be even verified")
end
Expand All @@ -55,6 +55,14 @@ class SignedIdTest < ActiveRecord::TestCase
end
end

test "find signed record with a bang with custom primary key" do
assert_equal @toy, Toy.find_signed!(@toy.signed_id)
end

test "find signed record with a bang for single table inheritance (STI Models)" do
assert_equal Company.first, Company.find_signed!(Company.first.signed_id)
end

test "fail to find record from broken signed id" do
assert_nil Account.find_signed("this won't find anything")
end
Expand All @@ -75,12 +83,26 @@ class SignedIdTest < ActiveRecord::TestCase
assert_nil Account.find_signed signed_id
end

test "find signed record with purpose" do
assert_equal @account, Account.find_signed(@account.signed_id(purpose: :v1), purpose: :v1)
end

test "fail to find signed record with purpose" do
assert_nil Account.find_signed(@account.signed_id(purpose: :v1))

assert_nil Account.find_signed(@account.signed_id(purpose: :v1), purpose: :v2)
end

test "finding record from broken signed id raises on the bang" do
assert_raises(ActiveSupport::MessageVerifier::InvalidSignature) do
Account.find_signed! "this will blow up"
end
end

test "find signed record with a bang within expiration date" do
assert_equal @account, Account.find_signed!(@account.signed_id(expires_in: 1.minute))
end

test "finding signed record outside expiration date raises on the bang" do
signed_id = @account.signed_id(expires_in: 1.minute)
travel 2.minutes
Expand All @@ -99,6 +121,20 @@ class SignedIdTest < ActiveRecord::TestCase
end
end

test "find signed record with bang with purpose" do
assert_equal @account, Account.find_signed!(@account.signed_id(purpose: :v1), purpose: :v1)
end

test "find signed record with bang with purpose raises" do
assert_raises(ActiveSupport::MessageVerifier::InvalidSignature) do
Account.find_signed!(@account.signed_id(purpose: :v1))
end

assert_raises(ActiveSupport::MessageVerifier::InvalidSignature) do
Account.find_signed!(@account.signed_id(purpose: :v1), purpose: :v2)
end
end

test "fail to work without a signed_id_verifier_secret" do
ActiveRecord::Base.signed_id_verifier_secret = nil
Account.instance_variable_set :@signed_id_verifier, nil
Expand Down

0 comments on commit 21889ff

Please sign in to comment.