comparison test/integration/api_test/attachments_test.rb @ 1115:433d4f72a19b redmine-2.2

Update to Redmine SVN revision 11137 on 2.2-stable branch
author Chris Cannam
date Mon, 07 Jan 2013 12:01:42 +0000
parents cbb26bc654de
children 622f24f53b42
comparison
equal deleted inserted replaced
929:5f33065ddc4b 1115:433d4f72a19b
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
28 :workflows, 28 :workflows,
29 :attachments 29 :attachments
30 30
31 def setup 31 def setup
32 Setting.rest_api_enabled = '1' 32 Setting.rest_api_enabled = '1'
33 Attachment.storage_path = "#{Rails.root}/test/fixtures/files" 33 set_fixtures_attachments_directory
34 end 34 end
35 35
36 context "/attachments/:id" do 36 def teardown
37 context "GET" do 37 set_tmp_attachments_directory
38 should "return the attachment" do 38 end
39 get '/attachments/7.xml', {}, :authorization => credentials('jsmith') 39
40 assert_response :success 40 test "GET /attachments/:id.xml should return the attachment" do
41 assert_equal 'application/xml', @response.content_type 41 get '/attachments/7.xml', {}, credentials('jsmith')
42 assert_tag :tag => 'attachment', 42 assert_response :success
43 :child => { 43 assert_equal 'application/xml', @response.content_type
44 :tag => 'id', 44 assert_tag :tag => 'attachment',
45 :content => '7', 45 :child => {
46 :sibling => { 46 :tag => 'id',
47 :tag => 'filename', 47 :content => '7',
48 :content => 'archive.zip', 48 :sibling => {
49 :sibling => { 49 :tag => 'filename',
50 :tag => 'content_url', 50 :content => 'archive.zip',
51 :content => 'http://www.example.com/attachments/download/7/archive.zip' 51 :sibling => {
52 } 52 :tag => 'content_url',
53 } 53 :content => 'http://www.example.com/attachments/download/7/archive.zip'
54 } 54 }
55 end 55 }
56 }
57 end
56 58
57 should "deny access without credentials" do 59 test "GET /attachments/:id.xml should deny access without credentials" do
58 get '/attachments/7.xml' 60 get '/attachments/7.xml'
59 assert_response 401 61 assert_response 401
60 set_tmp_attachments_directory 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/
61 end 147 end
62 end 148 end
63 end 149 end
64
65 context "/attachments/download/:id/:filename" do
66 context "GET" do
67 should "return the attachment content" do
68 get '/attachments/download/7/archive.zip',
69 {}, :authorization => credentials('jsmith')
70 assert_response :success
71 assert_equal 'application/octet-stream', @response.content_type
72 set_tmp_attachments_directory
73 end
74
75 should "deny access without credentials" do
76 get '/attachments/download/7/archive.zip'
77 assert_response 302
78 set_tmp_attachments_directory
79 end
80 end
81 end
82
83 def credentials(user, password=nil)
84 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
85 end
86 end 150 end