/spec/models/user_spec.rb

https://github.com/andrewrobbins/sample_app · Ruby · 285 lines · 265 code · 20 blank · 0 comment · 3 complexity · 6472ec3cc5228c11a16ef50cdb0a87f2 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 "should create a new instance given valid attributes" do
  12. User.create!(@attr)
  13. end
  14. it "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 "should require an email" do
  19. no_email_user = User.new(@attr.merge(:email => ""))
  20. no_email_user.should_not be_valid
  21. end
  22. it "should reject names that are too long" do
  23. long_name = "a" * 51
  24. long_name_user = User.new(@attr.merge(:name => long_name))
  25. long_name_user.should_not be_valid
  26. end
  27. it "should accept valid email addresses" do
  28. addresses = %w[user@foo.com THE_USER@foo.bar.com first.last@foo.jp]
  29. addresses.each do |address|
  30. valid_email_user = User.new(@attr.merge(:email => address))
  31. valid_email_user.should be_valid
  32. end
  33. end
  34. it "should reject invalid email addresses" do
  35. addresses = %w[user@foo,com user_at_foo.org example-user@foo]
  36. addresses.each do |address|
  37. invalid_email_user = User.new(@attr.merge(:email => address))
  38. invalid_email_user.should_not be_valid
  39. end
  40. end
  41. it "should reject duplicate email addresses" do
  42. User.create!(@attr)
  43. user_with_duplicate_email = User.new(@attr)
  44. user_with_duplicate_email.should_not be_valid
  45. end
  46. it "should reject duplicate email addresses identical except for case" do
  47. upcased_email = @attr[:email].upcase
  48. User.create!(@attr.merge(:email => upcased_email))
  49. user_with_duplicate_email = User.new(@attr)
  50. user_with_duplicate_email.should_not be_valid
  51. end
  52. describe "password validation" do
  53. it "should require a password" do
  54. User.new(@attr.merge(:password => "", :password_confirmation => "")).
  55. should_not be_valid
  56. end
  57. it "should require a matching password and confirmation" do
  58. User.new(@attr.merge(:password => "foo")).should_not be_valid
  59. end
  60. it "should reject short passwords" do
  61. short = "a" * 5
  62. hash = @attr.merge(:password => short, :password_confirmation => short)
  63. User.new(hash).should_not be_valid
  64. end
  65. it "should reject long passwords" do
  66. long = "a" * 41
  67. hash = @attr.merge(:password => long, :password_confirmation => long)
  68. User.new(hash).should_not be_valid
  69. end
  70. end
  71. describe "password encryption" do
  72. before (:each) do
  73. @user = User.create!(@attr)
  74. end
  75. it "should have an encrypted password attribute" do
  76. @user.should respond_to(:encrypted_password)
  77. end
  78. it "should set the encrypted password" do
  79. @user.encrypted_password.should_not be_blank
  80. end
  81. describe "has_password? method" do
  82. it "should be true if the passwords match" do
  83. @user.has_password?(@attr[:password]).should be_true
  84. end
  85. it "should be false if the passwords don't match" do
  86. @user.has_password?("invalid").should be_false
  87. end
  88. end
  89. describe "authenticate method" do
  90. it "should return nil on email/password mismatch" do
  91. wrong_password_user = User.authenticate(@attr[:email], "wrongpass");
  92. wrong_password_user.should be_nil
  93. end
  94. it "should return nil for an email address with no user" do
  95. nonexistant_user = User.authenticate("invalid@invalid.com", @attr[:password])
  96. nonexistant_user.should be_nil
  97. end
  98. it "should return the user on email/password match" do
  99. matching_user = User.authenticate(@attr[:email], @attr[:password])
  100. matching_user.should == @user
  101. end
  102. end
  103. end
  104. describe "admin attribute" do
  105. before(:each) do
  106. @user = User.create!(@attr)
  107. end
  108. it "should response to admin" do
  109. @user.should respond_to(:admin)
  110. end
  111. it "should not be an admin by default" do
  112. @user.should_not be_admin
  113. end
  114. it "should be convertible to an admin" do
  115. @user.toggle!(:admin)
  116. @user.should be_admin
  117. end
  118. end
  119. describe "micropost associations" do
  120. before(:each) do
  121. @user = User.create(@attr)
  122. @mp1 = Factory(:micropost, :user => @user, :created_at => 1.day.ago)
  123. @mp2 = Factory(:micropost, :user => @user, :created_at => 1.hour.ago)
  124. end
  125. it "should have a microposts attribute" do
  126. @user.should respond_to(:microposts)
  127. end
  128. it "should have the right microposts in the right order" do
  129. @user.microposts.should == [@mp2, @mp1]
  130. end
  131. it "should destroy associated microposts" do
  132. @user.destroy
  133. [@mp1, @mp2].each do |micropost|
  134. Micropost.find_by_id(micropost.id).should be_nil
  135. end
  136. end
  137. describe "status feed" do
  138. it "should have a feed" do
  139. @user.should respond_to(:feed)
  140. end
  141. it "should include the user's microposts" do
  142. @user.feed.should include(@mp1)
  143. @user.feed.should include(@mp2)
  144. end
  145. it "should not include a different user's microposts" do
  146. mp3 = Factory(:micropost,
  147. :user => Factory(:user, :email => Factory.next(:email)))
  148. @user.feed.should_not include(mp3)
  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 follow! method" do
  170. @user.should respond_to(:follow!)
  171. end
  172. it "Should have a following? method" do
  173. @user.should respond_to(:following?)
  174. end
  175. it "should have a unfollow! method" do
  176. @user.should respond_to(:unfollow!)
  177. end
  178. it "should follow another user" do
  179. @user.follow!(@followed)
  180. @user.should be_following(@followed)
  181. end
  182. it "should include the followed user in the following array" do
  183. @user.follow!(@followed)
  184. @user.following.should include(@followed)
  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 followed in the followers array" do
  198. @user.follow!(@followed)
  199. @followed.followers.should include(@user)
  200. end
  201. describe "dependent destruction" do
  202. before(:each) do
  203. @followed = Factory(:user, :email => Factory.next(:email))
  204. @relationship = @user.relationships.build(:followed_id => @followed.id)
  205. @relationship.save!
  206. end
  207. it "should remove all existing following relationships when destroyed" do
  208. Relationship.where("follower_id = ?", @user.id).count.should == 1
  209. @user.destroy
  210. Relationship.where("follower_id = ?", @user.id).should be_empty
  211. end
  212. it "should remove all existing followed relationships when destroyed" do
  213. Relationship.where("followed_id = ?", @followed.id).count.should == 1
  214. @followed.destroy
  215. Relationship.where("followed_id = ?", @followed.id).should be_empty
  216. end
  217. end
  218. end
  219. end
  220. # == Schema Information
  221. #
  222. # Table name: users
  223. #
  224. # id :integer not null, primary key
  225. # name :string(255)
  226. # email :string(255)
  227. # created_at :datetime
  228. # updated_at :datetime
  229. # encrypted_password :string(255)
  230. # salt :string(255)
  231. #