REST API for creating/updating wiki pages (#7082).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@10717 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2012-10-25 20:38:29 +00:00
parent 9e31308720
commit eff874b29a
5 changed files with 173 additions and 48 deletions

View File

@ -553,8 +553,13 @@ class ApplicationController < ActionController::Base
# Renders a 200 response for successfull updates or deletions via the API
def render_api_ok
# head :ok would return a response body with one space
render :text => '', :status => :ok, :layout => nil
render_api_head :ok
end
# Renders a head API response
def render_api_head(status)
# #head would return a response body with one space
render :text => '', :status => status, :layout => nil
end
# Renders API response on validation failure

View File

@ -36,7 +36,7 @@ class WikiController < ApplicationController
before_filter :find_wiki, :authorize
before_filter :find_existing_or_new_page, :only => [:show, :edit, :update]
before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy, :destroy_version]
accept_api_auth :index, :show
accept_api_auth :index, :show, :update
helper :attachments
include AttachmentsHelper
@ -130,15 +130,18 @@ class WikiController < ApplicationController
# Creates a new page or updates an existing one
def update
return render_403 unless editable?
was_new_page = @page.new_record?
@page.content = WikiContent.new(:page => @page) if @page.new_record?
@page.safe_attributes = params[:wiki_page]
@content = @page.content_for_version(params[:version])
@content.text = initial_page_content(@page) if @content.text.blank?
# don't keep previous comment
@content.comments = nil
@content = @page.content
content_params = params[:content]
if content_params.nil? && params[:wiki_page].is_a?(Hash)
content_params = params[:wiki_page].slice(:text, :comments, :version)
end
content_params ||= {}
if !@page.new_record? && params[:content].present? && @content.text == params[:content][:text]
if !@page.new_record? && content_params.present? && @content.text == content_params[:text]
attachments = Attachment.attach_files(@page, params[:attachments])
render_attachment_warning_if_needed(@page)
# don't save content if text wasn't changed
@ -147,14 +150,14 @@ class WikiController < ApplicationController
return
end
@content.comments = params[:content][:comments]
@text = params[:content][:text]
@content.comments = content_params[:comments]
@text = content_params[:text]
if params[:section].present? && Redmine::WikiFormatting.supports_section_edit?
@section = params[:section].to_i
@section_hash = params[:section_hash]
@content.text = Redmine::WikiFormatting.formatter.new(@content.text).update_section(params[:section].to_i, @text, @section_hash)
else
@content.version = params[:content][:version]
@content.version = content_params[:version] if content_params[:version]
@content.text = @text
end
@content.author = User.current
@ -163,17 +166,38 @@ class WikiController < ApplicationController
attachments = Attachment.attach_files(@page, params[:attachments])
render_attachment_warning_if_needed(@page)
call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
redirect_to :action => 'show', :project_id => @project, :id => @page.title
respond_to do |format|
format.html { redirect_to :action => 'show', :project_id => @project, :id => @page.title }
format.api {
if was_new_page
render :action => 'show', :status => :created, :location => url_for(:controller => 'wiki', :action => 'show', :project_id => @project, :id => @page.title)
else
render :action => 'edit'
render_api_ok
end
}
end
else
respond_to do |format|
format.html { render :action => 'edit' }
format.api { render_validation_errors(@content) }
end
end
rescue ActiveRecord::StaleObjectError, Redmine::WikiFormatting::StaleSectionError
# Optimistic locking exception
respond_to do |format|
format.html {
flash.now[:error] = l(:notice_locking_conflict)
render :action => 'edit'
}
format.api { render_api_head :conflict }
end
rescue ActiveRecord::RecordNotSaved
render :action => 'edit'
respond_to do |format|
format.html { render :action => 'edit' }
format.api { render_validation_errors(@content) }
end
end
# rename a page

View File

@ -57,7 +57,7 @@ class WikiPage < ActiveRecord::Base
# Wiki pages that are protected by default
DEFAULT_PROTECTED_PAGES = %w(sidebar)
safe_attributes 'parent_id',
safe_attributes 'parent_id', 'parent_title',
:if => lambda {|page, user| page.new_record? || user.allowed_to?(:rename_wiki_pages, page.project)}
def initialize(attributes=nil, *args)

View File

