From 0db75588220719d5f812b33a3e45970118c6acb9 Mon Sep 17 00:00:00 2001 From: Daniel M Brasil Date: Mon, 13 Jan 2025 10:50:58 -0300 Subject: [PATCH] Fix HTTP 500 on `POST /api/v1/admin/ip_blocks` (#29308) --- app/models/ip_block.rb | 2 +- spec/models/ip_block_spec.rb | 2 ++ spec/requests/api/v1/admin/ip_blocks_spec.rb | 10 ++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/models/ip_block.rb b/app/models/ip_block.rb index 416ae383827..4c95ac38dea 100644 --- a/app/models/ip_block.rb +++ b/app/models/ip_block.rb @@ -24,7 +24,7 @@ class IpBlock < ApplicationRecord sign_up_requires_approval: 5000, sign_up_block: 5500, no_access: 9999, - }, prefix: true + }, prefix: true, validate: true validates :ip, :severity, presence: true validates :ip, uniqueness: true diff --git a/spec/models/ip_block_spec.rb b/spec/models/ip_block_spec.rb index 93ee72423b4..856d55be9d8 100644 --- a/spec/models/ip_block_spec.rb +++ b/spec/models/ip_block_spec.rb @@ -12,6 +12,8 @@ RSpec.describe IpBlock do it { is_expected.to validate_presence_of(:severity) } it { is_expected.to validate_uniqueness_of(:ip) } + + it { is_expected.to allow_values(:sign_up_requires_approval, :sign_up_block, :no_access).for(:severity) } end describe '#to_log_human_identifier' do diff --git a/spec/requests/api/v1/admin/ip_blocks_spec.rb b/spec/requests/api/v1/admin/ip_blocks_spec.rb index aa3db339154..59ef8d29665 100644 --- a/spec/requests/api/v1/admin/ip_blocks_spec.rb +++ b/spec/requests/api/v1/admin/ip_blocks_spec.rb @@ -187,6 +187,16 @@ RSpec.describe 'IP Blocks' do .to start_with('application/json') end end + + context 'when the given severity is invalid' do + let(:params) { { ip: '151.0.32.55', severity: 'invalid' } } + + it 'returns http unprocessable entity' do + subject + + expect(response).to have_http_status(422) + end + end end describe 'PUT /api/v1/admin/ip_blocks/:id' do