user_tracking_concern_spec.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe ApplicationController, type: :controller do
  4. controller do
  5. include UserTrackingConcern
  6. def show
  7. render plain: 'show'
  8. end
  9. end
  10. before do
  11. routes.draw { get 'show' => 'anonymous#show' }
  12. end
  13. describe 'when signed in' do
  14. let(:user) { Fabricate(:user) }
  15. it 'does not track when there is a recent sign in' do
  16. user.update(current_sign_in_at: 60.minutes.ago)
  17. prior = user.current_sign_in_at
  18. sign_in user, scope: :user
  19. get :show
  20. expect(user.reload.current_sign_in_at).to be_within(1.0).of(prior)
  21. end
  22. it 'tracks when sign in is nil' do
  23. user.update(current_sign_in_at: nil)
  24. sign_in user, scope: :user
  25. get :show
  26. expect_updated_sign_in_at(user)
  27. end
  28. it 'tracks when sign in is older than one day' do
  29. user.update(current_sign_in_at: 2.days.ago)
  30. sign_in user, scope: :user
  31. get :show
  32. expect_updated_sign_in_at(user)
  33. end
  34. it 'regenerates feed when sign in is older than two weeks' do
  35. allow(RegenerationWorker).to receive(:perform_async)
  36. user.update(current_sign_in_at: 3.weeks.ago)
  37. sign_in user, scope: :user
  38. get :show
  39. expect_updated_sign_in_at(user)
  40. expect(RegenerationWorker).to have_received(:perform_async)
  41. end
  42. def expect_updated_sign_in_at(user)
  43. expect(user.reload.current_sign_in_at).to be_within(1.0).of(Time.now.utc)
  44. end
  45. end
  46. end