/spec/models/user_spec.rb

https://github.com/diegopacheco/rails3-tutorial-sample-app · Ruby · 276 lines · 252 code · 24 blank · 0 comment · 2 complexity · 92b27db75dcc2b7fae0479cca1dc1aff MD5 · raw file

  1. require 'spec_helper'
  2. describe User do
  3. before(:each) do
  4. @attr = {
  5. :name => "Example User",
  6. :email => "user@example.com",
  7. :password => "foobar",
  8. :password_confirmation => "foobar"
  9. }
  10. end
  11. it "1. should create a new instance given valid attributes" do
  12. User.create!(@attr)
  13. end
  14. it "2. should require a name" do
  15. no_name_user = User.new(@attr.merge(:name => ""))
  16. no_name_user.should_not be_valid
  17. end
  18. it "3. should reject names that are too long" do
  19. long_name = "a" * 51
  20. long_name_user = User.new(@attr.merge(:name => long_name))
  21. long_name_user.should_not be_valid
  22. end
  23. it "4. should accept valid email addresses" do
  24. addresses = %w[user@foo.com THE_USER@foo.bar.org first.last@foo.jp]
  25. addresses.each do |address|
  26. valid_email_user = User.new(@attr.merge(:email => address))
  27. valid_email_user.should be_valid
  28. end
  29. end
  30. it "5. should reject invalid email addresses" do
  31. addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
  32. addresses.each do |address|
  33. valid_email_user = User.new(@attr.merge(:email => address))
  34. valid_email_user.should_not be_valid
  35. end
  36. end
  37. it "6. should reject duplicate email addresses" do
  38. User.create!(@attr)
  39. user_with_duplicate_email = User.new(@attr)
  40. user_with_duplicate_email.should_not be_valid
  41. end
  42. it "7. should reject email addresses identical up to case" do
  43. upcased_email = @attr[:email].upcase
  44. User.create!(@attr.merge(:email => upcased_email))
  45. user_with_duplicate_email = User.new(@attr)
  46. user_with_duplicate_email.should_not be_valid
  47. end
  48. it "8. should require a email" do
  49. no_name_user = User.new(@attr.merge(:email => ""))
  50. no_name_user.should_not be_valid
  51. end
  52. describe "password validations" do
  53. it "9. should require a password" do
  54. User.new(@attr.merge(:password => "", :password_confirmation => "")).
  55. should_not be_valid
  56. end
  57. it "10. should reject short passwords" do
  58. short = "a" * 5
  59. hash = @attr.merge(:password => short, :password_confirmation => short)
  60. User.new(hash).should_not be_valid
  61. end
  62. it "11. should reject long passwords" do
  63. long = "a" * 41
  64. hash = @attr.merge(:password => long, :password_confirmation => long)
  65. User.new(hash).should_not be_valid
  66. end
  67. it "12. should require a matching password confirmation" do
  68. User.new(@attr.merge(:password_confirmation => 'invalid')).
  69. should_not be_valid
  70. end
  71. end
  72. describe "password encryption" do
  73. before(:each) do
  74. @user = User.create!(@attr)
  75. end
  76. it "13. should have a encrypted password attribute" do
  77. @user.should respond_to(:encrypted_password)
  78. end
  79. it "14. should set the encrypted password" do
  80. @user.encrypted_password.should_not be_blank
  81. end
  82. describe "has_password? method" do
  83. it "15. should be true if passwords match" do
  84. @user.has_password?(@attr[:password]).should be_true
  85. end
  86. it "16. should be false if the passwords don't match" do
  87. @user.has_password?("invalid").should be_false
  88. end
  89. end
  90. describe "authenticate method" do
  91. it "17. should return nil on email/password mismatch" do
  92. wrong_password_user = User.authenticate(@attr[:email], "wrongpass")
  93. wrong_password_user.should be_nil
  94. end
  95. it "18. should return nil for an email addresses with no user_with_duplicate_email" do
  96. nonexistent_user = User.authenticate("bar@foo.com", @attr[:passwords])
  97. nonexistent_user.should be_nil
  98. end
  99. it "19. should return the user on email/password match" do
  100. matching_user = User.authenticate(@attr[:email],@attr[:password])
  101. matching_user == @user
  102. end
  103. end
  104. end
  105. describe "admin attribute" do
  106. before(:each) do
  107. @user = User.create!(@attr)
  108. end
  109. it "should respond to admin" do
  110. @user.should respond_to(:admin)
  111. end
  112. it "should not be an admin by default" do
  113. @user.should_not be_admin
  114. end
  115. it "should be convertible to an admin" do
  116. @user.toggle!(:admin)
  117. @user.should be_admin
  118. end
  119. end
  120. describe "micropost associations" do
  121. before(:each) do
  122. @user = User.create(@attr)
  123. @mp1 = Factory(:micropost, :user => @user, :created_at => 1.day.ago)
  124. @mp2 = Factory(:micropost, :user => @user, :created_at => 1.hour.ago)
  125. end
  126. it "should have a microposts attribute" do
  127. @user.should respond_to(:microposts)
  128. end
  129. it "should have the right microposts in the right order" do
  130. @user.microposts.should == [@mp2, @mp1]
  131. end
  132. it "should destroy associated microposts" do
  133. @user.destroy
  134. [@mp1,@mp2].each do |micropost|
  135. Micropost.find_by_id(micropost.id).should be_nil
  136. end
  137. end
  138. describe "status feed" do
  139. it "should have a feed" do
  140. @user.should respond_to(:feed)
  141. end
  142. it "should include the user's microposts" do
  143. @user.feed.include?(@mp1).should be_true
  144. @user.feed.include?(@mp2).should be_true
  145. end
  146. it "should not include a different user's microposts" do
  147. mp3 = Factory(:micropost,:user => Factory(:user,:email => Factory.next(:email)))
  148. @user.feed.include?(mp3).should be_false
  149. end
  150. it "should include the microposts of followed users" do
  151. followed = Factory(:user, :email => Factory.next(:email))
  152. mp3 = Factory(:micropost, :user => followed)
  153. @user.follow!(followed)
  154. @user.feed.should include(mp3)
  155. end
  156. end
  157. end
  158. describe "relationships" do
  159. before(:each) do
  160. @user = User.create!(@attr)
  161. @followed = Factory(:user)
  162. end
  163. it "should have a relationships method" do
  164. @user.should respond_to(:relationships)
  165. end
  166. it "should have a following method" do
  167. @user.should respond_to(:following)
  168. end
  169. it "should have a following? method" do
  170. @user.should respond_to(:following?)
  171. end
  172. it "should have a follow! method" do
  173. @user.should respond_to(:follow!)
  174. end
  175. it "should follow another user" do
  176. @user.follow!(@followed)
  177. @user.should be_following(@followed)
  178. end
  179. it "should include the followed user in the following array" do
  180. @user.follow!(@followed)
  181. @user.following.should include(@followed)
  182. end
  183. it "should have a nunfollow! method" do
  184. @followed.should respond_to(:unfollow!)
  185. end
  186. it "should unfollow a user" do
  187. @user.follow!(@followed)
  188. @user.unfollow!(@followed)
  189. @user.should_not be_following(@followed)
  190. end
  191. it "should have a reverse_relationships method" do
  192. @user.should respond_to(:reverse_relationships)
  193. end
  194. it "should have a followers method" do
  195. @user.should respond_to(:followers)
  196. end
  197. it "should include the follower in the followers array" do
  198. @user.follow!(@followed)
  199. @followed.followers.should include(@user)
  200. end
  201. end
  202. end
  203. # == Schema Information
  204. #
  205. # Table name: users
  206. #
  207. # id :integer not null, primary key
  208. # name :string(255)
  209. # email :string(255)
  210. # created_at :datetime
  211. # updated_at :datetime
  212. #