user_tracking_concern_spec.rb 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. describe 'feed regeneration' do
  35. before do
  36. alice = Fabricate(:account)
  37. bob = Fabricate(:account)
  38. user.account.follow!(alice)
  39. user.account.follow!(bob)
  40. Fabricate(:status, account: alice, text: 'hello world')
  41. Fabricate(:status, account: bob, text: 'yes hello')
  42. Fabricate(:status, account: user.account, text: 'test')
  43. user.update(last_sign_in_at: 'Tue, 04 Jul 2017 14:45:56 UTC +00:00', current_sign_in_at: 'Wed, 05 Jul 2017 22:10:52 UTC +00:00')
  44. sign_in user, scope: :user
  45. end
  46. it 'sets a regeneration marker while regenerating' do
  47. allow(RegenerationWorker).to receive(:perform_async)
  48. get :show
  49. expect_updated_sign_in_at(user)
  50. expect(Redis.current.get("account:#{user.account_id}:regeneration")).to eq 'true'
  51. expect(RegenerationWorker).to have_received(:perform_async)
  52. end
  53. it 'sets the regeneration marker to expire' do
  54. allow(RegenerationWorker).to receive(:perform_async)
  55. get :show
  56. expect(Redis.current.ttl("account:#{user.account_id}:regeneration")).to be >= 0
  57. end
  58. it 'regenerates feed when sign in is older than two weeks' do
  59. get :show
  60. expect_updated_sign_in_at(user)
  61. expect(Redis.current.zcard(FeedManager.instance.key(:home, user.account_id))).to eq 3
  62. expect(Redis.current.get("account:#{user.account_id}:regeneration")).to be_nil
  63. end
  64. end
  65. def expect_updated_sign_in_at(user)
  66. expect(user.reload.current_sign_in_at).to be_within(1.0).of(Time.now.utc)
  67. end
  68. end
  69. end