debian-mirror-gitlab/spec/lib/gitlab/anonymous_session_spec.rb

68 lines
1.8 KiB
Ruby
Raw Normal View History

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
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::AnonymousSession, :clean_gitlab_redis_shared_state 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
Gitlab::Redis::SharedState.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
Gitlab::Redis::SharedState.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
Gitlab::Redis::SharedState.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
Gitlab::Redis::SharedState.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
Gitlab::Redis::SharedState.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
Gitlab::Redis::SharedState.with do |redis|
2020-11-24 15:15:51 +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