forked from tcocca/active_pdftk
/
wrapper_spec.rb
450 lines (387 loc) · 19.9 KB
/
wrapper_spec.rb
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
require 'spec_helper'
inputs = [:path, :hash, :file, :tempfile, :stringio]
outputs = [:path, :file, :tempfile, :stringio, :nil]
def get_input(input_type, file_name = 'spec.fields.pdf')
case input_type
when :path
path_to_pdf(file_name)
when :hash
{path_to_pdf(file_name) => nil}
when :file
File.new(path_to_pdf(file_name), 'rb')
when :tempfile
t = Tempfile.new('input.spec')
t.write(File.read(path_to_pdf(file_name)))
t
when :stringio
StringIO.new(File.read(path_to_pdf(file_name)))
end
end
def get_output(output_type)
case output_type
when :path
path_to_pdf('output.spec')
when :file
File.new(path_to_pdf('output.spec'), 'wb+')
when :tempfile
Tempfile.new('output.spec')
when :stringio
StringIO.new()
when :nil
nil
end
end
def map_output_type(output_specified)
case output_specified
when :path
String
when :file
File
when :tempfile
Tempfile
when :stringio, :nil
StringIO
end
end
describe ActivePdftk::Wrapper do
before(:all) { @pdftk = ActivePdftk::Wrapper.new }
context "new" do
it "should instantiate the object." do
@pdftk.should be_an_instance_of(ActivePdftk::Wrapper)
end
it "should pass the defaults statements to the call instance." do
path = ActivePdftk::Call.new.locate_pdftk
@pdftk_opt = ActivePdftk::Wrapper.new(:path => path, :operation => {:fill_form => 'a.fdf'}, :options => { :flatten => false, :owner_pw => 'bar', :user_pw => 'baz', :encrypt => :'40bit'})
@pdftk_opt.default_statements.should == {:path => path, :operation => {:fill_form => 'a.fdf'}, :options => { :flatten => false, :owner_pw => 'bar', :user_pw => 'baz', :encrypt => :'40bit'}}
end
end
shared_examples "a command" do
it "should return a #{@output.nil? ? StringIO : @output.class}" do
@call_output.should be_kind_of(@output.nil? ? StringIO : @output.class)
end
it "should return expected data" do
if example.metadata[:genesis] && @output.is_a?(String)
FileUtils.copy_entry(@output, @example_expect.to_s, true, false, true)
elsif example.metadata[:cleanup]
#cleanup_file_content!(File.open(@output, 'r:binary').read).should == cleanup_file_content!(File.open(@example_expect, 'r:binary').read) if @output.is_a?(String) # lets keep this line for debugging purpose.
@call_output.should look_like_the_same_pdf_as(@example_expect)
else
@call_output.should have_the_content_of(@example_expect)
end
end
after(:each) { remove_output(@call_output) }
end
inputs.each do |input_type|
outputs.each do |output_type|
context "(Input:#{input_type}|Output:#{output_type})" do
before :each do
@input = get_input(input_type)
@input.rewind rescue nil # rewind if possible.
@output = get_output(output_type)
end
describe "#dump_data_fields" do
it_behaves_like "a command" do
before(:all) { @example_expect = fixtures_path('dump_data_fields/expect.data_fields') }
before(:each) { @call_output = @pdftk.dump_data_fields(@input, :output => @output) }
end
end
describe "#fill_form" do
it_behaves_like "a command" do
before(:all) { @example_expect = fixtures_path('fill_form/expect.pdf') }
before(:each) { @call_output = @pdftk.fill_form(@input, path_to_pdf('fill_form/spec.fdf'), :output => @output) }
end
it_behaves_like "a command" do
before(:all) { @example_expect = fixtures_path('fill_form/expect.pdf') }
before(:each) { @call_output = @pdftk.fill_form(@input, path_to_pdf('fill_form/spec.xfdf'), :output => @output) }
end
end
describe "#generate_fdf" do
it_behaves_like "a command" do
before(:all) { @example_expect = fixtures_path('generate_fdf/expect.fdf') }
before(:each) { @call_output = @pdftk.generate_fdf(@input,:output => @output) }
end
end
describe "#dump_data" do
it_behaves_like "a command" do
before(:all) { @example_expect = fixtures_path('dump_data/expect.data') }
before(:each) { @call_output = @pdftk.dump_data(@input,:output => @output) }
end
end
describe "#update_info" do
it_behaves_like "a command" do
before(:all) { @example_expect = fixtures_path('update_info/expect.pdf') }
before(:each) { @call_output = @pdftk.update_info(@input, path_to_pdf('update_info/spec.data'), :output => @output) }
end
end
describe "#unpack_files" do
before(:all) { @example_expect = fixtures_path('unpack_files/expect') }
context "to path", :if => output_type == :path do
before(:each) do
@input = get_input(input_type, 'unpack_files/spec.pdf')
@input.rewind rescue nil # rewind if possible.
Dir.mkdir(out_path = fixtures_path('output'))
@output = Dir.new(out_path)
@call_output = @pdftk.unpack_files(@input, @output.path)
end
after(:each) { FileUtils.remove_entry_secure @output.path }
it "should unpack the files" do
@call_output.should == @output.path
fixtures_path('output').should have_the_content_of(@example_expect)
end
end
context "to temporary directory", :if => output_type == :nil do
before(:each) do
@input = get_input(input_type, 'unpack_files/spec.pdf')
@input.rewind rescue nil # rewind if possible.
@call_output = @pdftk.unpack_files(@input, nil)
end
it "should unpack the files" do
@call_output.should == Dir.tmpdir
@example_expect.children(false).each do |file|
(Pathname.new(Dir.tmpdir) + file).should have_the_content_of(@example_expect + file)
end
end
end
end
describe "#attach_files" do
before(:each) do
@call_output = @pdftk.attach_files(@input, fixtures_path('attach_files/expect', true).collect(&:to_s), :output => @output)
end
it "should bind the file in the pdf" do
Dir.mktmpdir do |directory|
@pdftk.unpack_files(@call_output, directory)
Pathname.new(directory).should have_the_content_of(fixtures_path('attach_files/expect'))
end
end
it "should output the correct type" do
@call_output.should be_kind_of(map_output_type(output_type))
end
end
describe "#background", :cleanup => true do
it_behaves_like "a command" do
before(:all) { @example_expect = fixtures_path('background/expect.pdf') }
before(:each) { @call_output = @pdftk.background(get_input(input_type, 'multi.pdf'), path_to_pdf('poly.pdf'), :output => @output) }
end
end
describe "#multibackground", :cleanup => true do
it_behaves_like "a command" do
before(:all) { @example_expect = fixtures_path('multibackground/expect.pdf') }
before(:each) { @call_output = @pdftk.multibackground(get_input(input_type, 'multi.pdf'), path_to_pdf('poly.pdf'), :output => @output) }
end
end
describe "#stamp", :cleanup => true do
it_behaves_like "a command" do
before(:all) { @example_expect = fixtures_path('stamp/expect.pdf') }
before(:each) { @call_output = @pdftk.stamp(get_input(input_type, 'multi.pdf'), path_to_pdf('poly.pdf'), :output => @output) }
end
end
describe "#multistamp", :cleanup => true do
it_behaves_like "a command" do
before(:all) { @example_expect = fixtures_path('multistamp/expect.pdf') }
before(:each) { @call_output = @pdftk.multistamp(get_input(input_type, 'multi.pdf'), path_to_pdf('poly.pdf'), :output => @output) }
end
end
describe "#cat", :cleanup => true do
it_behaves_like "a command" do
before(:all) { @example_expect = fixtures_path('cat/expect.pdf')}
before(:each) { @call_output = @pdftk.cat([{:pdf => path_to_pdf('multi.pdf')}, {:pdf => path_to_pdf('poly.pdf'), :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}], :output => @output) }
end
end
describe "#shuffle", :cleanup => true do
it_behaves_like "a command" do
before(:all) { @example_expect = fixtures_path('shuffle/expect.pdf')}
before(:each) { @call_output = @pdftk.shuffle([{:pdf => path_to_pdf('multi.pdf')}, {:pdf => path_to_pdf('poly.pdf'), :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}], :output => @output) }
end
end
describe "#burst" do
before(:all) { @example_expect = fixtures_path('burst/expect') }
context 'to path', :if => output_type == :path do
before(:each) do
@input = get_input(input_type, 'multi.pdf')
@input.rewind rescue nil # rewind if possible.
Dir.mkdir(out_path = fixtures_path('output'))
@output = Dir.new(out_path)
@call_output = @pdftk.burst(@input, :output => @output.path + '/pg_%04d.pdf')
end
after(:each) { FileUtils.remove_entry_secure @output.path }
it "should file into single pages" do
if example.metadata[:genesis]
FileUtils.copy_entry(@output.path, @example_expect.to_s, true, false, true)
else
@call_output.should == @output.path + '/pg_%04d.pdf'
fixtures_path('output').should look_like_the_same_pdf_as(@example_expect)
end
end
end
context "#to temporary directory", :if => output_type == :nil do
before(:each) do
@input = get_input(input_type, 'multi.pdf')
@input.rewind rescue nil # rewind if possible.
end
it "should file into single pages" do
@pdftk.burst(@input, :output => nil).should == Dir.tmpdir
@example_expect.children(false).each do |file|
(Pathname.new(Dir.tmpdir) + file).should look_like_the_same_pdf_as(@example_expect + file)
FileUtils.remove_file(Pathname.new(Dir.tmpdir) + file)
end
end
it "should put a file in the system tmpdir when no output location given but a page name format given" do
@pdftk.burst(@input, :output => 'page_%02d.pdf').should == 'page_%02d.pdf'
@example_expect.children(false).each do |file|
index = file.basename.to_s.match(/(\d+)/)[0].to_i
(Pathname.new(Dir.tmpdir) + ("page_%02d.pdf" % index)).should look_like_the_same_pdf_as(@example_expect + file)
FileUtils.remove_file(Pathname.new(Dir.tmpdir) + ("page_%02d.pdf" % index))
end
end
end
end
end
end # each outputs
end # each inputs
context "nop" do
it "should call #pdftk on @call" do
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => nil)
@pdftk.nop('a.pdf')
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => nil, :encrypt => :'40bit')
@pdftk.nop('a.pdf', :encrypt => :'40bit')
end
end
context "generate_fdf" do
it "should call #pdftk on @call" do
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => :generate_fdf)
@pdftk.generate_fdf('a.pdf')
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => :generate_fdf, :encrypt => :'40bit')
@pdftk.generate_fdf('a.pdf', :encrypt => :'40bit')
end
end
context "burst" do
it "should call #pdftk on @call" do
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => :burst)
@pdftk.burst('a.pdf')
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => :burst, :options => {:encrypt => :'40bit'})
@pdftk.burst('a.pdf', :options => {:encrypt => :'40bit'})
end
end
context "cat" do
it "should call #pdftk on @call" do
@pdftk.call.should_receive(:pdftk).with(:input => {'a.pdf' => 'foo', 'b.pdf' => nil}, :operation => {:cat => [{:pdf => 'a.pdf'}, {:pdf => 'b.pdf', :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}]})
@pdftk.cat([{:pdf => 'a.pdf', :pass => 'foo'}, {:pdf => 'b.pdf', :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}])
@pdftk.call.should_receive(:pdftk).with(:input => {'a.pdf' => 'foo', 'b.pdf' => nil}, :operation => {:cat => [{:pdf => 'a.pdf'}, {:pdf => 'b.pdf', :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}]}, :output => 'c.pdf')
@pdftk.cat([{:pdf => 'a.pdf', :pass => 'foo'}, {:pdf => 'b.pdf', :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}], :output => 'c.pdf')
end
end
context "shuffle" do
it "should call #pdftk on @call" do
@pdftk.call.should_receive(:pdftk).with(:input => {'a.pdf' => 'foo', 'b.pdf' => nil}, :operation => {:shuffle => [{:pdf => 'a.pdf'}, {:pdf => 'b.pdf', :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}]})
@pdftk.shuffle([{:pdf => 'a.pdf', :pass => 'foo'}, {:pdf => 'b.pdf', :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}])
@pdftk.call.should_receive(:pdftk).with(:input => {'a.pdf' => 'foo', 'b.pdf' => nil}, :operation => {:shuffle => [{:pdf => 'a.pdf'}, {:pdf => 'b.pdf', :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}]}, :output => 'c.pdf')
@pdftk.shuffle([{:pdf => 'a.pdf', :pass => 'foo'}, {:pdf => 'b.pdf', :start => 1, :end => 'end', :orientation => 'N', :pages => 'even'}], :output => 'c.pdf')
end
end
context "dump_data_fields" do
it "should call #pdftk on @call" do
@pdftk.call.stub(:utf8_support?) { false }
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => :dump_data_fields)
@pdftk.dump_data_fields('a.pdf')
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => :dump_data_fields, :output => 'data_fields.txt')
@pdftk.dump_data_fields('a.pdf', :output => 'data_fields.txt')
@pdftk.call.stub(:utf8_support?) { true }
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => :dump_data_fields_utf8)
@pdftk.dump_data_fields('a.pdf')
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => :dump_data_fields_utf8, :output => 'data_fields.txt')
@pdftk.dump_data_fields('a.pdf', :output => 'data_fields.txt')
end
end
context "dump_data" do
it "should call #pdftk on @call" do
@pdftk.call.stub(:utf8_support?) { false }
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => :dump_data)
@pdftk.dump_data('a.pdf')
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => :dump_data, :output => 'data_fields.txt')
@pdftk.dump_data('a.pdf', :output => 'data_fields.txt')
@pdftk.call.stub(:utf8_support?) { true }
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => :dump_data_utf8)
@pdftk.dump_data('a.pdf')
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => :dump_data_utf8, :output => 'data_fields.txt')
@pdftk.dump_data('a.pdf', :output => 'data_fields.txt')
end
end
context "fill_form" do
it "should call #pdftk on @call" do
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:fill_form => 'form.xfdf'})
@pdftk.fill_form('a.pdf', 'form.xfdf')
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:fill_form => 'form.xfdf'}, :encrypt => :'40bit')
@pdftk.fill_form('a.pdf', 'form.xfdf', :encrypt => :'40bit')
end
end
context "update_info" do
it "should call #pdftk on @call" do
@pdftk.call.stub(:utf8_support?) { false }
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:update_info => 'meta.txt'})
@pdftk.update_info('a.pdf', 'meta.txt')
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:update_info => 'meta.txt'}, :encrypt => :'40bit')
@pdftk.update_info('a.pdf', 'meta.txt', :encrypt => :'40bit')
@pdftk.call.stub(:utf8_support?) { true }
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:update_info_utf8 => 'meta.txt'})
@pdftk.update_info('a.pdf', 'meta.txt')
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:update_info_utf8 => 'meta.txt'}, :encrypt => :'40bit')
@pdftk.update_info('a.pdf', 'meta.txt', :encrypt => :'40bit')
end
end
context "attach_files" do
it "should call #pdftk on @call" do
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:attach_files => ['attach.txt']})
@pdftk.attach_files('a.pdf', ['attach.txt'])
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:attach_files => ['attach.txt']}, :encrypt => :'40bit')
@pdftk.attach_files('a.pdf', ['attach.txt'], :encrypt => :'40bit')
end
end
context "unpack_files" do
it "should call #pdftk on @call" do
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => :unpack_files, :output => nil)
@pdftk.unpack_files('a.pdf')
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => :unpack_files, :output => 'test')
@pdftk.unpack_files('a.pdf', 'test')
end
end
context "background" do
it "should call #pdftk on @call" do
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:background => 'b.pdf'})
@pdftk.background('a.pdf', 'b.pdf')
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:multibackground => 'b.pdf'})
@pdftk.background('a.pdf', 'b.pdf', :multi => true)
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:background => 'b.pdf'}, :encrypt => :'40bit')
@pdftk.background('a.pdf', 'b.pdf', :encrypt => :'40bit')
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:multibackground => 'b.pdf'}, :encrypt => :'40bit')
@pdftk.background('a.pdf', 'b.pdf', :encrypt => :'40bit', :multi => true)
end
end
context "multibackground" do
it "should call #pdftk on @call" do
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:multibackground => 'b.pdf'})
@pdftk.multibackground('a.pdf', 'b.pdf')
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:multibackground => 'b.pdf'}, :encrypt => :'40bit')
@pdftk.multibackground('a.pdf', 'b.pdf', :encrypt => :'40bit')
end
end
context "stamp" do
it "should call #pdftk on @call" do
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:stamp => 'b.pdf'})
@pdftk.stamp('a.pdf', 'b.pdf')
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:multistamp => 'b.pdf'})
@pdftk.stamp('a.pdf', 'b.pdf', :multi => true)
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:stamp => 'b.pdf'}, :encrypt => :'40bit')
@pdftk.stamp('a.pdf', 'b.pdf', :encrypt => :'40bit')
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:multistamp => 'b.pdf'}, :encrypt => :'40bit')
@pdftk.stamp('a.pdf', 'b.pdf', :encrypt => :'40bit', :multi => true)
end
end
context "multistamp" do
it "should call #pdftk on @call" do
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:multistamp => 'b.pdf'})
@pdftk.multistamp('a.pdf', 'b.pdf')
@pdftk.call.should_receive(:pdftk).with(:input => 'a.pdf', :operation => {:multistamp => 'b.pdf'}, :encrypt => :'40bit')
@pdftk.multistamp('a.pdf', 'b.pdf', :encrypt => :'40bit')
end
end
end # Wrapper