Skip to content

Instantly share code, notes, and snippets.

@ratnikov
Created January 7, 2011 17:06
Show Gist options
  • Select an option

  • Save ratnikov/769745 to your computer and use it in GitHub Desktop.

Select an option

Save ratnikov/769745 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
EMAIL_FORMAT = /\A[a-z0-9][a-z0-9._-]*\z/i.freeze
validates :username, :presence => true, :uniqueness => true, :length => {:minimum => 4, :maximum => 16 }, :format => { :with => EMAIL_FORMAT }
validates :phone_number, :length => {:minimum => 6, :maximum => 25}, :format => { :with => /\A\S[0-9\+\/\(\)\s\-]*\z/i }, :allow_blank => true
#----------------#
it "should be invalid with an invalid email address" do
invalid_email_addresses.each |invalid_email|
invalid_email.should_not =~ User::EMAIL_REGEX
end
end
it "should be valid with an valid email address" do
valid_email_addresses.each do |valid|
Factory.build(:user, :email => valid).should be_valid
end
end
it "should be invalid with an invalid username" do
invalid_usernames.each do |invalid|
Factory.build(:user, :username => invalid).should_not be_valid
end
end
it "should be valid with an valid username" do
valid_usernames.each do |valid|
Factory.build(:user, :username => valid).should be_valid
end
end
it "should be invalid with an invalid password" do
invalid_passwords.each do |invalid|
Factory.build(:user, :password => invalid).should_not be_valid
end
end
it "should be valid with an valid password" do
valid_passwords.each do |valid|
Factory.build(:user, :password => valid).should be_valid
end
end
it "should be invalid with an invalid phonenumber" do
invalid_phonenumbers.each do |invalid|
Factory.build(:user, :phone_number => invalid).should_not be_valid
end
end
it "should be valid with an valid phonenumber" do
valid_phonenumbers.each do |valid|
Factory.build(:user, :phone_number => valid).should be_valid
end
def invalid_email_addresses
[" ","missingDomain@.com","@missingLocal.org","missingatSign.net","missingDot@com","two@@signs.com","colonButNoPort@127.0.0.1:","someone-else@127.0.0.1.26",".localStartsWithDot@domain.com","localEndsWithDot.@domain.com","two..consecutiveDots@domain.com","domainStartsWithDash@-domain.com","domainEndsWithDash@domain-.com","numbersInTLD@domain.c0m","missingTLD@domain.","!#$\%(),/;<>[]`|@invalidCharsInLocal.org","invalidCharsInDomain@!#$\%(),/;<>_[]`|.org","local@SecondLevelDomainNamesAreInvalidIfTheyAreLongerThan64Charactersss.org"]
end
def valid_email_addresses
["l3tt3rsAndNumb3rs@domain.com","has-dash@domain.com","hasApostrophe.o'leary@domain.org","uncommonTLD@domain.museum","uncommonTLD@domain.travel","uncommonTLD@domain.mobi","countryCodeTLD@domain.uk","countryCodeTLD@domain.rw","lettersInDomain@911.com","underscore_inLocal@domain.net","IPInsteadOfDomain@127.0.0.1","IPAndPort@127.0.0.1:25","subdomain@sub.domain.com","local@dash-inDomain.com","dot.inLocal@foo.com","a@singleLetterLocal.org","singleLetterDomain@x.org","&*=?^+{}'~@validCharsInLocal.net","foor@bar.newTLD"]
end
def invalid_usernames
[" ","a&&lkdf","_-_.l##df","a@adkfd","**www.foer.de*"," mark.pochert "," hanswurdst","wursthans ","kno","Euro€Man"]
end
def valid_usernames
["greta_marlene","chic5.0-m33","lakeball007","www.founder.de","4038christian","knor"]
end
def invalid_passwords
["aa","aaa","aaaa","aaaaa","aaaaa"," "]
end
def valid_passwords
["fasf§434","##\'\'+dssde€$\%@","huHrdeschie0e","www.founder.de34","tia4038christian","\%ZhP<'\%5{b$y:w'5i79%!7O","x[[b{e7.7M7'19+5@€~zB6Q*F1)&O","vg[{pAK]2s\"v7L{l;'{971Aao~Fp}"]
end
def invalid_phonenumbers
["Hans Wurst","+49 221 Hans","Gebe ich nicht ein","110"] ## Die Nummer " ","" geht noch nicht
end
def valid_phonenumbers
["+492203969534","0221/549534","0800-2222 800","+49-0221-2222-390","+49 (221) / 549534 - 23","+49 (0) 221 / 549534 - 23","0221269534"]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment