ankane/blind_index

Validation & fixture

olimart opened this issue · 2 comments

Hi Andrew, great work as usual.

I'm having trouble understand validation & fixtures.

In my unit test I have

test 'has valid test data' do
    Subscriber.find_each do |subscriber|
      assert_valid subscriber
    end
end

def assert_valid(record, message = nil)
    message ||= "Expected #{record.inspect} to be valid. Errors: #{record.errors.full_messages.to_sentence}"
    assert record.valid?, message
end

In my fixture I have

test_user:
  encrypted_email_bidx: <%= User.compute_email_bidx("test@example.org").inspect %>

In my model I have

attr_encrypted :email, key: [ENV["EMAIL_ENCRYPTION_KEY"]].pack("H*")
blind_index    :email, key: [ENV["EMAIL_BLIND_INDEX_KEY"]].pack("H*")

validates_uniqueness_of :email, scope: :list_id
validates_presence_of :email
validates :email, email: true

When I run my test it raises

test_has_valid_test_data                                        FAIL (0.01s)
Minitest::Assertion:         Expected #<Subscriber id: 113629430, list_id: 980190962, created_at: "2018-07-19 03:13:52", updated_at: "2018-07-19 03:13:52", status: "unsubscribed", merge_tags: {}, enriched_profile_id: nil, token: nil, encrypted_email: nil, encrypted_email_iv: nil, encrypted_email_bidx: "q/izxB5aVSBkv6sboTT73Pjf8BzDUh5BQCb9junpA60=\n", email: nil> to be valid. Errors: 

Any idea how to prevent tests from failing since it no longer considers fixtures as valid? Thanks

It looks like the validation message is cut off, but looking at the output, encrypted_email and encrypted_email_iv are nil. Check out the examples in #2 for how to populate those.

Thanks a lot. Problem solved.
Leaving a note for anyone interested.

I added in my fixture:

test_user:
  encrypted_email: <%= User.encrypt_email("a@email.com", iv: Base64.decode64('G4fbeClWL3aFMY9I')) %>
  encrypted_email_iv: "G4fbeClWL3aFMY9I"
  encrypted_email_bidx: <%= User.compute_email_bidx("a@email.com").inspect %>