@ -28,7 +28,7 @@ class ApiTest::WikiPagesTest < ActionController::IntegrationTest
test "GET /projects/:project_id/wiki/index.xml should return wiki pages" do
get '/projects/ecookbook/wiki/index.xml'
assert_response :success
assert_response 200
assert_equal 'application/xml', response.content_type
assert_select 'wiki_pages[type=array]' do
assert_select 'wiki_page', :count => Wiki.find(1).pages.count
@ -38,12 +38,16 @@ class ApiTest::WikiPagesTest < ActionController::IntegrationTest
assert_select 'created_on'
assert_select 'updated_on'
end
assert_select 'wiki_page' do
assert_select 'title', :text => 'Page_with_an_inline_image'
assert_select 'parent[title=?]', 'CookBook_documentation'
end
end
end
test "GET /projects/:project_id/wiki/:title.xml should return wiki page" do
get '/projects/ecookbook/wiki/CookBook_documentation.xml'
assert_response :success
assert_response 200
assert_equal 'application/xml', response.content_type
assert_select 'wiki_page' do
assert_select 'title', :text => 'CookBook_documentation'
@ -63,7 +67,7 @@ class ApiTest::WikiPagesTest < ActionController::IntegrationTest
test "GET /projects/:project_id/wiki/:title/:version.xml should return wiki page version" do
get '/projects/ecookbook/wiki/CookBook_documentation/2.xml'
assert_response :success
assert_response 200
assert_equal 'application/xml', response.content_type
assert_select 'wiki_page' do
assert_select 'title', :text => 'CookBook_documentation'
@ -82,4 +86,83 @@ class ApiTest::WikiPagesTest < ActionController::IntegrationTest
assert_response 401
assert_equal 'application/xml', response.content_type
end
test "PUT /projects/:project_id/wiki/:title.xml should update wiki page" do
assert_no_difference 'WikiPage.count' do
assert_difference 'WikiContent::Version.count' do
put '/projects/ecookbook/wiki/CookBook_documentation.xml',
{:wiki_page => {:text => 'New content from API', :comments => 'API update'}},
credentials('jsmith')
assert_response 200
end
end
page = WikiPage.find(1)
assert_equal 'New content from API', page.content.text
assert_equal 4, page.content.version
assert_equal 'API update', page.content.comments
assert_equal 'jsmith', page.content.author.login
end
test "PUT /projects/:project_id/wiki/:title.xml with current versino should update wiki page" do
assert_no_difference 'WikiPage.count' do
assert_difference 'WikiContent::Version.count' do
put '/projects/ecookbook/wiki/CookBook_documentation.xml',
{:wiki_page => {:text => 'New content from API', :comments => 'API update', :version => '3'}},
credentials('jsmith')
assert_response 200
end
end
page = WikiPage.find(1)
assert_equal 'New content from API', page.content.text
assert_equal 4, page.content.version
assert_equal 'API update', page.content.comments
assert_equal 'jsmith', page.content.author.login
end
test "PUT /projects/:project_id/wiki/:title.xml with stale version should respond with 409" do
assert_no_difference 'WikiPage.count' do
assert_no_difference 'WikiContent::Version.count' do
put '/projects/ecookbook/wiki/CookBook_documentation.xml',
{:wiki_page => {:text => 'New content from API', :comments => 'API update', :version => '2'}},
credentials('jsmith')
assert_response 409
end
end
end
test "PUT /projects/:project_id/wiki/:title.xml should create the page if it does not exist" do
assert_difference 'WikiPage.count' do
assert_difference 'WikiContent::Version.count' do
put '/projects/ecookbook/wiki/New_page_from_API.xml',
{:wiki_page => {:text => 'New content from API', :comments => 'API create'}},
credentials('jsmith')
assert_response 201
end
end
page = WikiPage.order('id DESC').first
assert_equal 'New_page_from_API', page.title
assert_equal 'New content from API', page.content.text
assert_equal 1, page.content.version
assert_equal 'API create', page.content.comments
assert_equal 'jsmith', page.content.author.login
assert_nil page.parent
end
test "PUT /projects/:project_id/wiki/:title.xml with parent" do
assert_difference 'WikiPage.count' do
assert_difference 'WikiContent::Version.count' do
put '/projects/ecookbook/wiki/New_subpage_from_API.xml',
{:wiki_page => {:parent_title => 'CookBook_documentation', :text => 'New content from API', :comments => 'API create'}},
credentials('jsmith')
assert_response 201
end
end
page = WikiPage.order('id DESC').first
assert_equal 'New_subpage_from_API', page.title
assert_equal WikiPage.find(1), page.parent
end
end

