Class | SetupAndSignInTest
In: | test/integration/setup_and_sign_in_test.rb
Parent: | ActionController::IntegrationTest

Copyright (c) 2006 Logica

### Methods

test_cookies test_sign_up test_signup_central_admin

### Public Instance methods

test_cookies()

Shows

1. new sign up user5 we can logon
2. user can be remembered
3. a cookie with wrong id is deleted and the user is prompted to logon again

        Note: this can happen when a user uses multiple EPF Wiki sites

[Source]

         # File test/integration/setup_and_sign_in_test.rb, line 180
    180:    def test_cookies
    181:     ENV['EPFWIKI_GENERATE_PASSWORDS'] = '1'
    182:     @user5 = User.new(:name => 'user5', :email=>'user5@epf.eclipse.org')
    183:     # 1
    184:     assert_not_nil User.find_central_admin
    185:     post 'login/sign_up', :user => {:name => @user5.name, :email => @user5.email} # , :i_agree_to_the_terms_of_use => '1'
    186:     assert_not_nil assigns(:user)
    187:     assert_no_errors(assigns(:user))
    188:     assert_redirected_to :action => 'login'
    189:     @user5 = assigns(:user)
    190:     @user5.id = User.find_by_name(@user5.name).id     # Note: we don't reload @user5 because we loose the password
    191:     assert_equal "Password sent!", flash['success']
    192:     post 'login/login', :user => {:email => @user5.email, :password => @user5.password}
    193:     assert_not_nil session['user']
    194:     assert_nil cookies[:epfwiki_id]
    195:     session['user'] = nil
    196:     # 2
    197:     post 'login/login', :user => {:email => @user5.email, :password => @user5.password, :remember_me => "0"}
    198:     assert_not_nil assigns(:user)
    199:     assert_not_nil session['user']
    200:
    201:     logger.info("Cookies: #{cookies.inspect}")
    202:
    203:     assert_not_nil cookies['epfwiki_id']
    204:     # cookies[:epfwiki_id] doesn't work either
    205:     # Testing cookies with functional tests is hard work but here is not easy either
    206:     # We also cannot use symbols, and values are converted to strings
    207:     assert_equal cookies['epfwiki_id'],session['user'].id.to_s
    208:
    209:     session['user'] = nil
    210:     get 'login/login'
    211:     assert_not_nil assigns(:user)
    212:     assert_not_nil session['user']
    213:     # 3
    214:     session['user'] = nil
    215:     assert_not_nil cookies['epfwiki_id']
    216:     assert_equal cookies['epfwiki_id'], @user5.id.to_s
    217:     cookies[:epfwiki_id] = 123456 # cookie with a non-existing id
    218:     #create_cookie(User.new(:id => 123456, :token => 'doesn\'t matter'))
    219:     get 'login/login'
    220:     assert_response :success
    221:     assert_nil session['user']
    222:     assert cookies['epfwiki_id'].blank?
    223:     # 4
    224:     cookies[:epfwiki_id] = @user5.id
    225:     cookies[:epfwiki_token] = 'xyz'
    226:     get 'login/login'
    227:     assert_response :success
    228:     assert_nil session['user']
    229:     assert cookies['epfwiki_id'].blank?
    230:     @user5.destroy
    231:   end

test_sign_up()

if ENV[‘EPFWIKI_DOMAINS’] is set sign-up is restricted to those domains if ENV[‘EPFWIKI_GENERATE_PASSWORDS’] = ‘1’ passwords are generated

