Guest User

Untitled

a guest
May 3rd, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. ##the error message:
  2. Spec::Mocks::MockExpectationError in 'UsersController responding to POST create with valid params should redirect to the created user'
  3. Mock 'User_1038' received unexpected message :screen_name with (no args)
  4. C:/projects/Learning/app/controllers/users_controller.rb:47:in `create'
  5. C:/projects/Learning/app/controllers/users_controller.rb:45:in `create'
  6. ./spec/controllers/users_controller_spec.rb:85:
  7.  
  8. ## the spec:
  9. describe UsersController do
  10.  
  11. def mock_user(stubs={})
  12. @mock_user ||= mock_model(User, stubs)
  13. endwhat
  14. ...
  15. describe "responding to POST create" do
  16.  
  17. describe "with valid params" do
  18.  
  19. it "should expose a newly created user as @user" do
  20. User.should_receive(:new).with({'these' => 'params'}).and_return(mock_user(:save => true))
  21. post :create, :user => {:these => 'params'}
  22. assigns(:user).should equal(mock_user)
  23. end
  24.  
  25. it "should redirect to the created user" do
  26. User.stub!(:new).and_return(mock_user(:save => true))
  27. post :create, :user => {}
  28. response.should redirect_to(user_url(mock_user))
  29. end
  30.  
  31. end
  32.  
  33. ##user model:
  34. require 'digest/sha1'
  35. class User < ActiveRecord::Base
  36. SCREEN_NAME_MIN_LENGTH =4
  37. SCREEN_NAME_MAX_LENGTH = 20
  38. EMAIL_MIN_LENGTH = 6
  39. EMAIL_MAX_LENGTH = 50
  40. EMAIL_SIZE = 30
  41. SCREEN_NAME_SIZE = 20
  42. PASSWORD_MIN_LENGTH =5
  43. PASSWORD_MAX_LENGTH = 50
  44. PASSWORD_SIZE = 20
  45. validates_uniqueness_of :screen_name
  46. validates_length_of :screen_name, :within =>SCREEN_NAME_MIN_LENGTH..SCREEN_NAME_MAX_LENGTH
  47. validates_length_of :email, :within => EMAIL_MIN_LENGTH..EMAIL_MAX_LENGTH
  48. validates_presence_of :email,:screen_name
  49. validates_format_of :screen_name, :with =>/^[A-Z0-9_]*$/i, :message =>"must contain only letters, numbers, and underscores"
  50. validates_format_of :email, :with =>/^[A-Z0-9._%-+]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i, :message => "must be a valid e-mail address"
  51. validates_confirmation_of :password
  52. attr_accessor :password_confirmation
  53.  
  54.  
  55. def validate
  56. if self.hashed_password.nil?
  57. errors.add_to_base("The password needs to be between #{PASSWORD_MIN_LENGTH} and #{PASSWORD_MAX_LENGTH} characters.")
  58. end
  59. end
  60.  
  61. #this returns the virtual attribute password
  62. def password
  63. @password
  64. end
  65.  
  66. #This ensures that the password is long enough and assigns it or it returns nil
  67. def password=(pass)
  68. if (PASSWORD_MIN_LENGTH..PASSWORD_MAX_LENGTH) === pass.length #I bypassed this to make sure that it is working
  69. @password = pass
  70. self.salt = create_salt
  71. self.hashed_password = encrypt_password( pass, self.salt)
  72. else
  73. return nil
  74. end
  75. self.hashed_password
  76. end
  77.  
  78. #this returns the user if the password is correct or it returns nil
  79. def self.authenticate( login, password)
  80. bob = find_by_screen_name( login)
  81. bob && bob.hashed_password == encrypt_password( password, bob.salt)? bob: nil
  82. end
  83.  
  84. protected
  85.  
  86. #This function takes a password, salts it, stretches it, and returns it.
  87. #This depends on SITE_KEY and STRETCH_ITERATIONS being set.
  88. #I don't know if this is implemented securely.
  89. def encrypt_password(password, salt)
  90. encrypted_password = SITE_KEY
  91. STRETCH_ITERATIONS.times do
  92. encrypted_password = Digest::SHA1.hexdigest(password + '::' + encrypted_password + '::' + salt + '::' + SITE_KEY)
  93. end
  94. encrypted_password
  95. end
  96.  
  97. #this returns a new salt
  98. def create_salt
  99. self.object_id.to_s + rand.to_s
  100. end
  101. end
Add Comment
Please, Sign In to add comment