diff --git a/app/models/txt.rb b/app/models/txt.rb new file mode 100644 index 0000000..cd8c6e7 --- /dev/null +++ b/app/models/txt.rb @@ -0,0 +1,8 @@ +class TXT < Record + validates :content, + format: { + with: /\A".*"\Z/, + message: 'TXT records should be enclosed in quotes' + } + +end diff --git a/test/factories/txt.rb b/test/factories/txt.rb new file mode 100644 index 0000000..9490d26 --- /dev/null +++ b/test/factories/txt.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :txt, class: 'TXT' do + domain + name '' + content '"v=spf1 a:mail.example.com -all"' + end +end diff --git a/test/models/txt_test.rb b/test/models/txt_test.rb new file mode 100644 index 0000000..9203c12 --- /dev/null +++ b/test/models/txt_test.rb @@ -0,0 +1,36 @@ +require 'test_helper' + +class TXTTest < ActiveSupport::TestCase + + test 'saves' do + rec = build(:txt) + rec.valid? + + assert_empty rec.errors + assert rec.save + end + + [ + '""', + '"The quick brown fox"' + ].each { |txt| + test "valid content #{txt}" do + rec = build(:txt, content: txt) + rec.valid? + assert_empty rec.errors[:content], "#{txt.inspect} should be valid!" + end + } + + [ + '', + '"', + 'no quotes', + ].each { |txt| + test "invalid content #{txt}" do + rec = build(:txt, content: txt) + rec.valid? + assert_not_empty rec.errors[:content], "#{txt.inspect} should be invalid!" + end + } + +end