janko / rodauth-rails

Rails integration for Rodauth authentication framework
https://github.com/jeremyevans/rodauth
MIT License
593 stars 40 forks source link

'uninitialized constant Rodauth::BCrypt' when attempting a login on a RSpec request spec #279

Closed FelipeBodelon closed 7 months ago

FelipeBodelon commented 7 months ago

While trying to replace my previous auth helpers (from devise) to work with rodauth JSON requests, I created some of my own helpers as suggested by the docs/wiki and some discussions.

I figured my login endpoint wasn't working properly so I tried testing them with the following RSpec request spec (using factory_bot):

RSpec.describe "POST /auth/login" do
  describe "POST #login" do
    let(:account_attributes) { attributes_for(:account) }

    before do
      account = Account.create!(account_attributes)
    end

    let(:valid_account_params) do
      { email: account.email, password: account.password }
    end

    let(:invalid_account_params) do
      { email: account.email, password: 'invalid' }
    end

    it 'valid account attributes' do
      post "/auth/login", as: :json, params: valid_account_params

      expect(response).to have_http_status(:ok)
    end

    it 'invalid account attributes' do
      post "/auth/login", as: :json, params: invalid_account_params

      expect(response).to have_http_status(:unauthorized)
    end
  end
end

I get a failure on Account creation, either way if I try with factory_bot create or directly on a before hook.

Failure/Error: account = Account.create!(account_attributes)

     NameError:
       uninitialized constant Rodauth::BCrypt
     # /usr/share/rvm/gems/ruby-3.2.2/gems/rodauth-2.33.0/lib/rodauth/features/login_password_requirements_base.rb:79:in `password_hash'
     # /usr/share/rvm/gems/ruby-3.2.2/gems/rodauth-model-0.2.1/lib/rodauth/model/active_record.rb:19:in `block in define_methods'
     # ./spec/requests/auth/login_spec.rb:8:in `block (3 levels) in <top (required)>'
     # /usr/share/rvm/gems/ruby-3.2.2/gems/webmock-3.19.1/lib/webmock/rspec.rb:39:in `block (2 levels) in <top (required)>'

This only fails on my local test environment, and works properly on development.

janko commented 7 months ago

See https://github.com/janko/rodauth-rails/issues/275#issuecomment-1938188962. TL;DR replace include Rodauth::Model(RodauthMain) with include Rodauth::Rails.model in your account model. This will be fixed in the upcoming release, at the install generator level.

whysthatso commented 7 months ago

@FelipeBodelon would be you willing to share your factory_bot factory for account? i'm trying to get testing working with minitest and factory_bot, and i'm trying to wrap my head around how to actually produce a valid account.