Module: CarrierWaveDirect::Test::CapybaraHelpers

Includes:
Helpers
Defined in:
lib/carrierwave_direct/test/capybara_helpers.rb

Instance Method Summary collapse

Methods included from Helpers

#sample_key

Instance Method Details

#attach_file_for_direct_upload(path) ⇒ Object



9
10
11
# File 'lib/carrierwave_direct/test/capybara_helpers.rb', line 9

def attach_file_for_direct_upload(path)
  attach_file("file", path)
end

#find_keyObject



13
14
15
# File 'lib/carrierwave_direct/test/capybara_helpers.rb', line 13

def find_key
  key = page.find("input[name='key']", visible: false).value
end

#find_upload_pathObject



17
18
19
# File 'lib/carrierwave_direct/test/capybara_helpers.rb', line 17

def find_upload_path
  page.find("input[name='file']", visible: false).value
end

#upload_directly(uploader, button_locator, **options) ⇒ Object



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
# File 'lib/carrierwave_direct/test/capybara_helpers.rb', line 21

def upload_directly(uploader, button_locator, **options)
  options[:success] = true unless options[:success] == false
  options[:success] &&= !options[:fail]

  if options[:success]
    # simulate a successful upload

    # form's success action redirect url
    redirect_url = URI.parse(page.find("input[name='success_action_redirect']", visible: false).value)

    unless options[:redirect_key]
      sample_key_args = [{:base => find_key, :filename => File.basename(find_upload_path)}]
      sample_key_args.unshift(uploader) if method(:sample_key).arity == -2
      options[:redirect_key] = sample_key(*sample_key_args)
    end

    redirect_url_params = Rack::Utils.parse_nested_query(redirect_url.query)

    redirect_url.query = Rack::Utils.build_nested_query({
      :bucket => uploader.fog_directory,
      :key => options[:redirect_key],
      :etag => "\"d41d8cd98f00b204e9800998ecf8427\""
    }.merge(redirect_url_params))

    # click the button
    click_button button_locator

    # simulate success redirect
    visit redirect_url.to_s
  else
    # simulate an unsuccessful upload

    # click the button
    click_button button_locator
  end
end