Guest User

Untitled

a guest
Mar 11th, 2018
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. require File.join( File.dirname(__FILE__), "..", "spec_helper" )
  2.  
  3. describe User do
  4.  
  5. before(:all) do
  6. load_fixtures(:users)
  7. end
  8.  
  9. after(:all) do
  10. User.delete_all
  11. end
  12.  
  13. it "should require an email address" do
  14. user = User.new(valid_attrs.except(:email))
  15. user.should_not be_valid
  16.  
  17. user.errors.full_messages.first.should eql("Email must not be blank")
  18. end
  19.  
  20. it "should require a valid email address" do
  21. user = User.new(valid_attrs.with(:email => 'cameroncox@gmail'))
  22. user.should_not be_valid
  23.  
  24. user.errors.full_messages.first.should eql("Email is invalid")
  25. end
  26.  
  27. it "should ensure email addresses are unique" do
  28. user = User.new(valid_attrs.with(:email => 'john-doe@gmail.com'))
  29. user.should_not be_valid
  30.  
  31. user.errors.full_messages.first.should eql("Email has already been taken")
  32. end
  33.  
  34. it "should ensure usernames are unique" do
  35. user = User.new(valid_attrs.with(:username => 'john-doe'))
  36. user.should_not be_valid
  37.  
  38. user.errors.full_messages.first.should eql("Username has already been taken")
  39. end
  40.  
  41. it "should require an username" do
  42. user = User.new(valid_attrs.except(:username))
  43. user.should_not be_valid
  44.  
  45. user.errors.full_messages.first.should eql("Username must not be blank")
  46. end
  47.  
  48. it "should ensure that usernames are between 4 and 25 characters long" do
  49. user = User.new(valid_attrs.with(:username => 'bob'))
  50. user.should_not be_valid
  51.  
  52. user.errors.full_messages.first.should eql("Username must be between 4 and 25 characters long")
  53.  
  54. user.username = 'supercalifragilicousexpialidocious' # I -totally- spelled that wrong :D
  55. user.should_not be_valid
  56.  
  57. user.errors.full_messages.first.should eql("Username must be between 4 and 25 characters long")
  58. end
  59.  
  60. it "should require password attribute when crypted password is blank" do
  61. user = User.new(valid_attrs.except(:password))
  62. user.should_not be_valid
  63.  
  64. user.errors.full_messages.first.should eql("Password must not be blank")
  65. end
  66.  
  67. it "should not require password attribute when crypted password is not blank" do
  68. user = User.new(valid_attrs.except(:password).with(:crypted_password => "testing"))
  69. user.should be_valid
  70. end
  71.  
  72. it "should require password to be more than 5 characters long" do
  73. user = User.new(valid_attrs.with(:password => 'short', :password_confirmation => 'short'))
  74. user.should_not be_valid
  75.  
  76. user.errors.full_messages.first.should eql("Password must be more than 5 characters long")
  77. end
  78.  
  79. it "should should require confirmation of password" do
  80. user = User.new(valid_attrs.with(:password_confirmation => '@gonow'))
  81. user.should_not be_valid
  82.  
  83. user.errors.full_messages.first.should eql("Password does not match the confirmation")
  84. end
  85.  
  86. it "should set salt before saving" do
  87. user = User.new(valid_attrs)
  88. user.should be_valid
  89.  
  90. user.salt.blank?.should eql(nil)
  91. end
  92.  
  93. it "should encrypt password before saving" do
  94. user = User.new(valid_attrs)
  95. user.should be_valid
  96.  
  97. user.crypted_password.blank?.should eql(nil)
  98. end
  99.  
  100. it "should ensure a username does not contain a restricted keyword that is a String" do
  101. user = User.new(valid_attrs.with(:username => 'root'))
  102. user.should_not be_valid
  103.  
  104. user.errors.full_messages.first.should eql("Username cannot contain the phrase 'root'")
  105. end
  106.  
  107. it "should ensure a username does not contain a restricted keyword that is a Regexp" do
  108. user = User.new(valid_attrs.with(:username => 'wolfadmin'))
  109. user.should_not be_valid
  110.  
  111. user.errors.full_messages.first.should eql("Username cannot contain the phrase 'admin'")
  112. end
  113.  
  114. it "should have an activation code before create" do
  115. user = User.new(valid_attrs)
  116. user.should be_valid
  117. user.save
  118. user.activation_code.blank?.should eql(nil)
  119.  
  120. end
  121.  
  122. it "should have an activation code if inactive" do
  123. user = User.first :conditions => ["username = ?", 'cameroncox']
  124. user.deactivate
  125.  
  126. user.active?.should eql(nil)
  127. user.activation_code.blank?.should_not eql(true)
  128. end
  129.  
  130. it "should not have an activation code if active" do
  131. user = User.first :conditions => ["username = ?", 'cameroncox']
  132. user.activate
  133.  
  134. user.active?.should_not eql(nil)
  135. user.activation_code.blank?.should eql(true)
  136. end
  137.  
  138. protected
  139. def valid_attrs
  140. { :username => 'cameroncox', :password => '!@gonow', :password_confirmation => '!@gonow', :email => 'cameroncox@gmail.com' }
  141. end
  142. end
Add Comment
Please, Sign In to add comment