refile 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +56 -0
  3. data/.travis.yml +11 -0
  4. data/Gemfile +1 -1
  5. data/History.md +15 -0
  6. data/README.md +41 -5
  7. data/Rakefile +4 -1
  8. data/config/locales/en.yml +2 -0
  9. data/config/routes.rb +4 -2
  10. data/lib/refile/app.rb +96 -71
  11. data/lib/refile/attacher.rb +115 -0
  12. data/lib/refile/attachment/active_record.rb +5 -5
  13. data/lib/refile/attachment.rb +50 -117
  14. data/lib/refile/backend/file_system.rb +3 -3
  15. data/lib/refile/backend/s3.rb +7 -12
  16. data/lib/refile/file.rb +1 -3
  17. data/lib/refile/image_processing.rb +6 -3
  18. data/lib/refile/rails/attachment_helper.rb +18 -21
  19. data/lib/refile/rails.rb +2 -1
  20. data/lib/refile/random_hasher.rb +9 -8
  21. data/lib/refile/signature.rb +16 -0
  22. data/lib/refile/version.rb +1 -1
  23. data/lib/refile.rb +59 -1
  24. data/refile.gemspec +10 -6
  25. data/spec/refile/app_spec.rb +28 -4
  26. data/spec/refile/attachment_spec.rb +134 -33
  27. data/spec/refile/backend_examples.rb +7 -11
  28. data/spec/refile/features/direct_upload_spec.rb +0 -1
  29. data/spec/refile/features/normal_upload_spec.rb +21 -0
  30. data/spec/refile/features/presigned_upload_spec.rb +0 -1
  31. data/spec/refile/rails/attachment_helper_spec.rb +61 -0
  32. data/spec/refile/spec_helper.rb +26 -20
  33. data/spec/refile/test_app/app/controllers/normal_posts_controller.rb +10 -1
  34. data/spec/refile/test_app/app/controllers/presigned_posts_controller.rb +1 -0
  35. data/spec/refile/test_app/app/models/post.rb +1 -1
  36. data/spec/refile/test_app/app/views/normal_posts/index.html +5 -0
  37. data/spec/refile/test_app/app/views/normal_posts/show.html.erb +2 -0
  38. data/spec/refile/test_app/config/routes.rb +1 -1
  39. data/spec/refile/test_app.rb +13 -6
  40. data/spec/refile_spec.rb +39 -0
  41. metadata +51 -2
@@ -1,6 +1,5 @@
1
1
  describe Refile::Attachment do
2
- let(:options) { { } }
3
- let(:post) { Post.new }
2
+ let(:options) { {} }
4
3
  let(:klass) do
5
4
  opts = options
6
5
  Class.new do
@@ -82,7 +81,7 @@ describe Refile::Attachment do
82
81
  expect do
83
82
  instance.remote_document_url = "http://www.example.com/loop"
84
83
  end.not_to raise_error
85
- expect(instance.document_attachment.errors).to eq([:download_failed])
84
+ expect(instance.document_attacher.errors).to eq([:download_failed])
86
85
  expect(instance.document).to be_nil
87
86
  end
88
87
  end
@@ -98,12 +97,12 @@ describe Refile::Attachment do
98
97
  end
99
98
  end
100
99
 
101
- describe ":name_attachment.store!" do
100
+ describe ":name_attacher.store!" do
102
101
  it "puts a cached file into the store" do
103
102
  instance.document = Refile::FileDouble.new("hello")
104
103
  cache = instance.document
105
104
 
106
- instance.document_attachment.store!
105
+ instance.document_attacher.store!
107
106
 
108
107
  expect(Refile.store.get(instance.document_id).read).to eq("hello")
109
108
  expect(Refile.store.get(instance.document.id).read).to eq("hello")
@@ -116,7 +115,7 @@ describe Refile::Attachment do
116
115
  file = Refile.store.upload(Refile::FileDouble.new("hello"))
117
116
  instance.document_id = file.id
118
117
 
119
- instance.document_attachment.store!
118
+ instance.document_attacher.store!
120
119
 
121
120
  expect(Refile.store.get(instance.document_id).read).to eq("hello")
122
121
  expect(Refile.store.get(instance.document.id).read).to eq("hello")
@@ -129,7 +128,7 @@ describe Refile::Attachment do
129
128
  instance.document = Refile::FileDouble.new("world")
130
129
  cache = instance.document
131
130
 
132
- instance.document_attachment.store!
131
+ instance.document_attacher.store!
133
132
 
134
133
  expect(Refile.store.get(instance.document_id).read).to eq("world")
135
134
  expect(Refile.store.get(instance.document.id).read).to eq("world")
@@ -143,20 +142,20 @@ describe Refile::Attachment do
143
142
  file = Refile.store.upload(Refile::FileDouble.new("hello"))
144
143
  instance.document_id = file.id
145
144
 
146
- instance.document_attachment.remove = true
147
- instance.document_attachment.store!
145
+ instance.document_attacher.remove = true
146
+ instance.document_attacher.store!
148
147
 
149
148
  expect(instance.document_id).to be_nil
150
149
  expect(Refile.store.exists?(file.id)).to be_falsy
151
150
  end
152
151
  end
153
152
 
154
- describe ":name_attachment.delete!" do
153
+ describe ":name_attacher.delete!" do
155
154
  it "deletes a stored file" do
156
155
  file = Refile.store.upload(Refile::FileDouble.new("hello"))
157
156
  instance.document_id = file.id
158
157
 
159
- instance.document_attachment.delete!
158
+ instance.document_attacher.delete!
160
159
 
161
160
  expect(instance.document_id).to be_nil
162
161
  expect(Refile.store.exists?(file.id)).to be_falsy
@@ -166,7 +165,7 @@ describe Refile::Attachment do
166
165
  file = Refile.cache.upload(Refile::FileDouble.new("hello"))
167
166
  instance.document_cache_id = file.id
168
167
 
169
- instance.document_attachment.delete!
168
+ instance.document_attacher.delete!
170
169
 
171
170
  expect(instance.document_id).to be_nil
172
171
  expect(instance.document_cache_id).to be_nil
@@ -174,74 +173,92 @@ describe Refile::Attachment do
174
173
  end
175
174
  end
176
175
 
177
- describe ":name_attachment.remove?" do
176
+ describe ":name_attacher.remove?" do
178
177
  it "should be true when the value is truthy" do
179
- instance.document_attachment.remove = true
180
- expect(instance.document_attachment.remove?).to be_truthy
178
+ instance.document_attacher.remove = true
179
+ expect(instance.document_attacher.remove?).to be_truthy
181
180
  end
182
181
 
183
182
  it "should be false when the value is falsey" do
184
- instance.document_attachment.remove = false
185
- expect(instance.document_attachment.remove?).to be_falsy
183
+ instance.document_attacher.remove = false
184
+ expect(instance.document_attacher.remove?).to be_falsy
186
185
  end
187
186
 
188
187
  it "should be false when the value is ''" do
189
- instance.document_attachment.remove = ''
190
- expect(instance.document_attachment.remove?).to be_falsy
188
+ instance.document_attacher.remove = ""
189
+ expect(instance.document_attacher.remove?).to be_falsy
191
190
  end
192
191
 
193
192
  it "should be false when the value is '0'" do
194
- instance.document_attachment.remove = '0'
195
- expect(instance.document_attachment.remove?).to be_falsy
193
+ instance.document_attacher.remove = "0"
194
+ expect(instance.document_attacher.remove?).to be_falsy
196
195
  end
197
196
 
198
197
  it "should be false when the value is 'false'" do
199
- instance.document_attachment.remove = 'false'
200
- expect(instance.document_attachment.remove?).to be_falsy
198
+ instance.document_attacher.remove = "false"
199
+ expect(instance.document_attacher.remove?).to be_falsy
201
200
  end
202
201
  end
203
202
 
204
- describe ":name_attachment.error" do
203
+ describe ":name_attacher.error" do
205
204
  let(:options) { { cache: :limited_cache, raise_errors: false } }
206
205
 
207
206
  it "is blank when valid file uploaded" do
208
207
  file = Refile::FileDouble.new("hello")
209
208
  instance.document = file
210
209
 
