Skip to content

Instantly share code, notes, and snippets.

@floehopper
Created February 18, 2010 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floehopper/307500 to your computer and use it in GitHub Desktop.
Save floehopper/307500 to your computer and use it in GitHub Desktop.
def activate
begin
@user = User.find_using_perishable_token!(params[:activation_code], 1.week)
rescue Exception => e
flash[:notice] = 'Either you activated it already Or that was an old request'
redirect_to login_url
else
if @user.activate!
@user.deliver_activation_confirmation!
flash[:notice] = 'Your username has been entered in the form below. Simply click Login to enter your account!'
redirect_to login_url(:email => @user.email)
end
end #begin
end
# Now I could write for the else case like :-
context "when a user tries to activate in a wrong way" do
setup do
user = mock('user',:perishable_token => 'abcd')
time = mock('time')
perishable_token = mock('perishable_token')
User.stubs(:find_using_perishable_token!).with(user.perishable_token,time).raises(Exception)
get :activate,:activation_code => perishable_token
end
should_set_the_flash_to /Either you activated it already Or that was an old request/i
should_redirect_to("the login page") { login_url }
end
# But I can't write code for success case I tried like below but not correct.
# Please help to correct it :-
context "when a user is activated by clicking url" do
setup do
@user = mock('user',:perishable_token => 'abcd',:email => 'user@test.com')
time = mock('time')
perishable_token = mock('perishable_token')
User.stubs(:find_using_perishable_token!).with(@user.perishable_token,time).returns(@user)
@user.stubs(:activate!).returns(true)
@user.stubs(:deliver_activation_confirmation!).returns(true)
get :activate,:activation_code => perishable_token
end
should_set_the_flash_to /Your username has been entered in the form below/i
should_redirect_to("the login page") { login_url(:email => @user.email) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment