spec/command/resources_spec.rb in conjur-cli-5.5.0 vs spec/command/resources_spec.rb in conjur-cli-5.6.3

- old
+ new

@@ -5,10 +5,11 @@ let (:full_resource_id) { [account, KIND, ID].join(":") } let (:resource_instance) { double(attributes: resource_attributes) } let (:resource_attributes) { { "some" => "attribute"} } before :each do + allow(api).to receive(:resource).and_call_original allow(api).to receive(:resource).with(full_resource_id).and_return(resource_instance) end def invoke_silently expect { invoke }.to write @@ -133,21 +134,102 @@ invoke_silently end it { expect { invoke }.to write "Ownership granted" } end - describe_command "resource:permitted_roles #{KIND}:#{ID} #{PRIVILEGE}" do - let(:roles_list) { %W[klaatu barada nikto] } - before(:each) { - allow(resource_instance).to receive(:permitted_roles).and_return(roles_list) + context "list" do + def make_resource(kind, identifier, attributes) + authz_host = "http://conjur/authz" + credentials = {} + id = "the-account:#{kind}:#{identifier}" + api.resource(id).tap do |resource| + resource.attributes = attributes.merge(resourceid: id) + end + end + let(:resources) { + [ + make_resource("food", "bacon", {}), + make_resource("food", "eggs", {}) + ] } - it_behaves_like "it obtains resource by id" - it "calls resource.permitted_roles(#{PRIVILEGE}" do - expect(resource_instance).to receive(:permitted_roles) - invoke_silently + let(:resource_ids) { + [ + "the-account:food:bacon", + "the-account:food:eggs" + ] + } + describe_command "resource:list" do + it "displays JSONised list of resources" do + expect(api).to receive(:resources).with({}).and_return(resources) + expect(JSON.parse( expect { invoke }.to write )).to eq([ + {"resourceid"=>"the-account:food:bacon", "annotations"=>{}}, + {"resourceid"=>"the-account:food:eggs", "annotations"=>{}} + ]) + end end - it "displays JSONised list of roles" do - expect(JSON.parse( expect { invoke }.to write )).to eq(roles_list) + describe_command "resource:list -i -k jobs" do + it "searches by resource kind" do + expect(api).to receive(:resources).with({kind: 'jobs'}).and_return(resources) + expect(JSON.parse( expect { invoke }.to write )).to eq(resource_ids) + end + end + describe_command "resource:list -i" do + it "displays resource ids" do + expect(api).to receive(:resources).with({}).and_return(resources) + expect(JSON.parse( expect { invoke }.to write )).to eq(resource_ids) + end + end + { search: "hamster", offset: 10, limit: 10 }.each do |k,v| + describe_command "resource:list -i --#{k} #{v}" do + it "displays the items" do + expect(api).to receive(:resources).with({k => v.to_s}).and_return(resources) + expect(JSON.parse( expect { invoke }.to write )).to eq(resource_ids) + end + end + end + end + + context "permitted roles" do + let(:roles_list) { %W[klaatu barada nikto] } + describe_command "resource:permitted_roles #{KIND}:#{ID} #{PRIVILEGE}" do + before(:each) { + allow(resource_instance).to receive(:permitted_roles).and_return(roles_list) + } + it_behaves_like "it obtains resource by id" + it "calls resource.permitted_roles(#{PRIVILEGE}" do + expect(resource_instance).to receive(:permitted_roles).with(PRIVILEGE, {}) + invoke_silently + end + it "displays JSONised list of roles" do + expect(JSON.parse( expect { invoke }.to write )).to eq(roles_list) + end + end + + describe_command "resource:permitted_roles --count #{KIND}:#{ID} #{PRIVILEGE}" do + before { + expect(resource_instance).to receive(:permitted_roles).with(PRIVILEGE, count: true). + and_return(12) + } + it_behaves_like "it obtains resource by id" + it "calls resource.permitted_roles(#{PRIVILEGE}" do + invoke_silently + end + it "displays role count" do + expect(JSON.parse( expect { invoke }.to write )).to eq(12) + end + end + + + describe_command "resource:permitted_roles -s frontend #{KIND}:#{ID} #{PRIVILEGE}" do + let(:roles_list) { %W[klaatu barada nikto] } + before { + expect(resource_instance).to receive(:permitted_roles).with(PRIVILEGE, search: "frontend"). + and_return(roles_list) + } + it_behaves_like "it obtains resource by id" + it "displays JSONised list of roles" do + expect(JSON.parse( expect { invoke }.to write )).to eq(roles_list) + end end end context "interactivity" do subject { Conjur::Command::Resources }