require 'spec_helper'

describe JamTracksCleaner do
  include UsesTempFiles
  include CarrierWave::Test::Matchers
  RIGHT_NAME = 'abc.jkz'
  in_directory_with_file(RIGHT_NAME)

  before (:all) do
    @user = FactoryGirl.create(:user)
    @jam_track = FactoryGirl.create(:jam_track)
    original_storage = JamTrackRightUploader.storage = :fog
  end

  after(:all) do
    JamTrackRightUploader.storage = @original_storage
  end


  before(:each) do
    content_for_file('abc')
  end

  it "should clean" do
    pending "re-enable cleaner after manual testing"
    jam_track_right = JamTrackRight.create(:user=>@user, :jam_track=>@jam_track)
    jam_track_right.signed_48=true
    jam_track_right
    
    jam_track_right.url_48.store!(File.open(RIGHT_NAME))
    jam_track_right.downloaded_since_sign=true        
    jam_track_right.save!    

    jam_track_right[:url_48].should == jam_track_right.store_dir + '/' + jam_track_right.filename(:url_48)
    jam_track_right.reload
    
    # Should exist after uploading:
    url = jam_track_right[:url_48]
    s3 = S3Manager.new(APP_CONFIG.aws_bucket, APP_CONFIG.aws_access_key_id, APP_CONFIG.aws_secret_access_key)

    url.should_not be_nil
    s3 = S3Manager.new(APP_CONFIG.aws_bucket, APP_CONFIG.aws_access_key_id, APP_CONFIG.aws_secret_access_key)
    s3.exists?(jam_track_right[:url_48]).should be_true

    JamRuby::JamTracksCleaner.perform
    s3.exists?(url).should be_true
    s3 = S3Manager.new(APP_CONFIG.aws_bucket, APP_CONFIG.aws_access_key_id, APP_CONFIG.aws_secret_access_key)
    jam_track_right.update_attribute("updated_at", 6.minutes.ago)

    # But not after running cleaner job:
    JamRuby::JamTracksCleaner.perform
    s3.exists?(url).should be_false
  end
end