Compare commits

...

3 Commits

Author SHA1 Message Date
Wolfgang Fournès
10e5f21aef
Merge 9983edd739 into 4c0e44ebbe 2024-10-04 06:50:50 +02:00
Wolfgang Fournès
9983edd739 fix invalid domain block severities
This commit fixes any invalid domain block severities by setting them to
2 if they are bigger than 2 or 0 if they are smaller than 0.
This ensures that the domain block severities are always within the valid
range of 0 to 2.
2024-10-04 06:50:46 +02:00
Wolfgang Fournès
560700b50c Add severity validation 2024-10-04 06:50:46 +02:00
3 changed files with 29 additions and 1 deletions

View File

@ -21,7 +21,7 @@ class DomainBlock < ApplicationRecord
include DomainNormalizable
include DomainMaterializable
enum :severity, { silence: 0, suspend: 1, noop: 2 }
enum :severity, { silence: 0, suspend: 1, noop: 2 }, validate: true
validates :domain, presence: true, uniqueness: true, domain: true

View File

@ -0,0 +1,17 @@
# frozen_string_literal: true
class FixInvalidDomainBlockSeverities < ActiveRecord::Migration[7.1]
disable_ddl_transaction!
def up
safety_assured do
execute <<~SQL.squish
UPDATE domain_blocks
SET severity = CASE WHEN severity > 2 THEN 2 WHEN severity < 0 THEN 0 END
WHERE severity > 2 OR severity < 0 RETURNING id;
SQL
end
end
def down; end
end

View File

@ -217,6 +217,17 @@ RSpec.describe 'Domain Blocks' do
.to start_with('application/json')
end
end
context 'when severity is invalid' do
let(:params) { { domain: 'bar.com', severity: :bar } }
it 'returns http unprocessable entity' do
subject
expect(response).to have_http_status(422)
expect(body_as_json[:error]).to eq('Validation failed: Severity is not included in the list')
end
end
end
describe 'PUT /api/v1/admin/domain_blocks/:id' do