211
- expect(instance.document_attachment.errors).to be_empty
210
+ expect(instance.document_attacher.errors).to be_empty
212
211
  expect(Refile.cache.get(instance.document.id).exists?).to be_truthy
213
212
  end
214
213
 
215
214
  it "contains a list of errors when invalid file uploaded" do
216
- file = Refile::FileDouble.new("a"*120)
215
+ file = Refile::FileDouble.new("a" * 120)
217
216
  instance.document = file
218
217
 
219
- expect(instance.document_attachment.errors).to eq([:too_large])
218
+ expect(instance.document_attacher.errors).to eq([:too_large])
220
219
  expect(instance.document).to be_nil
221
220
  end
222
221
 
223
222
  it "is reset when valid file uploaded" do
224
- file = Refile::FileDouble.new("a"*120)
223
+ file = Refile::FileDouble.new("a" * 120)
225
224
  instance.document = file
226
225
 
227
226
  file = Refile::FileDouble.new("hello")
228
227
  instance.document = file
229
228
 
230
- expect(instance.document_attachment.errors).to be_empty
229
+ expect(instance.document_attacher.errors).to be_empty
231
230
  expect(Refile.cache.get(instance.document.id).exists?).to be_truthy
232
231
  end
233
232
  end
234
233
 
234
+ describe ":name_attacher.accept" do
235
+ context "with `extension`" do
236
+ let(:options) { { extension: %w[jpg png] } }
237
+
238
+ it "returns an accept string" do
239
+ expect(instance.document_attacher.accept).to eq(".jpg,.png")
240
+ end
241
+ end
242
+
243
+ context "with `content_type`" do
244
+ let(:options) { { content_type: %w[image/jpeg image/png], extension: "zip" } }
245
+
246
+ it "returns an accept string" do
247
+ expect(instance.document_attacher.accept).to eq("image/jpeg,image/png")
248
+ end
249
+ end
250
+ end
251
+
235
252
  describe "with option `raise_errors: true" do
236
253
  let(:options) { { cache: :limited_cache, raise_errors: true } }
237
254
 
238
255
  it "raises an error when invalid file assigned" do
239
- file = Refile::FileDouble.new("a"*120)
256
+ file = Refile::FileDouble.new("a" * 120)
240
257
  expect do
241
258
  instance.document = file
242
259
  end.to raise_error(Refile::Invalid)
243
260
 
244
- expect(instance.document_attachment.errors).to eq([:too_large])
261
+ expect(instance.document_attacher.errors).to eq([:too_large])
245
262
  expect(instance.document).to be_nil
246
263
  end
247
264
  end
@@ -250,10 +267,94 @@ describe Refile::Attachment do
250
267
  let(:options) { { cache: :limited_cache, raise_errors: false } }
251
268
 
252
269
  it "does not raise an error when invalid file assigned" do
