@@ -1,175 +1,175 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class AccountController < ApplicationController |
|
18 | class AccountController < ApplicationController | |
19 | layout 'base' |
|
19 | layout 'base' | |
20 | helper :custom_fields |
|
20 | helper :custom_fields | |
21 | include CustomFieldsHelper |
|
21 | include CustomFieldsHelper | |
22 |
|
22 | |||
23 | # prevents login action to be filtered by check_if_login_required application scope filter |
|
23 | # prevents login action to be filtered by check_if_login_required application scope filter | |
24 | skip_before_filter :check_if_login_required, :only => [:login, :lost_password, :register, :activate] |
|
24 | skip_before_filter :check_if_login_required, :only => [:login, :lost_password, :register, :activate] | |
25 |
|
25 | |||
26 | # Show user's account |
|
26 | # Show user's account | |
27 | def show |
|
27 | def show | |
28 | @user = User.find_active(params[:id]) |
|
28 | @user = User.find_active(params[:id]) | |
29 | @custom_values = @user.custom_values.find(:all, :include => :custom_field) |
|
29 | @custom_values = @user.custom_values.find(:all, :include => :custom_field) | |
30 |
|
30 | |||
31 | # show only public projects and private projects that the logged in user is also a member of |
|
31 | # show only public projects and private projects that the logged in user is also a member of | |
32 | @memberships = @user.memberships.select do |membership| |
|
32 | @memberships = @user.memberships.select do |membership| | |
33 | membership.project.is_public? || (User.current.member_of?(membership.project)) |
|
33 | membership.project.is_public? || (User.current.member_of?(membership.project)) | |
34 | end |
|
34 | end | |
35 | rescue ActiveRecord::RecordNotFound |
|
35 | rescue ActiveRecord::RecordNotFound | |
36 | render_404 |
|
36 | render_404 | |
37 | end |
|
37 | end | |
38 |
|
38 | |||
39 | # Login request and validation |
|
39 | # Login request and validation | |
40 | def login |
|
40 | def login | |
41 | if request.get? |
|
41 | if request.get? | |
42 | # Logout user |
|
42 | # Logout user | |
43 | self.logged_user = nil |
|
43 | self.logged_user = nil | |
44 | else |
|
44 | else | |
45 | # Authenticate user |
|
45 | # Authenticate user | |
46 |
user = User.try_to_login(params[: |
|
46 | user = User.try_to_login(params[:username], params[:password]) | |
47 | if user |
|
47 | if user | |
48 | self.logged_user = user |
|
48 | self.logged_user = user | |
49 | # generate a key and set cookie if autologin |
|
49 | # generate a key and set cookie if autologin | |
50 | if params[:autologin] && Setting.autologin? |
|
50 | if params[:autologin] && Setting.autologin? | |
51 | token = Token.create(:user => user, :action => 'autologin') |
|
51 | token = Token.create(:user => user, :action => 'autologin') | |
52 | cookies[:autologin] = { :value => token.value, :expires => 1.year.from_now } |
|
52 | cookies[:autologin] = { :value => token.value, :expires => 1.year.from_now } | |
53 | end |
|
53 | end | |
54 | redirect_back_or_default :controller => 'my', :action => 'page' |
|
54 | redirect_back_or_default :controller => 'my', :action => 'page' | |
55 | else |
|
55 | else | |
56 | flash.now[:error] = l(:notice_account_invalid_creditentials) |
|
56 | flash.now[:error] = l(:notice_account_invalid_creditentials) | |
57 | end |
|
57 | end | |
58 | end |
|
58 | end | |
59 | end |
|
59 | end | |
60 |
|
60 | |||
61 | # Log out current user and redirect to welcome page |
|
61 | # Log out current user and redirect to welcome page | |
62 | def logout |
|
62 | def logout | |
63 | cookies.delete :autologin |
|
63 | cookies.delete :autologin | |
64 | Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) if User.current.logged? |
|
64 | Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) if User.current.logged? | |
65 | self.logged_user = nil |
|
65 | self.logged_user = nil | |
66 | redirect_to home_url |
|
66 | redirect_to home_url | |
67 | end |
|
67 | end | |
68 |
|
68 | |||
69 | # Enable user to choose a new password |
|
69 | # Enable user to choose a new password | |
70 | def lost_password |
|
70 | def lost_password | |
71 | redirect_to(home_url) && return unless Setting.lost_password? |
|
71 | redirect_to(home_url) && return unless Setting.lost_password? | |
72 | if params[:token] |
|
72 | if params[:token] | |
73 | @token = Token.find_by_action_and_value("recovery", params[:token]) |
|
73 | @token = Token.find_by_action_and_value("recovery", params[:token]) | |
74 | redirect_to(home_url) && return unless @token and !@token.expired? |
|
74 | redirect_to(home_url) && return unless @token and !@token.expired? | |
75 | @user = @token.user |
|
75 | @user = @token.user | |
76 | if request.post? |
|
76 | if request.post? | |
77 | @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] |
|
77 | @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] | |
78 | if @user.save |
|
78 | if @user.save | |
79 | @token.destroy |
|
79 | @token.destroy | |
80 | flash[:notice] = l(:notice_account_password_updated) |
|
80 | flash[:notice] = l(:notice_account_password_updated) | |
81 | redirect_to :action => 'login' |
|
81 | redirect_to :action => 'login' | |
82 | return |
|
82 | return | |
83 | end |
|
83 | end | |
84 | end |
|
84 | end | |
85 | render :template => "account/password_recovery" |
|
85 | render :template => "account/password_recovery" | |
86 | return |
|
86 | return | |
87 | else |
|
87 | else | |
88 | if request.post? |
|
88 | if request.post? | |
89 | user = User.find_by_mail(params[:mail]) |
|
89 | user = User.find_by_mail(params[:mail]) | |
90 | # user not found in db |
|
90 | # user not found in db | |
91 | flash.now[:error] = l(:notice_account_unknown_email) and return unless user |
|
91 | flash.now[:error] = l(:notice_account_unknown_email) and return unless user | |
92 | # user uses an external authentification |
|
92 | # user uses an external authentification | |
93 | flash.now[:error] = l(:notice_can_t_change_password) and return if user.auth_source_id |
|
93 | flash.now[:error] = l(:notice_can_t_change_password) and return if user.auth_source_id | |
94 | # create a new token for password recovery |
|
94 | # create a new token for password recovery | |
95 | token = Token.new(:user => user, :action => "recovery") |
|
95 | token = Token.new(:user => user, :action => "recovery") | |
96 | if token.save |
|
96 | if token.save | |
97 | Mailer.deliver_lost_password(token) |
|
97 | Mailer.deliver_lost_password(token) | |
98 | flash[:notice] = l(:notice_account_lost_email_sent) |
|
98 | flash[:notice] = l(:notice_account_lost_email_sent) | |
99 | redirect_to :action => 'login' |
|
99 | redirect_to :action => 'login' | |
100 | return |
|
100 | return | |
101 | end |
|
101 | end | |
102 | end |
|
102 | end | |
103 | end |
|
103 | end | |
104 | end |
|
104 | end | |
105 |
|
105 | |||
106 | # User self-registration |
|
106 | # User self-registration | |
107 | def register |
|
107 | def register | |
108 | redirect_to(home_url) && return unless Setting.self_registration? |
|
108 | redirect_to(home_url) && return unless Setting.self_registration? | |
109 | if request.get? |
|
109 | if request.get? | |
110 | @user = User.new(:language => Setting.default_language) |
|
110 | @user = User.new(:language => Setting.default_language) | |
111 | @custom_values = UserCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @user) } |
|
111 | @custom_values = UserCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @user) } | |
112 | else |
|
112 | else | |
113 | @user = User.new(params[:user]) |
|
113 | @user = User.new(params[:user]) | |
114 | @user.admin = false |
|
114 | @user.admin = false | |
115 | @user.login = params[:user][:login] |
|
115 | @user.login = params[:user][:login] | |
116 | @user.status = User::STATUS_REGISTERED |
|
116 | @user.status = User::STATUS_REGISTERED | |
117 | @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] |
|
117 | @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] | |
118 | @custom_values = UserCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, |
|
118 | @custom_values = UserCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, | |
119 | :customized => @user, |
|
119 | :customized => @user, | |
120 | :value => (params["custom_fields"] ? params["custom_fields"][x.id.to_s] : nil)) } |
|
120 | :value => (params["custom_fields"] ? params["custom_fields"][x.id.to_s] : nil)) } | |
121 | @user.custom_values = @custom_values |
|
121 | @user.custom_values = @custom_values | |
122 | case Setting.self_registration |
|
122 | case Setting.self_registration | |
123 | when '1' |
|
123 | when '1' | |
124 | # Email activation |
|
124 | # Email activation | |
125 | token = Token.new(:user => @user, :action => "register") |
|
125 | token = Token.new(:user => @user, :action => "register") | |
126 | if @user.save and token.save |
|
126 | if @user.save and token.save | |
127 | Mailer.deliver_register(token) |
|
127 | Mailer.deliver_register(token) | |
128 | flash[:notice] = l(:notice_account_register_done) |
|
128 | flash[:notice] = l(:notice_account_register_done) | |
129 | redirect_to :action => 'login' |
|
129 | redirect_to :action => 'login' | |
130 | end |
|
130 | end | |
131 | when '3' |
|
131 | when '3' | |
132 | # Automatic activation |
|
132 | # Automatic activation | |
133 | @user.status = User::STATUS_ACTIVE |
|
133 | @user.status = User::STATUS_ACTIVE | |
134 | if @user.save |
|
134 | if @user.save | |
135 | flash[:notice] = l(:notice_account_activated) |
|
135 | flash[:notice] = l(:notice_account_activated) | |
136 | redirect_to :action => 'login' |
|
136 | redirect_to :action => 'login' | |
137 | end |
|
137 | end | |
138 | else |
|
138 | else | |
139 | # Manual activation by the administrator |
|
139 | # Manual activation by the administrator | |
140 | if @user.save |
|
140 | if @user.save | |
141 | # Sends an email to the administrators |
|
141 | # Sends an email to the administrators | |
142 | Mailer.deliver_account_activation_request(@user) |
|
142 | Mailer.deliver_account_activation_request(@user) | |
143 | flash[:notice] = l(:notice_account_pending) |
|
143 | flash[:notice] = l(:notice_account_pending) | |
144 | redirect_to :action => 'login' |
|
144 | redirect_to :action => 'login' | |
145 | end |
|
145 | end | |
146 | end |
|
146 | end | |
147 | end |
|
147 | end | |
148 | end |
|
148 | end | |
149 |
|
149 | |||
150 | # Token based account activation |
|
150 | # Token based account activation | |
151 | def activate |
|
151 | def activate | |
152 | redirect_to(home_url) && return unless Setting.self_registration? && params[:token] |
|
152 | redirect_to(home_url) && return unless Setting.self_registration? && params[:token] | |
153 | token = Token.find_by_action_and_value('register', params[:token]) |
|
153 | token = Token.find_by_action_and_value('register', params[:token]) | |
154 | redirect_to(home_url) && return unless token and !token.expired? |
|
154 | redirect_to(home_url) && return unless token and !token.expired? | |
155 | user = token.user |
|
155 | user = token.user | |
156 | redirect_to(home_url) && return unless user.status == User::STATUS_REGISTERED |
|
156 | redirect_to(home_url) && return unless user.status == User::STATUS_REGISTERED | |
157 | user.status = User::STATUS_ACTIVE |
|
157 | user.status = User::STATUS_ACTIVE | |
158 | if user.save |
|
158 | if user.save | |
159 | token.destroy |
|
159 | token.destroy | |
160 | flash[:notice] = l(:notice_account_activated) |
|
160 | flash[:notice] = l(:notice_account_activated) | |
161 | end |
|
161 | end | |
162 | redirect_to :action => 'login' |
|
162 | redirect_to :action => 'login' | |
163 | end |
|
163 | end | |
164 |
|
164 | |||
165 | private |
|
165 | private | |
166 | def logged_user=(user) |
|
166 | def logged_user=(user) | |
167 | if user && user.is_a?(User) |
|
167 | if user && user.is_a?(User) | |
168 | User.current = user |
|
168 | User.current = user | |
169 | session[:user_id] = user.id |
|
169 | session[:user_id] = user.id | |
170 | else |
|
170 | else | |
171 | User.current = User.anonymous |
|
171 | User.current = User.anonymous | |
172 | session[:user_id] = nil |
|
172 | session[:user_id] = nil | |
173 | end |
|
173 | end | |
174 | end |
|
174 | end | |
175 | end |
|
175 | end |
@@ -1,33 +1,33 | |||||
1 | <div id="login-form"> |
|
1 | <div id="login-form"> | |
2 | <% form_tag({:action=> "login"}) do %> |
|
2 | <% form_tag({:action=> "login"}) do %> | |
3 | <table> |
|
3 | <table> | |
4 | <tr> |
|
4 | <tr> | |
5 |
<td align="right"><label for=" |
|
5 | <td align="right"><label for="username"><%=l(:field_login)%>:</label></td> | |
6 |
<td align="left"><p><%= text_field_tag ' |
|
6 | <td align="left"><p><%= text_field_tag 'username', nil, :size => 40 %></p></td> | |
7 | </tr> |
|
7 | </tr> | |
8 | <tr> |
|
8 | <tr> | |
9 | <td align="right"><label for="password"><%=l(:field_password)%>:</label></td> |
|
9 | <td align="right"><label for="password"><%=l(:field_password)%>:</label></td> | |
10 | <td align="left"><%= password_field_tag 'password', nil, :size => 40 %></td> |
|
10 | <td align="left"><%= password_field_tag 'password', nil, :size => 40 %></td> | |
11 | </tr> |
|
11 | </tr> | |
12 | <tr> |
|
12 | <tr> | |
13 | <td></td> |
|
13 | <td></td> | |
14 | <td align="left"> |
|
14 | <td align="left"> | |
15 | <% if Setting.autologin? %> |
|
15 | <% if Setting.autologin? %> | |
16 | <label for="autologin"><%= check_box_tag 'autologin' %> <%= l(:label_stay_logged_in) %></label> |
|
16 | <label for="autologin"><%= check_box_tag 'autologin' %> <%= l(:label_stay_logged_in) %></label> | |
17 | <% end %> |
|
17 | <% end %> | |
18 | </td> |
|
18 | </td> | |
19 | </tr> |
|
19 | </tr> | |
20 | <tr> |
|
20 | <tr> | |
21 | <td align="left"> |
|
21 | <td align="left"> | |
22 | <% if Setting.lost_password? %> |
|
22 | <% if Setting.lost_password? %> | |
23 | <%= link_to l(:label_password_lost), :controller => 'account', :action => 'lost_password' %> |
|
23 | <%= link_to l(:label_password_lost), :controller => 'account', :action => 'lost_password' %> | |
24 | <% end %> |
|
24 | <% end %> | |
25 | </td> |
|
25 | </td> | |
26 | <td align="right"> |
|
26 | <td align="right"> | |
27 | <input type="submit" name="login" value="<%=l(:button_login)%> »" /> |
|
27 | <input type="submit" name="login" value="<%=l(:button_login)%> »" /> | |
28 | </td> |
|
28 | </td> | |
29 | </tr> |
|
29 | </tr> | |
30 | </table> |
|
30 | </table> | |
31 |
<%= javascript_tag "Form.Element.focus(' |
|
31 | <%= javascript_tag "Form.Element.focus('username');" %> | |
32 | <% end %> |
|
32 | <% end %> | |
33 | </div> |
|
33 | </div> |
@@ -1,73 +1,73 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | require File.dirname(__FILE__) + '/../test_helper' |
|
18 | require File.dirname(__FILE__) + '/../test_helper' | |
19 | require 'account_controller' |
|
19 | require 'account_controller' | |
20 |
|
20 | |||
21 | # Re-raise errors caught by the controller. |
|
21 | # Re-raise errors caught by the controller. | |
22 | class AccountController; def rescue_action(e) raise e end; end |
|
22 | class AccountController; def rescue_action(e) raise e end; end | |
23 |
|
23 | |||
24 | class AccountControllerTest < Test::Unit::TestCase |
|
24 | class AccountControllerTest < Test::Unit::TestCase | |
25 | fixtures :users |
|
25 | fixtures :users | |
26 |
|
26 | |||
27 | def setup |
|
27 | def setup | |
28 | @controller = AccountController.new |
|
28 | @controller = AccountController.new | |
29 | @request = ActionController::TestRequest.new |
|
29 | @request = ActionController::TestRequest.new | |
30 | @response = ActionController::TestResponse.new |
|
30 | @response = ActionController::TestResponse.new | |
31 | User.current = nil |
|
31 | User.current = nil | |
32 | end |
|
32 | end | |
33 |
|
33 | |||
34 | def test_show |
|
34 | def test_show | |
35 | get :show, :id => 2 |
|
35 | get :show, :id => 2 | |
36 | assert_response :success |
|
36 | assert_response :success | |
37 | assert_template 'show' |
|
37 | assert_template 'show' | |
38 | assert_not_nil assigns(:user) |
|
38 | assert_not_nil assigns(:user) | |
39 | end |
|
39 | end | |
40 |
|
40 | |||
41 | def test_show_inactive |
|
41 | def test_show_inactive | |
42 | get :show, :id => 5 |
|
42 | get :show, :id => 5 | |
43 | assert_response 404 |
|
43 | assert_response 404 | |
44 | assert_nil assigns(:user) |
|
44 | assert_nil assigns(:user) | |
45 | end |
|
45 | end | |
46 |
|
46 | |||
47 | def test_login_with_wrong_password |
|
47 | def test_login_with_wrong_password | |
48 |
post :login, : |
|
48 | post :login, :username => 'admin', :password => 'bad' | |
49 | assert_response :success |
|
49 | assert_response :success | |
50 | assert_template 'login' |
|
50 | assert_template 'login' | |
51 | assert_tag 'div', |
|
51 | assert_tag 'div', | |
52 | :attributes => { :class => "flash error" }, |
|
52 | :attributes => { :class => "flash error" }, | |
53 | :content => /Invalid user or password/ |
|
53 | :content => /Invalid user or password/ | |
54 | end |
|
54 | end | |
55 |
|
55 | |||
56 | def test_autologin |
|
56 | def test_autologin | |
57 | Setting.autologin = "7" |
|
57 | Setting.autologin = "7" | |
58 | Token.delete_all |
|
58 | Token.delete_all | |
59 |
post :login, : |
|
59 | post :login, :username => 'admin', :password => 'admin', :autologin => 1 | |
60 | assert_redirected_to 'my/page' |
|
60 | assert_redirected_to 'my/page' | |
61 | token = Token.find :first |
|
61 | token = Token.find :first | |
62 | assert_not_nil token |
|
62 | assert_not_nil token | |
63 | assert_equal User.find_by_login('admin'), token.user |
|
63 | assert_equal User.find_by_login('admin'), token.user | |
64 | assert_equal 'autologin', token.action |
|
64 | assert_equal 'autologin', token.action | |
65 | end |
|
65 | end | |
66 |
|
66 | |||
67 | def test_logout |
|
67 | def test_logout | |
68 | @request.session[:user_id] = 2 |
|
68 | @request.session[:user_id] = 2 | |
69 | get :logout |
|
69 | get :logout | |
70 | assert_redirected_to '' |
|
70 | assert_redirected_to '' | |
71 | assert_nil @request.session[:user_id] |
|
71 | assert_nil @request.session[:user_id] | |
72 | end |
|
72 | end | |
73 | end |
|
73 | end |
@@ -1,77 +1,77 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | ENV["RAILS_ENV"] ||= "test" |
|
18 | ENV["RAILS_ENV"] ||= "test" | |
19 | require File.expand_path(File.dirname(__FILE__) + "/../config/environment") |
|
19 | require File.expand_path(File.dirname(__FILE__) + "/../config/environment") | |
20 | require 'test_help' |
|
20 | require 'test_help' | |
21 | require File.expand_path(File.dirname(__FILE__) + '/helper_testcase') |
|
21 | require File.expand_path(File.dirname(__FILE__) + '/helper_testcase') | |
22 |
|
22 | |||
23 | class Test::Unit::TestCase |
|
23 | class Test::Unit::TestCase | |
24 | # Transactional fixtures accelerate your tests by wrapping each test method |
|
24 | # Transactional fixtures accelerate your tests by wrapping each test method | |
25 | # in a transaction that's rolled back on completion. This ensures that the |
|
25 | # in a transaction that's rolled back on completion. This ensures that the | |
26 | # test database remains unchanged so your fixtures don't have to be reloaded |
|
26 | # test database remains unchanged so your fixtures don't have to be reloaded | |
27 | # between every test method. Fewer database queries means faster tests. |
|
27 | # between every test method. Fewer database queries means faster tests. | |
28 | # |
|
28 | # | |
29 | # Read Mike Clark's excellent walkthrough at |
|
29 | # Read Mike Clark's excellent walkthrough at | |
30 | # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting |
|
30 | # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting | |
31 | # |
|
31 | # | |
32 | # Every Active Record database supports transactions except MyISAM tables |
|
32 | # Every Active Record database supports transactions except MyISAM tables | |
33 | # in MySQL. Turn off transactional fixtures in this case; however, if you |
|
33 | # in MySQL. Turn off transactional fixtures in this case; however, if you | |
34 | # don't care one way or the other, switching from MyISAM to InnoDB tables |
|
34 | # don't care one way or the other, switching from MyISAM to InnoDB tables | |
35 | # is recommended. |
|
35 | # is recommended. | |
36 | self.use_transactional_fixtures = true |
|
36 | self.use_transactional_fixtures = true | |
37 |
|
37 | |||
38 | # Instantiated fixtures are slow, but give you @david where otherwise you |
|
38 | # Instantiated fixtures are slow, but give you @david where otherwise you | |
39 | # would need people(:david). If you don't want to migrate your existing |
|
39 | # would need people(:david). If you don't want to migrate your existing | |
40 | # test cases which use the @david style and don't mind the speed hit (each |
|
40 | # test cases which use the @david style and don't mind the speed hit (each | |
41 | # instantiated fixtures translates to a database query per test method), |
|
41 | # instantiated fixtures translates to a database query per test method), | |
42 | # then set this back to true. |
|
42 | # then set this back to true. | |
43 | self.use_instantiated_fixtures = false |
|
43 | self.use_instantiated_fixtures = false | |
44 |
|
44 | |||
45 | # Add more helper methods to be used by all tests here... |
|
45 | # Add more helper methods to be used by all tests here... | |
46 |
|
46 | |||
47 | def log_user(login, password) |
|
47 | def log_user(login, password) | |
48 | get "/account/login" |
|
48 | get "/account/login" | |
49 | assert_equal nil, session[:user_id] |
|
49 | assert_equal nil, session[:user_id] | |
50 | assert_response :success |
|
50 | assert_response :success | |
51 | assert_template "account/login" |
|
51 | assert_template "account/login" | |
52 |
post "/account/login", : |
|
52 | post "/account/login", :username => login, :password => password | |
53 | assert_redirected_to "my/page" |
|
53 | assert_redirected_to "my/page" | |
54 | assert_equal login, User.find(session[:user_id]).login |
|
54 | assert_equal login, User.find(session[:user_id]).login | |
55 | end |
|
55 | end | |
56 |
|
56 | |||
57 | def test_uploaded_file(name, mime) |
|
57 | def test_uploaded_file(name, mime) | |
58 | ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + "/files/#{name}", mime) |
|
58 | ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + "/files/#{name}", mime) | |
59 | end |
|
59 | end | |
60 | end |
|
60 | end | |
61 |
|
61 | |||
62 |
|
62 | |||
63 | # ActionController::TestUploadedFile bug |
|
63 | # ActionController::TestUploadedFile bug | |
64 | # see http://dev.rubyonrails.org/ticket/4635 |
|
64 | # see http://dev.rubyonrails.org/ticket/4635 | |
65 | class String |
|
65 | class String | |
66 | def original_filename |
|
66 | def original_filename | |
67 | "testfile.txt" |
|
67 | "testfile.txt" | |
68 | end |
|
68 | end | |
69 |
|
69 | |||
70 | def content_type |
|
70 | def content_type | |
71 | "text/plain" |
|
71 | "text/plain" | |
72 | end |
|
72 | end | |
73 |
|
73 | |||
74 | def read |
|
74 | def read | |
75 | self.to_s |
|
75 | self.to_s | |
76 | end |
|
76 | end | |
77 | end |
|
77 | end |
General Comments 0
You need to be logged in to leave comments.
Login now