Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.

Testing with the same database #21

Open
stepantubanov opened this issue Nov 24, 2015 · 0 comments
Open

Testing with the same database #21

stepantubanov opened this issue Nov 24, 2015 · 0 comments

Comments

@stepantubanov
Copy link

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 [email protected]'
  end
end

Which is failing with "Incorrect username/passoword" message. What happens is that when the authenticator tries to fetch the record here:

user = @model.send("find_by_#{@options[:username_column]}!", username)

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.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant