-
Notifications
You must be signed in to change notification settings - Fork 229
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
Docs for Testing-Rails need update for request specs #319
Comments
This messed up my user specs - so I changed it to before do
User.class_eval { clear_validators! }
end
after do
Object.send(:remove_const, :User)
load 'app/models/user.rb'
end So I decided to look in the code - https://github.com/Sorcery/sorcery/blob/master/lib/sorcery/test_helpers/rails/request.rb def login_user(user = nil, password = 'secret', route = nil, http_method = :post) I see the password param My call is login_user( create(:user), password: 'password' ) That doesnt work - using pry in user_sessions controller. params
|
Unfortunately, Sorcery is pretty irksome in that you can't sign in a user until they're active... and if you create a user with an active state sorcery will overwrite the user's activation state and make them an inactive user before creating the record. I suspect you're running into that. You need to ensure # spec/factories/users.rb
FactoryBot.define do
factory :user, class: 'User' do
password { 'MyDefaultFactoryPassword!' }
# Alternative:
# let(:user) { create :user, &:activate! }
trait :active do
transient do
activation_state { :active }
end
end
after :create do |user, evaluator|
user.activate! if evaluator.activation_state == :active
end
end
end To support module Sorcery::TestHelpers::Rails
# Adapted from Sorcery::TestHelpers::Rails::Request
def sign_in(user, password = 'MyDefaultFactoryPassword!', route = nil, http_method = :post)
route ||= login_url
username_attr = user.sorcery_config.username_attribute_names.first
password_attr = user.sorcery_config.password_attribute_name
send(
http_method,
route,
params: {
username_attr => user.send(username_attr),
password_attr => password
}
)
end
end But since you're testing actual login here, you would write something like: describe '/user_sessions' do
subject { response }
let(:user) { create :user, :active, password: }
let(:password) { 'P@ssw0rd!' }
describe 'POST /login' do
let(:request) { post login_path(params: { email: user.email, password: }) }
it 'succeeds' do
request
should have_http_status(:success)
end
it 'logs in user' do
expect { request }.to change { user.reload.logged_in? }.from(false).to(true)
end
end
end |
This page https://github.com/Sorcery/sorcery/wiki/Testing-Rails needs updating.
For request specs (I dont need helpers for system specs - and I dont use controller specs)
I added:
config.include Sorcery::TestHelpers::Rails::Request, type: :request
- in rails_helper.rbI had to make this change - because I got
undefined method user_sessions_url
Then using pry I figured out that the password assumed is
secret
- which is not a good password since my user model hasIn my test - I added
Configuration
0.16.3
3.0.4p208
rails-7.0.3.1
OSX
The text was updated successfully, but these errors were encountered: