You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wiki_pages_test.rb 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../../test_helper', __FILE__)
  19. class Redmine::ApiTest::WikiPagesTest < Redmine::ApiTest::Base
  20. fixtures :projects, :users, :roles, :members, :member_roles,
  21. :enabled_modules, :wikis, :wiki_pages, :wiki_contents,
  22. :wiki_content_versions, :attachments
  23. test "GET /projects/:project_id/wiki/index.xml should return wiki pages" do
  24. get '/projects/ecookbook/wiki/index.xml'
  25. assert_response 200
  26. assert_equal 'application/xml', response.media_type
  27. assert_select 'wiki_pages[type=array]' do
  28. assert_select 'wiki_page', :count => Wiki.find(1).pages.count
  29. assert_select 'wiki_page' do
  30. assert_select 'title', :text => 'CookBook_documentation'
  31. assert_select 'version', :text => '3'
  32. assert_select 'created_on'
  33. assert_select 'updated_on'
  34. end
  35. assert_select 'wiki_page' do
  36. assert_select 'title', :text => 'Page_with_an_inline_image'
  37. assert_select 'parent[title=?]', 'CookBook_documentation'
  38. end
  39. end
  40. end
  41. test "GET /projects/:project_id/wiki/:title.xml should return wiki page" do
  42. get '/projects/ecookbook/wiki/CookBook_documentation.xml'
  43. assert_response 200
  44. assert_equal 'application/xml', response.media_type
  45. assert_select 'wiki_page' do
  46. assert_select 'title', :text => 'CookBook_documentation'
  47. assert_select 'version', :text => '3'
  48. assert_select 'text'
  49. assert_select 'author'
  50. assert_select 'comments'
  51. assert_select 'created_on'
  52. assert_select 'updated_on'
  53. end
  54. end
  55. test "GET /projects/:project_id/wiki/:title.xml?include=attachments should include attachments" do
  56. get '/projects/ecookbook/wiki/Page_with_an_inline_image.xml?include=attachments'
  57. assert_response 200
  58. assert_equal 'application/xml', response.media_type
  59. assert_select 'wiki_page' do
  60. assert_select 'title', :text => 'Page_with_an_inline_image'
  61. assert_select 'attachments[type=array]' do
  62. assert_select 'attachment' do
  63. assert_select 'id', :text => '3'
  64. assert_select 'filename', :text => 'logo.gif'
  65. end
  66. end
  67. end
  68. end
  69. test "GET /projects/:project_id/wiki/:title.xml with unknown title and edit permission should respond with 404" do
  70. get '/projects/ecookbook/wiki/Invalid_Page.xml', :headers => credentials('jsmith')
  71. assert_response 404
  72. assert_equal 'application/xml', response.media_type
  73. end
  74. test "GET /projects/:project_id/wiki/:title/:version.xml should return wiki page version" do
  75. get '/projects/ecookbook/wiki/CookBook_documentation/2.xml'
  76. assert_response 200
  77. assert_equal 'application/xml', response.media_type
  78. assert_select 'wiki_page' do
  79. assert_select 'title', :text => 'CookBook_documentation'
  80. assert_select 'version', :text => '2'
  81. assert_select 'text'
  82. assert_select 'author'
  83. assert_select 'comments', :text => 'Small update'
  84. assert_select 'created_on'
  85. assert_select 'updated_on'
  86. end
  87. end
  88. test "GET /projects/:project_id/wiki/:title/:version.xml without permission should be denied" do
  89. Role.anonymous.remove_permission! :view_wiki_edits
  90. get '/projects/ecookbook/wiki/CookBook_documentation/2.xml'
  91. assert_response 401
  92. assert_equal 'application/xml', response.media_type
  93. end
  94. test "PUT /projects/:project_id/wiki/:title.xml should update wiki page" do
  95. assert_no_difference 'WikiPage.count' do
  96. assert_difference 'WikiContent::Version.count' do
  97. put '/projects/ecookbook/wiki/CookBook_documentation.xml',
  98. :params => {:wiki_page => {:text => 'New content from API', :comments => 'API update'}},
  99. :headers => credentials('jsmith')
  100. assert_response :no_content
  101. end
  102. end
  103. page = WikiPage.find(1)
  104. assert_equal 'New content from API', page.content.text
  105. assert_equal 4, page.content.version
  106. assert_equal 'API update', page.content.comments
  107. assert_equal 'jsmith', page.content.author.login
  108. end
  109. test "PUT /projects/:project_id/wiki/:title.xml with current versino should update wiki page" do
  110. assert_no_difference 'WikiPage.count' do
  111. assert_difference 'WikiContent::Version.count' do
  112. put '/projects/ecookbook/wiki/CookBook_documentation.xml',
  113. :params => {:wiki_page => {:text => 'New content from API', :comments => 'API update', :version => '3'}},
  114. :headers => credentials('jsmith')
  115. assert_response :no_content
  116. end
  117. end
  118. page = WikiPage.find(1)
  119. assert_equal 'New content from API', page.content.text
  120. assert_equal 4, page.content.version
  121. assert_equal 'API update', page.content.comments
  122. assert_equal 'jsmith', page.content.author.login
  123. end
  124. test "PUT /projects/:project_id/wiki/:title.xml with stale version should respond with 409" do
  125. assert_no_difference 'WikiPage.count' do
  126. assert_no_difference 'WikiContent::Version.count' do
  127. put '/projects/ecookbook/wiki/CookBook_documentation.xml',
  128. :params => {:wiki_page => {:text => 'New content from API', :comments => 'API update', :version => '2'}},
  129. :headers => credentials('jsmith')
  130. assert_response 409
  131. end
  132. end
  133. end
  134. test "PUT /projects/:project_id/wiki/:title.xml should create the page if it does not exist" do
  135. assert_difference 'WikiPage.count' do
  136. assert_difference 'WikiContent::Version.count' do
  137. put '/projects/ecookbook/wiki/New_page_from_API.xml',
  138. :params => {:wiki_page => {:text => 'New content from API', :comments => 'API create'}},
  139. :headers => credentials('jsmith')
  140. assert_response 201
  141. end
  142. end
  143. page = WikiPage.order('id DESC').first
  144. assert_equal 'New_page_from_API', page.title
  145. assert_equal 'New content from API', page.content.text
  146. assert_equal 1, page.content.version
  147. assert_equal 'API create', page.content.comments
  148. assert_equal 'jsmith', page.content.author.login
  149. assert_nil page.parent
  150. end
  151. test "PUT /projects/:project_id/wiki/:title.xml with attachment" do
  152. set_tmp_attachments_directory
  153. attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
  154. assert_difference 'WikiPage.count' do
  155. assert_difference 'WikiContent::Version.count' do
  156. put '/projects/ecookbook/wiki/New_page_from_API.xml',
  157. :params => {:wiki_page => {:text => 'New content from API with Attachments', :comments => 'API create with Attachments',
  158. :uploads => [:token => attachment.token, :filename => 'testfile.txt', :content_type => "text/plain"]}},
  159. :headers => credentials('jsmith')
  160. assert_response 201
  161. end
  162. end
  163. page = WikiPage.order('id DESC').first
  164. assert_equal 'New_page_from_API', page.title
  165. assert_include attachment, page.attachments
  166. assert_equal attachment.filename, page.attachments.first.filename
  167. end
  168. test "PUT /projects/:project_id/wiki/:title.xml with parent" do
  169. assert_difference 'WikiPage.count' do
  170. assert_difference 'WikiContent::Version.count' do
  171. put '/projects/ecookbook/wiki/New_subpage_from_API.xml',
  172. :params => {:wiki_page => {:parent_title => 'CookBook_documentation', :text => 'New content from API', :comments => 'API create'}},
  173. :headers => credentials('jsmith')
  174. assert_response 201
  175. end
  176. end
  177. page = WikiPage.order('id DESC').first
  178. assert_equal 'New_subpage_from_API', page.title
  179. assert_equal WikiPage.find(1), page.parent
  180. end
  181. test "DELETE /projects/:project_id/wiki/:title.xml should destroy the page" do
  182. assert_difference 'WikiPage.count', -1 do
  183. delete '/projects/ecookbook/wiki/CookBook_documentation.xml', :headers => credentials('jsmith')
  184. assert_response :no_content
  185. end
  186. assert_nil WikiPage.find_by_id(1)
  187. end
  188. end