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