# frozen_string_literal: true

require 'spec_helper'

require 'puppet/transport/redfish'

RSpec.describe Puppet::Transport::Redfish do
  subject(:transport) { described_class.new(context, connection_info) }

  let(:context) { instance_double('Puppet::ResourceApi::BaseContext', 'context') }
  let(:password) { 'aih6cu6ohvohpahN' }
  let(:connection_info) do
    {
      hostname: 'api.example.com',
      username: 'admin',
      password: Puppet::Pops::Types::PSensitiveType::Sensitive.new(password),
    }
  end

  before(:each) do
    allow(context).to receive(:debug)
  end

  describe 'initialize(context, connection_info)' do
    it { expect { transport }.not_to raise_error }
  end

  describe 'verify(context)' do
    context 'with valid credentials' do
      it 'returns' do
        expect { transport.verify(context) }.not_to raise_error
      end
    end

    context 'with invalid credentials' do
      let(:password) { 'invalid' }

      it 'raises an error' do
        expect { transport.verify(context) }.to raise_error RuntimeError, %r{authentication error}
      end
    end
  end

  describe 'facts(context)' do
    let(:facts) { transport.facts(context) }

    it 'returns basic facts' do
      expect(facts).to include(:operatingsystem, :operatingsystemrelease)
    end
  end

  describe 'close(context)' do
    it 'releases resources' do
      transport.close(context)

      expect(transport.instance_variable_get(:@connection_info)).to be_nil
    end
  end
end
