/spec/models/user_spec.rb

https://github.com/gkeary/sample_app · Ruby · 280 lines · 244 code · 22 blank · 14 comment · 2 complexity · 15001b2a367dc924564ff2c393586623 MD5 · raw file

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