# All testing of the correct working of cookies is in integration testing because
# cookies don't work as expected with functional tests, see for instance
# http://www.pluitsolutions.com/2006/08/02/rails-functional-test-with-cookie/

require File.dirname(__FILE__) + '/../test_helper'
require 'login_controller'

#--######################################################################
# Copyright (c) 2006 Logica
# 
# 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 Logica}[link:files/COPYRIGHT.html]

class LoginController; def rescue_action(e) raise e end; end

class LoginControllerTest < Test::Unit::TestCase
  
  fixtures :users
  
  def setup
    logger.debug "Test Case: #{name}"  
    @controller = LoginController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
    @emails = ActionMailer::Base::deliveries
    @emails.clear
  end
  
  # Shows we can create the central admin
  def  test_signup_central_admin
    User.delete_all
    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 => 'George Shapiro', :email=> 'george.shapiro@epf.eclipse.org', :password => 'pass2', :password_confirmation => 'pass2'}
    assert_equal LoginController::FLASH_CENTRAL_ADMIN_CREATED, flash['success']
    assert_redirected_to :action => 'login'
  end
  
  # Shows that a user can sign up by supplying name, email, password and password confirmation
  def test_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"} # , :i_agree_to_the_terms_of_use => '1'
    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.eclipse.org", :password => 'user1', :password_confirmation => 'user1'} # , :i_agree_to_the_terms_of_use => '1'
    assert_equal 'test.host', @request.host + (@request.port == 80 ? '' : ':' + @request.port.to_s)
    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']}] Welcome", email.subject)
    assert_equal("user1@epf.eclipse.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 test_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 => "user3", :email=>"user3"} # , :i_agree_to_the_terms_of_use => '1'
    assert_errors
    assert_not_nil assigns(:user)
    assert_equal 'Email is invalid', assigns(:user).errors.full_messages.join(', ')        
    post :sign_up, :user => {:name => "user3", :email=>"user3@somedomain.com"} # , :i_agree_to_the_terms_of_use => '1'
    @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", email.subject)
    assert_equal("user3@somedomain.com", email.to[0])
    assert_equal([ENV['EPFWIKI_REPLY_ADDRESS']], email.from)
    assert_redirected_to :action => 'login'
  end
  
  # Shows:
  # 1. Request 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	test_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 => 'andy.kaufman@epf.eclipse.org'}
    user_by_email = assigns(:user_by_email)
    assert_not_nil user_by_email
    assert_equal LoginController::FLASH_PW_SENT, flash['success']
    assert_equal(1, @emails.size)
    email = @emails.first
    assert_equal("[#{ENV['EPFWIKI_APP_NAME']}] New Password", email.subject)
    assert_equal("andy.kaufman@epf.eclipse.org", 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_new, 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
  
end
