Guest User

Untitled

a guest
Mar 8th, 2018
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. require File.dirname(__FILE__) + '/../spec_helper'
  2.  
  3. describe User do
  4.  
  5. def user_attr
  6. {
  7. :login => "foo",
  8. :email => "foo@bar.ee",
  9. :first_name => "Foo",
  10. :last_name => "Bar",
  11. :password => "secret",
  12. :password_confirmation => "secret"
  13. }.extend(HashExtension)
  14. end
  15.  
  16. it "should be valid" do
  17. User.new(user_attr).should be_valid
  18. end
  19.  
  20. it "should be invalid without login" do
  21. user = User.new(user_attr.except(:login))
  22.  
  23. user.should_not be_valid
  24. user.errors.on(:login).should == "can't be blank"
  25.  
  26. user.login = user_attr[:login]
  27. user.should be_valid
  28. end
  29.  
  30. it "should be invalid without email" do
  31. user = User.new(user_attr.except(:email))
  32.  
  33. user.should_not be_valid
  34. user.errors.on(:email).should include("can't be blank")
  35.  
  36. user.email = user_attr[:email]
  37. user.should be_valid
  38. end
  39.  
  40. it "should be invalid without first_name" do
  41. user = User.new(user_attr.except(:first_name))
  42.  
  43. user.should_not be_valid
  44. user.errors.on(:first_name).should == "can't be blank"
  45.  
  46. user.first_name = user_attr[:first_name]
  47. user.should be_valid
  48. end
  49.  
  50. it "should be invalid without last_name" do
  51. user = User.new(user_attr.except(:last_name))
  52.  
  53. user.should_not be_valid
  54. user.errors.on(:last_name).should == "can't be blank"
  55.  
  56. user.last_name = user_attr[:last_name]
  57. user.should be_valid
  58. end
  59.  
  60. it "should be invalid with login longer than 16 chars" do
  61. user = User.new(user_attr.except(:login))
  62.  
  63. user.login = "thisisanotherwisevalidloginthatstoolong"
  64. user.should_not be_valid
  65. user.errors.on(:login).should == "is too long (maximum is 16 characters)"
  66.  
  67. user.login = user_attr[:login]
  68. user.should be_valid
  69. end
  70.  
  71. it "should be invalid with email longer than 64 chars" do
  72. user = User.new(user_attr.except(:email))
  73.  
  74. user.email = "thisyepisanotherwisevalidemail@thisisquiteabitlongerthanallowed.com"
  75. user.should_not be_valid
  76. user.errors.on(:email).should == "is too long (maximum is 64 characters)"
  77.  
  78. user.email = user_attr[:email]
  79. user.should be_valid
  80. end
  81.  
  82. it "should be invalid with first_name longer than 32 chars" do
  83. user = User.new(user_attr.except(:first_name))
  84.  
  85. user.first_name = "thisiswaytoolongnametobeallowedhere"
  86. user.should_not be_valid
  87. user.errors.on(:first_name).should == "is too long (maximum is 32 characters)"
  88.  
  89. user.first_name = user_attr[:first_name]
  90. user.should be_valid
  91. end
  92.  
  93. it "should be invalid with last name longer than 32 chars" do
  94. user = User.new(user_attr.except(:last_name))
  95.  
  96. user.last_name = "thisiswaytoolongnametobeallowedhere"
  97. user.should_not be_valid
  98. user.errors.on(:last_name).should == "is too long (maximum is 32 characters)"
  99.  
  100. user.last_name = user_attr[:last_name]
  101. user.should be_valid
  102. end
  103.  
  104. it "should only allow valid email address" do
  105. user = User.new(user_attr.except(:email))
  106.  
  107. user.email = "foo"
  108. user.should_not be_valid
  109. user.errors.on(:email).should == "is invalid"
  110.  
  111. user.email = "foo@bar"
  112. user.should_not be_valid
  113. user.errors.on(:email).should == "is invalid"
  114.  
  115. user.email = user_attr[:email]
  116. user.should be_valid
  117. end
  118.  
  119. it "should create password_hash (32) and password_salt (8) when setting password" do
  120. user = User.new(user_attr.except(:password, :password_confirmation))
  121.  
  122. user.password_salt.should == ""
  123. user.password_hash.should == ""
  124.  
  125. user.password = "secret"
  126.  
  127. user.password_salt.length.should == 8
  128. user.password_hash.length.should == 32
  129. end
  130.  
  131. it "should be invalid if password and password_confirm don't match" do
  132. user = User.new(user_attr.except(:password, :password_confirmation))
  133.  
  134. user.password = "foo"
  135. user.password_confirmation = "bar"
  136. user.should_not be_valid
  137. user.errors.on(:password).should == "doesn't match confirmation"
  138.  
  139. user.password = user_attr[:password]
  140. user.password_confirmation = user_attr[:password_confirmation]
  141. user.should be_valid
  142. end
  143.  
  144. it "should be invalid if password and password_confirmation is empty on create" do
  145. user = User.new(user_attr.except(:password, :password_confirmation))
  146.  
  147. user.should_not be_valid
  148. user.errors.on(:password).should == "can't be blank"
  149.  
  150. user.password = user_attr[:password]
  151. user.password_confirmation = user_attr[:password_confirmation]
  152. user.should be_valid
  153. end
  154.  
  155. it "should not allow duplicate email" do
  156. user1 = User.new(user_attr.except(:login))
  157. user1.login = "foo1"
  158. user1.save
  159.  
  160. user2 = User.new(user_attr.except(:login))
  161. user2.login = "foo2"
  162.  
  163. user2.should_not be_valid
  164. user2.errors.on(:email).should == "has already been taken"
  165.  
  166. user2.email = "foofoo@bar.ee"
  167. user2.should be_valid
  168.  
  169. user1.destroy
  170. end
  171.  
  172. it "should not allow duplicate login" do
  173. user1 = User.new(user_attr.except(:email))
  174. user1.email = "foo1@bar.ee"
  175. user1.save
  176.  
  177. user2 = User.new(user_attr.except(:email))
  178. user2.email = "foo2@bar.ee"
  179.  
  180. user2.should_not be_valid
  181. user2.errors.on(:login).should == "has already been taken"
  182.  
  183. user2.login = "foo2"
  184. user2.should be_valid
  185.  
  186. user2.destroy
  187. end
  188.  
  189. end
Add Comment
Please, Sign In to add comment