Project

General

Profile

Feature #14830 ยป add_rest_api_for_attaching_file_to_WIKI_page_trunk_r15317.patch

Takenori TAKAKI, 2016-04-11 09:02

View differences:

app/controllers/wiki_controller.rb
152 152
    @content.author = User.current
153 153

  
154 154
    if @page.save_with_content(@content)
155
      attachments = Attachment.attach_files(@page, params[:attachments])
155
      attachments = Attachment.attach_files(@page, params[:attachments] || (params[:wiki_page] && params[:wiki_page][:uploads]))
156 156
      render_attachment_warning_if_needed(@page)
157 157
      call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
158 158

  
test/integration/api_test/wiki_pages_test.rb
164 164
    assert_nil page.parent
165 165
  end
166 166

  
167
  test "PUT /projects/:project_id/wiki/:title.json with attachment via params[:attachments]" do
168
    set_tmp_attachments_directory
169
    attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
170
    assert_difference 'WikiPage.count' do
171
      assert_difference 'WikiContent::Version.count' do
172
        put '/projects/ecookbook/wiki/New_page_from_API.json',
173
            {:wiki_page => {:text => 'New content from API with Attachments', :comments => 'API create with Attachments'},
174
             :attachments => [:token => attachment.token, :filename => 'testfile.txt', :content_type => "text/plain"]},
175
            credentials('jsmith')
176
        assert_response 201
177
      end
178
    end
179

  
180
    page = WikiPage.order('id DESC').first
181
    assert_equal 'New_page_from_API', page.title
182
    assert_include attachment, page.attachments
183
    assert_equal attachment.filename, page.attachments.first.filename
184
  end
185

  
186
  test "PUT /projects/:project_id/wiki/:title.xml with attachment via params[:wiki_page][:uploads]" do
187
    set_tmp_attachments_directory
188
    attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
189
    assert_difference 'WikiPage.count' do
190
      assert_difference 'WikiContent::Version.count' do
191
        put '/projects/ecookbook/wiki/New_page_from_API.xml',
192
            {:wiki_page => {:text => 'New content from API with Attachments', :comments => 'API create with Attachments',
193
                            :uploads => [:token => attachment.token, :filename => 'testfile.txt', :content_type => "text/plain"]}},
194
            credentials('jsmith')
195
        assert_response 201
196
      end
197
    end
198

  
199
    page = WikiPage.order('id DESC').first
200
    assert_equal 'New_page_from_API', page.title
201
    assert_include attachment, page.attachments
202
    assert_equal attachment.filename, page.attachments.first.filename
203
  end
204

  
167 205
  test "PUT /projects/:project_id/wiki/:title.xml with parent" do
168 206
    assert_difference 'WikiPage.count' do
169 207
      assert_difference 'WikiContent::Version.count' do
    (1-1/1)