Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##the error message:
- Spec::Mocks::MockExpectationError in 'UsersController responding to POST create with valid params should redirect to the created user'
- Mock 'User_1038' received unexpected message :screen_name with (no args)
- C:/projects/Learning/app/controllers/users_controller.rb:47:in `create'
- C:/projects/Learning/app/controllers/users_controller.rb:45:in `create'
- ./spec/controllers/users_controller_spec.rb:85:
- ## the spec:
- describe UsersController do
- def mock_user(stubs={})
- @mock_user ||= mock_model(User, stubs)
- endwhat
- ...
- describe "responding to POST create" do
- describe "with valid params" do
- it "should expose a newly created user as @user" do
- User.should_receive(:new).with({'these' => 'params'}).and_return(mock_user(:save => true))
- post :create, :user => {:these => 'params'}
- assigns(:user).should equal(mock_user)
- end
- it "should redirect to the created user" do
- User.stub!(:new).and_return(mock_user(:save => true))
- post :create, :user => {}
- response.should redirect_to(user_url(mock_user))
- end
- end
- ##user model:
- require 'digest/sha1'
- class User < ActiveRecord::Base
- SCREEN_NAME_MIN_LENGTH =4
- SCREEN_NAME_MAX_LENGTH = 20
- EMAIL_MIN_LENGTH = 6
- EMAIL_MAX_LENGTH = 50
- EMAIL_SIZE = 30
- SCREEN_NAME_SIZE = 20
- PASSWORD_MIN_LENGTH =5
- PASSWORD_MAX_LENGTH = 50
- PASSWORD_SIZE = 20
- validates_uniqueness_of :screen_name
- validates_length_of :screen_name, :within =>SCREEN_NAME_MIN_LENGTH..SCREEN_NAME_MAX_LENGTH
- validates_length_of :email, :within => EMAIL_MIN_LENGTH..EMAIL_MAX_LENGTH
- validates_presence_of :email,:screen_name
- validates_format_of :screen_name, :with =>/^[A-Z0-9_]*$/i, :message =>"must contain only letters, numbers, and underscores"
- validates_format_of :email, :with =>/^[A-Z0-9._%-+]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i, :message => "must be a valid e-mail address"
- validates_confirmation_of :password
- attr_accessor :password_confirmation
- def validate
- if self.hashed_password.nil?
- errors.add_to_base("The password needs to be between #{PASSWORD_MIN_LENGTH} and #{PASSWORD_MAX_LENGTH} characters.")
- end
- end
- #this returns the virtual attribute password
- def password
- @password
- end
- #This ensures that the password is long enough and assigns it or it returns nil
- def password=(pass)
- if (PASSWORD_MIN_LENGTH..PASSWORD_MAX_LENGTH) === pass.length #I bypassed this to make sure that it is working
- @password = pass
- self.salt = create_salt
- self.hashed_password = encrypt_password( pass, self.salt)
- else
- return nil
- end
- self.hashed_password
- end
- #this returns the user if the password is correct or it returns nil
- def self.authenticate( login, password)
- bob = find_by_screen_name( login)
- bob && bob.hashed_password == encrypt_password( password, bob.salt)? bob: nil
- end
- protected
- #This function takes a password, salts it, stretches it, and returns it.
- #This depends on SITE_KEY and STRETCH_ITERATIONS being set.
- #I don't know if this is implemented securely.
- def encrypt_password(password, salt)
- encrypted_password = SITE_KEY
- STRETCH_ITERATIONS.times do
- encrypted_password = Digest::SHA1.hexdigest(password + '::' + encrypted_password + '::' + salt + '::' + SITE_KEY)
- end
- encrypted_password
- end
- #this returns a new salt
- def create_salt
- self.object_id.to_s + rand.to_s
- end
- end
Add Comment
Please, Sign In to add comment