Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pagination and tests for Client Grants #116

Merged
merged 2 commits into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/auth0/api/v2/client_grants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ module ClientGrants

# Retrieves a list of all client grants.
# @see https://auth0.com/docs/api/management/v2#!/client_grants/get_client_grants
#
# @param page [int] Page number to get, 0-based.
# @param per_page [int] Results per page if also passing a page number.
# @return [json] Returns the client grants.
def client_grants
get(client_grants_path)
def client_grants (page: nil, per_page: nil)
request_params = {
page: !page.nil? ? page.to_i : nil,
per_page: !page.nil? && !per_page.nil? ? per_page.to_i : nil
Copy link

@machuga machuga Jul 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to simplify this down to page.nil? || per_page.nil? ? nil : per_page.to_i. There are a lot of symbols going on already, so reducing the cognitive overhead of negated ANDs should help reduce the complexity.

I'll also add that if anyone drops in a boolean to these params, the method will raise since to_i is not implemented on booleans. I'm assuming this logic exists just to cast strings to ints though. And some of the logic seems confusing to read through. Here is a potential alternative to the method that I'm not sure I like any better, but 🤷‍♂️.

def client_grants(page: nil, per_page: nil)
  return get(client_grants_path) if page.nil?

  clean_per_page = per_page.nil? ? nil : per_page.to_i

  get(client_grants_path, page: page.to_i, per_page: clean_per_page) 
end

It explicitly shows that if page is nil we have nothing to do here, then continues on with the logic.

Ideally you could get rid of the last ternary if the logic of passing per_page supports it, but that's something you'd have to check out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good feedback, thank you. I'll see if I can simplify this a little better in a new commit.

}
get(client_grants_path, request_params)
end
alias get_all_client_grants client_grants

Expand Down
33 changes: 18 additions & 15 deletions spec/integration/lib/auth0/api/v2/api_client_grants_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,49 @@
before(:all) do
@client = Auth0Client.new(v2_creds)
@client_id = v2_creds[:client_id]
sleep 1
@existing_client = client.create_client("client#{entity_suffix}")
sleep 1
@existing_client = client.create_client("client-grant-test-#{entity_suffix}")
@audience = "https://#{client.clients[0]['tenant']}.auth0.com/api/v2/"
@scope = [Faker::Lorem.word]
sleep 1
@existing_grant = client.create_client_grant('client_id' => existing_client['client_id'],
'audience' => audience,
'scope' => scope)
@existing_grant = client.create_client_grant(
'client_id' => existing_client['client_id'],
'audience' => audience,
'scope' => scope
)
end

after(:all) do
grants = client.client_grants
grants.each do |grant|
sleep 1
client.delete_client_grant(grant['id'])
end
end

describe '.client_grants' do
let(:client_grants) do
sleep 1
client.client_grants
end

it do
sleep 1
it 'is expected to have a result' do
expect(client_grants.size).to be > 0
end
it do
sleep 1

it 'is expected to match the created grant' do
expect(client_grants).to include(existing_grant)
end

it 'is expected to return the first page of one result' do
results = client.client_grants(
page: 0,
per_page: 1
)
expect(results.first).to equal(results.last)
expect(results.first).to eq(existing_grant)
end
end

describe '.patch_client_grant' do
let(:new_scope) { [Faker::Lorem.word] }
it do
sleep 1
expect(
client.patch_client_grant(
existing_grant['id'],
Expand All @@ -55,7 +59,6 @@

describe '.delete_client_grant' do
it do
sleep 1
expect { client.delete_client_grant(existing_grant['id']) }.to_not raise_error
end
end
Expand Down
19 changes: 17 additions & 2 deletions spec/lib/auth0/api/v2/client_grants_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,28 @@
dummy_instance.extend(Auth0::Api::V2::ClientGrants)
@instance = dummy_instance
end

context '.client_grants' do
it { expect(@instance).to respond_to(:client_grants) }
it { expect(@instance).to respond_to(:get_all_client_grants) }
it 'is expected to send get request to /api/v2/client_grants/' do
expect(@instance).to receive(:get).with('/api/v2/client-grants')

it 'is expected to get /api/v2/client-grants/' do
expect(@instance).to receive(:get).with(
'/api/v2/client-grants',
page: nil,
per_page: nil
)
expect { @instance.client_grants }.not_to raise_error
end

it 'is expected to send get /api/v2/client-grants/ with pagination' do
expect(@instance).to receive(:get).with(
'/api/v2/client-grants',
page: 1,
per_page: 2
)
expect { @instance.client_grants(page: 1, per_page: 2) }.not_to raise_error
end
end

context '.create_client_grant' do
Expand Down