-
Notifications
You must be signed in to change notification settings - Fork 231
Stop mocking models in controller specs and switch to using ActiveRecord directly #390
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
Merged
willnet
merged 2 commits into
Sorcery:master
from
willnet:use-real-model-instead-of-mock
Sep 12, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,24 @@ | ||
require 'spec_helper' | ||
|
||
# require 'shared_examples/controller_activity_logging_shared_examples' | ||
|
||
describe SorceryController, type: :controller do | ||
before(:all) do | ||
MigrationHelper.migrate("#{Rails.root}/db/migrate/activity_logging") | ||
end | ||
|
||
after(:all) do | ||
MigrationHelper.rollback("#{Rails.root}/db/migrate/activity_logging") | ||
sorcery_controller_property_set(:register_login_time, true) | ||
sorcery_controller_property_set(:register_logout_time, true) | ||
sorcery_controller_property_set(:register_last_activity_time, true) | ||
# sorcery_controller_property_set(:last_login_from_ip_address_name, true) | ||
end | ||
|
||
# ----------------- ACTIVITY LOGGING ----------------------- | ||
context 'with activity logging features' do | ||
let(:adapter) { double('sorcery_adapter') } | ||
let(:user) { double('user', id: 42, sorcery_adapter: adapter) } | ||
let!(:user) { User.create!(username: 'test_user', email: '[email protected]', password: 'password') } | ||
|
||
before(:all) do | ||
sorcery_reload!([:activity_logging]) | ||
end | ||
before(:all) { sorcery_reload!([:activity_logging]) } | ||
|
||
before(:each) do | ||
allow(user).to receive(:username) | ||
allow(user).to receive_message_chain(:sorcery_config, :username_attribute_names, :first) { :username } | ||
allow(User.sorcery_config).to receive(:last_login_at_attribute_name) { :last_login_at } | ||
allow(User.sorcery_config).to receive(:last_login_from_ip_address_name) { :last_login_from_ip_address } | ||
|
||
sorcery_controller_property_set(:register_login_time, false) | ||
sorcery_controller_property_set(:register_last_ip_address, false) | ||
sorcery_controller_property_set(:register_last_activity_time, false) | ||
|
@@ -35,20 +29,22 @@ | |
Timecop.freeze(now) | ||
|
||
sorcery_controller_property_set(:register_login_time, true) | ||
expect(user).to receive(:set_last_login_at).with(be_within(0.1).of(now)) | ||
login_user(user) | ||
|
||
expect(user.reload.last_login_at).to be_within(0.1).of(now) | ||
|
||
Timecop.return | ||
end | ||
|
||
it 'logs logout time on logout' do | ||
login_user(user) | ||
now = Time.now.in_time_zone | ||
Timecop.freeze(now) | ||
expect(user).to receive(:set_last_logout_at).with(be_within(0.1).of(now)) | ||
|
||
logout_user | ||
|
||
expect(user.reload.last_logout_at).to be_within(0.1).of(now) | ||
|
||
Timecop.return | ||
end | ||
|
||
|
@@ -58,56 +54,66 @@ | |
login_user(user) | ||
now = Time.now.in_time_zone | ||
Timecop.freeze(now) | ||
expect(user).to receive(:set_last_activity_at).with(be_within(0.1).of(now)) | ||
|
||
get :some_action | ||
|
||
expect(user.reload.last_activity_at).to be_within(0.1).of(now) | ||
|
||
Timecop.return | ||
end | ||
|
||
it 'logs last IP address when logged in' do | ||
sorcery_controller_property_set(:register_last_ip_address, true) | ||
expect(user).to receive(:set_last_ip_address).with('0.0.0.0') | ||
|
||
login_user(user) | ||
|
||
expect(user.reload.last_login_from_ip_address).to eq('0.0.0.0') | ||
end | ||
|
||
it 'updates nothing but activity fields' do | ||
pending 'Move to model' | ||
original_user_name = User.last.username | ||
sorcery_controller_property_set(:register_last_activity_time, true) | ||
user = User.last | ||
original_email = user.email | ||
original_activity_at = user.last_activity_at | ||
login_user(user) | ||
get :some_action_making_a_non_persisted_change_to_the_user | ||
|
||
expect(User.last.username).to eq original_user_name | ||
user.reload | ||
expect(user.email).to eq original_email | ||
expect(user.last_activity_at).not_to eq original_activity_at | ||
end | ||
|
||
it 'does not register login time if configured so' do | ||
sorcery_controller_property_set(:register_login_time, false) | ||
|
||
expect(user).to receive(:set_last_login_at).never | ||
login_user(user) | ||
|
||
expect(user.reload.last_login_at).to be_nil | ||
end | ||
|
||
it 'does not register logout time if configured so' do | ||
sorcery_controller_property_set(:register_logout_time, false) | ||
login_user(user) | ||
|
||
expect(user).to receive(:set_last_logout_at).never | ||
logout_user | ||
|
||
expect(user.reload.last_logout_at).to be_nil | ||
end | ||
|
||
it 'does not register last activity time if configured so' do | ||
sorcery_controller_property_set(:register_last_activity_time, false) | ||
|
||
expect(user).to receive(:set_last_activity_at).never | ||
brendon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
login_user(user) | ||
get :some_action | ||
|
||
expect(user.reload.last_activity_at).to be_nil | ||
end | ||
|
||
it 'does not register last IP address if configured so' do | ||
sorcery_controller_property_set(:register_last_ip_address, false) | ||
expect(user).to receive(:set_last_ip_address).never | ||
|
||
login_user(user) | ||
|
||
expect(user.reload.last_login_from_ip_address).to be_nil | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,21 @@ | ||
require 'spec_helper' | ||
|
||
describe SorceryController, type: :controller do | ||
let(:user) { double('user', id: 42, email: '[email protected]') } | ||
let!(:user) { User.create!(username: 'test_user', email: '[email protected]', password: 'password') } | ||
|
||
def request_test_login | ||
get :test_login, params: { email: '[email protected]', password: 'blabla' } | ||
end | ||
|
||
# ----------------- SESSION TIMEOUT ----------------------- | ||
# ----------------- BRUTE FORCE PROTECTION ----------------------- | ||
describe 'brute force protection features' do | ||
before(:all) do | ||
MigrationHelper.migrate("#{Rails.root}/db/migrate/brute_force_protection") | ||
sorcery_reload!([:brute_force_protection]) | ||
end | ||
|
||
after(:each) do | ||
Sorcery::Controller::Config.reset! | ||
sorcery_controller_property_set(:user_class, User) | ||
Timecop.return | ||
after(:all) do | ||
MigrationHelper.rollback("#{Rails.root}/db/migrate/brute_force_protection") | ||
end | ||
|
||
it 'counts login retries' do | ||
|
@@ -29,13 +28,15 @@ def request_test_login | |
end | ||
|
||
it 'resets the counter on a good login' do | ||
# dirty hack for rails 4 | ||
allow(@controller).to receive(:register_last_activity_time_to_db) | ||
# Set failed_logins_count to a non-zero value first | ||
user.update!(failed_logins_count: 3) | ||
|
||
allow(User).to receive(:authenticate) { |&block| block.call(user, nil) } | ||
expect(user).to receive_message_chain(:sorcery_adapter, :update_attribute).with(:failed_logins_count, 0) | ||
|
||
get :test_login, params: { email: '[email protected]', password: 'secret' } | ||
|
||
user.reload | ||
expect(user.failed_logins_count).to eq(0) | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
require 'spec_helper' | ||
|
||
describe SorceryController, type: :controller do | ||
let(:user) { double('user', id: 42, email: '[email protected]') } | ||
let!(:user) { User.create!(username: 'test_user', email: '[email protected]', password: 'password') } | ||
|
||
describe 'with http basic auth features' do | ||
before(:all) do | ||
|
@@ -21,9 +21,6 @@ | |
end | ||
|
||
it 'authenticates from http basic if credentials are sent' do | ||
# dirty hack for rails 4 | ||
allow(subject).to receive(:register_last_activity_time_to_db) | ||
|
||
@request.env['HTTP_AUTHORIZATION'] = "Basic #{Base64.encode64("#{user.email}:secret")}" | ||
expect(User).to receive('authenticate').with('[email protected]', 'secret').and_return(user) | ||
get :test_http_basic_auth, params: {}, session: { http_authentication_used: true } | ||
|
@@ -53,15 +50,12 @@ | |
end | ||
|
||
it "signs in the user's session on successful login" do | ||
# dirty hack for rails 4 | ||
allow(controller).to receive(:register_last_activity_time_to_db) | ||
|
||
@request.env['HTTP_AUTHORIZATION'] = "Basic #{Base64.encode64("#{user.email}:secret")}" | ||
expect(User).to receive('authenticate').with('[email protected]', 'secret').and_return(user) | ||
|
||
get :test_http_basic_auth, params: {}, session: { http_authentication_used: true } | ||
|
||
expect(session[:user_id]).to eq '42' | ||
expect(session[:user_id]).to eq user.id.to_s | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.