Skip to content

Commit

Permalink
Refactoring: Simplyfied if statement and removed reference to issue #…
Browse files Browse the repository at this point in the history
…2431 because it's more of a byproduct than an actual fix.
  • Loading branch information
Billy Zhou authored and thorsteneckel committed Feb 26, 2019
1 parent fa0ed8c commit 399fef6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,10 @@ def self.password_reset_via_token(token, password)
=end

def update_last_login
self.last_login = Time.zone.now
# reduce DB/ES load by updating last_login every 10 minutes only
if !last_login || last_login < 10.minutes.ago
self.last_login = Time.zone.now
end

# reset login failed
self.login_failed = 0
Expand Down
27 changes: 27 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,33 @@
.to be(nil)
end
end

it "updates user's updates last_login and updated_at attributes" do
expect { described_class.authenticate(user.login, password) }
.to change { user.reload.last_login }
.and change { user.reload.updated_at }
end

context 'when authenticated multiple after another' do

before { described_class.authenticate(user.login, password) }

it "doesn't update last_login and updated_at when last login was less than 10 minutes ago" do
travel 9.minutes

expect { described_class.authenticate(user.login, password) }
.to not_change { user.reload.last_login }
.and not_change { user.reload.updated_at }
end

it 'updates last_login and updated_at when last login was more than 10 minutes ago' do
travel 11.minutes

expect { described_class.authenticate(user.login, password) }
.to change { user.reload.last_login }
.and change { user.reload.updated_at }
end
end
end

context 'with valid user and invalid password' do
Expand Down

0 comments on commit 399fef6

Please sign in to comment.