diff options
author | Imre Farkas <ifarkas@gitlab.com> | 2019-07-12 15:25:12 +0300 |
---|---|---|
committer | Imre Farkas <ifarkas@gitlab.com> | 2019-07-22 18:15:57 +0300 |
commit | 313f145b5594ebba7ce4675905061144adb3b44a (patch) | |
tree | 33683a02092d00fd587832dd40f99c5452dd2454 /spec | |
parent | c5fac1034f43d81a17242d8ade2d7eb8741a72e2 (diff) |
Rake task to cleanup expired ActiveSession lookup keys
In some cases ActiveSession.cleanup was not called after authentication,
so for some user ActiveSession lookup keys grew without ever cleaning
up. This Rake task manually iterates over the lookup keys and removes
ones without existing ActiveSession.
Diffstat (limited to 'spec')
-rw-r--r-- | spec/models/active_session_spec.rb | 2 | ||||
-rw-r--r-- | spec/tasks/gitlab/cleanup_rake_spec.rb | 30 |
2 files changed, 31 insertions, 1 deletions
diff --git a/spec/models/active_session_spec.rb b/spec/models/active_session_spec.rb index 09c2878663a..2a689754ee0 100644 --- a/spec/models/active_session_spec.rb +++ b/spec/models/active_session_spec.rb @@ -114,7 +114,7 @@ RSpec.describe ActiveSession, :clean_gitlab_redis_shared_state do redis.sadd("session:lookup:user:gitlab:#{user.id}", session_ids) end - expect(ActiveSession.session_ids_for_user(user)).to eq(session_ids) + expect(ActiveSession.session_ids_for_user(user.id)).to eq(session_ids) end end diff --git a/spec/tasks/gitlab/cleanup_rake_spec.rb b/spec/tasks/gitlab/cleanup_rake_spec.rb index 92c094f08a4..4aee6d005a8 100644 --- a/spec/tasks/gitlab/cleanup_rake_spec.rb +++ b/spec/tasks/gitlab/cleanup_rake_spec.rb @@ -185,4 +185,34 @@ describe 'gitlab:cleanup rake tasks' do end end end + + context 'sessions' do + describe 'gitlab:cleanup:sessions:active_sessions_lookup_keys', :clean_gitlab_redis_shared_state do + subject(:rake_task) { run_rake_task('gitlab:cleanup:sessions:active_sessions_lookup_keys') } + + let!(:user) { create(:user) } + let(:existing_session_id) { '5' } + + before do + Gitlab::Redis::SharedState.with do |redis| + redis.set("session:user:gitlab:#{user.id}:#{existing_session_id}", + Marshal.dump(true)) + redis.sadd("session:lookup:user:gitlab:#{user.id}", (1..10).to_a) + end + end + + it 'runs the task without errors' do + expect { rake_task }.not_to raise_error + end + + it 'removes expired active session lookup keys' do + Gitlab::Redis::SharedState.with do |redis| + lookup_key = "session:lookup:user:gitlab:#{user.id}" + expect { subject }.to change { redis.scard(lookup_key) }.from(10).to(1) + expect(redis.smembers("session:lookup:user:gitlab:#{user.id}")).to( + eql([existing_session_id])) + end + end + end + end end |