annotate .svn/pristine/ff/ff1c47ab3a4a85cdc878d408e530d4ba3db0f578.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 require File.expand_path('../../../test_helper', __FILE__)
Chris@1295 19
Chris@1295 20 class Redmine::ApiTest::AttachmentsTest < Redmine::ApiTest::Base
Chris@1295 21 fixtures :projects, :trackers, :issue_statuses, :issues,
Chris@1295 22 :enumerations, :users, :issue_categories,
Chris@1295 23 :projects_trackers,
Chris@1295 24 :roles,
Chris@1295 25 :member_roles,
Chris@1295 26 :members,
Chris@1295 27 :enabled_modules,
Chris@1295 28 :attachments
Chris@1295 29
Chris@1295 30 def setup
Chris@1295 31 Setting.rest_api_enabled = '1'
Chris@1295 32 set_fixtures_attachments_directory
Chris@1295 33 end
Chris@1295 34
Chris@1295 35 def teardown
Chris@1295 36 set_tmp_attachments_directory
Chris@1295 37 end
Chris@1295 38
Chris@1295 39 test "GET /attachments/:id.xml should return the attachment" do
Chris@1295 40 get '/attachments/7.xml', {}, credentials('jsmith')
Chris@1295 41 assert_response :success
Chris@1295 42 assert_equal 'application/xml', @response.content_type
Chris@1295 43 assert_tag :tag => 'attachment',
Chris@1295 44 :child => {
Chris@1295 45 :tag => 'id',
Chris@1295 46 :content => '7',
Chris@1295 47 :sibling => {
Chris@1295 48 :tag => 'filename',
Chris@1295 49 :content => 'archive.zip',
Chris@1295 50 :sibling => {
Chris@1295 51 :tag => 'content_url',
Chris@1295 52 :content => 'http://www.example.com/attachments/download/7/archive.zip'
Chris@1295 53 }
Chris@1295 54 }
Chris@1295 55 }
Chris@1295 56 end
Chris@1295 57
Chris@1295 58 test "GET /attachments/:id.xml should deny access without credentials" do
Chris@1295 59 get '/attachments/7.xml'
Chris@1295 60 assert_response 401
Chris@1295 61 set_tmp_attachments_directory
Chris@1295 62 end
Chris@1295 63
Chris@1295 64 test "GET /attachments/download/:id/:filename should return the attachment content" do
Chris@1295 65 get '/attachments/download/7/archive.zip', {}, credentials('jsmith')
Chris@1295 66 assert_response :success
Chris@1295 67 assert_equal 'application/octet-stream', @response.content_type
Chris@1295 68 set_tmp_attachments_directory
Chris@1295 69 end
Chris@1295 70
Chris@1295 71 test "GET /attachments/download/:id/:filename should deny access without credentials" do
Chris@1295 72 get '/attachments/download/7/archive.zip'
Chris@1295 73 assert_response 302
Chris@1295 74 set_tmp_attachments_directory
Chris@1295 75 end
Chris@1295 76
Chris@1295 77 test "POST /uploads.xml should return the token" do
Chris@1295 78 set_tmp_attachments_directory
Chris@1295 79 assert_difference 'Attachment.count' do
Chris@1295 80 post '/uploads.xml', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
Chris@1295 81 assert_response :created
Chris@1295 82 assert_equal 'application/xml', response.content_type
Chris@1295 83 end
Chris@1295 84
Chris@1295 85 xml = Hash.from_xml(response.body)
Chris@1295 86 assert_kind_of Hash, xml['upload']
Chris@1295 87 token = xml['upload']['token']
Chris@1295 88 assert_not_nil token
Chris@1295 89
Chris@1295 90 attachment = Attachment.first(:order => 'id DESC')
Chris@1295 91 assert_equal token, attachment.token
Chris@1295 92 assert_nil attachment.container
Chris@1295 93 assert_equal 2, attachment.author_id
Chris@1295 94 assert_equal 'File content'.size, attachment.filesize
Chris@1295 95 assert attachment.content_type.blank?
Chris@1295 96 assert attachment.filename.present?
Chris@1295 97 assert_match /\d+_[0-9a-z]+/, attachment.diskfile
Chris@1295 98 assert File.exist?(attachment.diskfile)
Chris@1295 99 assert_equal 'File content', File.read(attachment.diskfile)
Chris@1295 100 end
Chris@1295 101
Chris@1295 102 test "POST /uploads.json should return the token" do
Chris@1295 103 set_tmp_attachments_directory
Chris@1295 104 assert_difference 'Attachment.count' do
Chris@1295 105 post '/uploads.json', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
Chris@1295 106 assert_response :created
Chris@1295 107 assert_equal 'application/json', response.content_type
Chris@1295 108 end
Chris@1295 109
Chris@1295 110 json = ActiveSupport::JSON.decode(response.body)
Chris@1295 111 assert_kind_of Hash, json['upload']
Chris@1295 112 token = json['upload']['token']
Chris@1295 113 assert_not_nil token
Chris@1295 114
Chris@1295 115 attachment = Attachment.first(:order => 'id DESC')
Chris@1295 116 assert_equal token, attachment.token
Chris@1295 117 end
Chris@1295 118
Chris@1295 119 test "POST /uploads.xml should accept :filename param as the attachment filename" do
Chris@1295 120 set_tmp_attachments_directory
Chris@1295 121 assert_difference 'Attachment.count' do
Chris@1295 122 post '/uploads.xml?filename=test.txt', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
Chris@1295 123 assert_response :created
Chris@1295 124 end
Chris@1295 125
Chris@1295 126 attachment = Attachment.order('id DESC').first
Chris@1295 127 assert_equal 'test.txt', attachment.filename
Chris@1295 128 assert_match /_test\.txt$/, attachment.diskfile
Chris@1295 129 end
Chris@1295 130
Chris@1295 131 test "POST /uploads.xml should not accept other content types" do
Chris@1295 132 set_tmp_attachments_directory
Chris@1295 133 assert_no_difference 'Attachment.count' do
Chris@1295 134 post '/uploads.xml', 'PNG DATA', {"CONTENT_TYPE" => 'image/png'}.merge(credentials('jsmith'))
Chris@1295 135 assert_response 406
Chris@1295 136 end
Chris@1295 137 end
Chris@1295 138
Chris@1295 139 test "POST /uploads.xml should return errors if file is too big" do
Chris@1295 140 set_tmp_attachments_directory
Chris@1295 141 with_settings :attachment_max_size => 1 do
Chris@1295 142 assert_no_difference 'Attachment.count' do
Chris@1295 143 post '/uploads.xml', ('x' * 2048), {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
Chris@1295 144 assert_response 422
Chris@1295 145 assert_tag 'error', :content => /exceeds the maximum allowed file size/
Chris@1295 146 end
Chris@1295 147 end
Chris@1295 148 end
Chris@1295 149 end