Guest User

Untitled

a guest
Apr 13th, 2018
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. require File.dirname(__FILE__) + '/../spec_helper'
  2.  
  3. def valid_user_attributes
  4. {
  5. :name => "master",
  6. :email => "zookeeperson@glasshanded.net",
  7. :pass => "agarina"
  8. }
  9. end
  10.  
  11. describe User do
  12. before(:each) do
  13. @user = User.new
  14. end
  15.  
  16. it "should have many memberships and have many sites through those memberships" do
  17. User.should have_many(:memberships).with_dependent(:destroy)
  18. User.should have_many(:sites).through(:memberships)
  19. end
  20.  
  21. it "should not be valid without a unique name between 2 and 20 characters long" do
  22. @user.attributes = valid_user_attributes.except(:name)
  23. @user.should_not be_valid
  24. @user.errors_on(:name).should == ["can't be blank"]
  25. {
  26. "I" => "can't be less than 2 characters"
  27. }.each do |name, message|
  28. @user.name = name
  29. @user.should_not be_valid
  30. @user.errors_on(:name).should == [message]
  31. end
  32. @user.name = "master"
  33. @user.should be_valid
  34. end
  35.  
  36. it "should not be valid without an email which at least looks valid and is less than 100 characters" do
  37. @user.attributes = valid_user_attributes.except(:email)
  38. @user.should_not be_valid
  39. @user.errors_on(:email).should == ["can't be blank"]
  40. %w{foo foo@bar bar.com}.each do |bad_email|
  41. @user.email = bad_email
  42. @user.should_not be_valid
  43. @user.errors_on(:email).should == ["doesn't seem to be a valid email address"]
  44. end
  45. @user.email = "#{'foo' * 40}@bar.com"
  46. @user.should_not be_valid
  47. @user.errors_on(:email).should == ["can't be more than 100 characters"]
  48. %w{foo@bar.com foo@bar.baz.com foo_bar@baz.com foo.bar@baz.com foo+bar@baz.com}.each do |good_email|
  49. @user.email = good_email
  50. @user.should be_valid
  51. end
  52. end
  53.  
  54. it "should not be valid if either the name or email is already in use" do
  55. @previous_user = User.create!(valid_user_attributes)
  56. @user.attributes = valid_user_attributes
  57. @user.should_not be_valid
  58. @user.errors_on(:name).should == ["is already taken"]
  59. @user.errors_on(:email).should == ["is already registered"]
  60. end
  61.  
  62. it "should encrypt the password and save it as salty_pass" do
  63. @user.attributes = valid_user_attributes
  64. @user.pass.should == "agarina"
  65. @user.salty_pass.should be_blank
  66. @user.save!
  67. @user.salty_pass.should_not be_blank
  68. @user.salty_pass.should_not == "agarina"
  69. end
  70.  
  71. it "should create a random password if one is not provided" do
  72. @user.attributes = valid_user_attributes.except(:pass)
  73. @user.pass.should be_blank
  74. @user.salty_pass.should be_blank
  75. @user.save!
  76. @user.salty_pass.should_not be_blank
  77. end
  78.  
  79. it "should not be valid unless the new pass matches a confirmation" do
  80. @user = User.create!(valid_user_attributes)
  81. @user.pass = "updated"
  82. @user.pass_confirmation = nil
  83. @user.should_not be_valid
  84. @user.errors_on(:pass_confirmation).should == ["can't be blank"]
  85. @user.pass_confirmation = "unmatched"
  86. @user.should_not be_valid
  87. @user.errors_on(:pass).should == ["doesn't match the confirmation"]
  88. end
  89.  
  90. it "should authenticate users by name and password, returning the user on success and false on failure" do
  91. @user.attributes = valid_user_attributes.merge(:pass => "agarina")
  92. @user.save!
  93. User.authenticate("master", "agarina").should == @user
  94. User.authenticate("whoever", "whatever").should_not be_true
  95. end
  96.  
  97. it "should use text_callbacks to generate the html attributes of display_name" do
  98. @user.attributes = valid_user_attributes
  99. @user.should generate_html_attributes_via_callbacks(:display_name)
  100. end
  101. end
Add Comment
Please, Sign In to add comment