-
Notifications
You must be signed in to change notification settings - Fork 227
Testing Rails
jiikko edited this page Aug 6, 2022
·
8 revisions
You can setup fixtures like this:
noam:
email: [email protected]
salt: <%= salt = "asdasdastr4325234324sdfds" %>
crypted_password: <%= Sorcery::CryptoProviders::BCrypt.encrypt("secret", salt) %>
activation_state: active
For simple out of the box testing:
# if you are inheriting from ActionController::TestCase try this:
class TeamsControllerTest < ActionController::TestCase
include Sorcery::TestHelpers::Rails::Integration
include Sorcery::TestHelpers::Rails::Controller
setup do
@team = teams(:one)
@user = users(:one)
login_user(user = @user, route = login_url) # replace with your login url path
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:teams)
end
## But if you are using Rails 5, Rails 6, you will notice that Controller tests
## are now dropped in favour of integration tests: and they inherit from
## ActionDispatch::IntegrationTest. If that is the case, then consider doing one of
## the following: Assuming you are using mini test - try this - you can add similar
## helper methods if you are using rspec.
class ActionDispatch::IntegrationTest
include Sorcery::TestHelpers::Rails::Integration ## Add these test helpers.
def login_user(user)
# post the login and follow through
post user_sessions_path, params: { email: user.email, password: 'secret' } # ensure that the password you set here conforms to what you have set in your fixtures/factory. and also ensure that your session creation URL is set appropriately: whether it be: user_sessions_path (if you have been following the tutorials) or otherwise.
follow_redirect!
end
end
...
In your test/spec helper make sure to include:
RSpec.configure do |config|
config.include Sorcery::TestHelpers::Rails::Controller, type: :controller
config.include Sorcery::TestHelpers::Rails::Request, type: :request
config.include Sorcery::TestHelpers::Rails::Integration, type: :feature
end
And now you can use the following helpers:
login_user(user = nil) # by default looks for @user
logout_user
See the example app for a working example.
In spec/support/authentication.rb
module AuthenticationForFeatureRequest
def login user, password = 'login'
user.update_attributes password: password
page.driver.post sessions_url, {email: user.email, password: password}
visit root_url
end
end
Note that you need to update password with update_attributes
or update_attributes!
, not with update_attribute
because generating salt and encrypting password are triggered by before_validation
since v0.9.0 but update_attribute
skips validations.
In spec/spec_helper.rb
RSpec.configure do |config|
# ...
config.include AuthenticationForFeatureRequest, type: :feature
# ...
end
In spec/features/your_feature_spec.rb
feature 'Your Feature' do
let(:user) { Fabricate :user }
scenario 'Your Scenario' do
login user
end
end
Please add working examples if you got any. The Rack::Test solution doesn't work since page.driver.post
is not available.
Meta
Using Sorcery
- Activity Logging
- Brute Force Protection
- DataMapper Support
- DelayedJob Integration
- Distinguish login failure reasons
- External
- External---Microsoft-Graph-authentication
- Fetching Currently Active Users
- HTTP Basic Auth
- Integration Testing
- OAuth Landing Page
- Password-less Activation
- Remember Me
- Reset Password
- Routes Constraints
- Session Timeout
- Simple Password Authentication
- Single Table Inheritance Support
- Testing Rails
- User Activation
Contributing to Sorcery