253
- file = Refile::FileDouble.new("a"*120)
270
+ file = Refile::FileDouble.new("a" * 120)
271
+ instance.document = file
272
+
273
+ expect(instance.document_attacher.errors).to eq([:too_large])
274
+ expect(instance.document).to be_nil
275
+ end
276
+ end
277
+
278
+ describe "with option `extension`: %w[txt]`" do
279
+ let(:options) { { extension: "txt", raise_errors: false } }
280
+
281
+ it "allows file with correct extension to be uploaded" do
282
+ file = Refile::FileDouble.new("hello", "hello.txt")
283
+ instance.document = file
284
+
285
+ expect(instance.document_attacher.errors).to be_empty
286
+ expect(Refile.cache.get(instance.document.id).exists?).to be_truthy
287
+ end
288
+
289
+ it "sets error when file with other extension is uploaded" do
290
+ file = Refile::FileDouble.new("hello", "hello.php")
291
+ instance.document = file
292
+
293
+ expect(instance.document_attacher.errors).to eq([:invalid_extension])
294
+ expect(instance.document).to be_nil
295
+ end
296
+
297
+ it "sets error when file with no extension is uploaded" do
298
+ file = Refile::FileDouble.new("hello")
299
+ instance.document = file
300
+
301
+ expect(instance.document_attacher.errors).to eq([:invalid_extension])
302
+ expect(instance.document).to be_nil
303
+ end
304
+ end
305
+
306
+ describe "with option `content_type: %w[txt]`" do
307
+ let(:options) { { content_type: "text/plain", raise_errors: false } }
308
+
309
+ it "allows file with correct content type to be uploaded" do
310
+ file = Refile::FileDouble.new("hello", content_type: "text/plain")
311
+ instance.document = file
312
+
313
+ expect(instance.document_attacher.errors).to be_empty
314
+ expect(Refile.cache.get(instance.document.id).exists?).to be_truthy
315
+ end
316
+
317
+ it "sets error when file with other content type is uploaded" do
318
+ file = Refile::FileDouble.new("hello", content_type: "application/php")
319
+ instance.document = file
320
+
321
+ expect(instance.document_attacher.errors).to eq([:invalid_content_type])
322
+ expect(instance.document).to be_nil
323
+ end
324
+
325
+ it "sets error when file with no content type is uploaded" do
326
+ file = Refile::FileDouble.new("hello")
327
+ instance.document = file
328
+
329
+ expect(instance.document_attacher.errors).to eq([:invalid_content_type])
330
+ expect(instance.document).to be_nil
331
+ end
332
+ end
333
+
334
+ describe "with option `type: :image`" do
335
+ let(:options) { { type: :image, raise_errors: false } }
336
+
337
+ it "allows image to be uploaded" do
338
+ file = Refile::FileDouble.new("hello", content_type: "image/jpeg")
339
+ instance.document = file
340
+
341
+ expect(instance.document_attacher.errors).to be_empty
342
+ expect(Refile.cache.get(instance.document.id).exists?).to be_truthy
343
+ end
344
+
345
+ it "sets error when file with other content type is uploaded" do
346
+ file = Refile::FileDouble.new("hello", content_type: "application/php")
347
+ instance.document = file
348
+
349
+ expect(instance.document_attacher.errors).to eq([:invalid_content_type])
350
+ expect(instance.document).to be_nil
351
+ end
352
+
353
+ it "sets error when file with no content type is uploaded" do
354
+ file = Refile::FileDouble.new("hello")
254
355
  instance.document = file
255
356
 
256
- expect(instance.document_attachment.errors).to eq([:too_large])
357
+ expect(instance.document_attacher.errors).to eq([:invalid_content_type])
257
358
  expect(instance.document).to be_nil
258
359
  end
259
360
  end
@@ -3,10 +3,6 @@ RSpec.shared_examples_for :backend do
3
3
  Refile::FileDouble.new(data)
4
4
  end
5
5
 
6
- def open_files
7
- ObjectSpace.each_object(File).reject { |f| f.closed? } if defined?(ObjectSpace)
8
- end
9
-
10
6
  describe "#upload" do
11
7
  it "raises ArgumentError when invalid object is uploaded" do
12
8
  expect { backend.upload(double(size: 123)) }.to raise_error(ArgumentError)
@@ -133,16 +129,20 @@ RSpec.shared_examples_for :backend do
133
129
  it "complains when called without confirm" do
134
130
  file = backend.upload(uploadable)
135
131
 
136
- expect { backend.clear! }.to raise_error(ArgumentError)
132
+ expect { backend.clear! }.to raise_error(Refile::Confirm)
137
133
 
138
134
  expect(backend.get(file.id).exists?).to be_truthy
139
135
  end
140
136
  end
141
137
 
138
+ describe "#max_size" do
139
+ it "returns the given max size" do
140
+ expect(backend.max_size).to eq(100)
141
+ end
142
+ end
143
+
142
144
  describe "File" do
143
145
  it "is an io-like object" do
144
- before = open_files
145
-
146
146
  file = backend.upload(uploadable)
147
147
 
148
148
  buffer = ""
@@ -159,16 +159,12 @@ RSpec.shared_examples_for :backend do
159
159
  file.close
160
160
 
161
161
  expect { file.read(1, buffer) }.to raise_error
162
-
163
- expect(open_files).to eq(before)
164
162
  end
165
163
 
166
164
  describe "#read" do
167
165
  it "can read file contents" do
168
166
  file = backend.upload(uploadable)
169
167
 
170
- buffer = ""
171
-
172
168
  expect(file.read).to eq("hello")
173
169
  end
174
170
 
