2016-10-05 10:41:32 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Users::ActivityService, services: true do
|
2016-11-25 11:10:25 -05:00
|
|
|
include UserActivitiesHelpers
|
|
|
|
|
2016-10-05 10:41:32 -04:00
|
|
|
let(:user) { create(:user) }
|
2016-11-25 11:10:25 -05:00
|
|
|
|
2016-10-05 10:41:32 -04:00
|
|
|
subject(:service) { described_class.new(user, 'type') }
|
|
|
|
|
2016-11-25 11:10:25 -05:00
|
|
|
describe '#execute', :redis do
|
2016-10-05 10:41:32 -04:00
|
|
|
context 'when last activity is nil' do
|
2016-11-25 11:10:25 -05:00
|
|
|
before do
|
2016-10-05 10:41:32 -04:00
|
|
|
service.execute
|
2016-11-25 11:10:25 -05:00
|
|
|
end
|
2016-10-05 10:41:32 -04:00
|
|
|
|
2016-11-25 11:10:25 -05:00
|
|
|
it 'sets the last activity timestamp for the user' do
|
2017-03-07 13:34:43 -05:00
|
|
|
expect(last_hour_user_ids).to eq([user.id])
|
2016-11-25 11:10:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the same user' do
|
|
|
|
service.execute
|
|
|
|
|
2017-03-07 13:34:43 -05:00
|
|
|
expect(last_hour_user_ids).to eq([user.id])
|
2016-11-25 11:10:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the timestamp of an existing user' do
|
|
|
|
Timecop.freeze(Date.tomorrow) do
|
2017-03-07 13:34:43 -05:00
|
|
|
expect { service.execute }.to change { user_activity(user) }.to(Time.now.to_i.to_s)
|
2016-11-25 11:10:25 -05:00
|
|
|
end
|
2016-10-05 10:41:32 -04:00
|
|
|
end
|
|
|
|
|
2016-11-25 11:10:25 -05:00
|
|
|
describe 'other user' do
|
|
|
|
it 'updates other user' do
|
|
|
|
other_user = create(:user)
|
|
|
|
described_class.new(other_user, 'type').execute
|
2016-10-05 10:41:32 -04:00
|
|
|
|
2017-03-07 13:34:43 -05:00
|
|
|
expect(last_hour_user_ids).to match_array([user.id, other_user.id])
|
2016-10-05 10:41:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-03-07 13:34:43 -05:00
|
|
|
|
|
|
|
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
|
2016-10-05 10:41:32 -04:00
|
|
|
end
|