diff --git a/app/models/account.rb b/app/models/account.rb index 9c6a607..c185c22 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -39,7 +39,9 @@ class Account < ApplicationRecord validates :public_name, allow_nil: true, length: { in: 1..255 } - validates :biography, allow_nil: true, length: { in: 1..10_000 } + validates :biography, + allow_nil: true, + good_big_text: true validates :avatar, allow_nil: true, image: true diff --git a/app/models/session.rb b/app/models/session.rb index a0face9..1b5ba86 100644 --- a/app/models/session.rb +++ b/app/models/session.rb @@ -22,12 +22,14 @@ class Session < ApplicationRecord validates :ip_address, presence: true - validates :user_agent, length: { maximum: 10_000 } + validates :user_agent, + allow_nil: true, + good_big_text: true private def turn_nils_into_blanks - self.user_agent = '' if user_agent.blank? + self.user_agent = nil if user_agent.blank? end def strip_extra_spaces diff --git a/app/validators/good_big_text_validator.rb b/app/validators/good_big_text_validator.rb new file mode 100644 index 0000000..36b54a5 --- /dev/null +++ b/app/validators/good_big_text_validator.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class GoodBigTextValidator < GoodTextValidator + class Validation < Validation + MIN = 1 + MAX = 10_000 + + def perform + super + error! :too_short, count: MIN if value.to_s.length < MIN + error! :too_long, count: MAX if value.to_s.length > MAX + end + end +end diff --git a/db/migrate/20190929131544_change_user_agent.rb b/db/migrate/20190929131544_change_user_agent.rb new file mode 100644 index 0000000..bc17b33 --- /dev/null +++ b/db/migrate/20190929131544_change_user_agent.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +class ChangeUserAgent < ActiveRecord::Migration[6.0] + include Partynest::Migration + + def change + drop_constraint :sessions, :user_agent, <<~SQL + user_agent = '' OR is_good_big_text(user_agent) + SQL + + remove_column :sessions, :user_agent, :string, null: false, default: '' + add_column :sessions, :user_agent, :string, null: true + + constraint :sessions, :user_agent, <<~SQL + user_agent IS NULL OR is_good_big_text(user_agent) + SQL + end +end diff --git a/db/structure.sql b/db/structure.sql index 114e503..8605418 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -763,9 +763,9 @@ CREATE TABLE public.sessions ( account_id bigint NOT NULL, logged_at timestamp without time zone NOT NULL, ip_address character varying NOT NULL, - user_agent character varying DEFAULT ''::character varying NOT NULL, + user_agent character varying, CONSTRAINT ip_address CHECK (public.is_good_small_text((ip_address)::text)), - CONSTRAINT user_agent CHECK ((((user_agent)::text = ''::text) OR public.is_good_big_text((user_agent)::text))) + CONSTRAINT user_agent CHECK (((user_agent IS NULL) OR public.is_good_big_text((user_agent)::text))) ); @@ -1617,6 +1617,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20190921142404'), ('20190921161613'), ('20190921191213'), -('20190928171705'); +('20190928171705'), +('20190929131544'); diff --git a/factories/sessions.rb b/factories/sessions.rb index 9aa09d7..ac73afd 100644 --- a/factories/sessions.rb +++ b/factories/sessions.rb @@ -13,6 +13,10 @@ FactoryBot.define do trait :with_ipv6_address do ip_address { Faker::Internet.ip_v6_address } end + + trait :without_user_agent do + user_agent { nil } + end end factory :some_session_with_ipv6_address, diff --git a/spec/models/session_spec.rb b/spec/models/session_spec.rb index 9df54bc..6974e37 100644 --- a/spec/models/session_spec.rb +++ b/spec/models/session_spec.rb @@ -33,19 +33,19 @@ RSpec.describe Session do it { is_expected.to validate_length_of(:user_agent).is_at_most(10_000) } - it { is_expected.to allow_value '' } + it { is_expected.to allow_value nil } it { is_expected.to allow_value Faker::Internet.user_agent } context 'when it was set to nil' do subject { build :some_session, user_agent: nil } before { subject.validate } - specify { expect(subject.user_agent).to eq '' } + specify { expect(subject.user_agent).to eq nil } end context 'when it was set to blank' do subject { build :some_session, user_agent: ' ' * rand(1..3) } before { subject.validate } - specify { expect(subject.user_agent).to eq '' } + specify { expect(subject.user_agent).to eq nil } end context 'when it has leading spaces' do diff --git a/spec/validators/good_big_text_validator_spec.rb b/spec/validators/good_big_text_validator_spec.rb new file mode 100644 index 0000000..0df863d --- /dev/null +++ b/spec/validators/good_big_text_validator_spec.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe GoodBigTextValidator do + pending "add some examples to (or delete) #{__FILE__}" +end