@@ -26,4 +26,3 @@ feature "Direct HTTP post file uploads", :js do
26
26
  expect(page).to have_content("Upload failure error")
27
27
  end
28
28
  end
29
-
@@ -22,6 +22,16 @@ feature "Normal HTTP Post file uploads" do
22
22
  expect(page).to have_content("Document is too large")
23
23
  end
24
24
 
25
+ scenario "Fail to upload a file that has the wrong format" do
26
+ visit "/normal/posts/new"
27
+ fill_in "Title", with: "A cool post"
28
+ attach_file "Image", path("hello.txt")
29
+ click_button "Create"
30
+
31
+ expect(page).to have_selector(".field_with_errors")
32
+ expect(page).to have_content("Image has an invalid file format")
33
+ end
34
+
25
35
  scenario "Upload a file via form redisplay" do
26
36
  visit "/normal/posts/new"
27
37
  attach_file "Document", path("hello.txt")
@@ -61,6 +71,17 @@ feature "Normal HTTP Post file uploads" do
61
71
  expect(page).to_not have_selector(:link, "Document")
62
72
  end
63
73
 
74
+ scenario "Successfully remove a record with an uploaded file" do
75
+ visit "/normal/posts/new"
76
+ fill_in "Title", with: "A cool post about to be deleted"
77
+ attach_file "Document", path("hello.txt")
78
+ click_button "Create"
79
+
80
+ expect(page).to have_selector("h1", text: "A cool post about to be deleted")
81
+ click_link("Delete")
82
+ expect(page).to_not have_content("A cool post about to be deleted")
83
+ end
84
+
64
85
  scenario "Upload a file from a remote URL" do
65
86
  stub_request(:get, "http://www.example.com/some_file").to_return(status: 200, body: "abc", headers: { "Content-Length" => 3 })
66
87
 
@@ -26,4 +26,3 @@ feature "Direct HTTP post file uploads", :js do
26
26
  expect(page).to have_content("Upload failure too large")
27
27
  end
28
28
  end
29
-
@@ -0,0 +1,61 @@
1
+ require "refile"
2
+ require "active_support/inflector"
3
+ require "refile/rails/attachment_helper"
4
+
5
+ describe Refile::AttachmentHelper do
6
+ describe "#attachment_url" do
7
+ let(:view) { Struct.new(:main_app, :request).new(main_app, request) }
8
+ let(:main_app) { double :main_app, refile_app_path: "attachments" }
9
+ let(:request) { double :request, base_url: "http://www.example.com" }
10
+ let(:record) { double :record, name: name, image: file }
11
+ let(:name) { :image }
12
+ let(:file) { double :file, backend: backend, id: "123abc" }
13
+ let(:backend) { double :backend }
14
+ before do
15
+ view.extend(Refile::AttachmentHelper)
16
+ Refile.backends["test_backend"] = backend
17
+ end
18
+ after do
19
+ Refile.backends.delete("test_backend")
20
+ end
21
+
22
+ context "with a host passed in" do
23
+ let(:host) { "//cdn.example.com" }
24
+
25
+ it "creates the expected url" do
26
+ url = view.attachment_url(record, name, "fill", 300, 300, filename: "test", format: "jpg", host: host)
27
+ expect(url).to eq "//cdn.example.com/attachments/test_backend/fill/300/300/123abc/test.jpg"
28
+ end
29
+ end
30
+
31
+ context "with no host passed in, but Refile.host set" do
32
+ before do
33
+ @original_host = Refile.host
34
+ Refile.host = "//cdn.example.com"
35
+ end
36
+ after do
37
+ Refile.host = @original_host
38
+ end
39
+
40
+ it "creates the expected url" do
41
+ url = view.attachment_url(record, name, "fill", 300, 300, filename: "test", format: "jpg")
42
+ expect(url).to eq "//cdn.example.com/attachments/test_backend/fill/300/300/123abc/test.jpg"
43
+ end
44
+ end
45
+
46
+ context "with no host passed in, and no Refile.host set" do
47
+ before do
48
+ @original_host = Refile.host
49
+ Refile.host = nil
50
+ end
51
+ after do
52
+ Refile.host = @original_host
53
+ end
54
+
55
+ it "creates the expected url" do
56
+ url = view.attachment_url(record, name, "fill", 300, 300, filename: "test", format: "jpg")
57
+ expect(url).to eq "http://www.example.com/attachments/test_backend/fill/300/300/123abc/test.jpg"
58
+ end
59
+ end
60
+ end
61
+ end
@@ -22,7 +22,7 @@ end
22
22
 
