Reduce factory creation in User#reset_password! spec (#32021)

This commit is contained in:
Matt Jankowski 2024-09-23 05:18:04 -04:00 committed by GitHub
parent 5d6a3f2cb0
commit cd7b670cd8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -434,7 +434,9 @@ RSpec.describe User do
end end
describe '#reset_password!' do describe '#reset_password!' do
subject(:user) { Fabricate(:user, password: 'foobar12345') } subject(:user) { Fabricate(:user, password: original_password) }
let(:original_password) { 'foobar12345' }
let!(:session_activation) { Fabricate(:session_activation, user: user) } let!(:session_activation) { Fabricate(:session_activation, user: user) }
let!(:access_token) { Fabricate(:access_token, resource_owner_id: user.id) } let!(:access_token) { Fabricate(:access_token, resource_owner_id: user.id) }
@ -442,31 +444,40 @@ RSpec.describe User do
let(:redis_pipeline_stub) { instance_double(Redis::Namespace, publish: nil) } let(:redis_pipeline_stub) { instance_double(Redis::Namespace, publish: nil) }
before do before { stub_redis }
allow(redis).to receive(:pipelined).and_yield(redis_pipeline_stub)
user.reset_password! it 'changes the password immediately and revokes related access' do
expect { user.reset_password! }
.to remove_activated_sessions
.and remove_active_user_tokens
.and remove_user_web_subscriptions
expect(user)
.to_not be_external_or_valid_password(original_password)
expect { session_activation.reload }
.to raise_error(ActiveRecord::RecordNotFound)
expect { web_push_subscription.reload }
.to raise_error(ActiveRecord::RecordNotFound)
expect(redis_pipeline_stub)
.to have_received(:publish).with("timeline:access_token:#{access_token.id}", Oj.dump(event: :kill)).once
end end
it 'changes the password immediately' do def remove_activated_sessions
expect(user.external_or_valid_password?('foobar12345')).to be false change(user.session_activations, :count).to(0)
end end
it 'deactivates all sessions' do def remove_active_user_tokens
expect(user.session_activations.count).to eq 0 change { Doorkeeper::AccessToken.active_for(user).count }.to(0)
expect { session_activation.reload }.to raise_error(ActiveRecord::RecordNotFound)
end end
it 'revokes all access tokens' do def remove_user_web_subscriptions
expect(Doorkeeper::AccessToken.active_for(user).count).to eq 0 change { Web::PushSubscription.where(user: user).or(Web::PushSubscription.where(access_token: access_token)).count }.to(0)
end end
it 'revokes streaming access for all access tokens' do def stub_redis
expect(redis_pipeline_stub).to have_received(:publish).with("timeline:access_token:#{access_token.id}", Oj.dump(event: :kill)).once allow(redis)
end .to receive(:pipelined)
.and_yield(redis_pipeline_stub)
it 'removes push subscriptions' do
expect(Web::PushSubscription.where(user: user).or(Web::PushSubscription.where(access_token: access_token)).count).to eq 0
expect { web_push_subscription.reload }.to raise_error(ActiveRecord::RecordNotFound)
end end
end end