blob: dbc731d2504c253c4a00e85f612c8e9713e1d14e [file]
require File.dirname(__FILE__) + '/../test_helper'
require 'login_controller'
#--######################################################################
# Copyright (c) 2006 LogicaCMG
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
#
# Onno van der Straaten:: initial implementation
#++######################################################################
# {Copyright (c) 2006 LogicaCMG}[link:files/COPYRIGHT.html]
# Re-raise errors caught by the controller.
class LoginController; def rescue_action(e) raise e end; end
class LoginControllerTest < Test::Unit::TestCase
#fixtures :baselines
#fixtures :baselines_pages
#fixtures :baselines_sites
#fixtures :checkouts
#fixtures :comments
#fixtures :pages
#fixtures :pages_sites
#fixtures :sites
#fixtures :users
#fixtures :versions
#fixtures :notifications
def setup
@controller = LoginController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@emails = ActionMailer::Base::deliveries
@emails.clear
end
# Can create the central admin
def test01_signup_central_admin
assert User.count == 0
get :index
assert_redirected_to :action => 'login'
get :login
assert_redirected_to :action => 'new_cadmin'
get :new_cadmin
assert_field 'user_name'
assert_field 'user_email'
assert_field 'user_password'
assert_field 'user_password_confirmation'
assert_tag :tag => 'input', :attributes => {:type => 'submit'}
assert_tag :tag => 'form', :attributes => {:action => '/login/new_cadmin'}
post :new_cadmin, :user => {:name => 'Onno', :email=> 'onno.van.der.straaten@logicacmg.com', :password => 'pass2', :password_confirmation => 'pass2'}
assert_equal LoginController::FLASH_CENTRAL_ADMIN_CREATED, flash['success']
assert_redirected_to :action => 'login'
end
# A user can sign up by supplying name, email, password and password confirmation
def test02_sign_up_with_pw
get :sign_up
assert_response :success
assert_field 'user_name'
assert_field 'user_email'
assert_equal '0', ENV['EPFWIKI_GENERATE_PASSWORDS']
assert_field 'user_password'
assert_field 'user_password_confirmation'
assert !ENV['EPFWIKI_DOMAINS'].blank?
assert_tag :tag => 'select', :attributes => {:name => 'user[email_extension]'}
post :sign_up, :user => {:name => "user1", :email=>"user1", :email_extension => "@somedomain.com"}
assert_errors
assert_not_nil assigns(:user)
assert_equal 'Password confirmation can\'t be blank, Password can\'t be blank, Email domain not valid', assigns(:user).errors.full_messages.join(', ')
post :sign_up, :user => {:name => "user1", :email=>"user1", :email_extension => "@epf.org", :password => 'user1', :password_confirmation => 'user1'}
assert_equal 'test.host', @request.host
assert_redirected_to :action => 'login'
assert_equal LoginController::FLASH_PW_CONFIRMATION_EMAIL_SENT, flash['success']
assert_equal(1, @emails.size)
email = @emails.first
assert_equal("[#{ENV['EPFWIKI_APP_NAME']}] Confirm Your New Account", email.subject)
assert_equal("user1@epf.org", email.to[0])
assert_equal([ENV['EPFWIKI_REPLY_ADDRESS']], email.from)
assert_redirected_to :action => 'login'
end
# A user can sign up with name and email, a password is generated and sent to the user
def test03_sign_up_without_pw
ENV['EPFWIKI_GENERATE_PASSWORDS'] = '1'
ENV['EPFWIKI_DOMAINS'] = ''
get :sign_up
assert_response :success
assert_field 'user_name'
assert_field 'user_email'
assert_no_field 'user_password'
assert_no_field 'user_password_confirmation'
assert ENV['EPFWIKI_DOMAINS'].blank?
assert_no_tag :tag => 'select'
post :sign_up, :user => {:name => "user2", :email=>"user2"}
assert_errors
assert_not_nil assigns(:user)
assert_equal 'Email is invalid', assigns(:user).errors.full_messages.join(', ')
post :sign_up, :user => {:name => "user2", :email=>"user2@somedomain.com"}
@password = assigns(:user).password
assert !@password.blank?
assert_equal LoginController::FLASH_PW_SENT, flash['success']
assert_equal(1, @emails.size)
email = @emails.first
assert_equal("[#{ENV['EPFWIKI_APP_NAME']}] Welcome to #{ENV['EPFWIKI_APP_NAME']}", email.subject)
assert_equal("user2@somedomain.com", email.to[0])
assert_equal([ENV['EPFWIKI_REPLY_ADDRESS']], email.from)
assert_redirected_to :action => 'login'
end
# Shows:
# 1. Lost password form
# 2. Error on wrong email addresses
# 3. New password set, email sent with password and token
# 4. Cannot logon with 'new' password (not activated yet)
def test04_lost_password
# 1
get :lost_password
assert_response :success
assert_field 'user_email'
assert_tag :tag => 'form', :attributes => {:action => '/login/lost_password'}
# 2
post :lost_password, :user => {:email => 'noneexisting email'}
assert_response :success
assert_equal LoginController::FLASH_EMAIL_NOT_FOUND, flash['notice']
# 3
post :lost_password, :user => {:email => 'user2@somedomain.com'}
user_by_email = assigns(:user_by_email)
assert_not_nil user_by_email
assert_equal LoginController::FLASH_PW_SENT, flash['success']
assert_equal(2, @emails.size)
email = @emails.first
assert_equal("[#{ENV['EPFWIKI_APP_NAME']}] New Password", email.subject)
assert_equal("user2@somedomain.com", email.to[0])
assert_equal([ENV['EPFWIKI_REPLY_ADDRESS']], email.from)
assert_match(assigns(:user_by_email).password, email.body)
assert_match(assigns(:user_by_email).token, email.body)
assert_redirected_to :action => 'login'
@emails.clear
# 4
post :login, :user => {:email => user_by_email.email, :password => user_by_email.password}
assert_equal LoginController::FLASH_INVALID_PW, flash['notice']
end
# Shows:
# 1. get is not allowed
# 2. user cannot resend password of a user
# 3. admin can resend password of a user
def test05_resend
get :index
users = User.find_all_by_admin('N')
user1 = users[0]
user2 = users[1]
session['user'] = user1
# 1
get :resend, :id => user2.id
assert_illegal_get
post :resend, :id => user2.id
assert_unot_admin_message
# 2
@emails.clear
session['user'] = User.find_central_admin
assert_not_nil session['user']
post :resend, :id => user2.id
assert assigns(:user)
assert_not_nil assigns(:user)
assert_equal '', assigns(:user).errors.full_messages.join(', ')
assert_equal LoginController::FLASH_PW_SENT, flash['success']
assert_equal(1, @emails.size)
email = @emails.first
assert_equal("[#{ENV['EPFWIKI_APP_NAME']}] Welcome to #{ENV['EPFWIKI_APP_NAME']}", email.subject)
assert_equal("user2@somedomain.com", email.to[0])
assert_equal([ENV['EPFWIKI_REPLY_ADDRESS']], email.from)
assert_match(assigns(:user).password, email.body)
assert !assigns(:user).confirmed_on.nil?
#assert_match(assigns(:user).token, email.body)
@emails.clear
assert_redirected_to :controller => 'users', :action => 'list'
end
end