Skip to content

Commit

Permalink
New Email Verification Fields (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpatrick authored Oct 2, 2020
1 parent a7ffeb5 commit dc1a6b5
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 3 deletions.
12 changes: 11 additions & 1 deletion lib/auth0/api/v2/jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,24 @@ def export_users(options = {})
# @see https://auth0.com/docs/api/management/v2#!/Jobs/post_verification_email
# @param user_id [string] The user_id of the user to whom the email will be sent.
# @param client_id [string] Client ID to send an Application-specific email.
# @param identity [hash] Used to verify secondary, federated, and passwordless-email identities.
# * :user_id [string] user_id of the identity.
# * :provider [string] provider of the identity.
#
# @return [json] Returns the job status and properties.
def send_verification_email(user_id, client_id = nil)
def send_verification_email(user_id, client_id = nil, identity: nil)
raise Auth0::InvalidParameter, 'Must specify a user id' if user_id.to_s.empty?

request_params = { user_id: user_id }
request_params[:client_id] = client_id unless client_id.nil?

if identity
unless identity.is_a? Hash
raise Auth0::InvalidParameter, 'Identity must be a hash send an email verification'
end
request_params[:identity] = identity
end

path = "#{jobs_path}/verification-email"
post(path, request_params)
end
Expand Down
13 changes: 12 additions & 1 deletion lib/auth0/api/v2/tickets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ module Tickets
# @param ttl_sec [integer] The ticket's lifetime in seconds starting from the moment of creation.
# After expiration, the ticket cannot be used to verify the user's email. If not specified or if
# you send 0, the Auth0 default lifetime of five days will be applied
# @param identity [hash] Used to verify secondary, federated, and passwordless-email identities.
# * :user_id [string] user_id of the identity.
# * :provider [string] provider of the identity.
#
# @return [json] Returns the created ticket url.
def post_email_verification(user_id, result_url: nil, ttl_sec: nil)
def post_email_verification(user_id, result_url: nil, ttl_sec: nil, identity: nil)
if user_id.to_s.empty?
raise Auth0::InvalidParameter, 'Must supply a valid user id to post an email verification'
end
Expand All @@ -24,6 +27,14 @@ def post_email_verification(user_id, result_url: nil, ttl_sec: nil)
result_url: result_url,
ttl_sec: ttl_sec.is_a?(Integer) ? ttl_sec : nil
}

if identity
unless identity.is_a? Hash
raise Auth0::InvalidParameter, 'Identity must be a hash to post an email verification'
end
request_params[:identity] = identity
end

post(path, request_params)
end

Expand Down
12 changes: 12 additions & 0 deletions spec/integration/lib/auth0/api/v2/api_jobs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@
client.send_verification_email(user['user_id'], Random.new(32).to_s)
end.to raise_error Auth0::BadRequest
end

it 'should raise an error if the user id is empty' do
expect do
client.send_verification_email( '' )
end.to raise_error Auth0::InvalidParameter, 'Must specify a user id'
end

it 'should raise an error if the identity supplied is not a Hash' do
expect do
client.send_verification_email( 'user_id', identity: 'not a hash')
end.to raise_error Auth0::InvalidParameter, 'Identity must be a hash send an email verification'
end
end

after(:all) do
Expand Down
8 changes: 7 additions & 1 deletion spec/integration/lib/auth0/api/v2/api_tickets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@
it 'should raise an error if the user id is empty' do
expect do
client.post_email_verification( '' )
end.to raise_error Auth0::InvalidParameter
end.to raise_error Auth0::InvalidParameter, 'Must supply a valid user id to post an email verification'
end

it 'should raise an error if the identity supplied is not a Hash' do
expect do
client.post_email_verification( '', identity: 'not a hash')
end.to raise_error Auth0::InvalidParameter, 'Must supply a valid user id to post an email verification'
end
end

Expand Down
17 changes: 17 additions & 0 deletions spec/lib/auth0/api/v2/jobs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@
end.not_to raise_error
end

it 'expect client to accept hash identity' do
expect(@instance).to receive(:post).with('/api/v2/jobs/verification-email', user_id: 'user_id',
identity: {
provider: "auth0",
user_id: "user_id"
})
expect {
@instance.send_verification_email('user_id', identity: { provider: "auth0", user_id: "user_id"})
}.not_to raise_error
end

it 'expect client to return nil when calling with a non-hash identity' do
expect { @instance.send_verification_email('user_id', identity: "nonhash") }.to raise_error(
'Identity must be a hash send an email verification'
)
end

it 'should raise an error if the user_id is empty' do
expect do
@instance.send_verification_email('')
Expand Down
17 changes: 17 additions & 0 deletions spec/lib/auth0/api/v2/tickets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@
result_url: nil, ttl_sec: nil)
expect { @instance.post_email_verification('user_id', ttl_sec: "noninteger") }.not_to raise_error
end
it 'expect client to accept hash identity' do
expect(@instance).to receive(:post).with('/api/v2/tickets/email-verification', user_id: 'user_id',
result_url: nil,
ttl_sec: nil,
identity: {
provider: "auth0",
user_id: "user_id"
})
expect {
@instance.post_email_verification('user_id', identity: { provider: "auth0", user_id: "user_id"})
}.not_to raise_error
end
it 'expect client to return nil when calling with a non-hash identity' do
expect { @instance.post_email_verification('user_id', identity: "nonhash") }.to raise_error(
'Identity must be a hash to post an email verification'
)
end
it 'expect client to rasie error when calling with empty body' do
expect { @instance.post_email_verification(nil) }.to raise_error(
'Must supply a valid user id to post an email verification'
Expand Down

0 comments on commit dc1a6b5

Please sign in to comment.