##// END OF EJS Templates
Fixed: Missing template wiki/update.erb error introduced in r4272 (#6987)....
Jean-Philippe Lang -
r4315:4a6a551d074c
parent child
Show More
@@ -1,277 +1,279
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'diff'
19 19
20 20 # The WikiController follows the Rails REST controller pattern but with
21 21 # a few differences
22 22 #
23 23 # * index - shows a list of WikiPages grouped by page or date
24 24 # * new - not used
25 25 # * create - not used
26 26 # * show - will also show the form for creating a new wiki page
27 27 # * edit - used to edit an existing or new page
28 28 # * update - used to save a wiki page update to the database, including new pages
29 29 # * destroy - normal
30 30 #
31 31 # Other member and collection methods are also used
32 32 #
33 33 # TODO: still being worked on
34 34 class WikiController < ApplicationController
35 35 default_search_scope :wiki_pages
36 36 before_filter :find_wiki, :authorize
37 37 before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy]
38 38
39 39 verify :method => :post, :only => [:protect], :redirect_to => { :action => :show }
40 40
41 41 helper :attachments
42 42 include AttachmentsHelper
43 43 helper :watchers
44 44
45 45 # List of pages, sorted alphabetically and by parent (hierarchy)
46 46 def index
47 47 load_pages_grouped_by_date_without_content
48 48 end
49 49
50 50 # display a page (in editing mode if it doesn't exist)
51 51 def show
52 52 page_title = params[:id]
53 53 @page = @wiki.find_or_new_page(page_title)
54 54 if @page.new_record?
55 55 if User.current.allowed_to?(:edit_wiki_pages, @project) && editable?
56 56 edit
57 57 render :action => 'edit'
58 58 else
59 59 render_404
60 60 end
61 61 return
62 62 end
63 63 if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
64 64 # Redirects user to the current version if he's not allowed to view previous versions
65 65 redirect_to :version => nil
66 66 return
67 67 end
68 68 @content = @page.content_for_version(params[:version])
69 69 if User.current.allowed_to?(:export_wiki_pages, @project)
70 70 if params[:format] == 'html'
71 71 export = render_to_string :action => 'export', :layout => false
72 72 send_data(export, :type => 'text/html', :filename => "#{@page.title}.html")
73 73 return
74 74 elsif params[:format] == 'txt'
75 75 send_data(@content.text, :type => 'text/plain', :filename => "#{@page.title}.txt")
76 76 return
77 77 end
78 78 end
79 79 @editable = editable?
80 80 render :action => 'show'
81 81 end
82 82
83 83 # edit an existing page or a new one
84 84 def edit
85 85 @page = @wiki.find_or_new_page(params[:id])
86 86 return render_403 unless editable?
87 87 @page.content = WikiContent.new(:page => @page) if @page.new_record?
88 88
89 89 @content = @page.content_for_version(params[:version])
90 90 @content.text = initial_page_content(@page) if @content.text.blank?
91 91 # don't keep previous comment
92 92 @content.comments = nil
93 93
94 94 # To prevent StaleObjectError exception when reverting to a previous version
95 95 @content.version = @page.content.version
96 96 rescue ActiveRecord::StaleObjectError
97 97 # Optimistic locking exception
98 98 flash[:error] = l(:notice_locking_conflict)
99 99 end
100 100
101 101 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
102 102 # Creates a new page or updates an existing one
103 103 def update
104 104 @page = @wiki.find_or_new_page(params[:id])
105 105 return render_403 unless editable?
106 106 @page.content = WikiContent.new(:page => @page) if @page.new_record?
107 107
108 108 @content = @page.content_for_version(params[:version])
109 109 @content.text = initial_page_content(@page) if @content.text.blank?
110 110 # don't keep previous comment
111 111 @content.comments = nil
112 112
113 113 if !@page.new_record? && params[:content].present? && @content.text == params[:content][:text]
114 114 attachments = Attachment.attach_files(@page, params[:attachments])
115 115 render_attachment_warning_if_needed(@page)
116 116 # don't save if text wasn't changed
117 117 redirect_to :action => 'show', :project_id => @project, :id => @page.title
118 118 return
119 119 end
120 120 @content.attributes = params[:content]
121 121 @content.author = User.current
122 122 # if page is new @page.save will also save content, but not if page isn't a new record
123 123 if (@page.new_record? ? @page.save : @content.save)
124 124 attachments = Attachment.attach_files(@page, params[:attachments])
125 125 render_attachment_warning_if_needed(@page)
126 126 call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
127 127 redirect_to :action => 'show', :project_id => @project, :id => @page.title
128 else
129 render :action => 'edit'
128 130 end
129 131
130 132 rescue ActiveRecord::StaleObjectError
131 133 # Optimistic locking exception
132 134 flash[:error] = l(:notice_locking_conflict)
133 135 end
134 136
135 137 # rename a page
136 138 def rename
137 139 return render_403 unless editable?
138 140 @page.redirect_existing_links = true
139 141 # used to display the *original* title if some AR validation errors occur
140 142 @original_title = @page.pretty_title
141 143 if request.post? && @page.update_attributes(params[:wiki_page])
142 144 flash[:notice] = l(:notice_successful_update)
143 145 redirect_to :action => 'show', :project_id => @project, :id => @page.title
144 146 end
145 147 end
146 148
147 149 def protect
148 150 @page.update_attribute :protected, params[:protected]
149 151 redirect_to :action => 'show', :project_id => @project, :id => @page.title
150 152 end
151 153
152 154 # show page history
153 155 def history
154 156 @version_count = @page.content.versions.count
155 157 @version_pages = Paginator.new self, @version_count, per_page_option, params['p']
156 158 # don't load text
157 159 @versions = @page.content.versions.find :all,
158 160 :select => "id, author_id, comments, updated_on, version",
159 161 :order => 'version DESC',
160 162 :limit => @version_pages.items_per_page + 1,
161 163 :offset => @version_pages.current.offset
162 164
163 165 render :layout => false if request.xhr?
164 166 end
165 167
166 168 def diff
167 169 @diff = @page.diff(params[:version], params[:version_from])
168 170 render_404 unless @diff
169 171 end
170 172
171 173 def annotate
172 174 @annotate = @page.annotate(params[:version])
173 175 render_404 unless @annotate
174 176 end
175 177
176 178 verify :method => :delete, :only => [:destroy], :redirect_to => { :action => :show }
177 179 # Removes a wiki page and its history
178 180 # Children can be either set as root pages, removed or reassigned to another parent page
179 181 def destroy
180 182 return render_403 unless editable?
181 183
182 184 @descendants_count = @page.descendants.size
183 185 if @descendants_count > 0
184 186 case params[:todo]
185 187 when 'nullify'
186 188 # Nothing to do
187 189 when 'destroy'
188 190 # Removes all its descendants
189 191 @page.descendants.each(&:destroy)
190 192 when 'reassign'
191 193 # Reassign children to another parent page
192 194 reassign_to = @wiki.pages.find_by_id(params[:reassign_to_id].to_i)
193 195 return unless reassign_to
194 196 @page.children.each do |child|
195 197 child.update_attribute(:parent, reassign_to)
196 198 end
197 199 else
198 200 @reassignable_to = @wiki.pages - @page.self_and_descendants
199 201 return
200 202 end
201 203 end
202 204 @page.destroy
203 205 redirect_to :action => 'index', :project_id => @project
204 206 end
205 207
206 208 # Export wiki to a single html file
207 209 def export
208 210 if User.current.allowed_to?(:export_wiki_pages, @project)
209 211 @pages = @wiki.pages.find :all, :order => 'title'
210 212 export = render_to_string :action => 'export_multiple', :layout => false
211 213 send_data(export, :type => 'text/html', :filename => "wiki.html")
212 214 else
213 215 redirect_to :action => 'show', :project_id => @project, :id => nil
214 216 end
215 217 end
216 218
217 219 def date_index
218 220 load_pages_grouped_by_date_without_content
219 221 end
220 222
221 223 def preview
222 224 page = @wiki.find_page(params[:id])
223 225 # page is nil when previewing a new page
224 226 return render_403 unless page.nil? || editable?(page)
225 227 if page
226 228 @attachements = page.attachments
227 229 @previewed = page.content
228 230 end
229 231 @text = params[:content][:text]
230 232 render :partial => 'common/preview'
231 233 end
232 234
233 235 def add_attachment
234 236 return render_403 unless editable?
235 237 attachments = Attachment.attach_files(@page, params[:attachments])
236 238 render_attachment_warning_if_needed(@page)
237 239 redirect_to :action => 'show', :id => @page.title, :project_id => @project
238 240 end
239 241
240 242 private
241 243
242 244 def find_wiki
243 245 @project = Project.find(params[:project_id])
244 246 @wiki = @project.wiki
245 247 render_404 unless @wiki
246 248 rescue ActiveRecord::RecordNotFound
247 249 render_404
248 250 end
249 251
250 252 # Finds the requested page and returns a 404 error if it doesn't exist
251 253 def find_existing_page
252 254 @page = @wiki.find_page(params[:id])
253 255 render_404 if @page.nil?
254 256 end
255 257
256 258 # Returns true if the current user is allowed to edit the page, otherwise false
257 259 def editable?(page = @page)
258 260 page.editable_by?(User.current)
259 261 end
260 262
261 263 # Returns the default content of a new wiki page
262 264 def initial_page_content(page)
263 265 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
264 266 extend helper unless self.instance_of?(helper)
265 267 helper.instance_method(:initial_page_content).bind(self).call(page)
266 268 end
267 269
268 270 # eager load information about last updates, without loading text
269 271 def load_pages_grouped_by_date_without_content
270 272 @pages = @wiki.pages.find :all, :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
271 273 :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id",
272 274 :order => 'title'
273 275 @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
274 276 @pages_by_parent_id = @pages.group_by(&:parent_id)
275 277 end
276 278
277 279 end
@@ -1,413 +1,459
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.dirname(__FILE__) + '/../test_helper'
19 19 require 'wiki_controller'
20 20
21 21 # Re-raise errors caught by the controller.
22 22 class WikiController; def rescue_action(e) raise e end; end
23 23
24 24 class WikiControllerTest < ActionController::TestCase
25 25 fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions, :attachments
26 26
27 27 def setup
28 28 @controller = WikiController.new
29 29 @request = ActionController::TestRequest.new
30 30 @response = ActionController::TestResponse.new
31 31 User.current = nil
32 32 end
33 33
34 34 def test_show_start_page
35 35 get :show, :project_id => 'ecookbook'
36 36 assert_response :success
37 37 assert_template 'show'
38 38 assert_tag :tag => 'h1', :content => /CookBook documentation/
39 39
40 40 # child_pages macro
41 41 assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
42 42 :child => { :tag => 'li',
43 43 :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
44 44 :content => 'Page with an inline image' } }
45 45 end
46 46
47 47 def test_show_page_with_name
48 48 get :show, :project_id => 1, :id => 'Another_page'
49 49 assert_response :success
50 50 assert_template 'show'
51 51 assert_tag :tag => 'h1', :content => /Another page/
52 52 # Included page with an inline image
53 53 assert_tag :tag => 'p', :content => /This is an inline image/
54 54 assert_tag :tag => 'img', :attributes => { :src => '/attachments/download/3',
55 55 :alt => 'This is a logo' }
56 56 end
57 57
58 58 def test_show_with_sidebar
59 59 page = Project.find(1).wiki.pages.new(:title => 'Sidebar')
60 60 page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar')
61 61 page.save!
62 62
63 63 get :show, :project_id => 1, :id => 'Another_page'
64 64 assert_response :success
65 65 assert_tag :tag => 'div', :attributes => {:id => 'sidebar'},
66 66 :content => /Side bar content for test_show_with_sidebar/
67 67 end
68 68
69 69 def test_show_unexistent_page_without_edit_right
70 70 get :show, :project_id => 1, :id => 'Unexistent page'
71 71 assert_response 404
72 72 end
73 73
74 74 def test_show_unexistent_page_with_edit_right
75 75 @request.session[:user_id] = 2
76 76 get :show, :project_id => 1, :id => 'Unexistent page'
77 77 assert_response :success
78 78 assert_template 'edit'
79 79 end
80 80
81 81 def test_create_page
82 82 @request.session[:user_id] = 2
83 83 put :update, :project_id => 1,
84 84 :id => 'New page',
85 85 :content => {:comments => 'Created the page',
86 86 :text => "h1. New page\n\nThis is a new page",
87 87 :version => 0}
88 88 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'New_page'
89 89 page = Project.find(1).wiki.find_page('New page')
90 90 assert !page.new_record?
91 91 assert_not_nil page.content
92 92 assert_equal 'Created the page', page.content.comments
93 93 end
94 94
95 95 def test_create_page_with_attachments
96 96 @request.session[:user_id] = 2
97 97 assert_difference 'WikiPage.count' do
98 98 assert_difference 'Attachment.count' do
99 99 put :update, :project_id => 1,
100 100 :id => 'New page',
101 101 :content => {:comments => 'Created the page',
102 102 :text => "h1. New page\n\nThis is a new page",
103 103 :version => 0},
104 104 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
105 105 end
106 106 end
107 107 page = Project.find(1).wiki.find_page('New page')
108 108 assert_equal 1, page.attachments.count
109 109 assert_equal 'testfile.txt', page.attachments.first.filename
110 110 end
111
112 def test_update_page
113 @request.session[:user_id] = 2
114 assert_no_difference 'WikiPage.count' do
115 assert_no_difference 'WikiContent.count' do
116 assert_difference 'WikiContent::Version.count' do
117 put :update, :project_id => 1,
118 :id => 'Another_page',
119 :content => {
120 :comments => "my comments",
121 :text => "edited",
122 :version => 1
123 }
124 end
125 end
126 end
127 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
128
129 page = Wiki.find(1).pages.find_by_title('Another_page')
130 assert_equal "edited", page.content.text
131 assert_equal 2, page.content.version
132 assert_equal "my comments", page.content.comments
133 end
134
135 def test_update_page_with_failure
136 @request.session[:user_id] = 2
137 assert_no_difference 'WikiPage.count' do
138 assert_no_difference 'WikiContent.count' do
139 assert_no_difference 'WikiContent::Version.count' do
140 put :update, :project_id => 1,
141 :id => 'Another_page',
142 :content => {
143 :comments => 'a' * 300, # failure here, comment is too long
144 :text => 'edited',
145 :version => 1
146 }
147 end
148 end
149 end
150 assert_response :success
151 assert_template 'edit'
152
153 assert_error_tag :descendant => {:content => /Comment is too long/}
154 assert_tag :tag => 'textarea', :attributes => {:id => 'content_text'}, :content => 'edited'
155 assert_tag :tag => 'input', :attributes => {:id => 'content_version', :value => '1'}
156 end
111 157
112 158 def test_preview
113 159 @request.session[:user_id] = 2
114 160 xhr :post, :preview, :project_id => 1, :id => 'CookBook_documentation',
115 161 :content => { :comments => '',
116 162 :text => 'this is a *previewed text*',
117 163 :version => 3 }
118 164 assert_response :success
119 165 assert_template 'common/_preview'
120 166 assert_tag :tag => 'strong', :content => /previewed text/
121 167 end
122 168
123 169 def test_preview_new_page
124 170 @request.session[:user_id] = 2
125 171 xhr :post, :preview, :project_id => 1, :id => 'New page',
126 172 :content => { :text => 'h1. New page',
127 173 :comments => '',
128 174 :version => 0 }
129 175 assert_response :success
130 176 assert_template 'common/_preview'
131 177 assert_tag :tag => 'h1', :content => /New page/
132 178 end
133 179
134 180 def test_history
135 181 get :history, :project_id => 1, :id => 'CookBook_documentation'
136 182 assert_response :success
137 183 assert_template 'history'
138 184 assert_not_nil assigns(:versions)
139 185 assert_equal 3, assigns(:versions).size
140 186 assert_select "input[type=submit][name=commit]"
141 187 end
142 188
143 189 def test_history_with_one_version
144 190 get :history, :project_id => 1, :id => 'Another_page'
145 191 assert_response :success
146 192 assert_template 'history'
147 193 assert_not_nil assigns(:versions)
148 194 assert_equal 1, assigns(:versions).size
149 195 assert_select "input[type=submit][name=commit]", false
150 196 end
151 197
152 198 def test_diff
153 199 get :diff, :project_id => 1, :id => 'CookBook_documentation', :version => 2, :version_from => 1
154 200 assert_response :success
155 201 assert_template 'diff'
156 202 assert_tag :tag => 'span', :attributes => { :class => 'diff_in'},
157 203 :content => /updated/
158 204 end
159 205
160 206 def test_annotate
161 207 get :annotate, :project_id => 1, :id => 'CookBook_documentation', :version => 2
162 208 assert_response :success
163 209 assert_template 'annotate'
164 210 # Line 1
165 211 assert_tag :tag => 'tr', :child => { :tag => 'th', :attributes => {:class => 'line-num'}, :content => '1' },
166 212 :child => { :tag => 'td', :attributes => {:class => 'author'}, :content => /John Smith/ },
167 213 :child => { :tag => 'td', :content => /h1\. CookBook documentation/ }
168 214 # Line 2
169 215 assert_tag :tag => 'tr', :child => { :tag => 'th', :attributes => {:class => 'line-num'}, :content => '2' },
170 216 :child => { :tag => 'td', :attributes => {:class => 'author'}, :content => /redMine Admin/ },
171 217 :child => { :tag => 'td', :content => /Some updated \[\[documentation\]\] here/ }
172 218 end
173 219
174 220 def test_get_rename
175 221 @request.session[:user_id] = 2
176 222 get :rename, :project_id => 1, :id => 'Another_page'
177 223 assert_response :success
178 224 assert_template 'rename'
179 225 assert_tag 'option',
180 226 :attributes => {:value => ''},
181 227 :content => '',
182 228 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
183 229 assert_no_tag 'option',
184 230 :attributes => {:selected => 'selected'},
185 231 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
186 232 end
187 233
188 234 def test_get_rename_child_page
189 235 @request.session[:user_id] = 2
190 236 get :rename, :project_id => 1, :id => 'Child_1'
191 237 assert_response :success
192 238 assert_template 'rename'
193 239 assert_tag 'option',
194 240 :attributes => {:value => ''},
195 241 :content => '',
196 242 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
197 243 assert_tag 'option',
198 244 :attributes => {:value => '2', :selected => 'selected'},
199 245 :content => /Another page/,
200 246 :parent => {
201 247 :tag => 'select',
202 248 :attributes => {:name => 'wiki_page[parent_id]'}
203 249 }
204 250 end
205 251
206 252 def test_rename_with_redirect
207 253 @request.session[:user_id] = 2
208 254 post :rename, :project_id => 1, :id => 'Another_page',
209 255 :wiki_page => { :title => 'Another renamed page',
210 256 :redirect_existing_links => 1 }
211 257 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
212 258 wiki = Project.find(1).wiki
213 259 # Check redirects
214 260 assert_not_nil wiki.find_page('Another page')
215 261 assert_nil wiki.find_page('Another page', :with_redirect => false)
216 262 end
217 263
218 264 def test_rename_without_redirect
219 265 @request.session[:user_id] = 2
220 266 post :rename, :project_id => 1, :id => 'Another_page',
221 267 :wiki_page => { :title => 'Another renamed page',
222 268 :redirect_existing_links => "0" }
223 269 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
224 270 wiki = Project.find(1).wiki
225 271 # Check that there's no redirects
226 272 assert_nil wiki.find_page('Another page')
227 273 end
228 274
229 275 def test_rename_with_parent_assignment
230 276 @request.session[:user_id] = 2
231 277 post :rename, :project_id => 1, :id => 'Another_page',
232 278 :wiki_page => { :title => 'Another page', :redirect_existing_links => "0", :parent_id => '4' }
233 279 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
234 280 assert_equal WikiPage.find(4), WikiPage.find_by_title('Another_page').parent
235 281 end
236 282
237 283 def test_rename_with_parent_unassignment
238 284 @request.session[:user_id] = 2
239 285 post :rename, :project_id => 1, :id => 'Child_1',
240 286 :wiki_page => { :title => 'Child 1', :redirect_existing_links => "0", :parent_id => '' }
241 287 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Child_1'
242 288 assert_nil WikiPage.find_by_title('Child_1').parent
243 289 end
244 290
245 291 def test_destroy_child
246 292 @request.session[:user_id] = 2
247 293 delete :destroy, :project_id => 1, :id => 'Child_1'
248 294 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
249 295 end
250 296
251 297 def test_destroy_parent
252 298 @request.session[:user_id] = 2
253 299 assert_no_difference('WikiPage.count') do
254 300 delete :destroy, :project_id => 1, :id => 'Another_page'
255 301 end
256 302 assert_response :success
257 303 assert_template 'destroy'
258 304 end
259 305
260 306 def test_destroy_parent_with_nullify
261 307 @request.session[:user_id] = 2
262 308 assert_difference('WikiPage.count', -1) do
263 309 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'nullify'
264 310 end
265 311 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
266 312 assert_nil WikiPage.find_by_id(2)
267 313 end
268 314
269 315 def test_destroy_parent_with_cascade
270 316 @request.session[:user_id] = 2
271 317 assert_difference('WikiPage.count', -3) do
272 318 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'destroy'
273 319 end
274 320 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
275 321 assert_nil WikiPage.find_by_id(2)
276 322 assert_nil WikiPage.find_by_id(5)
277 323 end
278 324
279 325 def test_destroy_parent_with_reassign
280 326 @request.session[:user_id] = 2
281 327 assert_difference('WikiPage.count', -1) do
282 328 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'reassign', :reassign_to_id => 1
283 329 end
284 330 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
285 331 assert_nil WikiPage.find_by_id(2)
286 332 assert_equal WikiPage.find(1), WikiPage.find_by_id(5).parent
287 333 end
288 334
289 335 def test_index
290 336 get :index, :project_id => 'ecookbook'
291 337 assert_response :success
292 338 assert_template 'index'
293 339 pages = assigns(:pages)
294 340 assert_not_nil pages
295 341 assert_equal Project.find(1).wiki.pages.size, pages.size
296 342
297 343 assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
298 344 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/CookBook_documentation' },
299 345 :content => 'CookBook documentation' },
300 346 :child => { :tag => 'ul',
301 347 :child => { :tag => 'li',
302 348 :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
303 349 :content => 'Page with an inline image' } } } },
304 350 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Another_page' },
305 351 :content => 'Another page' } }
306 352 end
307 353
308 354 context "GET :export" do
309 355 context "with an authorized user to export the wiki" do
310 356 setup do
311 357 @request.session[:user_id] = 2
312 358 get :export, :project_id => 'ecookbook'
313 359 end
314 360
315 361 should_respond_with :success
316 362 should_assign_to :pages
317 363 should_respond_with_content_type "text/html"
318 364 should "export all of the wiki pages to a single html file" do
319 365 assert_select "a[name=?]", "CookBook_documentation"
320 366 assert_select "a[name=?]", "Another_page"
321 367 assert_select "a[name=?]", "Page_with_an_inline_image"
322 368 end
323 369
324 370 end
325 371
326 372 context "with an unauthorized user" do
327 373 setup do
328 374 get :export, :project_id => 'ecookbook'
329 375
330 376 should_respond_with :redirect
331 377 should_redirect_to('wiki index') { {:action => 'show', :project_id => @project, :id => nil} }
332 378 end
333 379 end
334 380 end
335 381
336 382 context "GET :date_index" do
337 383 setup do
338 384 get :date_index, :project_id => 'ecookbook'
339 385 end
340 386
341 387 should_respond_with :success
342 388 should_assign_to :pages
343 389 should_assign_to :pages_by_date
344 390 should_render_template 'wiki/date_index'
345 391
346 392 end
347 393
348 394 def test_not_found
349 395 get :show, :project_id => 999
350 396 assert_response 404
351 397 end
352 398
353 399 def test_protect_page
354 400 page = WikiPage.find_by_wiki_id_and_title(1, 'Another_page')
355 401 assert !page.protected?
356 402 @request.session[:user_id] = 2
357 403 post :protect, :project_id => 1, :id => page.title, :protected => '1'
358 404 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
359 405 assert page.reload.protected?
360 406 end
361 407
362 408 def test_unprotect_page
363 409 page = WikiPage.find_by_wiki_id_and_title(1, 'CookBook_documentation')
364 410 assert page.protected?
365 411 @request.session[:user_id] = 2
366 412 post :protect, :project_id => 1, :id => page.title, :protected => '0'
367 413 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'CookBook_documentation'
368 414 assert !page.reload.protected?
369 415 end
370 416
371 417 def test_show_page_with_edit_link
372 418 @request.session[:user_id] = 2
373 419 get :show, :project_id => 1
374 420 assert_response :success
375 421 assert_template 'show'
376 422 assert_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
377 423 end
378 424
379 425 def test_show_page_without_edit_link
380 426 @request.session[:user_id] = 4
381 427 get :show, :project_id => 1
382 428 assert_response :success
383 429 assert_template 'show'
384 430 assert_no_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
385 431 end
386 432
387 433 def test_edit_unprotected_page
388 434 # Non members can edit unprotected wiki pages
389 435 @request.session[:user_id] = 4
390 436 get :edit, :project_id => 1, :id => 'Another_page'
391 437 assert_response :success
392 438 assert_template 'edit'
393 439 end
394 440
395 441 def test_edit_protected_page_by_nonmember
396 442 # Non members can't edit protected wiki pages
397 443 @request.session[:user_id] = 4
398 444 get :edit, :project_id => 1, :id => 'CookBook_documentation'
399 445 assert_response 403
400 446 end
401 447
402 448 def test_edit_protected_page_by_member
403 449 @request.session[:user_id] = 2
404 450 get :edit, :project_id => 1, :id => 'CookBook_documentation'
405 451 assert_response :success
406 452 assert_template 'edit'
407 453 end
408 454
409 455 def test_history_of_non_existing_page_should_return_404
410 456 get :history, :project_id => 1, :id => 'Unknown_page'
411 457 assert_response 404
412 458 end
413 459 end
@@ -1,420 +1,420
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 ENV["RAILS_ENV"] = "test"
19 19 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
20 20 require 'test_help'
21 21 require File.expand_path(File.dirname(__FILE__) + '/helper_testcase')
22 22 require File.join(RAILS_ROOT,'test', 'mocks', 'open_id_authentication_mock.rb')
23 23
24 24 require File.expand_path(File.dirname(__FILE__) + '/object_daddy_helpers')
25 25 include ObjectDaddyHelpers
26 26
27 27 class ActiveSupport::TestCase
28 28 # Transactional fixtures accelerate your tests by wrapping each test method
29 29 # in a transaction that's rolled back on completion. This ensures that the
30 30 # test database remains unchanged so your fixtures don't have to be reloaded
31 31 # between every test method. Fewer database queries means faster tests.
32 32 #
33 33 # Read Mike Clark's excellent walkthrough at
34 34 # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
35 35 #
36 36 # Every Active Record database supports transactions except MyISAM tables
37 37 # in MySQL. Turn off transactional fixtures in this case; however, if you
38 38 # don't care one way or the other, switching from MyISAM to InnoDB tables
39 39 # is recommended.
40 40 self.use_transactional_fixtures = true
41 41
42 42 # Instantiated fixtures are slow, but give you @david where otherwise you
43 43 # would need people(:david). If you don't want to migrate your existing
44 44 # test cases which use the @david style and don't mind the speed hit (each
45 45 # instantiated fixtures translates to a database query per test method),
46 46 # then set this back to true.
47 47 self.use_instantiated_fixtures = false
48 48
49 49 # Add more helper methods to be used by all tests here...
50 50
51 51 def log_user(login, password)
52 52 User.anonymous
53 53 get "/login"
54 54 assert_equal nil, session[:user_id]
55 55 assert_response :success
56 56 assert_template "account/login"
57 57 post "/login", :username => login, :password => password
58 58 assert_equal login, User.find(session[:user_id]).login
59 59 end
60 60
61 61 def uploaded_test_file(name, mime)
62 62 ActionController::TestUploadedFile.new(ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime)
63 63 end
64 64
65 65 # Mock out a file
66 66 def self.mock_file
67 67 file = 'a_file.png'
68 68 file.stubs(:size).returns(32)
69 69 file.stubs(:original_filename).returns('a_file.png')
70 70 file.stubs(:content_type).returns('image/png')
71 71 file.stubs(:read).returns(false)
72 72 file
73 73 end
74 74
75 75 def mock_file
76 76 self.class.mock_file
77 77 end
78 78
79 79 # Use a temporary directory for attachment related tests
80 80 def set_tmp_attachments_directory
81 81 Dir.mkdir "#{RAILS_ROOT}/tmp/test" unless File.directory?("#{RAILS_ROOT}/tmp/test")
82 82 Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments")
83 83 Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments"
84 84 end
85 85
86 86 def with_settings(options, &block)
87 87 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h}
88 88 options.each {|k, v| Setting[k] = v}
89 89 yield
90 90 saved_settings.each {|k, v| Setting[k] = v}
91 91 end
92 92
93 93 def change_user_password(login, new_password)
94 94 user = User.first(:conditions => {:login => login})
95 95 user.password, user.password_confirmation = new_password, new_password
96 96 user.save!
97 97 end
98 98
99 99 def self.ldap_configured?
100 100 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
101 101 return @test_ldap.bind
102 102 rescue Exception => e
103 103 # LDAP is not listening
104 104 return nil
105 105 end
106 106
107 107 # Returns the path to the test +vendor+ repository
108 108 def self.repository_path(vendor)
109 109 File.join(RAILS_ROOT.gsub(%r{config\/\.\.}, ''), "/tmp/test/#{vendor.downcase}_repository")
110 110 end
111 111
112 112 # Returns true if the +vendor+ test repository is configured
113 113 def self.repository_configured?(vendor)
114 114 File.directory?(repository_path(vendor))
115 115 end
116 116
117 117 def assert_error_tag(options={})
118 assert_tag({:tag => 'p', :attributes => { :id => 'errorExplanation' }}.merge(options))
118 assert_tag({:attributes => { :id => 'errorExplanation' }}.merge(options))
119 119 end
120 120
121 121 # Shoulda macros
122 122 def self.should_render_404
123 123 should_respond_with :not_found
124 124 should_render_template 'common/error'
125 125 end
126 126
127 127 def self.should_have_before_filter(expected_method, options = {})
128 128 should_have_filter('before', expected_method, options)
129 129 end
130 130
131 131 def self.should_have_after_filter(expected_method, options = {})
132 132 should_have_filter('after', expected_method, options)
133 133 end
134 134
135 135 def self.should_have_filter(filter_type, expected_method, options)
136 136 description = "have #{filter_type}_filter :#{expected_method}"
137 137 description << " with #{options.inspect}" unless options.empty?
138 138
139 139 should description do
140 140 klass = "action_controller/filters/#{filter_type}_filter".classify.constantize
141 141 expected = klass.new(:filter, expected_method.to_sym, options)
142 142 assert_equal 1, @controller.class.filter_chain.select { |filter|
143 143 filter.method == expected.method && filter.kind == expected.kind &&
144 144 filter.options == expected.options && filter.class == expected.class
145 145 }.size
146 146 end
147 147 end
148 148
149 149 def self.should_show_the_old_and_new_values_for(prop_key, model, &block)
150 150 context "" do
151 151 setup do
152 152 if block_given?
153 153 instance_eval &block
154 154 else
155 155 @old_value = model.generate!
156 156 @new_value = model.generate!
157 157 end
158 158 end
159 159
160 160 should "use the new value's name" do
161 161 @detail = JournalDetail.generate!(:property => 'attr',
162 162 :old_value => @old_value.id,
163 163 :value => @new_value.id,
164 164 :prop_key => prop_key)
165 165
166 166 assert_match @new_value.name, show_detail(@detail, true)
167 167 end
168 168
169 169 should "use the old value's name" do
170 170 @detail = JournalDetail.generate!(:property => 'attr',
171 171 :old_value => @old_value.id,
172 172 :value => @new_value.id,
173 173 :prop_key => prop_key)
174 174
175 175 assert_match @old_value.name, show_detail(@detail, true)
176 176 end
177 177 end
178 178 end
179 179
180 180 def self.should_create_a_new_user(&block)
181 181 should "create a new user" do
182 182 user = instance_eval &block
183 183 assert user
184 184 assert_kind_of User, user
185 185 assert !user.new_record?
186 186 end
187 187 end
188 188
189 189 # Test that a request allows the three types of API authentication
190 190 #
191 191 # * HTTP Basic with username and password
192 192 # * HTTP Basic with an api key for the username
193 193 # * Key based with the key=X parameter
194 194 #
195 195 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
196 196 # @param [String] url the request url
197 197 # @param [optional, Hash] parameters additional request parameters
198 198 # @param [optional, Hash] options additional options
199 199 # @option options [Symbol] :success_code Successful response code (:success)
200 200 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
201 201 def self.should_allow_api_authentication(http_method, url, parameters={}, options={})
202 202 should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options)
203 203 should_allow_http_basic_auth_with_key(http_method, url, parameters, options)
204 204 should_allow_key_based_auth(http_method, url, parameters, options)
205 205 end
206 206
207 207 # Test that a request allows the username and password for HTTP BASIC
208 208 #
209 209 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
210 210 # @param [String] url the request url
211 211 # @param [optional, Hash] parameters additional request parameters
212 212 # @param [optional, Hash] options additional options
213 213 # @option options [Symbol] :success_code Successful response code (:success)
214 214 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
215 215 def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={})
216 216 success_code = options[:success_code] || :success
217 217 failure_code = options[:failure_code] || :unauthorized
218 218
219 219 context "should allow http basic auth using a username and password for #{http_method} #{url}" do
220 220 context "with a valid HTTP authentication" do
221 221 setup do
222 222 @user = User.generate_with_protected!(:password => 'my_password', :password_confirmation => 'my_password', :admin => true) # Admin so they can access the project
223 223 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'my_password')
224 224 send(http_method, url, parameters, {:authorization => @authorization})
225 225 end
226 226
227 227 should_respond_with success_code
228 228 should_respond_with_content_type_based_on_url(url)
229 229 should "login as the user" do
230 230 assert_equal @user, User.current
231 231 end
232 232 end
233 233
234 234 context "with an invalid HTTP authentication" do
235 235 setup do
236 236 @user = User.generate_with_protected!
237 237 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'wrong_password')
238 238 send(http_method, url, parameters, {:authorization => @authorization})
239 239 end
240 240
241 241 should_respond_with failure_code
242 242 should_respond_with_content_type_based_on_url(url)
243 243 should "not login as the user" do
244 244 assert_equal User.anonymous, User.current
245 245 end
246 246 end
247 247
248 248 context "without credentials" do
249 249 setup do
250 250 send(http_method, url, parameters, {:authorization => ''})
251 251 end
252 252
253 253 should_respond_with failure_code
254 254 should_respond_with_content_type_based_on_url(url)
255 255 should "include_www_authenticate_header" do
256 256 assert @controller.response.headers.has_key?('WWW-Authenticate')
257 257 end
258 258 end
259 259 end
260 260
261 261 end
262 262
263 263 # Test that a request allows the API key with HTTP BASIC
264 264 #
265 265 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
266 266 # @param [String] url the request url
267 267 # @param [optional, Hash] parameters additional request parameters
268 268 # @param [optional, Hash] options additional options
269 269 # @option options [Symbol] :success_code Successful response code (:success)
270 270 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
271 271 def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={})
272 272 success_code = options[:success_code] || :success
273 273 failure_code = options[:failure_code] || :unauthorized
274 274
275 275 context "should allow http basic auth with a key for #{http_method} #{url}" do
276 276 context "with a valid HTTP authentication using the API token" do
277 277 setup do
278 278 @user = User.generate_with_protected!(:admin => true)
279 279 @token = Token.generate!(:user => @user, :action => 'api')
280 280 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X')
281 281 send(http_method, url, parameters, {:authorization => @authorization})
282 282 end
283 283
284 284 should_respond_with success_code
285 285 should_respond_with_content_type_based_on_url(url)
286 286 should_be_a_valid_response_string_based_on_url(url)
287 287 should "login as the user" do
288 288 assert_equal @user, User.current
289 289 end
290 290 end
291 291
292 292 context "with an invalid HTTP authentication" do
293 293 setup do
294 294 @user = User.generate_with_protected!
295 295 @token = Token.generate!(:user => @user, :action => 'feeds')
296 296 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X')
297 297 send(http_method, url, parameters, {:authorization => @authorization})
298 298 end
299 299
300 300 should_respond_with failure_code
301 301 should_respond_with_content_type_based_on_url(url)
302 302 should "not login as the user" do
303 303 assert_equal User.anonymous, User.current
304 304 end
305 305 end
306 306 end
307 307 end
308 308
309 309 # Test that a request allows full key authentication
310 310 #
311 311 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
312 312 # @param [String] url the request url, without the key=ZXY parameter
313 313 # @param [optional, Hash] parameters additional request parameters
314 314 # @param [optional, Hash] options additional options
315 315 # @option options [Symbol] :success_code Successful response code (:success)
316 316 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
317 317 def self.should_allow_key_based_auth(http_method, url, parameters={}, options={})
318 318 success_code = options[:success_code] || :success
319 319 failure_code = options[:failure_code] || :unauthorized
320 320
321 321 context "should allow key based auth using key=X for #{http_method} #{url}" do
322 322 context "with a valid api token" do
323 323 setup do
324 324 @user = User.generate_with_protected!(:admin => true)
325 325 @token = Token.generate!(:user => @user, :action => 'api')
326 326 # Simple url parse to add on ?key= or &key=
327 327 request_url = if url.match(/\?/)
328 328 url + "&key=#{@token.value}"
329 329 else
330 330 url + "?key=#{@token.value}"
331 331 end
332 332 send(http_method, request_url, parameters)
333 333 end
334 334
335 335 should_respond_with success_code
336 336 should_respond_with_content_type_based_on_url(url)
337 337 should_be_a_valid_response_string_based_on_url(url)
338 338 should "login as the user" do
339 339 assert_equal @user, User.current
340 340 end
341 341 end
342 342
343 343 context "with an invalid api token" do
344 344 setup do
345 345 @user = User.generate_with_protected!
346 346 @token = Token.generate!(:user => @user, :action => 'feeds')
347 347 # Simple url parse to add on ?key= or &key=
348 348 request_url = if url.match(/\?/)
349 349 url + "&key=#{@token.value}"
350 350 else
351 351 url + "?key=#{@token.value}"
352 352 end
353 353 send(http_method, request_url, parameters)
354 354 end
355 355
356 356 should_respond_with failure_code
357 357 should_respond_with_content_type_based_on_url(url)
358 358 should "not login as the user" do
359 359 assert_equal User.anonymous, User.current
360 360 end
361 361 end
362 362 end
363 363
364 364 end
365 365
366 366 # Uses should_respond_with_content_type based on what's in the url:
367 367 #
368 368 # '/project/issues.xml' => should_respond_with_content_type :xml
369 369 # '/project/issues.json' => should_respond_with_content_type :json
370 370 #
371 371 # @param [String] url Request
372 372 def self.should_respond_with_content_type_based_on_url(url)
373 373 case
374 374 when url.match(/xml/i)
375 375 should_respond_with_content_type :xml
376 376 when url.match(/json/i)
377 377 should_respond_with_content_type :json
378 378 else
379 379 raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}"
380 380 end
381 381
382 382 end
383 383
384 384 # Uses the url to assert which format the response should be in
385 385 #
386 386 # '/project/issues.xml' => should_be_a_valid_xml_string
387 387 # '/project/issues.json' => should_be_a_valid_json_string
388 388 #
389 389 # @param [String] url Request
390 390 def self.should_be_a_valid_response_string_based_on_url(url)
391 391 case
392 392 when url.match(/xml/i)
393 393 should_be_a_valid_xml_string
394 394 when url.match(/json/i)
395 395 should_be_a_valid_json_string
396 396 else
397 397 raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}"
398 398 end
399 399
400 400 end
401 401
402 402 # Checks that the response is a valid JSON string
403 403 def self.should_be_a_valid_json_string
404 404 should "be a valid JSON string (or empty)" do
405 405 assert (response.body.blank? || ActiveSupport::JSON.decode(response.body))
406 406 end
407 407 end
408 408
409 409 # Checks that the response is a valid XML string
410 410 def self.should_be_a_valid_xml_string
411 411 should "be a valid XML string" do
412 412 assert REXML::Document.new(response.body)
413 413 end
414 414 end
415 415
416 416 end
417 417
418 418 # Simple module to "namespace" all of the API tests
419 419 module ApiTest
420 420 end
General Comments 0
You need to be logged in to leave comments. Login now