public
Description: Treat an ActiveRecord model as a file attachment, storing its patch, size, content type, etc.
Homepage: http://weblog.techno-weenie.net
Clone URL: git://github.com/technoweenie/attachment_fu.git
Click here to lend your support to: attachment_fu and make a donation at www.pledgie.com !
attachment_fu / spec / attachment_fu_spec.rb
100644 275 lines (224 sloc) 8.609 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
require File.dirname(__FILE__) + '/spec_helper'
 
module AttachmentFu
  class BasicAsset < ActiveRecord::Base
    is_faux_attachment
  end
 
  class QueuedAsset < ActiveRecord::Base
    is_faux_attachment :queued => true
  end
 
  describe "AttachmentFu" do
    describe "pending creation" do
      before do
        @asset = BasicAsset.new(:content_type => 'application/x-ruby', :temp_path => __FILE__)
      end
 
      it "has nil #full_filename" do
        @asset.full_filename.should be_nil
      end
 
      it "has nil #partitioned_path" do
        @asset.partitioned_path.should == nil
      end
    end
 
    describe "being processed" do
      before do
        @file = File.join(File.dirname(__FILE__), 'guinea_pig.rb')
        FileUtils.cp __FILE__, @file
      end
 
      after { @asset.destroy }
 
      it "attempts to process the attachment" do
        @asset = BasicAsset.create!(:content_type => 'application/x-ruby', :temp_path => @file)
        @asset.should_not be_queued
      end
      
      it "skips processing the queued attachment" do
        @asset = QueuedAsset.create!(:content_type => 'application/x-ruby', :temp_path => @file)
        @asset.should be_queued
      end
    end
    
    describe "being created" do
      before :all do
        @file = File.join(File.dirname(__FILE__), 'guinea_pig.rb')
        FileUtils.cp __FILE__, @file
 
        @asset = BasicAsset.create!(:content_type => 'application/x-ruby', :temp_path => @file)
      end
 
      after :all do
        @asset.destroy
      end
 
      it "stores asset in AttachmentFu root_path" do
        @asset.full_filename.should == File.expand_path(File.join(AttachmentFu.root_path, "public/afu_spec_assets/#{@asset.partitioned_path * '/'}/guinea_pig.rb"))
      end
 
      it "creates full_path from record id and attachment_path" do
        @asset.full_path.should == File.expand_path(File.join(AttachmentFu.root_path, "public/afu_spec_assets/#{@asset.partitioned_path * '/'}"))
        @asset.full_path("foo", "bar").should == File.expand_path(File.join(AttachmentFu.root_path, "public/afu_spec_assets/#{@asset.partitioned_path * '/'}/foo/bar"))
      end
 
      it "creates public_path from record id and attachment_path" do
        @asset.public_path.should == "/afu_spec_assets/#{@asset.partitioned_path * '/'}"
        @asset.public_path("foo", "bar").should == "/afu_spec_assets/#{@asset.partitioned_path * '/'}/foo/bar"
      end
 
      it "creates partitioned path from the record id" do
        @asset.partitioned_path.each { |piece| piece.should match(/^\d{4}$/) }
        @asset.partitioned_path.join.to_i.should == @asset.id
      end
 
      it "moves temp_path to new location" do
        File.exist?(@asset.full_filename).should == true
      end
 
      it "removes old temp_path location" do
        File.exist?(@file).should == false
      end
 
      it "clears #temp_path" do
        @asset.temp_path.should be_nil
      end
    end
    
    describe "being deleted" do
      before do
        @file = File.join(File.dirname(__FILE__), 'guinea_pig.rb')
        FileUtils.cp __FILE__, @file
 
        @asset = BasicAsset.create!(:content_type => 'application/x-ruby', :temp_path => @file)
        @dir = File.dirname(@asset.full_filename)
      end
      
      after do
        FileUtils.rm_rf AttachmentFu.root_path
      end
      
      it "removes the file" do
        @asset.destroy
        File.exist?(@asset.full_filename).should == false
      end
      
      (1..4).each do |i|
        it "deletes empty path ##{i}" do
          @asset.destroy
          dir_to_check = @dir.split("/")[0..-i] * "/"
          fail "#{dir_to_check.inspect} still exists" if File.directory?(dir_to_check)
        end
 
        it "keeps non-empty path ##{i}" do
          dir_to_check = @dir.split("/")[0..-i] * "/"
          FileUtils.touch File.join(dir_to_check, 'savior')
          @asset.destroy
          fail "#{dir_to_check.inspect} is deleted" unless File.directory?(dir_to_check)
        end
      end
      
      it "keeps AttachmentFu.root_path" do
        @asset.destroy
        dir_to_check = @dir.split("/")[0..-5] * "/"
        fail "#{dir_to_check.inspect} is deleted" unless File.directory?(dir_to_check)
      end
    end
    
    describe "being uploaded" do
      before do
        @asset = BasicAsset.new
        @file = __FILE__
        @file.stub!(:size).and_return(File.size(__FILE__))
        @file.stub!(:content_type).and_return("application/x-ruby")
        @file.stub!(:original_filename).and_return("/Users/rickybobby/shake_and_bake.rb")
        @file.stub!(:read).and_return { IO.read(__FILE__) }
      end
      
      describe "with temp file" do
        it "sets temp_path as string path to file" do
          @asset.uploaded_data = @file
          @asset.temp_path.should == __FILE__
          @asset.temp_path.size.should == File.size(__FILE__)
        end
 
        it "sets content_type" do
          @asset.uploaded_data = @file
          @asset.content_type.should == @file.content_type
        end
      
        it "sets filename" do
          @asset.uploaded_data = @file
          @asset.filename.should == @file.original_filename
        end
        
        it "ignores nil value" do
          @asset.uploaded_data = nil
          @asset.content_type.should be_nil
        end
        
        it "ignores uploaded file with size=0" do
          @file.stub!(:size).and_return(0)
          @asset.uploaded_data = @file
          @asset.content_type.should be_nil
        end
      end
      
      describe "with IO" do
        before do
          @file.stub!(:rewind)
        end
        
        it "sets temp_path as Tempfile" do
          @asset.uploaded_data = @file
          @asset.temp_path.class.should == Tempfile
          @asset.temp_path.size.should == File.size(__FILE__)
        end
 
        it "sets content_type" do
          @asset.uploaded_data = @file
          @asset.content_type.should == @file.content_type
        end
      
        it "sets filename" do
          @asset.uploaded_data = @file
          @asset.filename.should == @file.original_filename
        end
        
        it "ignores nil value" do
          @asset.uploaded_data = nil
          @asset.content_type.should be_nil
        end
        
        it "ignores uploaded file with size=0" do
          @file.stub!(:size).and_return(0)
          @asset.uploaded_data = @file
          @asset.content_type.should be_nil
        end
      end
    end
 
    describe "setting temp_path" do
      describe "with a String" do
        before { @asset = BasicAsset.new(:temp_path => __FILE__) }
        it "guesses filename" do
          @asset.filename.should == File.basename(__FILE__)
        end
        
        it "sets #size" do
          @asset.size.should == File.size(__FILE__)
        end
      end
 
      describe "with a Pathname" do
        before { @asset = BasicAsset.new(:temp_path => Pathname.new(__FILE__)) }
        it "guesses filename" do
          @asset.filename.should == File.basename(__FILE__)
        end
        
        it "sets #size" do
          @asset.size.should == File.size(__FILE__)
        end
      end
      
      describe "with a Tempfile" do
        before do
          @tmp = Tempfile.new File.basename(__FILE__)
          @tmp.write IO.read(__FILE__)
          @asset = BasicAsset.new(:temp_path => @tmp)
        end
 
        it "guesses filename" do
          name, ext = File.basename(__FILE__).split(".")
          @asset.filename.should include(name) # tempfile adds extra characters to the end
          @asset.filename.should match(/\.rb$/)
        end
        
        it "sets #size" do
          @asset.size.should == File.size(__FILE__)
        end
      end
    end
    
    describe "being subclassed" do
      before do
        @sub = Class.new(BasicAsset)
      end
 
      it "inherits superclass #attachment_path" do
        @sub.attachment_path.should == BasicAsset.attachment_path
      end
 
      it "inherits superclass #attachment_path after explicit #is_attachment call" do
        @sub.is_attachment
        @sub.attachment_path.should == BasicAsset.attachment_path
      end
      
      it "overwrites superclass #attachment_path with :path" do
        @sub.is_attachment :path => 'foobar'
        @sub.attachment_path.should_not == BasicAsset.attachment_path
        @sub.attachment_path.should == 'foobar'
      end
    end
 
    before :all do
      BasicAsset.setup_spec_env
    end
    
    after :all do
      BasicAsset.drop_spec_env
      FileUtils.rm_rf AttachmentFu.root_path
    end
  end
end