require 'rails_helper'

RSpec.describe RemoteStorageExpireAuthorizationJob, type: :job do
  before do
    allow_any_instance_of(AppCatalog::WebApp).to(
      receive(:update_metadata).and_return(true)
    )

    @user = create :user, cn: "ronald", ou: "kosmos.org"
    @rs_authorization = create :remote_storage_authorization,
                               user: @user, expire_at: 1.day.ago
  end

  after do
    clear_enqueued_jobs
    clear_performed_jobs
  end

  subject(:job) {
    described_class.perform_later(@rs_authorization.id)
  }

  let(:redis) {
    @redis ||= Redis.new(url: Setting.rs_redis_url)
  }

  it "removes the RS authorization from redis" do
    redis_key = "authorizations:#{@user.cn}:#{@rs_authorization.token}"
    expect(redis.keys(redis_key)).to_not be_empty

    perform_enqueued_jobs { job }

    expect(redis.keys(redis_key)).to be_empty
  end

  it "deletes the RS authorization object" do
    expect {
      perform_enqueued_jobs { job }
    }.to change(RemoteStorageAuthorization, :count).by(-1)
  end
end
