rbCAS / casino-activerecord_authenticator

Provides mechanism to use ActiveRecord as an authenticator for CASino.
MIT License
19 stars 61 forks source link

Testing with the same database #21

Open stepantubanov opened 8 years ago

stepantubanov commented 8 years ago

Hi,

There might a bug related to caching or database connection when the same table is used for authenticator and for the main app.

Assuming we have a class model Account with "email" and "encrypted_password" fields and we use a table "accounts" to store those. We also specify the the same table for the authenticator:

test:
  <<: *defaults
  authenticators:
    users:
      options:
        url: "postgres://localhost/auth_test"
        table: "accounts"
        username_column: "email"
        password_column: "encrypted_password"

Then there is a simple test:

require 'rails_helper'

RSpec.describe "User log in", type: :feature do
  let!(:account) { Account.create!(email: 'test@example', password: 'secret') }

  scenario "user who has an account logs into the app" do
    visit '/'

    fill_in 'Username', with: account.email
    fill_in 'Password', with: 'secret'
    click_button 'Login'

    expect(page).to have_content 'You are currently logged in as test@example.com'
  end
end

Which is failing with "Incorrect username/passoword" message. What happens is that when the authenticator tries to fetch the record here: https://github.com/rbCAS/casino-activerecord_authenticator/blob/122746772f2955ad9b1b448bcecf3301e28e11af/lib/casino/activerecord_authenticator.rb#L41 it gets 0 results. Even though there is that account record in the table that was during the test. If I debug and do

::Account.count
# => 1
@model.count
# => 0
@model.create(email: "123", encrypted_password: "123")
# => ... (creates an actual record)
@model.count
# => 1
::Account.count
# => 2

# At this point If I do SQL queries through ::Account I get both records,
# but when doing SQL query from @model then I only get one record.