23
23
  Refile.backends["limited_cache"] = FakePresignBackend.new(File.expand_path("default_cache", tmp_path), max_size: 100)
24
24
 
25
- Refile.direct_upload = %w(cache limited_cache)
25
+ Refile.direct_upload = %w[cache limited_cache]
26
26
 
27
27
  Refile.processor(:reverse) do |file|
28
28
  StringIO.new(file.read.reverse)
@@ -49,25 +49,30 @@ Refile.processor(:convert_case) do |file, format:|
49
49
  end
50
50
  end
51
51
 
52
- class Refile::FileDouble
53
- def initialize(data)
54
- @io = StringIO.new(data)
55
- end
56
-
57
- def read(*args)
58
- @io.read(*args)
59
- end
60
-
61
- def size
62
- @io.size
63
- end
64
-
65
- def eof?
66
- @io.eof?
67
- end
68
-
69
- def close
70
- @io.close
52
+ module Refile
53
+ class FileDouble
54
+ attr_reader :original_filename, :content_type
55
+ def initialize(data, name = nil, content_type: nil)
56
+ @io = StringIO.new(data)
57
+ @original_filename = name
58
+ @content_type = content_type
59
+ end
60
+
61
+ def read(*args)
62
+ @io.read(*args)
63
+ end
64
+
65
+ def size
66
+ @io.size
67
+ end
68
+
69
+ def eof?
70
+ @io.eof?
71
+ end
72
+
73
+ def close
74
+ @io.close
75
+ end
71
76
  end
72
77
  end
73
78
 
@@ -80,6 +85,7 @@ end
80
85
  RSpec.configure do |config|
81
86
  config.include PathHelper
82
87
  config.before(:all) do
88
+ Refile.logger = Logger.new(nil)
83
89
  WebMock.disable_net_connect!(allow_localhost: true)
84
90
  end
85
91
  end
@@ -1,4 +1,8 @@
1
1
  class NormalPostsController < ApplicationController
2
+ def index
3
+ @posts = Post.all
4
+ end
5
+
2
6
  def show
3
7
  @post = Post.find(params[:id])
4
8
  end
@@ -31,7 +35,12 @@ class NormalPostsController < ApplicationController
31
35
  end
32
36
  end
33
37
 
34
- private
38
+ def destroy
39
+ Post.find(params[:id]).destroy
40
+ redirect_to :normal_posts
41
+ end
42
+
43
+ private
35
44
 
36
45
  def post_params
37
46
  params.require(:post).permit(:title, :image, :image_cache_id, :document, :document_cache_id, :remove_document, :remote_document_url)
@@ -13,6 +13,7 @@ class PresignedPostsController < ApplicationController
13
13
  end
14
14
  end
15
15
 
16
+ # rubocop:disable Metrics/AbcSize
16
17
  def upload
17
18
  if params[:token] == "xyz123"
18
19
  if params[:file].size < 100
@@ -1,5 +1,5 @@
1
1
  class Post < ActiveRecord::Base
2
- attachment :image
2
+ attachment :image, type: :image
3
3
  attachment :document, cache: :limited_cache
4
4
  validates_presence_of :title
5
5
  end
@@ -0,0 +1,5 @@
1
+ <h1>Posts</h1>
2
+
3
+ <% @posts.each do |post| %>
4
+ <h2><%= post.title %></h2>
5
+ <% end %>
@@ -13,3 +13,5 @@
13
13
  <% end %>
14
14
 
15
15
  <%= link_to "Edit", edit_normal_post_url(@post) %>
16
+
17
+ <%= link_to "Delete", normal_post_url(@post), method: :delete %>
@@ -2,7 +2,7 @@ Refile::TestApp.routes.draw do
2
2
  root to: "home#index"
3
3
 
4
4
  scope path: "normal", as: "normal" do
