From 5472dfd5d24ba2c756a2a8ec8a85c49d95152cdd Mon Sep 17 00:00:00 2001 From: Tungle Duy Date: Wed, 28 Oct 2026 08:36:13 +0700 Subject: [PATCH] Add RoleDefinition.find_all to list all role definitions Wraps GET /1.0/kb/security/roles, available in Kill Bill 0.25+ (killbill/killbill#2250). Needed by Kaui to list and edit roles (killbill/killbill-admin-ui#161). --- lib/killbill_client/models/role_definition.rb | 6 ++++ spec/killbill_client/role_definition_spec.rb | 36 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 spec/killbill_client/role_definition_spec.rb diff --git a/lib/killbill_client/models/role_definition.rb b/lib/killbill_client/models/role_definition.rb index 369b819..528f8e5 100644 --- a/lib/killbill_client/models/role_definition.rb +++ b/lib/killbill_client/models/role_definition.rb @@ -3,6 +3,12 @@ module Model class RoleDefinition < RoleDefinitionAttributes class << self + def find_all(options = {}) + get "#{Security::KILLBILL_API_SECURITY_PREFIX}/roles", + {}, + options + end + def find_by_name(role_name, options = {}) role = get "#{Security::KILLBILL_API_SECURITY_PREFIX}/roles/#{role_name}", {}, diff --git a/spec/killbill_client/role_definition_spec.rb b/spec/killbill_client/role_definition_spec.rb new file mode 100644 index 0000000..0c725eb --- /dev/null +++ b/spec/killbill_client/role_definition_spec.rb @@ -0,0 +1,36 @@ +require 'spec_helper' + +describe KillBillClient::Model::RoleDefinition do + + def json_response(body) + response = Net::HTTPOK.new('1.1', '200', 'OK') + response['Content-Type'] = 'application/json' + response.instance_variable_set(:@read, true) + response.instance_variable_set(:@body, body) + response + end + + describe '.find_all' do + it 'lists the role definitions' do + body = [{'role' => 'admin', 'permissions' => ['account:create', 'invoice:view']}, + {'role' => 'finance', 'permissions' => ['payment:view']}].to_json + + expect(KillBillClient::API).to receive(:get) + .with('/1.0/kb/security/roles', {}, {:api_key => 'key'}) + .and_return(json_response(body)) + + roles = KillBillClient::Model::RoleDefinition.find_all({:api_key => 'key'}) + + expect(roles.map(&:role)).to eq(%w(admin finance)) + expect(roles.first).to be_a(KillBillClient::Model::RoleDefinition) + expect(roles.first.permissions).to eq(['account:create', 'invoice:view']) + expect(roles.last.permissions).to eq(['payment:view']) + end + + it 'returns an empty list when there are no role definitions' do + allow(KillBillClient::API).to receive(:get).and_return(json_response('[]')) + + expect(KillBillClient::Model::RoleDefinition.find_all).to be_empty + end + end +end