[Source]

         # File test/integration/setup_and_sign_in_test.rb, line 68
     68:   def   test_sign_up
     69:     @emails = ActionMailer::Base::deliveries
     70:     @emails.clear
     71:     ENV['EPFWIKI_GENERATE_PASSWORDS'] = '1'
     72:     user_count = User.count
     73:     # 1 . sign with domain restriction
     74:     get "login/sign_up"
     75:     assert_field("user_email")
     76:     assert_field("user_name")
     77:     assert_no_field("user_password")
     78:     assert_no_field("user_password_confirmation")
     79:     assert_tag :tag => "select", :attributes => {:name => "user[email_extension]"}
     80:     @html_document = nil # workaround for bug in assert_tag used in assert_errors
     81:     post "login/sign_up", :user => {:name => "user1", :email => "user1", :email_extension => "@somedomain.nl"} # , :i_agree_to_the_terms_of_use => '1'
     82:     assert_errors
     83: #    assert_tag :tag => "div", :attributes => { :class => "fieldWithErrors" }
     84:     assert_response :success
     85:     assert_equal user_count, User.count
     86:     user = assigns(:user)
     87:     assert_equal "Email domain not valid",user.errors.full_messages.join(', ')
     88:     # this domain is allowed, the user is created
     89:     post        "login/sign_up", :user => {:name => "user1", :email=>"user1", :email_extension => "@epf.eclipse.org"} # , :i_agree_to_the_terms_of_use => '1'
     90:     user = assigns(:user)
     91:     assert_no_errors(user)
     92:     assert_redirected_to :action => 'login'
     93:     assert_equal user_count + 1 , User.count
     94:     assert_equal 1, @emails.size
     95:     email = @emails.first
     96:     assert_equal("[#{ENV['EPFWIKI_APP_NAME']}] Welcome", email.subject)
     97:     assert_equal("user1@epf.eclipse.org", email.to[0])
     98:     assert_equal([ENV['EPFWIKI_REPLY_ADDRESS']], email.from)
     99:     assert_redirected_to :action => 'login'
    100:     assert_equal LoginController::FLASH_PW_SENT, flash['success']
    101:     # cannot sign up with already taken name, email
    102:     @html_document = nil
    103:     post        "login/sign_up", :user => {:name => "user1", :email => "user1", :email_extension => "@epf.eclipse.org"} # , :i_agree_to_the_terms_of_use => '1'
    104:     assert_equal "Name has already been taken, Email has already been taken",assigns(:user).errors.full_messages.join(', ')
    105:     # sign up without domain restriction
    106:     ENV['EPFWIKI_DOMAINS'] = nil
    107:     get "login/sign_up"
    108:     assert_field("user_email")
    109:     assert_field("user_name")
    110:     assert_no_field("user_password")
    111:     assert_no_field("user_password_confirmation")
    112:     assert_no_tag :tag => "select", :attributes => {:name => "email_extension"}
    113:     user_count = User.count
    114:     @html_document = nil
    115:     post "login/sign_up", :user => {:name => "user3", :email => "user2@xyz.com"} # , :i_agree_to_the_terms_of_use => '1'
    116:     assert_no_errors(assigns(:user))
    117:     assert_equal user_count + 1, User.count
    118:     assert_redirected_to :controller => 'login', :action => 'login'
    119:     #assert_equal "Name has already been taken, Email has already been taken",assigns(:user).errors.full_messages.join(', ')
    120:     # sign up passwords are generated
    121:     ENV['EPFWIKI_GENERATE_PASSWORDS'] = '0'
    122:     @html_document = nil
    123:     get "login/sign_up"
    124:     assert_field("user_email")
    125:     assert_field("user_name")
    126:     assert_field("user_password")
    127:     assert_field("user_password_confirmation")
    128:     assert_no_tag :tag => "select", :attributes => {:name => "email_extension"}
    129:     user_count = User.count
    130:     # user exists
    131:     @html_document = nil
    132:     post "login/sign_up", :user => {:name => "user2", :email => "user2@xyz.com"} # , :i_agree_to_the_terms_of_use => '1'
    133:     assert_equal "Name has already been taken, Password confirmation can't be blank, Password can't be blank, Email has already been taken",assigns(:user).errors.full_messages.join(', ')
    134:     assert_equal user_count, User.count
    135:     assert_errors
    136:     # creating user3
    137:     @html_document = nil
    138:     post "login/sign_up", :user => {:name => "user4", :email => "user4@xyz.com", :password => 'user4', :password_confirmation => 'user4'} # , :i_agree_to_the_terms_of_use => '1'
    139:     assert_no_errors(assigns(:user))
    140:     assert_equal LoginController::FLASH_PW_CONFIRMATION_EMAIL_SENT,  flash['success']
    141:     assert_equal "",assigns(:user).errors.full_messages.join(', ')
    142:     assert_equal user_count + 1, User.count
    143:     assert_redirected_to :action => 'login'
    144:     assert_equal Digest::SHA1.hexdigest('user4'), assigns(:user).hashed_password
    145:     get "login/login"
    146:     assert_response :success
    147:     # assert_field("user_email")# TODO Rails bug?
    148:     # assert_field("user_password")    # TODO Rails bug?
    149:     # user3 cannot sign-in, it needs to be confirmed
    150:     user3 = User.find_by_name('user4')
    151:     post "login/login" , :user => {:email => 'user4@epf.org', :password => 'user3'}
    152:     assert_equal LoginController::FLASH_INVALID_PW, flash['notice']
    153:     # cannot confirm with wrong token
    154:     # ? log reports a RunTimeError but then the assert says there is no runtime error! assert_raise(RuntimeError){ get "login/confirm_account", :id => user3.id, :tk => "anystring"}
    155:     get "login/confirm_account", :id => user3.id, :tk => "anystring"
    156:     user3 = User.find_by_name('user4')
    157:     assert_equal nil, user3.confirmed_on
    158:     # can confirm with right token
    159:     get "login/confirm_account", :id => user3.id, :tk => Digest::SHA1.hexdigest(user3.hashed_password)
    160:     assert_equal LoginController::FLASH_PASSWORD_ACTIVATED, flash['success']
    161:     assert_not_nil assigns(:user).confirmed_on
    162:     # user can now logon
    163:     # user can sign in and check that they want to be remembered
    164:     post "login/login" , :user => {:email => 'user4@xyz.com', :password => 'user4', :remember_me => 0}
    165:     assert_equal User.find_by_name("user4"), session['user']
    166:     assert_not_nil cookies
    167:     # TODO we can't use cookies[:epfwiki_id] anymore?
    168:     assert_equal cookies["epfwiki_id"], session['user'].id.to_s
    169:     # automatically sign-in for remembered users
    170:     # redirected to user details or requested page (not tested)
    171:     get "login/login"
    172:     assert_redirected_to :controller => "users", :action => "show", :id => cookies[:epfwiki_id]
    173:   end

