summaryrefslogtreecommitdiff
path: root/spec/services/users/activity_service_spec.rb
blob: fef4da0c76e065521aa4f0818a0bc17994d331a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'spec_helper'

describe Users::ActivityService do
  include UserActivitiesHelpers

  let(:user) { create(:user) }

  subject(:service) { described_class.new(user, 'type') }

  describe '#execute', :clean_gitlab_redis_shared_state do
    context 'when last activity is nil' do
      before do
        service.execute
      end

      it 'sets the last activity timestamp for the user' do
        expect(last_hour_user_ids).to eq([user.id])
      end

      it 'updates the same user' do
        service.execute

        expect(last_hour_user_ids).to eq([user.id])
      end

      it 'updates the timestamp of an existing user' do
        Timecop.freeze(Date.tomorrow) do
          expect { service.execute }.to change { user_activity(user) }.to(Time.now.to_i.to_s)
        end
      end

      describe 'other user' do
        it 'updates other user' do
          other_user = create(:user)
          described_class.new(other_user, 'type').execute

          expect(last_hour_user_ids).to match_array([user.id, other_user.id])
        end
      end
    end
  end

  def last_hour_user_ids
    Gitlab::UserActivities.new
      .select { |k, v| v >= 1.hour.ago.to_i.to_s }
      .map { |k, _| k.to_i }
  end
end