Guest User

Untitled

a guest
Nov 24th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. # spec/models/user/authentication.rb
  2.  
  3. require 'spec_helper'
  4.  
  5. describe User::Authentication do
  6. subject { User.new }
  7.  
  8. describe ".authenticate" do
  9. it "tries to find the user" do
  10. User.should_receive(:find_by_username_or_email)
  11. User.authenticate('blah@blah.com', 'blahblah')
  12. end
  13.  
  14. it "checks if the password is correct" do
  15. user = double(:user)
  16. User.stub(:find_by_username_or_email).and_return user
  17. user.should_receive(:correct_password?)
  18. User.authenticate('blah@blah.com', 'blahblah')
  19. end
  20.  
  21. it "returns the found user if authenticated" do
  22. user = double(:user)
  23. User.stub(:find_by_username_or_email).and_return user
  24. user.stub(:correct_password?).and_return true
  25. User.authenticate('blah@blah.com', 'blahblah')
  26. end
  27.  
  28. it "returns nil if the user doesn't exists" do
  29. User.stub(:find_by_username_or_email).and_return nil
  30. User.authenticate('blah@blah.com', 'blahblah')
  31. end
  32.  
  33. it "returns nil if the password is incorrect" do
  34. user = double(:user)
  35. User.stub(:find_by_username_or_email).and_return user
  36. user.stub(:correct_password?).and_return false
  37. User.authenticate('blah@blah.com', 'blahblah')
  38. end
  39. end
  40.  
  41. describe ".find_by_username_or_email" do
  42. before :all do
  43. @user = FactoryGirl.create :user, email: 'kramer@vulume.com', username: 'cosmo'
  44. end
  45.  
  46. it "finds a user by username case insensitively" do
  47. User.find_by_username_or_email('cosmo').should be == @user
  48. User.find_by_username_or_email('COSMO').should be == @user
  49. end
  50.  
  51. it "finds a user by email case insensitively" do
  52. User.find_by_username_or_email('kramer@vulume.com').should be == @user
  53. User.find_by_username_or_email('KRAMER@VULUME.com').should be == @user
  54. end
  55.  
  56. it "is nil if nothing is found" do
  57. User.find_by_username_or_email('blahblahblah').should be_nil
  58. end
  59.  
  60. it "does not query the database if a blank value is passed in" do
  61. method = :find_by_username_or_email
  62. User.should_not query_the_database.when_calling(method).with('')
  63. User.should_not query_the_database.when_calling(method).with(nil)
  64. end
  65. end
  66.  
  67. describe "#correct_password" do
  68. it "is true if the passed in password matches the password digest" do
  69. subject.password = 'blah'
  70. subject.correct_password?('blah').should be_true
  71. end
  72.  
  73. it "false if the password does not match" do
  74. subject.password = 'omfg'
  75. subject.correct_password?('notthesame').should be_false
  76. end
  77. end
  78.  
  79. describe "#password=" do
  80. it "sets the password attribute" do
  81. subject.password = 'blah'
  82. subject.password.should be == 'blah'
  83. end
  84.  
  85. it "sets the password digest" do
  86. subject.should_receive(:password_digest=)
  87. subject.password = 'blah'
  88. end
  89.  
  90. it "does not set the password digest if the unencrypted password is blank" do
  91. subject.should_not_receive(:password_digest=)
  92. subject.password = ''
  93. subject.password = nil
  94. end
  95. end
  96. end
Add Comment
Please, Sign In to add comment