@@ -37,94 +37,84 class ApiTest::AttachmentsTest < ActionController::IntegrationTest | |||
|
37 | 37 | set_tmp_attachments_directory |
|
38 | 38 | end |
|
39 | 39 | |
|
40 | context "/attachments/:id" do | |
|
41 | context "GET" do | |
|
42 | should "return the attachment" do | |
|
43 | get '/attachments/7.xml', {}, credentials('jsmith') | |
|
44 | assert_response :success | |
|
45 | assert_equal 'application/xml', @response.content_type | |
|
46 |
|
|
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
|
50 | :sibling => { | |
|
51 | :tag => 'filename', | |
|
52 |
|
|
|
53 | :sibling => { | |
|
54 | :tag => 'content_url', | |
|
55 | :content => 'http://www.example.com/attachments/download/7/archive.zip' | |
|
56 | } | |
|
57 | } | |
|
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' | |
|
58 | 54 | } |
|
59 |
|
|
|
55 | } | |
|
56 | } | |
|
57 | end | |
|
60 | 58 | |
|
61 |
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
|
65 | end | |
|
66 | end | |
|
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 | |
|
67 | 63 | end |
|
68 | 64 | |
|
69 |
|
|
|
70 | context "GET" do | |
|
71 | should "return the attachment content" do | |
|
72 | get '/attachments/download/7/archive.zip', {}, credentials('jsmith') | |
|
73 | assert_response :success | |
|
74 | assert_equal 'application/octet-stream', @response.content_type | |
|
75 | set_tmp_attachments_directory | |
|
76 | end | |
|
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 | |
|
77 | 71 | |
|
78 |
|
|
|
79 |
|
|
|
80 |
|
|
|
81 |
|
|
|
82 | end | |
|
83 | end | |
|
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 | |
|
84 | 76 | end |
|
85 | 77 | |
|
86 | context "POST /uploads" do | |
|
87 | should "return the token" do | |
|
88 | set_tmp_attachments_directory | |
|
89 | assert_difference 'Attachment.count' do | |
|
90 | post '/uploads.xml', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith')) | |
|
91 | assert_response :created | |
|
92 | assert_equal 'application/xml', response.content_type | |
|
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 | |
|
93 | 85 | |
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
|
86 | xml = Hash.from_xml(response.body) | |
|
87 | assert_kind_of Hash, xml['upload'] | |
|
88 | token = xml['upload']['token'] | |
|
89 | assert_not_nil token | |
|
98 | 90 | |
|
99 |
|
|
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
|
110 | end | |
|
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 | |
|
111 | 102 | |
|
112 |
|
|
|
113 |
|
|
|
114 |
|
|
|
115 |
|
|
|
116 |
|
|
|
117 | end | |
|
103 | test "POST /uploads.xml should not accept other content types" do | |
|
104 | set_tmp_attachments_directory | |
|
105 | assert_no_difference 'Attachment.count' do | |
|
106 | post '/uploads.xml', 'PNG DATA', {"CONTENT_TYPE" => 'image/png'}.merge(credentials('jsmith')) | |
|
107 | assert_response 406 | |
|
118 | 108 | end |
|
109 | end | |
|
119 | 110 | |
|
120 |
|
|
|
121 |
|
|
|
122 |
|
|
|
123 |
|
|
|
124 |
|
|
|
125 |
|
|
|
126 |
|
|
|
127 | end | |
|
111 | test "POST /uploads.xml should return errors if file is too big" do | |
|
112 | set_tmp_attachments_directory | |
|
113 | with_settings :attachment_max_size => 1 do | |
|
114 | assert_no_difference 'Attachment.count' do | |
|
115 | post '/uploads.xml', ('x' * 2048), {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith')) | |
|
116 | assert_response 422 | |
|
117 | assert_tag 'error', :content => /exceeds the maximum allowed file size/ | |
|
128 | 118 | end |
|
129 | 119 | end |
|
130 | 120 | end |
General Comments 0
You need to be logged in to leave comments.
Login now