require 'spec_helper'
require 'google_client'
require 'rest_client'

describe "YouTube", :slow=>true, :js=>true, :type => :feature, :capybara_feature => true, intermittent: true do
  subject { page }

  # Authenticate with a test google account.  This should create 
  # a UserAuthorization record, just as if a real user logged into google:
  before(:all) do
    Capybara.javascript_driver = :poltergeist
    Capybara.current_driver = Capybara.javascript_driver
    Capybara.default_max_wait_time = 10
    @previous_run_server = Capybara.run_server
    Capybara.run_server = false
    @user=FactoryGirl.create(:user, :email => "jamkazamtest@gmail.com")
    @youtube_client = GoogleClient.new()
    authorize_google_user(@youtube_client, @user, "filthyblueberryjam")
    google_auth = UserAuthorization.google_auth(@user).first # Consider returning this from above now that it is reliable
  end

  after(:all) do
    @user.destroy
    @youtube_client.shutdown
    Capybara.run_server = @previous_run_server
  end

  # This test retrieves and verified the signed youtube
  # upload URL in the same manner as the JK client:
  it "should retrieve upload url" do
    length = 3276
    upload_hash=@youtube_client.sign_youtube_upload(@user, "test_video.mp4", length)
    upload_hash.should_not be_nil
    upload_hash.length.should be >=1
    upload_hash['method'].should eq("PUT")
    upload_hash['url'].should_not be_nil
    upload_hash['Authorization'].should_not be_nil
    upload_hash['Content-Length'].should_not be_nil
    upload_hash['Content-Length'].should eq(length)
    upload_hash['Content-Type'].should_not be_nil

    @youtube_client.verify_youtube_upload(@user, upload_hash['url'], length).should be false
  end

  # Check that we can actually upload to the signed youtube URL:
  it "upload url should allow uploading" do
    vid_path = Rails.root.join('spec', 'files', 'test_video.mp4')
    length = File.size?(vid_path)
    upload_hash=@youtube_client.sign_youtube_upload(@user, "test_video.mp4", length)
    #puts upload_hash.inspect
    upload_hash.should_not be_nil
    upload_hash.length.should be >=1
    upload_hash['method'].should eq("PUT")
    upload_hash['url'].should_not be_nil
    upload_hash['Authorization'].should_not be_nil
    upload_hash['Content-Length'].should_not be_nil
    upload_hash['Content-Length'].should eq(length)
    upload_hash['Content-Type'].should_not be_nil

    # Upload this file as the client would:
    RestClient.put(upload_hash['url'], File.read(vid_path))
    @youtube_client.verify_youtube_upload(@user, upload_hash['url'], length).should be true
    #@youtube_client.youtube_upload_status(@user, upload_hash['url'], length)
  end

  # Ensure that uploading, then verifying and completing the upload sets the appropriate flag: 
  it "sets upload flag when complete" do
    @music_session = FactoryGirl.create(:active_music_session, :creator => @user, :musician_access => true)
    @connection = FactoryGirl.create(:connection, :user => @user, :music_session => @music_session)
    @video_source = FactoryGirl.create(:video_source, :connection => @connection)
    @recording = FactoryGirl.create(:recording, owner: @user, band: nil, duration:1)


    vid_path = Rails.root.join('spec', 'files', 'test_video.mp4')
    length = File.size?(vid_path)
    upload_hash=@youtube_client.sign_youtube_upload(@user, "test_video.mp4", length)
    upload_hash.should_not be_nil
    upload_hash['url'].should_not be_nil
    RestClient.put(upload_hash['url'], File.read(vid_path))

    recorded_video = FactoryGirl.create(:recorded_video,
      recording: @recording,
      user: @recording.owner,
      fully_uploaded: false,
      url: upload_hash['url'],
      length: length
    )

    @recording.recorded_videos << recorded_video

    @youtube_client.verify_youtube_upload(@user, upload_hash['url'], length).should be true
    @youtube_client.complete_upload(recorded_video).should be true
    recorded_video.fully_uploaded.should be true
  end
end
