From 4ec1940e547abb99f5a3afe749bb086c24a1f486 Mon Sep 17 00:00:00 2001 From: Steve Hobbs Date: Wed, 5 Oct 2022 15:17:29 +0100 Subject: [PATCH 1/2] feat: support complex field names in export_users --- lib/auth0/api/v2/jobs.rb | 19 +++++++++++++++---- spec/lib/auth0/api/v2/jobs_spec.rb | 20 +++++++++++++++++++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/auth0/api/v2/jobs.rb b/lib/auth0/api/v2/jobs.rb index d1355829..07a2e545 100644 --- a/lib/auth0/api/v2/jobs.rb +++ b/lib/auth0/api/v2/jobs.rb @@ -60,6 +60,9 @@ def import_users(users_file, connection_id, options = {}) # :format [string] The format of the file. Valid values are: "json" and "csv". # :limit [integer] Limit the number of users to export. # :fields [array] A list of fields to be included in the CSV. + # This can either be an array of strings representing field names, or an object. + # If it's a string, it is mapped to the correct { name: '' } object required by the endpoint. + # If it's an object, it is passed through as-is to the endpoint. # If omitted, a set of predefined fields will be exported. # # @return [json] Returns the job status and properties. @@ -109,14 +112,22 @@ def jobs_path @jobs_path ||= '/api/v2/jobs' end - # Map array of field names for export to array of objects - # @param fields [array] Field names to be included in the export - + # Map array of fields for export to array of objects + # @param fields [array] Fields to be included in the export + # This can either be an array of strings representing field names, or an object. + # If it's a string, it is mapped to the correct { name: '' } object required by the endpoint. + # If it's an object, it is passed through as-is to the endpoint. # @return [array] Returns the fields mapped as array of objects for the export_users endpoint def fields_for_export(fields) return nil if fields.to_s.empty? - fields.map { |field| { name: field } } + fields.map { |field| + if field.is_a? String + { name: field } + else + field + end + } end end end diff --git a/spec/lib/auth0/api/v2/jobs_spec.rb b/spec/lib/auth0/api/v2/jobs_spec.rb index a2c33af0..98ca8a1a 100644 --- a/spec/lib/auth0/api/v2/jobs_spec.rb +++ b/spec/lib/auth0/api/v2/jobs_spec.rb @@ -56,7 +56,7 @@ it { expect { @instance.import_users('', 'connnection_id') }.to raise_error('Must specify a valid file') } it { expect { @instance.import_users('users', '') }.to raise_error('Must specify a connection_id') } end - context '.export_users' do + context '.export_users', focus: true do it { expect(@instance).to respond_to(:export_users) } it { expect { @instance.export_users }.not_to raise_error } it 'sends post to /api/v2/jobs/users-exports with correct params' do @@ -67,6 +67,7 @@ format: 'csv', limit: 10 }) + @instance.export_users( fields: ['author'], connection_id: 'test-connection', @@ -74,6 +75,23 @@ limit: 10 ) end + + it 'sends post to /api/v2/jobs/users-exports with export_as field' do + expect(@instance).to receive(:post).with( + '/api/v2/jobs/users-exports', { + fields: [{ name: 'author', export_as: 'writer' }], + connection_id: 'test-connection', + format: 'csv', + limit: 10 + }) + + @instance.export_users( + fields: [{ name: 'author', export_as: 'writer' }], + connection_id: 'test-connection', + format: 'csv', + limit: 10 + ) + end end context '.send_verification_email' do From be860bba2d148ae03a10a6101836ffe4f04cce4b Mon Sep 17 00:00:00 2001 From: Steve Hobbs Date: Wed, 5 Oct 2022 19:24:52 +0100 Subject: [PATCH 2/2] ci: remove focus: true on test context --- spec/lib/auth0/api/v2/jobs_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/lib/auth0/api/v2/jobs_spec.rb b/spec/lib/auth0/api/v2/jobs_spec.rb index 98ca8a1a..02ab4007 100644 --- a/spec/lib/auth0/api/v2/jobs_spec.rb +++ b/spec/lib/auth0/api/v2/jobs_spec.rb @@ -56,7 +56,7 @@ it { expect { @instance.import_users('', 'connnection_id') }.to raise_error('Must specify a valid file') } it { expect { @instance.import_users('users', '') }.to raise_error('Must specify a connection_id') } end - context '.export_users', focus: true do + context '.export_users' do it { expect(@instance).to respond_to(:export_users) } it { expect { @instance.export_users }.not_to raise_error } it 'sends post to /api/v2/jobs/users-exports with correct params' do