When(/^(existent|non-existent|not-owned) file is requested$/) do |file_type|
  case file_type
  when "existent"
    @role = "user"
    @res = retrieve_stream_url(PRODUCT_A_TUID, ALW_USER_SERVICE_HEADERS)
  when "non-existent"
    @res = retrieve_stream_url("999999999999999")
  when "not-owned"
    @res = retrieve_stream_url(GET_STREAM_TUID)
  end
end

When(/^existent file is requested by admin$/) do
  @res = retrieve_stream_url(PRODUCT_A_TUID, OA_USER_SERVICE_HEADERS)
  @role = "admin"
end

Then(/^an (404|403) error should be obtained in get stream response$/) do |error|
  parsed_response = JSON.parse(@res[:response])

  case error.to_i
  when 403
    assert_equal(403, @res[:code])
    assert_equal("Product not owned by account", parsed_response["message"])
    assert_equal("not_owned", parsed_response["code"])
  when 404
    assert_equal(404, @res[:code])
    assert_equal("Track not found for provided track id.", parsed_response["message"])
    assert_equal("not_found_error", parsed_response["code"])
  when 500
    # not sure how to simulate this yet
  end
end

Then(/^a valid stream url should be obtained$/) do
  parsed_response = JSON.parse(@res[:response])
  case @role
  when "user"
    user_type, user_id = ALW_USER_SERVICE_HEADERS["Orchard-User-Id"].split(":")
  when "admin"
    user_type, user_id = OA_USER_SERVICE_HEADERS["Orchard-User-Id"].split(":")
  end

  assert_equal(200, @res[:code])
  assert_equal(parsed_response["access"]["user_type"], user_type)
  assert_equal(parsed_response["access"]["user_id"], user_id.to_i)
  assert_equal(parsed_response["access"]["token_type"], "stream")
  refute_nil(parsed_response["stream"]["duration"], "Duration was empty, got: #{parsed_response}")

  stream_url = parsed_response["stream"]["url"]
  assert_includes(stream_url, "rtmp://streams.#{ENV['TEST_DOMAIN_URL']}orch.com/")
  assert_includes(stream_url, PRODUCT_A_TUID.to_s)
  assert_includes(stream_url, PRODUCT_A_UPC)

  assert_equal(PRODUCT_A_TUID.to_s, parsed_response["stream"]["track_unique_id"])
end
