Sha256: 86c7b72f7cd772d37f78cf2364706492eefecd2b395a3ae89427c48463838822

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

require 'spec_helper'

describe Conjur::HasAttributes do
  class ObjectWithAttributes
    include Conjur::HasAttributes

    def username; 'alice'; end
    def url; 'http://example.com/the-object'; end
  end

  def new_object
    ObjectWithAttributes.new
  end

  let(:object) { new_object }
  let(:second_object) { new_object }
  let(:attributes) { { 'id' => 'the-id' } }
  let(:rbac_resource_resource) { double(:rbac_resource_resource, url: object.url) }

  before {
    allow(object).to receive(:rbac_resource_resource).and_return(rbac_resource_resource)
    allow(second_object).to receive(:rbac_resource_resource).and_return(rbac_resource_resource)
    expect(rbac_resource_resource).to receive(:get).with(no_args).and_return(double(:response, body: attributes.to_json))
  }

  it "should fetch attributes from the server" do
    expect(object.attributes).to eq(attributes)
  end

  describe "caching" do
    let(:cache) {
      Struct.new(:dummy) do
        def table; @table ||= Hash.new; end

        def fetch_attributes cache_key, &block
          table[cache_key] || table[cache_key] = yield
        end
      end.new
    }

    around do |example|
      saved = Conjur.cache
      Conjur.cache = cache

      begin
        example.run
      ensure
        Conjur.cache = saved
      end
    end
    context "enabled" do
      it "caches the attributes across objects" do
        expect(object.attributes).to eq(attributes)
        expect(second_object.attributes).to eq(attributes)
        expect(cache.table).to eq({
          "alice.http://example.com/the-object" => attributes
        })
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
conjur-api-5.0.0 spec/has_attributes_spec.rb
conjur-api-5.0.0.rc1 spec/has_attributes_spec.rb