2019-09-04 21:01:54 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
require 'spec_helper'
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
RSpec.describe Gitlab::AnonymousSession, :clean_gitlab_redis_sessions do
|
2019-09-04 21:01:54 +05:30
|
|
|
let(:default_session_id) { '6919a6f1bb119dd7396fadc38fd18d0d' }
|
|
|
|
let(:additional_session_id) { '7919a6f1bb119dd7396fadc38fd18d0d' }
|
|
|
|
|
|
|
|
subject { new_anonymous_session }
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def new_anonymous_session
|
|
|
|
described_class.new('127.0.0.1')
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
describe '#store_session_ip' do
|
2019-09-04 21:01:54 +05:30
|
|
|
it 'adds session id to proper key' do
|
2020-11-24 15:15:51 +05:30
|
|
|
subject.count_session_ip
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
Gitlab::Redis::Sessions.with do |redis|
|
2020-11-24 15:15:51 +05:30
|
|
|
expect(redis.get("session:lookup:ip:gitlab2:127.0.0.1").to_i).to eq 1
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds expiration time to key' do
|
2021-02-22 17:27:13 +05:30
|
|
|
freeze_time do
|
2020-11-24 15:15:51 +05:30
|
|
|
subject.count_session_ip
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
Gitlab::Redis::Sessions.with do |redis|
|
2020-11-24 15:15:51 +05:30
|
|
|
expect(redis.ttl("session:lookup:ip:gitlab2:127.0.0.1")).to eq(24.hours.to_i)
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is already one session' do
|
2020-11-24 15:15:51 +05:30
|
|
|
it 'increments the session count' do
|
|
|
|
subject.count_session_ip
|
|
|
|
new_anonymous_session.count_session_ip
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
Gitlab::Redis::Sessions.with do |redis|
|
2020-11-24 15:15:51 +05:30
|
|
|
expect(redis.get("session:lookup:ip:gitlab2:127.0.0.1").to_i).to eq(2)
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#stored_sessions' do
|
|
|
|
it 'returns all anonymous sessions per ip' do
|
2022-01-26 12:08:38 +05:30
|
|
|
Gitlab::Redis::Sessions.with do |redis|
|
2020-11-24 15:15:51 +05:30
|
|
|
redis.set("session:lookup:ip:gitlab2:127.0.0.1", 2)
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
expect(subject.session_count).to eq(2)
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes obsolete lookup through ip entries' do
|
2022-01-26 12:08:38 +05:30
|
|
|
Gitlab::Redis::Sessions.with do |redis|
|
2020-11-24 15:15:51 +05:30
|
|
|
redis.set("session:lookup:ip:gitlab2:127.0.0.1", 2)
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
subject.cleanup_session_per_ip_count
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
Gitlab::Redis::Sessions.with do |redis|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(redis.exists?("session:lookup:ip:gitlab2:127.0.0.1")).to eq(false)
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|