2019-07-18 04:27:02 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-09-03 11:56:53 -04:00
|
|
|
require 'spec_helper'
|
2019-07-18 04:27:02 -04:00
|
|
|
|
2021-11-24 13:14:31 -05:00
|
|
|
RSpec.describe Gitlab::AnonymousSession, :clean_gitlab_redis_sessions do
|
2019-07-18 04:27:02 -04:00
|
|
|
let(:default_session_id) { '6919a6f1bb119dd7396fadc38fd18d0d' }
|
|
|
|
let(:additional_session_id) { '7919a6f1bb119dd7396fadc38fd18d0d' }
|
|
|
|
|
|
|
|
subject { new_anonymous_session }
|
|
|
|
|
2020-08-28 05:10:32 -04:00
|
|
|
def new_anonymous_session
|
|
|
|
described_class.new('127.0.0.1')
|
2019-07-18 04:27:02 -04:00
|
|
|
end
|
|
|
|
|
2020-08-28 05:10:32 -04:00
|
|
|
describe '#store_session_ip' do
|
2019-07-18 04:27:02 -04:00
|
|
|
it 'adds session id to proper key' do
|
2020-08-28 05:10:32 -04:00
|
|
|
subject.count_session_ip
|
2019-07-18 04:27:02 -04:00
|
|
|
|
2021-11-24 13:14:31 -05:00
|
|
|
Gitlab::Redis::Sessions.with do |redis|
|
2020-08-28 05:10:32 -04:00
|
|
|
expect(redis.get("session:lookup:ip:gitlab2:127.0.0.1").to_i).to eq 1
|
2019-07-18 04:27:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds expiration time to key' do
|
2020-12-01 10:09:28 -05:00
|
|
|
freeze_time do
|
2020-08-28 05:10:32 -04:00
|
|
|
subject.count_session_ip
|
2019-07-18 04:27:02 -04:00
|
|
|
|
2021-11-24 13:14:31 -05:00
|
|
|
Gitlab::Redis::Sessions.with do |redis|
|
2020-08-28 05:10:32 -04:00
|
|
|
expect(redis.ttl("session:lookup:ip:gitlab2:127.0.0.1")).to eq(24.hours.to_i)
|
2019-07-18 04:27:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is already one session' do
|
2020-08-28 05:10:32 -04:00
|
|
|
it 'increments the session count' do
|
|
|
|
subject.count_session_ip
|
|
|
|
new_anonymous_session.count_session_ip
|
2019-07-18 04:27:02 -04:00
|
|
|
|
2021-11-24 13:14:31 -05:00
|
|
|
Gitlab::Redis::Sessions.with do |redis|
|
2020-08-28 05:10:32 -04:00
|
|
|
expect(redis.get("session:lookup:ip:gitlab2:127.0.0.1").to_i).to eq(2)
|
2019-07-18 04:27:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#stored_sessions' do
|
|
|
|
it 'returns all anonymous sessions per ip' do
|
2021-11-24 13:14:31 -05:00
|
|
|
Gitlab::Redis::Sessions.with do |redis|
|
2020-08-28 05:10:32 -04:00
|
|
|
redis.set("session:lookup:ip:gitlab2:127.0.0.1", 2)
|
2019-07-18 04:27:02 -04:00
|
|
|
end
|
|
|
|
|
2020-08-28 05:10:32 -04:00
|
|
|
expect(subject.session_count).to eq(2)
|
2019-07-18 04:27:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes obsolete lookup through ip entries' do
|
2021-11-24 13:14:31 -05:00
|
|
|
Gitlab::Redis::Sessions.with do |redis|
|
2020-08-28 05:10:32 -04:00
|
|
|
redis.set("session:lookup:ip:gitlab2:127.0.0.1", 2)
|
2019-07-18 04:27:02 -04:00
|
|
|
end
|
|
|
|
|
2020-08-28 05:10:32 -04:00
|
|
|
subject.cleanup_session_per_ip_count
|
2019-07-18 04:27:02 -04:00
|
|
|
|
2021-11-24 13:14:31 -05:00
|
|
|
Gitlab::Redis::Sessions.with do |redis|
|
2020-08-28 05:10:32 -04:00
|
|
|
expect(redis.exists("session:lookup:ip:gitlab2:127.0.0.1")).to eq(false)
|
2019-07-18 04:27:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|