From 2228a6c0ed922eee98ca7c6fc2108d062505b826 Mon Sep 17 00:00:00 2001 From: Vishnu Narayanan Date: Sat, 23 May 2026 15:31:58 +0530 Subject: [PATCH] chore: fix rubocop --- .../sessions_controller_spec.rb | 2 +- .../user_session_tracking_service_spec.rb | 20 +++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/spec/controllers/devise_overrides/sessions_controller_spec.rb b/spec/controllers/devise_overrides/sessions_controller_spec.rb index 9a51a4a32..cbe93604c 100644 --- a/spec/controllers/devise_overrides/sessions_controller_spec.rb +++ b/spec/controllers/devise_overrides/sessions_controller_spec.rb @@ -303,7 +303,7 @@ RSpec.describe DeviseOverrides::SessionsController, type: :controller do end end - context 'on successful login' do + context 'with a successful login' do before { request.env['HTTP_USER_AGENT'] = browser_ua } it 'creates a UserSession row for the new client_id' do diff --git a/spec/services/user_session_tracking_service_spec.rb b/spec/services/user_session_tracking_service_spec.rb index c19d2e057..e3c4bdde5 100644 --- a/spec/services/user_session_tracking_service_spec.rb +++ b/spec/services/user_session_tracking_service_spec.rb @@ -11,26 +11,30 @@ RSpec.describe UserSessionTrackingService do ) end let(:service) { described_class.new(user: user, request: request, client_id: client_id) } + let(:geo_result) { OpenStruct.new(city: 'Mountain View', country: 'United States', country_code: 'US') } + let(:ip_lookup) { instance_double(IpLookupService, perform: geo_result) } - before do - allow_any_instance_of(IpLookupService).to receive(:perform).and_return( - double('GeocoderResult', city: 'Mountain View', country: 'United States', country_code: 'US') - ) - end + before { allow(IpLookupService).to receive(:new).and_return(ip_lookup) } describe '#create_or_update!' do - it 'creates a new UserSession with parsed metadata' do + it 'creates a new UserSession with the right client_id and timestamps' do expect { service.create_or_update! }.to change(user.user_sessions, :count).by(1) session = user.user_sessions.last expect(session.client_id).to eq(client_id) + expect(session.last_activity_at).to be_within(1.second).of(Time.current) + end + + it 'populates request, browser, and geo metadata on the new session', :aggregate_failures do + service.create_or_update! + + session = user.user_sessions.last expect(session.ip_address).to eq('8.8.8.8') expect(session.browser_name).to eq('Safari') expect(session.platform_name).to eq('macOS') expect(session.city).to eq('Mountain View') expect(session.country).to eq('United States') expect(session.country_code).to eq('US') - expect(session.last_activity_at).to be_within(1.second).of(Time.current) end it 'updates an existing session when client_id matches' do @@ -42,7 +46,7 @@ RSpec.describe UserSessionTrackingService do end it 'handles missing geo data gracefully' do - allow_any_instance_of(IpLookupService).to receive(:perform).and_return(nil) + allow(ip_lookup).to receive(:perform).and_return(nil) service.create_or_update!