test_signup_central_admin()

        If there are no users:

- all request are redirected to the new page
- the new page can be used to create the central admin account

After the first user is created (User.count > 0) it is not possible to create the central admin user

[Source]

        # File test/integration/setup_and_sign_in_test.rb, line 25
    25:   def   test_signup_central_admin
    26:     User.destroy_all
    27:     assert_equal 0, User.count
    28:     get "login/login"
    29:     assert_redirected_to :action => 'new_cadmin'
    30:     # fields cannot be null
    31:     post "login/new_cadmin"
    32:     assert_response :success
    33:     assert_errors
    34:     assert_equal 0, User.count
    35:     # no password confirmation
    36:     post "login/new_cadmin", :user => {:name => "cadmin", :email => "cadmin@logicacmg.com", :password => "cadmin"}
    37:     assert_response :success
    38:     assert nil, @response.body
    39:     assert_equal "Password confirmation can't be blank",assigns(:user).errors.full_messages.join(', ')
    40:     assert_equal 0, User.count
    41:     assert_errors
    42:     # passwords don't match
    43:     post "login/new_cadmin", :user => {:name => "cadmin", :email => "cadmin@logicacmg.com", :password => "cadmin", :password_confirmation => ""}
    44:     assert_equal "Password confirmation can't be blank, Password doesn't match confirmation",assigns(:user).errors.full_messages.join(', ')
    45:     assert_response :success #302?
    46:     assert_equal 0, User.count
    47:     assert_errors
    48:     # passwords should be present
    49:     post "login/new_cadmin", :user => {:name => "cadmin", :email => "cadmin@logicacmg.com"}
    50:     assert_equal "Password confirmation can't be blank, Password can't be blank",assigns(:user).errors.full_messages.join(', ')
    51:     assert_equal 0, User.count
    52:     assert_response :success
    53:     assert_errors
    54:     post "login/new_cadmin", :user => {:name => "cadmin", :email => "cadmin@logicacmg.com", :password => "cadmin", :password_confirmation => "cadmin"}
    55:     assert_equal "",assigns(:user).errors.full_messages.join(', ')
    56:     assert_equal 1, User.count
    57:     cadmin = User.find_central_admin
    58:     assert_not_nil cadmin
    59:     assert_redirected_to :action => 'login'
    60:     post "login/new_cadmin", :user => {:name => "cadmin", :email => "cadmin@logicacmg.com", :password => "cadmin", :password_confirmation => "cadmin"}
    61:     assert_equal LoginController::FLASH_CENTRAL_ADMIN_ALREADY_CREATED, flash['error']
    62:     assert_equal 1, User.count
    63:     assert_redirected_to :action => 'login'
    64:   end

[Validate]