require 'rails_helper'

RSpec.describe Api::DirectUploadsController, type: :controller do
  let(:current_user) { create(:user) }
  let(:project) { create(:project, account: current_user.primary_account) }

  before do
    sign_in_to_api(current_user)
  end

  describe '#create' do
    it 'returns data for a direct upload' do
      post :create, params: { direct_upload: direct_upload_params }

      expect(response).to be_successful
      expect(response_body_data_attributes).to include('id', 'key', 'signed_id', 'url', 'headers')
    end
  end

  private

  def direct_upload_params
    file = file_fixture("video_file.mp4")
    checksum = Digest::MD5.base64digest(StringIO.new(file.read).to_s)

    { filename: file.basename.to_s, content_type: "video/mp4", byte_size: file.size, checksum: checksum }
  end

  def response_body_data
    JSON.parse(response.body).dig('data')
  end

  def response_body_data_attributes
    response_body_data.dig('attributes')
  end
end