View File

@ -33,16 +33,6 @@ class RoutingWikiTest < ActionController::IntegrationTest
{ :controller => 'wiki', :action => 'show', :project_id => '567',
:id => 'lalala', :format => 'pdf' }
)
assert_routing(
{ :method => 'get', :path => "/projects/567/wiki/lalala.xml" },
{ :controller => 'wiki', :action => 'show', :project_id => '567',
:id => 'lalala', :format => 'xml' }
)
assert_routing(
{ :method => 'get', :path => "/projects/567/wiki/lalala.json" },
{ :controller => 'wiki', :action => 'show', :project_id => '567',
:id => 'lalala', :format => 'json' }
)
assert_routing(
{ :method => 'get', :path => "/projects/1/wiki/CookBook_documentation/diff" },
{ :controller => 'wiki', :action => 'diff', :project_id => '1',
@ -53,16 +43,6 @@ class RoutingWikiTest < ActionController::IntegrationTest
{ :controller => 'wiki', :action => 'show', :project_id => '1',
:id => 'CookBook_documentation', :version => '2' }
)
assert_routing(
{ :method => 'get', :path => "/projects/1/wiki/CookBook_documentation/2.xml" },
{ :controller => 'wiki', :action => 'show', :project_id => '1',
:id => 'CookBook_documentation', :version => '2', :format => 'xml' }
)
assert_routing(
{ :method => 'get', :path => "/projects/1/wiki/CookBook_documentation/2.json" },
{ :controller => 'wiki', :action => 'show', :project_id => '1',
:id => 'CookBook_documentation', :version => '2', :format => 'json' }
)
assert_routing(
{ :method => 'get', :path => "/projects/1/wiki/CookBook_documentation/2/diff" },
{ :controller => 'wiki', :action => 'diff', :project_id => '1',
@ -92,14 +72,6 @@ class RoutingWikiTest < ActionController::IntegrationTest
{ :method => 'get', :path => "/projects/567/wiki/index" },
{ :controller => 'wiki', :action => 'index', :project_id => '567' }
)
assert_routing(
{ :method => 'get', :path => "/projects/567/wiki/index.xml" },
{ :controller => 'wiki', :action => 'index', :project_id => '567', :format => 'xml' }
)
assert_routing(
{ :method => 'get', :path => "/projects/567/wiki/index.json" },
{ :controller => 'wiki', :action => 'index', :project_id => '567', :format => 'json' }
)
end
def test_wiki_resources
@ -156,4 +128,45 @@ class RoutingWikiTest < ActionController::IntegrationTest
:id => 'ladida', :version => '3' }
)
end
def test_api
assert_routing(
{ :method => 'get', :path => "/projects/567/wiki/my_page.xml" },
{ :controller => 'wiki', :action => 'show', :project_id => '567',
:id => 'my_page', :format => 'xml' }
)
assert_routing(
{ :method => 'get', :path => "/projects/567/wiki/my_page.json" },
{ :controller => 'wiki', :action => 'show', :project_id => '567',
:id => 'my_page', :format => 'json' }
)
assert_routing(
{ :method => 'get', :path => "/projects/1/wiki/CookBook_documentation/2.xml" },
{ :controller => 'wiki', :action => 'show', :project_id => '1',
:id => 'CookBook_documentation', :version => '2', :format => 'xml' }
)
assert_routing(
{ :method => 'get', :path => "/projects/1/wiki/CookBook_documentation/2.json" },
{ :controller => 'wiki', :action => 'show', :project_id => '1',
:id => 'CookBook_documentation', :version => '2', :format => 'json' }
)
assert_routing(
{ :method => 'get', :path => "/projects/567/wiki/index.xml" },
{ :controller => 'wiki', :action => 'index', :project_id => '567', :format => 'xml' }
)
assert_routing(
{ :method => 'get', :path => "/projects/567/wiki/index.json" },
{ :controller => 'wiki', :action => 'index', :project_id => '567', :format => 'json' }
)
assert_routing(
{ :method => 'put', :path => "/projects/567/wiki/my_page.xml" },
{ :controller => 'wiki', :action => 'update', :project_id => '567',
:id => 'my_page', :format => 'xml' }
)
assert_routing(
{ :method => 'put', :path => "/projects/567/wiki/my_page.json" },
{ :controller => 'wiki', :action => 'update', :project_id => '567',
:id => 'my_page', :format => 'json' }
)
end
end