Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@909: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@909: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@119: require File.expand_path('../../test_helper', __FILE__) Chris@0: require 'my_controller' Chris@0: Chris@0: # Re-raise errors caught by the controller. Chris@0: class MyController; def rescue_action(e) raise e end; end Chris@0: Chris@0: class MyControllerTest < ActionController::TestCase Chris@0: fixtures :users, :user_preferences, :roles, :projects, :issues, :issue_statuses, :trackers, :enumerations, :custom_fields Chris@909: Chris@0: def setup Chris@0: @controller = MyController.new Chris@0: @request = ActionController::TestRequest.new Chris@0: @request.session[:user_id] = 2 Chris@0: @response = ActionController::TestResponse.new Chris@0: end Chris@0: Chris@0: def test_index Chris@0: get :index Chris@0: assert_response :success Chris@0: assert_template 'page' Chris@0: end Chris@909: Chris@0: def test_page Chris@0: get :page Chris@0: assert_response :success Chris@0: assert_template 'page' Chris@0: end Chris@909: Chris@0: def test_my_account_should_show_editable_custom_fields Chris@0: get :account Chris@0: assert_response :success Chris@0: assert_template 'account' Chris@0: assert_equal User.find(2), assigns(:user) Chris@909: Chris@0: assert_tag :input, :attributes => { :name => 'user[custom_field_values][4]'} Chris@0: end Chris@909: Chris@0: def test_my_account_should_not_show_non_editable_custom_fields Chris@0: UserCustomField.find(4).update_attribute :editable, false Chris@909: Chris@0: get :account Chris@0: assert_response :success Chris@0: assert_template 'account' Chris@0: assert_equal User.find(2), assigns(:user) Chris@909: Chris@0: assert_no_tag :input, :attributes => { :name => 'user[custom_field_values][4]'} Chris@0: end Chris@0: Chris@0: def test_update_account Chris@119: post :account, Chris@119: :user => { Chris@119: :firstname => "Joe", Chris@119: :login => "root", Chris@119: :admin => 1, Chris@119: :group_ids => ['10'], Chris@119: :custom_field_values => {"4" => "0100562500"} Chris@119: } Chris@909: chris@37: assert_redirected_to '/my/account' Chris@0: user = User.find(2) Chris@0: assert_equal user, assigns(:user) Chris@0: assert_equal "Joe", user.firstname Chris@0: assert_equal "jsmith", user.login Chris@0: assert_equal "0100562500", user.custom_value_for(4).value Chris@119: # ignored Chris@0: assert !user.admin? Chris@119: assert user.groups.empty? Chris@0: end Chris@909: Chris@0: def test_change_password Chris@0: get :password Chris@0: assert_response :success Chris@0: assert_template 'password' Chris@909: Chris@0: # non matching password confirmation Chris@909: post :password, :password => 'jsmith', Chris@0: :new_password => 'hello', Chris@0: :new_password_confirmation => 'hello2' Chris@0: assert_response :success Chris@0: assert_template 'password' Chris@0: assert_tag :tag => "div", :attributes => { :class => "errorExplanation" } Chris@909: Chris@0: # wrong password Chris@909: post :password, :password => 'wrongpassword', Chris@0: :new_password => 'hello', Chris@0: :new_password_confirmation => 'hello' Chris@0: assert_response :success Chris@0: assert_template 'password' Chris@0: assert_equal 'Wrong password', flash[:error] Chris@909: Chris@0: # good password Chris@0: post :password, :password => 'jsmith', Chris@0: :new_password => 'hello', Chris@0: :new_password_confirmation => 'hello' chris@37: assert_redirected_to '/my/account' Chris@0: assert User.try_to_login('jsmith', 'hello') Chris@0: end Chris@909: Chris@0: def test_page_layout Chris@0: get :page_layout Chris@0: assert_response :success Chris@0: assert_template 'page_layout' Chris@0: end Chris@909: Chris@0: def test_add_block Chris@0: xhr :post, :add_block, :block => 'issuesreportedbyme' Chris@0: assert_response :success Chris@0: assert User.find(2).pref[:my_page_layout]['top'].include?('issuesreportedbyme') Chris@0: end Chris@0: Chris@0: def test_remove_block Chris@0: xhr :post, :remove_block, :block => 'issuesassignedtome' Chris@0: assert_response :success Chris@0: assert !User.find(2).pref[:my_page_layout].values.flatten.include?('issuesassignedtome') Chris@0: end Chris@0: Chris@0: def test_order_blocks Chris@0: xhr :post, :order_blocks, :group => 'left', 'list-left' => ['documents', 'calendar', 'latestnews'] Chris@0: assert_response :success Chris@0: assert_equal ['documents', 'calendar', 'latestnews'], User.find(2).pref[:my_page_layout]['left'] Chris@0: end Chris@0: Chris@0: context "POST to reset_rss_key" do Chris@0: context "with an existing rss_token" do Chris@0: setup do Chris@0: @previous_token_value = User.find(2).rss_key # Will generate one if it's missing Chris@0: post :reset_rss_key Chris@0: end Chris@0: Chris@0: should "destroy the existing token" do Chris@0: assert_not_equal @previous_token_value, User.find(2).rss_key Chris@0: end Chris@0: Chris@0: should "create a new token" do Chris@0: assert User.find(2).rss_token Chris@0: end Chris@0: Chris@0: should_set_the_flash_to /reset/ Chris@0: should_redirect_to('my account') {'/my/account' } Chris@0: end Chris@909: Chris@0: context "with no rss_token" do Chris@0: setup do Chris@0: assert_nil User.find(2).rss_token Chris@0: post :reset_rss_key Chris@0: end Chris@0: Chris@0: should "create a new token" do Chris@0: assert User.find(2).rss_token Chris@0: end Chris@0: Chris@0: should_set_the_flash_to /reset/ Chris@0: should_redirect_to('my account') {'/my/account' } Chris@0: end Chris@0: end Chris@0: Chris@0: context "POST to reset_api_key" do Chris@0: context "with an existing api_token" do Chris@0: setup do Chris@0: @previous_token_value = User.find(2).api_key # Will generate one if it's missing Chris@0: post :reset_api_key Chris@0: end Chris@0: Chris@0: should "destroy the existing token" do Chris@0: assert_not_equal @previous_token_value, User.find(2).api_key Chris@0: end Chris@0: Chris@0: should "create a new token" do Chris@0: assert User.find(2).api_token Chris@0: end Chris@0: Chris@0: should_set_the_flash_to /reset/ Chris@0: should_redirect_to('my account') {'/my/account' } Chris@0: end Chris@909: Chris@0: context "with no api_token" do Chris@0: setup do Chris@0: assert_nil User.find(2).api_token Chris@0: post :reset_api_key Chris@0: end Chris@0: Chris@0: should "create a new token" do Chris@0: assert User.find(2).api_token Chris@0: end Chris@0: Chris@0: should_set_the_flash_to /reset/ Chris@0: should_redirect_to('my account') {'/my/account' } Chris@0: end Chris@0: end Chris@0: end