From 8cdc1481679ef0c7a848d488dae1a265cdcfeac1 Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Thu, 12 Sep 2024 15:29:55 +0200 Subject: [PATCH] Handle invalid visibility (#31571) --- app/models/status.rb | 2 +- spec/services/post_status_service_spec.rb | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/models/status.rb b/app/models/status.rb index 73f0052673..e0630733d8 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -52,7 +52,7 @@ class Status < ApplicationRecord update_index('statuses', :proper) update_index('public_statuses', :proper) - enum :visibility, { public: 0, unlisted: 1, private: 2, direct: 3, limited: 4 }, suffix: :visibility + enum :visibility, { public: 0, unlisted: 1, private: 2, direct: 3, limited: 4 }, suffix: :visibility, validate: true belongs_to :application, class_name: 'Doorkeeper::Application', optional: true diff --git a/spec/services/post_status_service_spec.rb b/spec/services/post_status_service_spec.rb index 7e44789628..26db398d5e 100644 --- a/spec/services/post_status_service_spec.rb +++ b/spec/services/post_status_service_spec.rb @@ -68,7 +68,10 @@ RSpec.describe PostStatusService do it 'raises invalid record error' do expect do subject.call(account, text: 'Hi future!', scheduled_at: invalid_scheduled_time) - end.to raise_error(ActiveRecord::RecordInvalid) + end.to raise_error( + ActiveRecord::RecordInvalid, + 'Validation failed: Scheduled at The scheduled date must be in the future' + ) end end end @@ -123,6 +126,15 @@ RSpec.describe PostStatusService do expect(status.visibility).to eq 'private' end + it 'raises on an invalid visibility' do + expect do + create_status_with_options(visibility: :xxx) + end.to raise_error( + ActiveRecord::RecordInvalid, + 'Validation failed: Visibility is not included in the list' + ) + end + it 'creates a status with limited visibility for silenced users' do status = subject.call(Fabricate(:account, silenced: true), text: 'test', visibility: :public)