Commit 862d0b5c authored by nmilojevic1's avatar nmilojevic1

Simplify specs to only cover Redis::Sessions use case

- Remove shared example redis sessions store
- We are using single instance in CI
parent 229fd8af
......@@ -45,8 +45,7 @@ RSpec.describe Groups::DependencyProxyForContainersController do
expect(response).to have_gitlab_http_status(:not_found)
end
shared_examples 'active session' do
context 'with an active session' do
context 'with an active session', :clean_gitlab_redis_sessions do
let(:session_id) { '42' }
let(:session_time) { 5.minutes.ago }
let(:stored_session) do
......@@ -54,7 +53,7 @@ RSpec.describe Groups::DependencyProxyForContainersController do
end
before do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("session:gitlab:#{session_id}", Marshal.dump(stored_session))
redis.sadd("session:lookup:user:gitlab:#{user.id}", [session_id])
end
......@@ -64,9 +63,6 @@ RSpec.describe Groups::DependencyProxyForContainersController do
end
end
it_behaves_like 'redis sessions store', 'active session'
end
context 'when git check is not enforced' do
it_behaves_like successful_example
end
......
......@@ -85,12 +85,7 @@ RSpec.describe 'Login' do
expect(page.body).to have_link('Register now', href: new_user_registration_path)
end
RSpec.shared_examples_for 'two-factor authentication' do
before do
load Rails.root.join('config/initializers/session_store.rb')
end
describe 'with two-factor authentication required' do
describe 'with two-factor authentication required', :clean_gitlab_redis_sessions do
let_it_be(:user) { create(:user) }
let_it_be(:smartcard_identity) { create(:smartcard_identity, user: user) }
......@@ -129,9 +124,6 @@ RSpec.describe 'Login' do
end
end
end
it_behaves_like 'redis sessions store', 'two-factor authentication'
end
end
end
......
......@@ -9,7 +9,6 @@ RSpec.describe Gitlab::Auth::GroupSaml::SessionEnforcer do
end
end
RSpec.shared_examples_for 'group saml session enforcer' do
describe '#access_restricted' do
let_it_be(:saml_provider) { create(:saml_provider, enforced_sso: true) }
let_it_be(:user) { create(:user) }
......@@ -28,7 +27,7 @@ RSpec.describe Gitlab::Auth::GroupSaml::SessionEnforcer do
allow(saml_provider).to receive(:git_check_enforced?).and_return(true)
end
context 'with an active session' do
context 'with an active session', :clean_gitlab_redis_sessions do
let(:session_id) { '42' }
let(:session_time) { 5.minutes.ago }
let(:stored_session) do
......@@ -36,7 +35,7 @@ RSpec.describe Gitlab::Auth::GroupSaml::SessionEnforcer do
end
before do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("session:gitlab:#{session_id}", Marshal.dump(stored_session))
redis.sadd("session:lookup:user:gitlab:#{user.id}", [session_id])
end
......@@ -64,14 +63,14 @@ RSpec.describe Gitlab::Auth::GroupSaml::SessionEnforcer do
end
end
context 'with two active sessions' do
context 'with two active sessions', :clean_gitlab_redis_sessions do
let(:second_session_id) { '52' }
let(:second_stored_session) do
{ 'active_group_sso_sign_ins' => { create(:saml_provider, enforced_sso: true).id => session_time } }
end
before do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("session:gitlab:#{second_session_id}", Marshal.dump(second_stored_session))
redis.sadd("session:lookup:user:gitlab:#{user.id}", [session_id, second_session_id])
end
......@@ -80,7 +79,7 @@ RSpec.describe Gitlab::Auth::GroupSaml::SessionEnforcer do
it_behaves_like 'not enforced'
end
context 'with two active sessions for the same provider and one pre-sso' do
context 'with two active sessions for the same provider and one pre-sso', :clean_gitlab_redis_sessions do
let(:second_session_id) { '52' }
let(:third_session_id) { '62' }
let(:second_stored_session) do
......@@ -88,7 +87,7 @@ RSpec.describe Gitlab::Auth::GroupSaml::SessionEnforcer do
end
before do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("session:gitlab:#{second_session_id}", Marshal.dump(second_stored_session))
redis.set("session:gitlab:#{third_session_id}", Marshal.dump({}))
redis.sadd("session:lookup:user:gitlab:#{user.id}", [session_id, second_session_id, third_session_id])
......@@ -193,14 +192,14 @@ RSpec.describe Gitlab::Auth::GroupSaml::SessionEnforcer do
allow(saml_provider).to receive(:git_check_enforced?).and_return(false)
end
context 'with an active session' do
context 'with an active session', :clean_gitlab_redis_sessions do
let(:session_id) { '42' }
let(:stored_session) do
{ 'active_group_sso_sign_ins' => { saml_provider.id => 5.minutes.ago } }
end
before do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("session:gitlab:#{session_id}", Marshal.dump(stored_session))
redis.sadd("session:lookup:user:gitlab:#{user.id}", [session_id])
end
......@@ -214,7 +213,4 @@ RSpec.describe Gitlab::Auth::GroupSaml::SessionEnforcer do
end
end
end
end
it_behaves_like 'redis sessions store', 'group saml session enforcer'
end
......@@ -2,8 +2,7 @@
require 'spec_helper'
RSpec.describe Gitlab::Auth::Otp::SessionEnforcer do
shared_examples_for 'otp session enforcer' do
RSpec.describe Gitlab::Auth::Otp::SessionEnforcer, :clean_gitlab_redis_sessions do
let_it_be(:key) { create(:key)}
describe '#update_session' do
......@@ -14,7 +13,7 @@ RSpec.describe Gitlab::Auth::Otp::SessionEnforcer do
end
it 'registers a session in Redis' do
expect(redis_store_class).to receive(:with).and_yield(redis)
expect(Gitlab::Redis::Sessions).to receive(:with).and_yield(redis)
session_expiry_in_seconds = Gitlab::CurrentSettings.git_two_factor_session_expiry.minutes.to_i
expect(redis).to(
......@@ -49,7 +48,7 @@ RSpec.describe Gitlab::Auth::Otp::SessionEnforcer do
context 'with existing session' do
before do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("#{::Gitlab::Redis::Sessions::OTP_SESSIONS_NAMESPACE}:#{key.id}", true )
end
end
......@@ -61,7 +60,4 @@ RSpec.describe Gitlab::Auth::Otp::SessionEnforcer do
it { is_expected.to be_truthy }
end
end
end
it_behaves_like 'redis sessions store', 'otp session enforcer'
end
......@@ -29,15 +29,14 @@ RSpec.describe Gitlab::Auth::Smartcard::SessionEnforcer do
stub_smartcard_setting(enabled: true, required_for_git_access: true)
end
RSpec.shared_examples_for 'smartcard session' do
context 'with a smartcard session' do
context 'with a smartcard session', :clean_gitlab_redis_sessions do
let(:session_id) { '42' }
let(:stored_session) do
{ 'smartcard_signins' => { 'last_signin_at' => 5.minutes.ago } }
end
before do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("session:gitlab:#{session_id}", Marshal.dump(stored_session))
redis.sadd("session:lookup:user:gitlab:#{user.id}", [session_id])
end
......@@ -45,9 +44,6 @@ RSpec.describe Gitlab::Auth::Smartcard::SessionEnforcer do
it { is_expected.to be_falsey }
end
end
it_behaves_like 'redis sessions store', 'smartcard session'
context 'without any session' do
it { is_expected.to be_truthy }
......
......@@ -3,20 +3,19 @@
require 'spec_helper'
RSpec.describe Gitlab::Auth::Smartcard::Session do
RSpec.shared_examples_for 'smartcard session' do
describe '#active?' do
let(:user) { create(:user) }
subject { described_class.new.active?(user) }
context 'with a smartcard session' do
context 'with a smartcard session', :clean_gitlab_redis_sessions do
let(:session_id) { '42' }
let(:stored_session) do
{ 'smartcard_signins' => { 'last_signin_at' => 5.minutes.ago } }
end
before do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("session:gitlab:#{session_id}", Marshal.dump(stored_session))
redis.sadd("session:lookup:user:gitlab:#{user.id}", [session_id])
end
......@@ -45,7 +44,4 @@ RSpec.describe Gitlab::Auth::Smartcard::Session do
expect(Gitlab::Session.current[:smartcard_signins]).to eq({ 'last_signin_at' => now })
end
end
end
it_behaves_like 'redis sessions store', 'smartcard session'
end
......@@ -704,7 +704,6 @@ RSpec.describe Gitlab::GitAccess do
end
end
RSpec.shared_examples_for 'checks smartcard access & otp session' do
describe '#check_smartcard_access!' do
before do
stub_licensed_features(smartcard_auth: true)
......@@ -713,14 +712,14 @@ RSpec.describe Gitlab::GitAccess do
project.add_developer(user)
end
context 'user with a smartcard session' do
context 'user with a smartcard session', :clean_gitlab_redis_sessions do
let(:session_id) { '42' }
let(:stored_session) do
{ 'smartcard_signins' => { 'last_signin_at' => 5.minutes.ago } }
end
before do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("session:gitlab:#{session_id}", Marshal.dump(stored_session))
redis.sadd("session:lookup:user:gitlab:#{user.id}", [session_id])
end
......@@ -773,9 +772,9 @@ RSpec.describe Gitlab::GitAccess do
stub_licensed_features(git_two_factor_enforcement: true)
end
context 'with an OTP session' do
context 'with an OTP session', :clean_gitlab_redis_sessions do
before do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("#{Gitlab::Redis::Sessions::OTP_SESSIONS_NAMESPACE}:#{key.id}", true)
end
end
......@@ -804,7 +803,7 @@ RSpec.describe Gitlab::GitAccess do
def stub_redis
redis = double(:redis)
expect(redis_store_class).to receive(:with).at_most(:twice).and_yield(redis)
expect(Gitlab::Redis::Sessions).to receive(:with).at_most(:twice).and_yield(redis)
expect(redis).to(
receive(:get)
......@@ -930,9 +929,6 @@ RSpec.describe Gitlab::GitAccess do
end
end
end
end
it_behaves_like 'redis sessions store', 'checks smartcard access & otp session'
describe '#check_sso_session!' do
before do
......
......@@ -154,15 +154,14 @@ RSpec.describe API::Internal::Base do
project.add_developer(user)
end
RSpec.shared_examples_for 'smartcard session' do
context 'user with a smartcard session' do
context 'user with a smartcard session', :clean_gitlab_redis_sessions do
let(:session_id) { '42' }
let(:stored_session) do
{ 'smartcard_signins' => { 'last_signin_at' => 5.minutes.ago } }
end
before do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("session:gitlab:#{session_id}", Marshal.dump(stored_session))
redis.sadd("session:lookup:user:gitlab:#{user.id}", [session_id])
end
......@@ -174,9 +173,6 @@ RSpec.describe API::Internal::Base do
expect(response).to have_gitlab_http_status(:ok)
end
end
end
it_behaves_like 'redis sessions store', 'smartcard session'
context 'user without a smartcard session' do
it "does not allow access" do
......
......@@ -54,15 +54,14 @@ RSpec.describe Repositories::GitHttpController, type: :request do
project.add_developer(user)
end
RSpec.shared_examples_for 'smartcard session' do
context 'user with a smartcard session' do
context 'user with a smartcard session', :clean_gitlab_redis_sessions do
let(:session_id) { '42' }
let(:stored_session) do
{ 'smartcard_signins' => { 'last_signin_at' => 5.minutes.ago } }
end
before do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("session:gitlab:#{session_id}", Marshal.dump(stored_session))
redis.sadd("session:lookup:user:gitlab:#{user.id}", [session_id])
end
......@@ -74,9 +73,6 @@ RSpec.describe Repositories::GitHttpController, type: :request do
expect(response).to have_gitlab_http_status(:ok)
end
end
end
it_behaves_like 'redis sessions store', 'smartcard session'
context 'user without a smartcard session' do
it "does not allow access" do
......
......@@ -2,13 +2,12 @@
require 'spec_helper'
RSpec.describe ApplicationCable::Connection do
RSpec.shared_examples_for 'ApplicationCable::Connection' do
RSpec.describe ApplicationCable::Connection, :clean_gitlab_redis_sessions do
let(:session_id) { Rack::Session::SessionId.new('6919a6f1bb119dd7396fadc38fd18d0d') }
context 'when session cookie is set' do
before do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("session:gitlab:#{session_id.private_id}", Marshal.dump(session_hash))
end
......@@ -65,7 +64,4 @@ RSpec.describe ApplicationCable::Connection do
expect(connection.current_user).to be_nil
end
end
end
it_behaves_like 'redis sessions store', 'ApplicationCable::Connection'
end
......@@ -2,8 +2,7 @@
require 'spec_helper'
RSpec.describe 'Active user sessions' do
RSpec.shared_examples_for 'active user sessions' do
RSpec.describe 'Active user sessions', :clean_gitlab_redis_sessions do
it 'successful login adds a new active user login' do
now = Time.zone.parse('2018-03-12 09:06')
Timecop.freeze(now) do
......@@ -30,13 +29,13 @@ RSpec.describe 'Active user sessions' do
it 'successful login cleans up obsolete entries' do
user = create(:user)
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.sadd("session:lookup:user:gitlab:#{user.id}", '59822c7d9fcdfa03725eff41782ad97d')
end
gitlab_sign_in(user)
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
expect(redis.smembers("session:lookup:user:gitlab:#{user.id}")).not_to include '59822c7d9fcdfa03725eff41782ad97d'
end
end
......@@ -45,14 +44,14 @@ RSpec.describe 'Active user sessions' do
user = create(:user)
personal_access_token = create(:personal_access_token, user: user)
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.sadd("session:lookup:user:gitlab:#{user.id}", '59822c7d9fcdfa03725eff41782ad97d')
end
visit user_path(user, :atom, private_token: personal_access_token.token)
expect(page.status_code).to eq 200
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
expect(redis.smembers("session:lookup:user:gitlab:#{user.id}")).to include '59822c7d9fcdfa03725eff41782ad97d'
end
end
......@@ -69,7 +68,4 @@ RSpec.describe 'Active user sessions' do
expect(ActiveSession.list(user)).to be_empty
end
end
it_behaves_like 'redis sessions store', 'active user sessions'
end
......@@ -2,10 +2,9 @@
require 'spec_helper'
RSpec.describe 'Session TTLs' do
RSpec.describe 'Session TTLs', :clean_gitlab_redis_shared_state do
include SessionHelpers
RSpec.shared_examples_for 'session ttls' do
it 'creates a session with a short TTL when login fails' do
visit new_user_session_path
# The session key only gets created after a post
......@@ -15,7 +14,7 @@ RSpec.describe 'Session TTLs' do
expect(page).to have_content('Invalid login or password')
expect_single_session_with_short_ttl(redis_store_class)
expect_single_session_with_short_ttl
end
it 'increases the TTL when the login succeeds' do
......@@ -24,7 +23,7 @@ RSpec.describe 'Session TTLs' do
expect(page).to have_content(user.name)
expect_single_session_with_authenticated_ttl(redis_store_class)
expect_single_session_with_authenticated_ttl
end
context 'with an unauthorized project' do
......@@ -33,11 +32,8 @@ RSpec.describe 'Session TTLs' do
it 'creates a session with a short TTL' do
visit project_raw_path(project, 'master/README.md')
expect_single_session_with_short_ttl(redis_store_class)
expect_single_session_with_short_ttl
expect(page).to have_current_path(new_user_session_path)
end
end
end
it_behaves_like 'redis sessions store', 'session ttls'
end
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe 'Login' do
RSpec.describe 'Login', :clean_gitlab_redis_sessions do
include TermsHelper
include UserLoginHelper
include SessionHelpers
......@@ -11,11 +11,6 @@ RSpec.describe 'Login' do
stub_authentication_activity_metrics(debug: true)
end
RSpec.shared_examples_for 'login' do
before do
load Rails.root.join('config/initializers/session_store.rb')
end
describe 'password reset token after successful sign in' do
it 'invalidates password reset token' do
expect(authentication_metrics)
......@@ -65,7 +60,7 @@ RSpec.describe 'Login' do
fill_in 'user_password', with: 'password'
click_button 'Sign in'
expect_single_session_with_authenticated_ttl(redis_store_class)
expect_single_session_with_authenticated_ttl
expect(current_path).to eq root_path
end
......@@ -211,7 +206,7 @@ RSpec.describe 'Login' do
enter_code(user.current_otp)
expect(page).not_to have_content(I18n.t('devise.failure.already_authenticated'))
expect_single_session_with_authenticated_ttl(redis_store_class)
expect_single_session_with_authenticated_ttl
end
it 'does not allow sign-in if the user password is updated before entering a one-time code' do
......@@ -230,7 +225,7 @@ RSpec.describe 'Login' do
enter_code(user.current_otp)
expect_single_session_with_authenticated_ttl(redis_store_class)
expect_single_session_with_authenticated_ttl
expect(current_path).to eq root_path
end
......@@ -259,7 +254,7 @@ RSpec.describe 'Login' do
enter_code(user.current_otp)
expect_single_session_with_authenticated_ttl(redis_store_class)
expect_single_session_with_authenticated_ttl
expect(current_path).to eq root_path
end
......@@ -376,7 +371,7 @@ RSpec.describe 'Login' do
sign_in_using_saml!
expect_single_session_with_authenticated_ttl(redis_store_class)
expect_single_session_with_authenticated_ttl
expect(page).not_to have_content('Two-Factor Authentication')
expect(current_path).to eq root_path
end
......@@ -395,7 +390,7 @@ RSpec.describe 'Login' do
enter_code(user.current_otp)
expect_single_session_with_authenticated_ttl(redis_store_class)
expect_single_session_with_authenticated_ttl
expect(current_path).to eq root_path
end
end
......@@ -416,7 +411,7 @@ RSpec.describe 'Login' do
gitlab_sign_in(user)
expect_single_session_with_authenticated_ttl(redis_store_class)
expect_single_session_with_authenticated_ttl
expect(current_path).to eq root_path
expect(page).not_to have_content(I18n.t('devise.failure.already_authenticated'))
end
......@@ -428,7 +423,7 @@ RSpec.describe 'Login' do
gitlab_sign_in(user)
visit new_user_session_path
expect_single_session_with_authenticated_ttl(redis_store_class)
expect_single_session_with_authenticated_ttl
expect(page).not_to have_content(I18n.t('devise.failure.already_authenticated'))
end
......@@ -470,7 +465,7 @@ RSpec.describe 'Login' do
gitlab_sign_in(user)
expect_single_session_with_short_ttl(redis_store_class)
expect_single_session_with_short_ttl
expect(page).to have_content('Invalid login or password.')
end
end
......@@ -957,7 +952,4 @@ RSpec.describe 'Login' do
end
end
end
end
it_behaves_like 'redis sessions store', 'login'
end
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe Gitlab::AnonymousSession do
RSpec.describe Gitlab::AnonymousSession, :clean_gitlab_redis_sessions do
let(:default_session_id) { '6919a6f1bb119dd7396fadc38fd18d0d' }
let(:additional_session_id) { '7919a6f1bb119dd7396fadc38fd18d0d' }
......@@ -12,12 +12,11 @@ RSpec.describe Gitlab::AnonymousSession do
described_class.new('127.0.0.1')
end
RSpec.shared_examples_for 'anonymous sessions' do
describe '#store_session_ip' do
it 'adds session id to proper key' do
subject.count_session_ip
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
expect(redis.get("session:lookup:ip:gitlab2:127.0.0.1").to_i).to eq 1
end
end
......@@ -26,7 +25,7 @@ RSpec.describe Gitlab::AnonymousSession do
freeze_time do
subject.count_session_ip
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
expect(redis.ttl("session:lookup:ip:gitlab2:127.0.0.1")).to eq(24.hours.to_i)
end
end
......@@ -37,7 +36,7 @@ RSpec.describe Gitlab::AnonymousSession do
subject.count_session_ip
new_anonymous_session.count_session_ip
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
expect(redis.get("session:lookup:ip:gitlab2:127.0.0.1").to_i).to eq(2)
end
end
......@@ -46,7 +45,7 @@ RSpec.describe Gitlab::AnonymousSession do
describe '#stored_sessions' do
it 'returns all anonymous sessions per ip' do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("session:lookup:ip:gitlab2:127.0.0.1", 2)
end
......@@ -55,17 +54,14 @@ RSpec.describe Gitlab::AnonymousSession do
end
it 'removes obsolete lookup through ip entries' do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("session:lookup:ip:gitlab2:127.0.0.1", 2)
end
subject.cleanup_session_per_ip_count
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
expect(redis.exists("session:lookup:ip:gitlab2:127.0.0.1")).to eq(false)
end
end
end
it_behaves_like 'redis sessions store', 'anonymous sessions'
end
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe ActiveSession do
RSpec.describe ActiveSession, :clean_gitlab_redis_sessions do
let(:user) do
create(:user).tap do |user|
user.current_sign_in_at = Time.current
......@@ -21,7 +21,6 @@ RSpec.describe ActiveSession do
})
end
RSpec.shared_examples_for 'active session' do
describe '#current?' do
it 'returns true if the active session matches the current session' do
active_session = ActiveSession.new(session_private_id: rack_session.private_id)
......@@ -45,7 +44,7 @@ RSpec.describe ActiveSession do
describe '.list' do
it 'returns all sessions by user' do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("session:user:gitlab:#{user.id}:6919a6f1bb119dd7396fadc38fd18d0d", Marshal.dump({ session_id: 'a' }))
redis.set("session:user:gitlab:#{user.id}:59822c7d9fcdfa03725eff41782ad97d", Marshal.dump({ session_id: 'b' }))
redis.set("session:user:gitlab:9999:5c8611e4f9c69645ad1a1492f4131358", '')
......@@ -63,7 +62,7 @@ RSpec.describe ActiveSession do
end
it 'does not return obsolete entries and cleans them up' do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("session:user:gitlab:#{user.id}:6919a6f1bb119dd7396fadc38fd18d0d", Marshal.dump({ session_id: 'a' }))
redis.sadd(
......@@ -77,7 +76,7 @@ RSpec.describe ActiveSession do
expect(ActiveSession.list(user)).to eq [{ session_id: 'a' }]
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
expect(redis.sscan_each("session:lookup:user:gitlab:#{user.id}").to_a).to eq ['6919a6f1bb119dd7396fadc38fd18d0d']
end
end
......@@ -89,7 +88,7 @@ RSpec.describe ActiveSession do
describe '.list_sessions' do
it 'uses the ActiveSession lookup to return original sessions' do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
# Emulate redis-rack: https://github.com/redis-store/redis-rack/blob/c75f7f1a6016ee224e2615017fbfee964f23a837/lib/rack/session/redis.rb#L88
redis.set("session:gitlab:#{rack_session.private_id}", Marshal.dump({ _csrf_token: 'abcd' }))
......@@ -110,7 +109,7 @@ RSpec.describe ActiveSession do
it 'uses the user lookup table to return session ids' do
session_ids = ['59822c7d9fcdfa03725eff41782ad97d']
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.sadd("session:lookup:user:gitlab:#{user.id}", session_ids)
end
......@@ -120,7 +119,7 @@ RSpec.describe ActiveSession do
describe '.sessions_from_ids' do
it 'uses the ActiveSession lookup to return original sessions' do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
# Emulate redis-rack: https://github.com/redis-store/redis-rack/blob/c75f7f1a6016ee224e2615017fbfee964f23a837/lib/rack/session/redis.rb#L88
redis.set("session:gitlab:#{rack_session.private_id}", Marshal.dump({ _csrf_token: 'abcd' }))
end
......@@ -129,7 +128,7 @@ RSpec.describe ActiveSession do
end
it 'avoids a redis lookup for an empty array' do
expect(redis_store_class).not_to receive(:with)
expect(Gitlab::Redis::Sessions).not_to receive(:with)
expect(ActiveSession.sessions_from_ids([])).to eq([])
end
......@@ -138,7 +137,7 @@ RSpec.describe ActiveSession do
stub_const('ActiveSession::SESSION_BATCH_SIZE', 1)
redis = double(:redis)
expect(redis_store_class).to receive(:with).and_yield(redis)
expect(Gitlab::Redis::Sessions).to receive(:with).and_yield(redis)
sessions = %w[session-a session-b]
mget_responses = sessions.map { |session| [Marshal.dump(session)]}
......@@ -152,7 +151,7 @@ RSpec.describe ActiveSession do
it 'sets a new redis entry for the user session and a lookup entry' do
ActiveSession.set(user, request)
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
expect(redis.scan_each.to_a).to include(
"session:user:gitlab:#{user.id}:2::418729c72310bbf349a032f0bb6e3fce9f5a69df8f000d8ae0ac5d159d8f21ae",
"session:lookup:user:gitlab:#{user.id}"
......@@ -202,7 +201,7 @@ RSpec.describe ActiveSession do
describe '.destroy_session' do
shared_examples 'removes all session data' do
before do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("session:user:gitlab:#{user.id}:#{active_session_lookup_key}", '')
# Emulate redis-rack: https://github.com/redis-store/redis-rack/blob/c75f7f1a6016ee224e2615017fbfee964f23a837/lib/rack/session/redis.rb#L88
redis.set("session:gitlab:#{rack_session.private_id}", '')
......@@ -217,7 +216,7 @@ RSpec.describe ActiveSession do
it 'removes the devise session' do
subject
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
expect(redis.scan_each(match: "session:gitlab:*").to_a).to be_empty
end
end
......@@ -225,7 +224,7 @@ RSpec.describe ActiveSession do
it 'removes the lookup entry' do
subject
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
expect(redis.scan_each(match: "session:lookup:user:gitlab:#{user.id}").to_a).to be_empty
end
end
......@@ -233,7 +232,7 @@ RSpec.describe ActiveSession do
it 'removes the ActiveSession' do
subject
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
expect(redis.scan_each(match: "session:user:gitlab:*").to_a).to be_empty
end
end
......@@ -270,7 +269,7 @@ RSpec.describe ActiveSession do
let(:current_session_id) { '6919a6f1bb119dd7396fadc38fd18d0d' }
before do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
# setup for current user
[current_session_id, '59822c7d9fcdfa03725eff41782ad97d'].each do |session_public_id|
session_private_id = Rack::Session::SessionId.new(session_public_id).private_id
......@@ -304,7 +303,7 @@ RSpec.describe ActiveSession do
session_private_id = Rack::Session::SessionId.new(current_session_id).private_id
ActiveSession.destroy_all_but_current(user, request.session)
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
expect(
redis.smembers(described_class.lookup_key_name(user.id))
).to eq([session_private_id])
......@@ -313,7 +312,7 @@ RSpec.describe ActiveSession do
it 'does not remove impersonated sessions' do
impersonated_session_id = '6919a6f1bb119dd7396fadc38fd18eee'
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set(described_class.key_name(user.id, impersonated_session_id),
Marshal.dump(ActiveSession.new(session_id: Rack::Session::SessionId.new(impersonated_session_id), is_impersonated: true)))
redis.sadd(described_class.lookup_key_name(user.id), impersonated_session_id)
......@@ -332,7 +331,7 @@ RSpec.describe ActiveSession do
end
it 'removes obsolete lookup entries' do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("session:user:gitlab:#{user.id}:6919a6f1bb119dd7396fadc38fd18d0d", '')
redis.sadd("session:lookup:user:gitlab:#{user.id}", '6919a6f1bb119dd7396fadc38fd18d0d')
redis.sadd("session:lookup:user:gitlab:#{user.id}", '59822c7d9fcdfa03725eff41782ad97d')
......@@ -340,7 +339,7 @@ RSpec.describe ActiveSession do
ActiveSession.cleanup(user)
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
expect(redis.smembers("session:lookup:user:gitlab:#{user.id}")).to eq ['6919a6f1bb119dd7396fadc38fd18d0d']
end
end
......@@ -354,7 +353,7 @@ RSpec.describe ActiveSession do
let(:max_number_of_sessions_plus_two) { ActiveSession::ALLOWED_NUMBER_OF_ACTIVE_SESSIONS + 2 }
before do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
(1..max_number_of_sessions_plus_two).each do |number|
redis.set(
"session:user:gitlab:#{user.id}:#{number}",
......@@ -371,7 +370,7 @@ RSpec.describe ActiveSession do
it 'removes obsolete active sessions entries' do
ActiveSession.cleanup(user)
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
sessions = redis.scan_each(match: "session:user:gitlab:#{user.id}:*").to_a
expect(sessions.count).to eq(ActiveSession::ALLOWED_NUMBER_OF_ACTIVE_SESSIONS)
......@@ -382,7 +381,7 @@ RSpec.describe ActiveSession do
it 'removes obsolete lookup entries' do
ActiveSession.cleanup(user)
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
lookup_entries = redis.smembers("session:lookup:user:gitlab:#{user.id}")
expect(lookup_entries.count).to eq(ActiveSession::ALLOWED_NUMBER_OF_ACTIVE_SESSIONS)
......@@ -391,7 +390,7 @@ RSpec.describe ActiveSession do
end
it 'removes obsolete lookup entries even without active session' do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.sadd(
"session:lookup:user:gitlab:#{user.id}",
"#{max_number_of_sessions_plus_two + 1}"
......@@ -400,7 +399,7 @@ RSpec.describe ActiveSession do
ActiveSession.cleanup(user)
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
lookup_entries = redis.smembers("session:lookup:user:gitlab:#{user.id}")
expect(lookup_entries.count).to eq(ActiveSession::ALLOWED_NUMBER_OF_ACTIVE_SESSIONS)
......@@ -414,7 +413,7 @@ RSpec.describe ActiveSession do
context 'when the number of active sessions is lower than the limit' do
before do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
((max_number_of_sessions_plus_two - 4)..max_number_of_sessions_plus_two).each do |number|
redis.del("session:user:gitlab:#{user.id}:#{number}")
end
......@@ -422,17 +421,17 @@ RSpec.describe ActiveSession do
end
it 'does not remove active session entries, but removes lookup entries' do
lookup_entries_before_cleanup = redis_store_class.with do |redis|
lookup_entries_before_cleanup = Gitlab::Redis::Sessions.with do |redis|
redis.smembers("session:lookup:user:gitlab:#{user.id}")
end
sessions_before_cleanup = redis_store_class.with do |redis|
sessions_before_cleanup = Gitlab::Redis::Sessions.with do |redis|
redis.scan_each(match: "session:user:gitlab:#{user.id}:*").to_a
end
ActiveSession.cleanup(user)
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
lookup_entries = redis.smembers("session:lookup:user:gitlab:#{user.id}")
sessions = redis.scan_each(match: "session:user:gitlab:#{user.id}:*").to_a
expect(sessions.count).to eq(sessions_before_cleanup.count)
......@@ -447,7 +446,7 @@ RSpec.describe ActiveSession do
let(:max_number_of_sessions_plus_two) { ActiveSession::ALLOWED_NUMBER_OF_ACTIVE_SESSIONS + 2 }
before do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
(1..max_number_of_sessions_plus_two).each do |number|
redis.set(
"session:user:gitlab:#{user.id}:#{number}",
......@@ -464,7 +463,7 @@ RSpec.describe ActiveSession do
it 'removes obsolete active sessions entries' do
ActiveSession.cleanup(user)
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
sessions = redis.scan_each(match: "session:user:gitlab:#{user.id}:*").to_a
expect(sessions.count).to eq(ActiveSession::ALLOWED_NUMBER_OF_ACTIVE_SESSIONS)
......@@ -475,7 +474,4 @@ RSpec.describe ActiveSession do
end
end
end
end
it_behaves_like 'redis sessions store', 'active session'
end
......@@ -376,13 +376,12 @@ RSpec.describe API::Commits do
end
end
RSpec.shared_examples_for 'warden user session' do
context 'when using warden' do
it 'increments usage counters' do
it 'increments usage counters', :clean_gitlab_redis_sessions do
session_id = Rack::Session::SessionId.new('6919a6f1bb119dd7396fadc38fd18d0d')
session_hash = { 'warden.user.user.key' => [[user.id], user.encrypted_password[0, 29]] }
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("session:gitlab:#{session_id.private_id}", Marshal.dump(session_hash))
end
......@@ -394,9 +393,6 @@ RSpec.describe API::Commits do
post api(url), params: valid_c_params
end
end
end
it_behaves_like 'redis sessions store', 'warden user session'
context 'a new file in project repo' do
before do
......
# frozen_string_literal: true
module SessionHelpers
def expect_single_session_with_authenticated_ttl(redis_store_class)
expect_single_session_with_expiration(redis_store_class, Settings.gitlab['session_expire_delay'] * 60)
def expect_single_session_with_authenticated_ttl
expect_single_session_with_expiration(Settings.gitlab['session_expire_delay'] * 60)
end
def expect_single_session_with_short_ttl(redis_store_class)
expect_single_session_with_expiration(redis_store_class, Settings.gitlab['unauthenticated_session_expire_delay'])
def expect_single_session_with_short_ttl
expect_single_session_with_expiration(Settings.gitlab['unauthenticated_session_expire_delay'])
end
def expect_single_session_with_expiration(redis_store_class, expiration)
session_keys = get_session_keys(redis_store_class)
def expect_single_session_with_expiration(expiration)
session_keys = get_session_keys
expect(session_keys.size).to eq(1)
expect(get_ttl(redis_store_class, session_keys.first)).to be_within(5).of(expiration)
expect(get_ttl(session_keys.first)).to be_within(5).of(expiration)
end
def get_session_keys(redis_store_class)
redis_store_class.with { |redis| redis.scan_each(match: 'session:gitlab:*').to_a }
def get_session_keys
Gitlab::Redis::Sessions.with { |redis| redis.scan_each(match: 'session:gitlab:*').to_a }
end
def get_ttl(redis_store_class, key)
redis_store_class.with { |redis| redis.ttl(key) }
def get_ttl(key)
Gitlab::Redis::Sessions.with { |redis| redis.ttl(key) }
end
end
# frozen_string_literal: true
RSpec.shared_examples 'redis sessions store' do |example|
context 'when ENV[GITLAB_USE_REDIS_SESSIONS_STORE] is true', :clean_gitlab_redis_sessions do
before do
stub_env('GITLAB_USE_REDIS_SESSIONS_STORE', 'true')
end
it_behaves_like example do
let(:redis_store_class) { Gitlab::Redis::Sessions }
end
end
context 'when ENV[GITLAB_USE_REDIS_SESSIONS_STORE] is false', :clean_gitlab_redis_shared_state do
before do
stub_env('GITLAB_USE_REDIS_SESSIONS_STORE', 'false')
end
it_behaves_like example do
let(:redis_store_class) { Gitlab::Redis::SharedState }
end
end
end
......@@ -18,20 +18,19 @@ RSpec.shared_examples 'snippet edit usage data counters' do
end
end
RSpec.shared_examples_for 'sessionless user' do
context 'when user is not sessionless' do
context 'when user is not sessionless', :clean_gitlab_redis_sessions do
before do
session_id = Rack::Session::SessionId.new('6919a6f1bb119dd7396fadc38fd18d0d')
session_hash = { 'warden.user.user.key' => [[current_user.id], current_user.encrypted_password[0, 29]] }
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.with do |redis|
redis.set("session:gitlab:#{session_id.private_id}", Marshal.dump(session_hash))
end
cookies[Gitlab::Application.config.session_options[:key]] = session_id.public_id
end
it 'tracks usage data actions' do
it 'tracks usage data actions', :clean_gitlab_redis_sessions do
expect(::Gitlab::UsageDataCounters::EditorUniqueCounter).to receive(:track_snippet_editor_edit_action)
post_graphql_mutation(mutation)
......@@ -47,7 +46,4 @@ RSpec.shared_examples 'snippet edit usage data counters' do
end
end
end
end
it_behaves_like 'redis sessions store', 'sessionless user'
end
......@@ -3,7 +3,6 @@
require 'rake_helper'
RSpec.describe 'gitlab:cleanup rake tasks', :silence_stdout do
RSpec.shared_examples_for 'rake gitlab:cleanup' do
before do
Rake.application.rake_require 'tasks/gitlab/cleanup'
end
......@@ -167,14 +166,14 @@ RSpec.describe 'gitlab:cleanup rake tasks', :silence_stdout do
end
context 'sessions' do
describe 'gitlab:cleanup:sessions:active_sessions_lookup_keys' do
describe 'gitlab:cleanup:sessions:active_sessions_lookup_keys', :clean_gitlab_redis_sessions 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
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.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)
......@@ -186,7 +185,7 @@ RSpec.describe 'gitlab:cleanup rake tasks', :silence_stdout do
end
it 'removes expired active session lookup keys' do
redis_store_class.with do |redis|
Gitlab::Redis::Sessions.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(
......@@ -195,7 +194,4 @@ RSpec.describe 'gitlab:cleanup rake tasks', :silence_stdout do
end
end
end
end
it_behaves_like 'redis sessions store', 'rake gitlab:cleanup'
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment