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 to Clients and Connections endpoints #113

Merged
merged 3 commits into from
Jun 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 9 additions & 6 deletions lib/auth0/api/v2/clients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ module Clients
attr_reader :clients_path

# Retrieves a list of all client applications. Accepts a list of fields to include or exclude.
# @see https://auth0.com/docs/api/v2#!/clients/get_clients
# @param fields [string] A comma separated list of fields to include or exclude from the result.
# @see https://auth0.com/docs/api/management/v2#!/Clients/get_clients
# @param fields [string|Array] A comma separated list or an array of fields.
# @param include_fields [boolean] True if the fields specified are to be included in the result, false otherwise.
#
# @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 clients applications.
def clients(fields: nil, include_fields: nil)
def clients(fields: nil, include_fields: nil, page: nil, per_page: nil)
include_fields = true if !fields.nil? && include_fields.nil?
request_params = {
fields: fields,
include_fields: include_fields
fields: fields.is_a?(Array) ? fields.join(',') : fields,
include_fields: include_fields,
page: !page.nil? ? page.to_i : nil,
per_page: !page.nil? && !per_page.nil? ? per_page.to_i : nil
}
get(clients_path, request_params)
end
Expand Down
23 changes: 16 additions & 7 deletions lib/auth0/api/v2/connections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@ module Connections

# Retrieves every connection matching the specified strategy. All connections are retrieved if no strategy is
# being specified. Accepts a list of fields to include or exclude in the resulting list of connection objects.
# @see https://auth0.com/docs/api/v2#!/Connections/get_connections
# @param strategy [string] Provide a type of strategy to only retrieve connections with that strategy (e.g. 'ad',
# 'facebook', 'twitter').
# @see https://auth0.com/docs/api/management/v2#!/Connections/get_connections
# @param strategy [string] Strategy to filter connection results.
# @param fields [string] A comma separated list of fields to include or exclude from the result.
# @param include_fields [boolean] True if the fields specified are to be included in the result, false otherwise.
#
# @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 existing connections matching the strategy.
def connections(strategy: nil, fields: nil, include_fields: true)
def connections(
strategy: nil,
fields: nil,
include_fields: nil,
page: nil,
per_page: nil
)
include_fields = true if !fields.nil? && include_fields.nil?
request_params = {
strategy: strategy,
fields: fields,
include_fields: include_fields
fields: fields.is_a?(Array) ? fields.join(',') : fields,
include_fields: include_fields,
page: !page.nil? ? page.to_i : nil,
per_page: !page.nil? && !per_page.nil? ? per_page.to_i : nil
}
get(connections_path, request_params)
end
Expand Down
22 changes: 19 additions & 3 deletions spec/integration/lib/auth0/api/v2/api_clients_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,31 @@
context '#filters' do
it do
sleep 1
expect(client.clients(fields: [:name, :callbacks].join(',')).first).to(include('name', 'callbacks'))
expect(client.clients(
fields: [:name, :callbacks].join(',')
).first).to(include('name', 'callbacks'))
end
it do
sleep 1
expect(client.clients(fields: [:callbacks].join(',')).first).to_not(include('name'))
expect(client.clients(
fields: [:callbacks].join(',')).first
).to_not(include('name'))
end
it do
sleep 1
expect(client.clients(fields: [:callbacks].join(','), include_fields: false).first).to_not(include('callbacks'))
expect(client.clients(
fields: [:callbacks].join(','),
include_fields: false
).first).to_not(include('callbacks'))
end
it do
sleep 1
results = client.clients(
fields: :name,
page: 0,
per_page: 1
)
expect(results.first).to equal(results.last)
end
end
end
Expand Down
55 changes: 50 additions & 5 deletions spec/lib/auth0/api/v2/clients_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,62 @@
dummy_instance.extend(Auth0::Api::V2::Clients)
@instance = dummy_instance
end

context '.clients' do
it { expect(@instance).to respond_to(:clients) }
it { expect(@instance).to respond_to(:get_clients) }
it 'is expected to send get request to /api/v2/clients/' do
expect(@instance).to receive(:get).with('/api/v2/clients', fields: nil, include_fields: nil)

it 'is expected to send get request to the Clients endpoint' do
expect(@instance).to receive(:get).with(
'/api/v2/clients',
fields: nil,
include_fields: nil,
page: nil,
per_page: nil
)
expect { @instance.clients }.not_to raise_error
end
it 'is expected to send get request to /api/v2/clients?fields=name' do
expect(@instance).to receive(:get).with('/api/v2/clients', include_fields: true, fields: [:name])
expect { @instance.clients(fields: [:name], include_fields: true) }.not_to raise_error

it 'is expected to send get request to the Clients endpoint with a name parameter' do
expect(@instance).to receive(:get).with(
'/api/v2/clients',
include_fields: true,
fields: 'name',
page: nil,
per_page: nil
)
expect {
@instance.clients(fields: 'name', include_fields: true)
}.not_to raise_error
end

it 'is expected to send get request to Clients endpoint using an array of fields' do
expect(@instance).to receive(:get).with(
'/api/v2/clients',
include_fields: true,
fields: 'name,app_type',
page: nil,
per_page: nil
)
expect {
@instance.clients(fields: ['name','app_type'], include_fields: true)
}.not_to raise_error
end

it 'is expected to send get request to Clients endpoint with pagination' do
expect(@instance).to receive(:get).with(
'/api/v2/clients',
page: 1,
per_page: 10,
fields: nil,
include_fields: nil
)
expect {
@instance.clients(page: 1, per_page: 10)
}.not_to raise_error
end
end

context '.client' do
it { expect(@instance).to respond_to(:client) }
it 'is expected to send get request to /api/v2/clients/1' do
Expand All @@ -38,6 +82,7 @@
end
it { expect { @instance.create_client('') }.to raise_error 'Must specify a valid client name' }
end

context '.delete_client' do
it { expect(@instance).to respond_to(:delete_client) }
it 'is expected to send delete to /api/v2/clients/1' do
Expand Down
46 changes: 45 additions & 1 deletion spec/lib/auth0/api/v2/connections_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,54 @@
'/api/v2/connections',
strategy: nil,
fields: nil,
include_fields: true
include_fields: nil,
page: nil,
per_page: nil
)
expect { @instance.connections }.not_to raise_error
end

it 'is expected to send get request to /api/v2/connections?fields=name' do
expect(@instance).to receive(:get).with(
'/api/v2/connections',
include_fields: true,
fields: 'name',
strategy: nil,
page: nil,
per_page: nil
)
expect {
@instance.connections(fields: 'name', include_fields: true)
}.not_to raise_error
end

it 'is expected to convert fields param from Array to string' do
expect(@instance).to receive(:get).with(
'/api/v2/connections',
include_fields: true,
fields: 'name,strategy',
strategy: nil,
page: nil,
per_page: nil
)
expect {
@instance.connections(fields: ['name','strategy'], include_fields: true)
}.not_to raise_error
end

it 'is expected to add pagination' do
expect(@instance).to receive(:get).with(
'/api/v2/connections',
page: 1,
per_page: 10,
strategy: nil,
fields: nil,
include_fields: nil
)
expect {
@instance.connections(page: 1, per_page: 10)
}.not_to raise_error
end
end

context '.create_connection' do
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Dir['./spec/support/*.rb'].each { |f| require f }

def entity_suffix
(ENV['TRAVIS_JOB_ID'] || 'local').delete('_')
(ENV['TRAVIS_JOB_ID'] || ENV['TEST_ENTITY_SUFFIX'] || 'rubytest').delete('_')
end

puts "Entity suffix is #{entity_suffix}"
Expand Down
5 changes: 2 additions & 3 deletions spec/spec_helper_full.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
v2_client.delete_client(client['client_id'])
}
v2_client
.users
.select { |user| user['email'].split('@').first.include? entity_suffix }
.users(q: "email:#{entity_suffix}*", fields: 'user_id', page: 0, per_page: 50)
.each { |user|
sleep 1
sleep 0.6
v2_client.delete_user(user['user_id'])
}
puts "Finished cleaning up for #{entity_suffix}"
Expand Down