forked from Sorcery/sorcery
-
Notifications
You must be signed in to change notification settings - Fork 0
Testing Rails
Wes edited this page Jul 7, 2015
·
4 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:
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
...
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::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
- External
- 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
- Testing Rails
- User Activation
Contributing to Sorcery