annotate .svn/pristine/77/779ea9e70e281cf8e8422126215401ab50f4c8e4.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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