5
- resources :posts, only: [:show, :new, :edit, :create, :update], controller: "normal_posts"
5
+ resources :posts, controller: "normal_posts"
6
6
  end
7
7
 
8
8
  scope path: "direct", as: "direct" do
@@ -6,8 +6,8 @@ require "jquery/rails"
6
6
 
7
7
  module Refile
8
8
  class TestApp < Rails::Application
9
- config.secret_token = '6805012ab1750f461ef3c531bdce84c0'
10
- config.session_store :cookie_store, :key => '_refile_session'
9
+ config.secret_token = "6805012ab1750f461ef3c531bdce84c0"
10
+ config.session_store :cookie_store, key: "_refile_session"
11
11
  config.active_support.deprecation = :log
12
12
  config.eager_load = false
13
13
  config.action_dispatch.show_exceptions = false
@@ -21,7 +21,7 @@ end
21
21
 
22
22
  class TestMigration < ActiveRecord::Migration
23
23
  def self.up
24
- create_table :posts, :force => true do |t|
24
+ create_table :posts, force: true do |t|
25
25
  t.column :title, :string
26
26
  t.column :image_id, :string
27
27
  t.column :document_id, :string
@@ -44,13 +44,20 @@ if ENV["SAUCE_BROWSER"]
44
44
  url = "http://#{ENV["SAUCE_USERNAME"]}:#{ENV["SAUCE_ACCESS_KEY"]}@localhost:4445/wd/hub"
45
45
  capabilities = { browserName: ENV["SAUCE_BROWSER"], version: ENV["SAUCE_VERSION"] }
46
46
  driver = Capybara::Selenium::Driver.new(app, browser: :remote, url: url, desired_capabilities: capabilities)
47
- driver.browser.file_detector = lambda { |args| args.first if File.exist?(args.first) }
47
+ driver.browser.file_detector = ->(args) { args.first if File.exist?(args.first) }
48
48
  driver
49
49
  end
50
50
  end
51
51
 
52
52
  Capybara.configure do |config|
53
- config.server_port = 56120
53
+ config.server_port = 56_120
54
54
  end
55
55
 
56
- Refile.host = "//127.0.0.1:56120"
56
+ Refile.allow_origin = "*"
57
+ Refile.host = "//localhost:56120"
58
+
59
+ RSpec.configure do |config|
60
+ config.before(:all) do
61
+ Refile.logger = Rails.logger
62
+ end
63
+ end
data/spec/refile_spec.rb CHANGED
@@ -32,4 +32,43 @@ RSpec.describe Refile do
32
32
  expect { Refile.verify_uploadable(Refile::FileDouble.new("hello world"), 8) }.to raise_error(Refile::Invalid)
33
33
  end
34
34
  end
35
+
36
+ describe ".extract_filename" do
37
+ it "extracts filename from original_filename" do
38
+ name = Refile.extract_filename(double(original_filename: "/foo/bar/baz.png"))
39
+ expect(name).to eq("baz.png")
40
+ end
41
+
42
+ it "extracts filename from path" do
43
+ name = Refile.extract_filename(double(path: "/foo/bar/baz.png"))
44
+ expect(name).to eq("baz.png")
45
+ end
46
+
47
+ it "returns nil if it can't determine filename" do
48
+ name = Refile.extract_filename(double)
49
+ expect(name).to be_nil
50
+ end
51
+ end
52
+
53
+ describe ".extract_content_type" do
54
+ it "extracts content type" do
55
+ name = Refile.extract_content_type(double(content_type: "image/jpeg"))
56
+ expect(name).to eq("image/jpeg")
57
+ end
58
+
59
+ it "extracts content type from extension" do
60
+ name = Refile.extract_content_type(double(original_filename: "test.png"))
61
+ expect(name).to eq("image/png")
62
+ end
63
+
64
+ it "returns nil if it can't determine content type" do
65
+ name = Refile.extract_filename(double)
66
+ expect(name).to be_nil
67
+ end
68
+
69
+ it "returns nil if it has an unknown content type" do
70
+ name = Refile.extract_content_type(double(original_filename: "foo.blah"))
71
+ expect(name).to be_nil
72
+ end
73
+ end
35
74
  end