From aefc5f5112fb1cea276ebe637ef884cf667d62bc Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Thu, 25 Feb 2010 19:14:50 +0000 Subject: [PATCH] Add email validation plugin from: git://github.com/dancroak/validates_email_format_of.git --- .../validates_email_format_of/.gitignore | 3 + .../validates_email_format_of/CHANGELOG | 22 ++++ .../validates_email_format_of/MIT-LICENSE | 20 ++++ .../validates_email_format_of/README.markdown | 58 +++++++++++ .../validates_email_format_of/Rakefile | 12 +++ .../plugins/validates_email_format_of/init.rb | 1 + .../lib/validates_email_format_of.rb | 56 ++++++++++ .../validates_email_format_of/rails/init.rb | 1 + .../validates_email_format_of/rakefile | 12 +++ .../validates_email_format_of.rb | 97 ++++++++++++++++++ .../test/db/email_format_test.sqlite3 | Bin 0 -> 5120 bytes .../test/test_helper.rb | 47 +++++++++ .../test/validates_email_format_of_test.rb | 37 +++++++ .../validates_email_format_of.gemspec | 32 ++++++ 14 files changed, 398 insertions(+) create mode 100644 vendor/plugins/validates_email_format_of/.gitignore create mode 100644 vendor/plugins/validates_email_format_of/CHANGELOG create mode 100644 vendor/plugins/validates_email_format_of/MIT-LICENSE create mode 100644 vendor/plugins/validates_email_format_of/README.markdown create mode 100644 vendor/plugins/validates_email_format_of/Rakefile create mode 100644 vendor/plugins/validates_email_format_of/init.rb create mode 100644 vendor/plugins/validates_email_format_of/lib/validates_email_format_of.rb create mode 100644 vendor/plugins/validates_email_format_of/rails/init.rb create mode 100644 vendor/plugins/validates_email_format_of/rakefile create mode 100644 vendor/plugins/validates_email_format_of/shoulda_macros/validates_email_format_of.rb create mode 100644 vendor/plugins/validates_email_format_of/test/db/email_format_test.sqlite3 create mode 100644 vendor/plugins/validates_email_format_of/test/test_helper.rb create mode 100644 vendor/plugins/validates_email_format_of/test/validates_email_format_of_test.rb create mode 100644 vendor/plugins/validates_email_format_of/validates_email_format_of.gemspec diff --git a/vendor/plugins/validates_email_format_of/.gitignore b/vendor/plugins/validates_email_format_of/.gitignore new file mode 100644 index 000000000..c5f93e3ef --- /dev/null +++ b/vendor/plugins/validates_email_format_of/.gitignore @@ -0,0 +1,3 @@ +pkg +test/debug.log +*.swp diff --git a/vendor/plugins/validates_email_format_of/CHANGELOG b/vendor/plugins/validates_email_format_of/CHANGELOG new file mode 100644 index 000000000..ce4a5e158 --- /dev/null +++ b/vendor/plugins/validates_email_format_of/CHANGELOG @@ -0,0 +1,22 @@ += CHANGELOG + +== Version 1.0 + * initial version + +== Version 1.1 (the Francis Hwang edition) + * moved Regexp out of class methods into the ValidatesEmailFormatOf module + +== Version 1.2 (the Ismael Santos Kafeltz and Michael MacDonald edition) + * added support for un-escaped and escaped special characters in the local part, per RFC 3696 + * added :allow_nil option + +== Version 1.2.1 (the RTFM edition) + * added support for quoted local parts + * added length checks for domain and local parts + * corrected escaped character support for RFC 3696 Errata + * added :allow_blank option + * added :unless option + +== Unreleased + * Now available as a gem on GitHub + * added should_validate_email_format_of diff --git a/vendor/plugins/validates_email_format_of/MIT-LICENSE b/vendor/plugins/validates_email_format_of/MIT-LICENSE new file mode 100644 index 000000000..1bae24d25 --- /dev/null +++ b/vendor/plugins/validates_email_format_of/MIT-LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2006 Alex Dunae + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/plugins/validates_email_format_of/README.markdown b/vendor/plugins/validates_email_format_of/README.markdown new file mode 100644 index 000000000..d4325743a --- /dev/null +++ b/vendor/plugins/validates_email_format_of/README.markdown @@ -0,0 +1,58 @@ +Validates email format +====================== + +Validate various formats of email address against RFC 2822. + +Usage +----- + + class PersonTest < ActiveSupport::TestCase + should_validate_email_format_of :email + end + + class Person < ActiveRecord::Base + validates_email_format_of :email + end + +Options +------- + + :message => + String. A custom error message (default is: " does not appear to be a valid e-mail address") + + :on => + Symbol. Specifies when this validation is active (default is :save, other options :create, :update) + + :allow_nil => + Boolean. Allow nil values (default is false) + + :allow_blank => + Boolean. Allow blank values (default is false) + + :if => + Specifies a method, proc or string to call to determine if the validation should occur + (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The method, + proc or string should return or evaluate to a true or false value. + + :unless => + See :if option. + +Testing +------- + +To execute the unit tests run rake test. + +The unit tests for this plugin use an in-memory sqlite3 database. + +Installing the gem +------------------ + +* gem sources -a http://gems.github.com (only needed once) +* sudo gem install dancroak-validates\_email\_format\_of + +Credits +------- + +Written by Alex Dunae (dunae.ca), 2006-07. + +Thanks to Francis Hwang (http://fhwang.net/) at Diversion Media for creating the 1.1 update. diff --git a/vendor/plugins/validates_email_format_of/Rakefile b/vendor/plugins/validates_email_format_of/Rakefile new file mode 100644 index 000000000..0b617435e --- /dev/null +++ b/vendor/plugins/validates_email_format_of/Rakefile @@ -0,0 +1,12 @@ +require 'rake' +require 'rake/testtask' + +test_files_pattern = 'test/*_test.rb' +Rake::TestTask.new do |t| + t.libs << 'lib' + t.pattern = test_files_pattern + t.verbose = false +end + +desc "Run the test suite" +task :default => :test diff --git a/vendor/plugins/validates_email_format_of/init.rb b/vendor/plugins/validates_email_format_of/init.rb new file mode 100644 index 000000000..41a097dc9 --- /dev/null +++ b/vendor/plugins/validates_email_format_of/init.rb @@ -0,0 +1 @@ +require File.join(File.dirname(__FILE__), 'rails', 'init') \ No newline at end of file diff --git a/vendor/plugins/validates_email_format_of/lib/validates_email_format_of.rb b/vendor/plugins/validates_email_format_of/lib/validates_email_format_of.rb new file mode 100644 index 000000000..48017e37b --- /dev/null +++ b/vendor/plugins/validates_email_format_of/lib/validates_email_format_of.rb @@ -0,0 +1,56 @@ +# encoding: utf-8 + +module ValidatesEmailFormatOf + LocalPartSpecialChars = Regexp.escape('!#$%&\'*-/=?+-^_`{|}~') + LocalPartUnquoted = '(([[:alnum:]' + LocalPartSpecialChars + ']+[\.\+]+))*[[:alnum:]' + LocalPartSpecialChars + '+]+' + LocalPartQuoted = '\"(([[:alnum:]' + LocalPartSpecialChars + '\.\+]*|(\\\\[\u0001-\uFFFF]))*)\"' + Regex = Regexp.new('^((' + LocalPartUnquoted + ')|(' + LocalPartQuoted + ')+)@(((\w+\-+)|(\w+\.))*\w{1,63}\.[a-z]{2,6}$)', Regexp::EXTENDED | Regexp::IGNORECASE) +end + +module ActiveRecord + module Validations + module ClassMethods + # Validates whether the value of the specified attribute is a valid email address + # + # class User < ActiveRecord::Base + # validates_email_format_of :email, :on => :create + # end + # + # Configuration options: + # * message - A custom error message (default is: " does not appear to be a valid e-mail address") + # * on - Specifies when this validation is active (default is :save, other options :create, :update) + # * allow_nil - Allow nil values (default is false) + # * allow_blank - Allow blank values (default is false) + # * if - Specifies a method, proc or string to call to determine if the validation should + # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The + # method, proc or string should return or evaluate to a true or false value. + # * unless - See :if + def validates_email_format_of(*attr_names) + options = { :message => ' does not appear to be a valid e-mail address', + :on => :save, + :allow_nil => false, + :allow_blank => false, + :with => ValidatesEmailFormatOf::Regex } + + options.update(attr_names.pop) if attr_names.last.is_a?(Hash) + + validates_each(attr_names, options) do |record, attr_name, value| + v = value.to_s + + # local part max is 64 chars, domain part max is 255 chars + # TODO: should this decode escaped entities before counting? + begin + domain, local = v.reverse.split('@', 2) + rescue + record.errors.add(attr_name, options[:message]) + next + end + + unless v =~ options[:with] and not v =~ /\.\./ and domain.length <= 255 and local.length <= 64 + record.errors.add(attr_name, options[:message]) + end + end + end + end + end +end diff --git a/vendor/plugins/validates_email_format_of/rails/init.rb b/vendor/plugins/validates_email_format_of/rails/init.rb new file mode 100644 index 000000000..a30177232 --- /dev/null +++ b/vendor/plugins/validates_email_format_of/rails/init.rb @@ -0,0 +1 @@ +require 'validates_email_format_of' diff --git a/vendor/plugins/validates_email_format_of/rakefile b/vendor/plugins/validates_email_format_of/rakefile new file mode 100644 index 000000000..0b617435e --- /dev/null +++ b/vendor/plugins/validates_email_format_of/rakefile @@ -0,0 +1,12 @@ +require 'rake' +require 'rake/testtask' + +test_files_pattern = 'test/*_test.rb' +Rake::TestTask.new do |t| + t.libs << 'lib' + t.pattern = test_files_pattern + t.verbose = false +end + +desc "Run the test suite" +task :default => :test diff --git a/vendor/plugins/validates_email_format_of/shoulda_macros/validates_email_format_of.rb b/vendor/plugins/validates_email_format_of/shoulda_macros/validates_email_format_of.rb new file mode 100644 index 000000000..fb7780347 --- /dev/null +++ b/vendor/plugins/validates_email_format_of/shoulda_macros/validates_email_format_of.rb @@ -0,0 +1,97 @@ +module ValidatesEmailFormatOf + module Shoulda + def should_validate_email_format_of(field) + metaclass = (class << self; self; end) + metaclass.send(:define_method,:should_allow_values) do |klass,*values| + should_allow_values_for(field, *values) + end + metaclass.send(:define_method,:should_not_allow_values) do |klass, *values| + should_not_allow_values_for(field, values, :message => /valid e-mail/) + end + should_validate_email_format_of_klass(model_class, field) + end + + def should_validate_email_format_of_klass(klass, field) + context 'Typical valid email' do + should_allow_values(klass, + 'valid@example.com', + 'Valid@test.example.com', + 'valid+valid123@test.example.com', + 'valid_valid123@test.example.com', + 'valid-valid+123@test.example.co.uk', + 'valid-valid+1.23@test.example.com.au', + 'valid@example.co.uk', + 'v@example.com', + 'valid@example.ca', + 'valid_@example.com', + 'valid123.456@example.org', + 'valid123.456@example.travel', + 'valid123.456@example.museum', + 'valid@example.mobi', + 'valid@example.info', + 'valid-@example.com') + end + + context 'valid email from RFC 3696, page 6' do + should_allow_values(klass, + 'customer/department=shipping@example.com', + '$A12345@example.com', + '!def!xyz%abc@example.com', + '_somename@example.com') + end + + context 'valid email with apostrophe' do + should_allow_values(klass, "test'test@example.com") + end + + context 'valid email from http://www.rfc-editor.org/errata_search.php?rfc=3696' do + should_allow_values(klass, + '"Abc\@def"@example.com', + '"Fred\ Bloggs"@example.com', + '"Joe.\\Blow"@example.com') + end + + context 'Typical invalid email' do + should_not_allow_values(klass, + 'invalid@example-com', + 'invalid@example.com.', + 'invalid@example.com_', + 'invalid@example.com-', + 'invalid-example.com', + 'invalid@example.b#r.com', + 'invalid@example.c', + 'invali d@example.com', + 'invalidexample.com', + 'invalid@example.') + end + + context 'invalid email with period starting local part' do + should_not_allow_values(klass,'.invalid@example.com') + end + + context 'invalid email with period ending local part' do + should_not_allow_values(klass, 'invalid.@example.com') + end + + context 'invalid email with consecutive periods' do + should_not_allow_values(klass, 'invali..d@example.com') + end + + # corrected in http://www.rfc-editor.org/errata_search.php?rfc=3696 + context 'invalid email from http://tools.ietf.org/html/rfc3696, page 5' do + should_not_allow_values(klass, + 'Fred\ Bloggs_@example.com', + 'Abc\@def+@example.com', + 'Joe.\\Blow@example.com') + end + + context 'invalid email exceeding length limits' do + should_not_allow_values(klass, + "#{'a' * 65}@example.com", + "test@#{'a'*252}.com") + end + end + end +end + +Test::Unit::TestCase.extend(ValidatesEmailFormatOf::Shoulda) diff --git a/vendor/plugins/validates_email_format_of/test/db/email_format_test.sqlite3 b/vendor/plugins/validates_email_format_of/test/db/email_format_test.sqlite3 new file mode 100644 index 0000000000000000000000000000000000000000..0b2dc8c886f21d04a06650c80c2f3db1392f532a GIT binary patch literal 5120 zcmeI0&u`N(6vu79c4dRXijXqkV&y`kO>NgQ6$v4=uz)JEbStT}o~kT0YqTbzNjgp9 z)GHF=kKn`~!I2C90T)hO;WmjQP6|@HD&fY&^WE%|w%RaMYBU~eDm@ola7~A8a#EROAty-^p4jo2eEI?n})=3$|Of}(xVcdlx(-pPRFjzSJexw0mpleedL>=?YY5%1AeF3(VP1n zm1nl29#A<6muX9<^1>j|x=#4Q`3$l$!a1qL6Uk3S{%ZzgUZ~W;=T-P4k`nDr3_lPr z!h`}sflIBx8YW7mTz*ilRkkYC`?Ydyg|8RGcf^Y@p@2}}GAnQs6giYFWbTtLN7NJ)Y(UlZ*IYO@_qPY}wDMGV} oXeI@P2qlRqPDZEi|7Qr!F5=RPIfVj3fq$;RG6o69zg6 'sqlite3', + :database => ':memory:') + +ActiveRecord::Schema.define(:version => 0) do + create_table :users, :force => true do |t| + t.column 'email', :string + end +end + +class Person < ActiveRecord::Base + validates_email_format_of :email, :on => :create, :message => 'fails with custom message', :allow_nil => true +end + +require 'test/unit' +require 'shoulda' +require "#{File.dirname(__FILE__)}/../init" + +class Test::Unit::TestCase #:nodoc: + def self.should_allow_values(klass,*good_values) + good_values.each do |v| + should "allow email to be set to #{v.inspect}" do + user = klass.new(:email => v) + user.save + assert_nil user.errors.on(:email) + end + end + end + + def self.should_not_allow_values(klass,*bad_values) + bad_values.each do |v| + should "not allow email to be set to #{v.inspect}" do + user = klass.new(:email => v) + assert !user.save, "Saved user with email set to \"#{v}\"" + assert user.errors.on(:email), "There are no errors set on email after being set to \"#{v}\"" + end + end + end +end diff --git a/vendor/plugins/validates_email_format_of/test/validates_email_format_of_test.rb b/vendor/plugins/validates_email_format_of/test/validates_email_format_of_test.rb new file mode 100644 index 000000000..4b048c1b2 --- /dev/null +++ b/vendor/plugins/validates_email_format_of/test/validates_email_format_of_test.rb @@ -0,0 +1,37 @@ +require File.dirname(__FILE__) + '/test_helper' +require File.dirname(__FILE__) + '/../shoulda_macros/validates_email_format_of' + +class User < ActiveRecord::Base + validates_email_format_of :email, + :on => :create, + :message => 'fails with custom message', + :allow_nil => true +end + +class ValidatesEmailFormatOfTest < Test::Unit::TestCase + should_validate_email_format_of_klass(User, :email) + + context 'An invalid user on update' do + setup do + @user = User.new(:email => 'dcroak@thoughtbot.com') + assert @user.save + assert @user.update_attribute(:email, '..dcroak@thoughtbot.com') + end + + should 'pass validation' do + assert @user.valid? + assert @user.save + assert_nil @user.errors.on(:email) + end + end + + context 'A user with a nil email' do + setup { @user = User.new(:email => nil) } + + should 'pass validation' do + assert @user.valid? + assert @user.save + assert_nil @user.errors.on(:email) + end + end +end diff --git a/vendor/plugins/validates_email_format_of/validates_email_format_of.gemspec b/vendor/plugins/validates_email_format_of/validates_email_format_of.gemspec new file mode 100644 index 000000000..85ffdbb09 --- /dev/null +++ b/vendor/plugins/validates_email_format_of/validates_email_format_of.gemspec @@ -0,0 +1,32 @@ +Gem::Specification.new do |s| + s.name = "validates_email_format_of" + s.version = "1.3.0" + s.date = "2009-06-08" + s.summary = "Validate e-mail addreses against RFC 2822 and RFC 3696." + s.email = "dcroak@thoughtbot.com" + s.description = "Validate e-mail addreses against RFC 2822 and RFC 3696." + s.authors = ["Alex Dunae", "Dan Croak", "Mike Burns"] + s.extra_rdoc_files = ["CHANGELOG", + "lib/validates_email_format_of.rb", + "README.markdown"] + s.files = ["CHANGELOG", + "init.rb", + "lib/validates_email_format_of.rb", + "MIT-LICENSE", + "rails/init.rb", + "Rakefile", + "README", + "test/database.yml", + "test/fixtures/people.yml", + "test/fixtures/person.rb", + "test/schema.rb", + "test/test_helper.rb", + "test/validates_email_format_of_test.rb", + "Rakefile", + "validates_email_format_of.gemspec"] + s.has_rdoc = true + s.homepage = %q{http://code.dunae.ca/validates_email_format_of.html} + s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Validates_email_format_of"] + s.require_paths = ["lib"] +end + -- 2.43.2