Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
FelipeBodelon opened this issue Feb 23, 2024 · 2 comments

Comments

@FelipeBodelon
Copy link

FelipeBodelon commented Feb 23, 2024

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
Copy link
Owner

janko commented Feb 23, 2024

See #275 (comment). 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.

@janko janko closed this as completed Feb 23, 2024
@whysthatso
Copy link

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants