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