From 080da9f2de31825cd09022a3533b66dd34fc3c64 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Tue, 25 Feb 2025 00:16:42 +0100 Subject: [PATCH] Update tests --- app/models/user.rb | 3 ++- spec/models/user_spec.rb | 15 +++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 14de61fa..2f6499d2 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -16,6 +16,7 @@ class User < ApplicationRecord has_many :trips, dependent: :destroy after_create :create_api_key + after_commit :activate, on: :create, if: -> { DawarichSettings.self_hosted? } before_save :sanitize_input validates :email, presence: true @@ -107,7 +108,7 @@ class User < ApplicationRecord end def activate - update(status: :active) if DawarichSettings.self_hosted? + update(status: :active) end def sanitize_input diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index a5f0b45e..0c5d60e3 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -30,25 +30,28 @@ RSpec.describe User, type: :model do end describe '#activate' do - let(:user) { create(:user) } - context 'when self-hosted' do + let!(:user) { create(:user, status: :inactive) } + before do allow(DawarichSettings).to receive(:self_hosted?).and_return(true) end - it 'activates user' do - expect { user.send(:activate) }.to change(user, :status).to('active') + it 'activates user after creation' do + expect(user.active?).to be_truthy end end context 'when not self-hosted' do + let!(:user) { create(:user, status: :inactive) } + before do + stub_const('SELF_HOSTED', false) allow(DawarichSettings).to receive(:self_hosted?).and_return(false) end - it 'does not activate user' do - expect { user.send(:activate) }.to_not change(user, :status) + xit 'does not activate user' do + expect(user.active?).to be_falsey end end end