Skip to content

Commit

Permalink
fix: Return HTTP 422 when scheduled status time is less than 5 minutes (
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearlyClaire committed Jul 3, 2024
1 parent 9b6219c commit 6cd9bd6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/services/post_status_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def idempotency_duplicate?
end

def scheduled_in_the_past?
@scheduled_at.present? && @scheduled_at <= Time.now.utc + MIN_SCHEDULE_OFFSET
@scheduled_at.present? && @scheduled_at <= Time.now.utc
end

def bump_potential_friendship!
Expand Down
40 changes: 40 additions & 0 deletions spec/controllers/api/v1/statuses_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,46 @@
expect(response.headers['X-RateLimit-Remaining']).to eq '0'
end
end

context 'with missing thread' do
subject { post :create, params: params }

let(:params) { { status: 'Hello world', in_reply_to_id: 0 } }

it 'returns http not found' do
subject

expect(response).to have_http_status(404)
end
end

context 'when scheduling a status' do
subject { post :create, params: params }

let(:params) { { status: 'Hello world', scheduled_at: 10.minutes.from_now } }
let(:account) { user.account }

it 'returns HTTP 200' do
subject

expect(response).to have_http_status(200)
end

it 'creates a scheduled status' do
expect { subject }.to change { account.scheduled_statuses.count }.from(0).to(1)
end

context 'when the scheduling time is less than 5 minutes' do
let(:params) { { status: 'Hello world', scheduled_at: 4.minutes.from_now } }

it 'does not create a scheduled status', :aggregate_failures do
subject

expect(response).to have_http_status(422)
expect(account.scheduled_statuses).to be_empty
end
end
end
end

describe 'DELETE #destroy' do
Expand Down
10 changes: 10 additions & 0 deletions spec/services/post_status_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@
status2 = subject.call(account, text: 'test', idempotency: 'meepmeep', scheduled_at: future)
expect(status2.id).to eq status1.id
end

context 'when scheduled_at is less than min offset' do
let(:invalid_scheduled_time) { 4.minutes.from_now }

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
end
end

it 'creates response to the original status of boost' do
Expand Down

0 comments on commit 6cd9bd6

Please sign in to comment.