|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +describe "Transloadit end-to-end upload" do |
| 4 | + before do |
| 5 | + skip "Set RUBY_SDK_E2E=1 to run live upload tests" unless e2e_enabled? |
| 6 | + |
| 7 | + @key = ENV["TRANSLOADIT_KEY"] |
| 8 | + @secret = ENV["TRANSLOADIT_SECRET"] |
| 9 | + skip "TRANSLOADIT_KEY and TRANSLOADIT_SECRET must be set to run live upload tests" if blank?(@key) || blank?(@secret) |
| 10 | + |
| 11 | + @service = resolve_service |
| 12 | + |
| 13 | + @fixture_path = File.expand_path("../../chameleon.jpg", __dir__) |
| 14 | + skip "chameleon.jpg fixture missing; run tests from the repository root" unless File.file?(@fixture_path) |
| 15 | + end |
| 16 | + |
| 17 | + it "uploads and processes the chameleon image" do |
| 18 | + options = { |
| 19 | + key: @key, |
| 20 | + secret: @secret |
| 21 | + } |
| 22 | + options[:service] = @service if @service |
| 23 | + |
| 24 | + transloadit = Transloadit.new(options) |
| 25 | + |
| 26 | + resize_step = transloadit.step( |
| 27 | + "resize", |
| 28 | + "/image/resize", |
| 29 | + use: ":original", |
| 30 | + width: 128, |
| 31 | + height: 128, |
| 32 | + resize_strategy: "fit", |
| 33 | + format: "png" |
| 34 | + ) |
| 35 | + |
| 36 | + response = File.open(@fixture_path, "rb") do |upload| |
| 37 | + transloadit.assembly.create!( |
| 38 | + upload, |
| 39 | + wait: true, |
| 40 | + steps: resize_step |
| 41 | + ) |
| 42 | + end |
| 43 | + |
| 44 | + response.reload_until_finished!(tries: 120) unless response.finished? |
| 45 | + |
| 46 | + _(response.completed?).must_equal true, "Assembly did not complete successfully: #{response.body.inspect}" |
| 47 | + |
| 48 | + uploads = response["uploads"] || [] |
| 49 | + refute_empty uploads, "Expected uploads in the assembly response" |
| 50 | + |
| 51 | + upload_info = uploads.first |
| 52 | + basename = upload_info["basename"] |
| 53 | + _(basename).must_equal File.basename(@fixture_path, ".*") if basename |
| 54 | + |
| 55 | + filename = upload_info["name"] |
| 56 | + _(filename).must_equal File.basename(@fixture_path) if filename |
| 57 | + |
| 58 | + results = (response["results"] || {})["resize"] || [] |
| 59 | + refute_empty results, "Expected resize results in assembly response" |
| 60 | + |
| 61 | + first_result = results.first |
| 62 | + ssl_url = first_result["ssl_url"] |
| 63 | + refute_nil ssl_url, "Missing ssl_url in resize result: #{first_result.inspect}" |
| 64 | + _(ssl_url).must_match(/\Ahttps:\/\//) |
| 65 | + |
| 66 | + meta = first_result["meta"] || {} |
| 67 | + width = integer_if_present(meta["width"]) |
| 68 | + height = integer_if_present(meta["height"]) |
| 69 | + refute_nil width, "Missing width metadata: #{meta.inspect}" |
| 70 | + refute_nil height, "Missing height metadata: #{meta.inspect}" |
| 71 | + assert width.positive? && width <= 128, "Unexpected width #{width.inspect}" |
| 72 | + assert height.positive? && height <= 128, "Unexpected height #{height.inspect}" |
| 73 | + end |
| 74 | + |
| 75 | + private |
| 76 | + |
| 77 | + def e2e_enabled? |
| 78 | + flag = ENV["RUBY_SDK_E2E"] |
| 79 | + return false if blank?(flag) |
| 80 | + |
| 81 | + %w[1 true yes on].include?(flag.to_s.strip.downcase) |
| 82 | + end |
| 83 | + |
| 84 | + def resolve_service |
| 85 | + host = ENV["TRANSLOADIT_HOST"].to_s.strip |
| 86 | + return host unless host.empty? |
| 87 | + |
| 88 | + region = ENV["TRANSLOADIT_REGION"].to_s.strip |
| 89 | + return nil if region.empty? |
| 90 | + |
| 91 | + "https://api2-#{region}.transloadit.com" |
| 92 | + end |
| 93 | + |
| 94 | + def integer_if_present(value) |
| 95 | + return nil if blank?(value) |
| 96 | + |
| 97 | + value.to_i |
| 98 | + end |
| 99 | + |
| 100 | + def blank?(value) |
| 101 | + value.respond_to?(:empty?) ? value.empty? : !value |
| 102 | + end |
| 103 | +end |
0 commit comments