diff --git a/.rubocop.yml b/.rubocop.yml index 68ed82f2..8217dd58 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,5 +1,7 @@ +inherit_from: .rubocop_todo.yml AllCops: RunRailsCops: true Exclude: - bin/**/* - vendor/**/* + - auth0.gemspec diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 00000000..875f3f2a --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,7 @@ +# Configuration parameters: CountComments. +Metrics/MethodLength: + Max: 15 + +# Configuration parameters: AllowURI, URISchemes. +Metrics/LineLength: + Max: 121 diff --git a/Guardfile b/Guardfile index b7bb81d7..66600348 100644 --- a/Guardfile +++ b/Guardfile @@ -1,7 +1,8 @@ scope group: :unit_test group :unit_test do - guard 'rspec', cmd: "bundle exec rspec -P \"spec/lib/auth0/**/*#{ENV['PATTERN']}*_spec.rb\" --drb --format Fuubar --color" do + guard 'rspec', cmd: + "bundle exec rspec -P \"spec/lib/auth0/**/*#{ENV['PATTERN']}*_spec.rb\"--drb --format Fuubar --color" do # run every updated spec file watch(%r{^spec/.+_spec\.rb$}) # run the lib specs when a file in lib/ changes @@ -12,7 +13,8 @@ group :unit_test do end group :integration do - guard 'rspec', cmd: "MODE=full bundle exec rspec -P \"spec/integration/**/*#{ENV['PATTERN']}*_spec.rb\" --drb --format Fuubar --color" do + guard 'rspec', cmd: + "MODE=full bundle exec rspec -P \"spec/integration/**/*#{ENV['PATTERN']}*_spec.rb\" --drb --format Fuubar --color" do # run every updated spec file watch(%r{^spec/.+_spec\.rb$}) # run the lib specs when a file in lib/ changes @@ -23,7 +25,8 @@ group :integration do end group :full do - guard 'rspec', cmd: 'MODE=full bundle exec rspec --drb --format Fuubar --color' do + guard 'rspec', cmd: + 'MODE=full bundle exec rspec --drb --format Fuubar --color' do # run every updated spec file watch(%r{^spec/.+_spec\.rb$}) # run the lib specs when a file in lib/ changes diff --git a/Rakefile b/Rakefile index d38b5879..fa46c1ec 100644 --- a/Rakefile +++ b/Rakefile @@ -20,7 +20,7 @@ begin desc 'Run All Suites' RSpec::Core::RakeTask.new(:all) - task default: [ :rubocop, :spec ] + task default: [:rubocop, :spec] rescue LoadError - # No RSpec + puts 'Load Error - No RSpec' end diff --git a/lib/auth0/api/authentication_endpoints.rb b/lib/auth0/api/authentication_endpoints.rb index 0637b9be..79a8ad0a 100644 --- a/lib/auth0/api/authentication_endpoints.rb +++ b/lib/auth0/api/authentication_endpoints.rb @@ -3,6 +3,8 @@ module Api # {https://auth0.com/docs/auth-api} # Describing functionality of auth0 authentication endpoints module AuthenticationEndpoints + UP_AUTH = 'Username-Password-Authentication' + # {https://auth0.com/docs/auth-api#!#post--oauth-access_token} def obtain_access_token request_params = { @@ -44,7 +46,8 @@ def impersonate(user_id, app_client_id, impersonator_id, options) end # {https://auth0.com/docs/auth-api#!#post--oauth-ro} - def login(username, password, scope = 'openid', id_token = nil, connection_name = 'Username-Password-Authentication') + + def login(username, password, scope = 'openid', id_token = nil, connection_name = UP_AUTH) request_params = { client_id: @client_id, username: username, @@ -58,7 +61,7 @@ def login(username, password, scope = 'openid', id_token = nil, connection_name end # {https://auth0.com/docs/auth-api#!#post--dbconnections-signup} - def signup(email, password, connection_name = 'Username-Password-Authentication') + def signup(email, password, connection_name = UP_AUTH) request_params = { client_id: @client_id, email: email, @@ -69,7 +72,7 @@ def signup(email, password, connection_name = 'Username-Password-Authentication' end # {https://auth0.com/docs/auth-api#!#post--dbconnections-change_password} - def change_password(email, password, connection_name = 'Username-Password-Authentication') + def change_password(email, password, connection_name = UP_AUTH) request_params = { client_id: @client_id, email: email, diff --git a/lib/auth0/api/v1/connections.rb b/lib/auth0/api/v1/connections.rb index 7f2cb71d..6cc82c62 100644 --- a/lib/auth0/api/v1/connections.rb +++ b/lib/auth0/api/v1/connections.rb @@ -30,8 +30,7 @@ def create_connection(connection_name, strategy, tenant_domain, domain_aliases = strategy: strategy, options: { tenant_domain: tenant_domain, - domain_aliases: domain_aliases - } + domain_aliases: domain_aliases } } post(path, request_params) end diff --git a/lib/auth0/api/v1/logs.rb b/lib/auth0/api/v1/logs.rb index 08afe3dc..67d372a8 100644 --- a/lib/auth0/api/v1/logs.rb +++ b/lib/auth0/api/v1/logs.rb @@ -3,21 +3,15 @@ module Api module V1 # {https://auth0.com/docs/api#logs} module Logs - # {https://auth0.com/docs/api#!#get--api-logs-page--number--per_page--items--sort--field----1-1--fields--fields--exclude_fields-true-false-} - # - # {https://auth0.com/docs/api#!#get--api-logs-search--criteria-} - # - # {https://auth0.com/docs/api#!#get--api-logs-from--checkpointId--take--count-} + # https://auth0.com/docs/api/v1#!#logs def logs(options = {}) acceptable_params = %i(take from search_criteria page per_page sort fields exclude_fields) options.reject! do |key, value| - next unless key.nil? || \ - value.nil? || \ - !acceptable_params.include?(key.to_sym) + next unless key.nil? || value.nil? || !acceptable_params.include?(key.to_sym) warn "#{key} is not in acceptable params list: #{acceptable_params}" true end - path = '/api/logs?' + URI.encode_www_form(options) + path = "/api/logs?#{URI.encode_www_form(options)}" get(path) end diff --git a/lib/auth0/api/v1/users.rb b/lib/auth0/api/v1/users.rb index 912488a9..a8355600 100644 --- a/lib/auth0/api/v1/users.rb +++ b/lib/auth0/api/v1/users.rb @@ -1,5 +1,6 @@ module Auth0 module Api + # rubocop:disable Metrics/ModuleLength module V1 # {https://auth0.com/docs/api#users} module Users @@ -141,7 +142,7 @@ def delete_users # {https://auth0.com/docs/api#!#delete--api-users--user_id-} def delete_user(user_id) - fail Auth0::MissingUserId, 'if you want to remove all users user delete_users method' if user_id.to_s.empty? + fail Auth0::MissingUserId, 'if you want to remove all users use delete_users method' if user_id.to_s.empty? path = "/api/users/#{user_id}" delete(path) end diff --git a/lib/auth0/api/v2/clients.rb b/lib/auth0/api/v2/clients.rb index 15adfd81..a66ca086 100644 --- a/lib/auth0/api/v2/clients.rb +++ b/lib/auth0/api/v2/clients.rb @@ -20,7 +20,7 @@ def create_client(name, options = {}) # https://auth0.com/docs/apiv2#!/clients/get_clients_by_id def client(client_id, options = {}) - path = '/api/v2/clients/' + client_id.to_s + path = "/api/v2/clients/#{client_id}" get(path, options) end diff --git a/lib/auth0/api/v2/connections.rb b/lib/auth0/api/v2/connections.rb index f66c2eb1..cecc7d4c 100644 --- a/lib/auth0/api/v2/connections.rb +++ b/lib/auth0/api/v2/connections.rb @@ -1,6 +1,7 @@ module Auth0 module Api module V2 + # https://auth0.com/docs/api/v2#!/Connections module Connections def connections(strategy: nil, fields: nil, include_fields: true) request_params = { @@ -20,7 +21,7 @@ def create_connection(body) end def connection(connection_id, fields: nil, include_fields: true) - path = '/api/v2/connections/' + connection_id.to_s + path = "/api/v2/connections/#{connection_id}" request_params = { fields: fields, include_fields: include_fields @@ -36,7 +37,7 @@ def delete_connection(connection_id) def update_connection(connection_id, body) fail Auth0::MissingConnectionId, 'you must specify a connection id' if connection_id.to_s.empty? - path = '/api/v2/connections/' + connection_id.to_s + path = "/api/v2/connections/#{connection_id}" patch(path, body) end end diff --git a/lib/auth0/api/v2/jobs.rb b/lib/auth0/api/v2/jobs.rb index 22e7e913..ad52b679 100644 --- a/lib/auth0/api/v2/jobs.rb +++ b/lib/auth0/api/v2/jobs.rb @@ -9,9 +9,11 @@ def get_job(job_id) get(path) end - # HTTParty doesn't support multipart upload, will move this functionality to a separate PR + # HTTParty doesn't support multipart upload, will move this + # functionality to a separate PR # https://auth0.com/docs/apiv2#!/jobs/post_users_imports - def create_job(_users_file, _connection_name) + # rubocop:disable UnusedMethodArgument + def create_job(users_file, connection_name) fail NotImplementedError end end diff --git a/lib/auth0/api/v2/users.rb b/lib/auth0/api/v2/users.rb index 17067cbc..8cb6d048 100644 --- a/lib/auth0/api/v2/users.rb +++ b/lib/auth0/api/v2/users.rb @@ -38,7 +38,7 @@ def delete_users # https://auth0.com/docs/apiv2#!/users/get_users_by_id def user(user_id, fields: nil) - path = '/api/v2/users/' + user_id.to_s + path = "/api/v2/users/#{user_id}" request_params = { fields: fields } @@ -48,13 +48,13 @@ def user(user_id, fields: nil) # https://auth0.com/docs/apiv2#!/users/delete_users_by_id def delete_user(user_id) fail Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.nil? || user_id.to_s.empty? - path = '/api/v2/users/' + user_id.to_s + path = "/api/v2/users/#{user_id}" delete(path) end # https://auth0.com/docs/apiv2#!/users/patch_users_by_id def patch_user(user_id, options) - path = '/api/v2/users/' + user_id + path = "/api/v2/users/#{user_id}" patch(path, options) end diff --git a/lib/auth0/client.rb b/lib/auth0/client.rb index 9a68215e..940bc5de 100644 --- a/lib/auth0/client.rb +++ b/lib/auth0/client.rb @@ -1,7 +1,9 @@ -# Main class -# All Api calls are suposed to return hashes, but delete actions return strings. -class Auth0::Client - include Auth0::Mixins - include HTTParty - base_uri 'http://auth0.com' +module Auth0 + # Main class + # All Api calls are suposed to return hashes, but delete actions return strings. + class Client + include Auth0::Mixins + include HTTParty + base_uri 'http://auth0.com' + end end diff --git a/lib/auth0/exception.rb b/lib/auth0/exception.rb index f969774a..1dc2249b 100644 --- a/lib/auth0/exception.rb +++ b/lib/auth0/exception.rb @@ -3,26 +3,30 @@ module Auth0 # if you want to catch all exceptions, then you should use this one. # Network exceptions are not included class Exception < StandardError; end + # exception for unauthorized requests, if you see it, + # probably Bearer Token is not set correctly + class Unauthorized < Auth0::Exception; end + # exception for not found resource, you query for an + # unexistent resource, or wrong path + class NotFound < Auth0::Exception; end + # exception for unknown error + class Unsupported < Auth0::Exception; end + # exception for server error + class ServerError < Auth0::Exception; end + # exception for incorrect request, you've sent wrong params + class BadRequest < Auth0::Exception; end + # exception for unset user_id, this might cause removal of + # all users, or other unexpected bahaviour + class MissingUserId < Auth0::Exception; end + # exception for an unset connection_id + class MissingConnectionId < Auth0::Exception; end + # Api v2 access denied + class AccessDenied < Auth0::Exception; end + # Invalid parameter passed, e.g. empty where ID is required + class InvalidParameter < Auth0::Exception; end + # Invalid Auth0 credentials either client_id/secret for API v1 + # or JWT for API v2/ + class InvalidCredentials < Auth0::Exception; end + # Invalid Auth0 API namespace + class InvalidApiNamespace < Auth0::Exception; end end -# exception for unauthorized requests, if you see it, probably Bearer Token is not set correctly -class Auth0::Unauthorized < Auth0::Exception; end -# exception for not found resource, you query for an unexistent resource, or wrong path -class Auth0::NotFound < Auth0::Exception; end -# exception for unknown error -class Auth0::Unsupported < Auth0::Exception; end -# exception for server error -class Auth0::ServerError < Auth0::Exception; end -# exception for incorrect request, you've sent wrong params -class Auth0::BadRequest < Auth0::Exception; end -# exception for unset user_id, this might cause removal of all users, or other unexpected bahaviour -class Auth0::MissingUserId < Auth0::Exception; end -# exception for an unset connection_id -class Auth0::MissingConnectionId < Auth0::Exception; end -# Api v2 access denied -class Auth0::AccessDenied < Auth0::Exception; end -# Invalid parameter passed, e.g. empty where ID is required -class Auth0::InvalidParameter < Auth0::Exception; end -# Invalid Auth0 credentials either client_id/secret for API v1 or JWT for API v2/ -class Auth0::InvalidCredentials < Auth0::Exception; end -# Invalid Auth0 API namespace -class Auth0::InvalidApiNamespace < Auth0::Exception; end diff --git a/lib/auth0/mixins.rb b/lib/auth0/mixins.rb index 73222e93..bcc92902 100644 --- a/lib/auth0/mixins.rb +++ b/lib/auth0/mixins.rb @@ -5,8 +5,10 @@ require 'auth0/api/authentication_endpoints' require 'auth0/api/v1' require 'auth0/api/v2' -# Collecting dependencies here -module Auth0::Mixins - include Auth0::Mixins::HTTPartyProxy - include Auth0::Mixins::Initializer +module Auth0 + # Collecting dependencies here + module Mixins + include Auth0::Mixins::HTTPartyProxy + include Auth0::Mixins::Initializer + end end diff --git a/lib/auth0/mixins/httparty_proxy.rb b/lib/auth0/mixins/httparty_proxy.rb index a29653b8..c12461a8 100644 --- a/lib/auth0/mixins/httparty_proxy.rb +++ b/lib/auth0/mixins/httparty_proxy.rb @@ -1,12 +1,13 @@ module Auth0 module Mixins - # here's the proxy for HTTParty, we're building all request on that gem for now, if you want to feel free to use your own http client + # here's the proxy for HTTParty, we're building all request on that gem + # for now, if you want to feel free to use your own http client module HTTPartyProxy # proxying requests from instance methods to HTTParty class methods %i(get post put patch delete).each do |method| define_method(method) do |path, body = {}| safe_path = URI.escape(path) - body = body.delete_if { |_k, v| v.nil? } + body = body.delete_if { |_, v| v.nil? } if method == :get result = self.class.send(method, safe_path, query: body) else diff --git a/lib/auth0/mixins/initializer.rb b/lib/auth0/mixins/initializer.rb index 972cfd25..47db3ea5 100644 --- a/lib/auth0/mixins/initializer.rb +++ b/lib/auth0/mixins/initializer.rb @@ -6,19 +6,14 @@ module Initializer # accepts hash as parameter # you can get all required fields from here: https://auth0.com/docs/auth-api # - # To run using api v2, pass api_version: 2 when creating a client + # By Default API v2 def initialize(config) options = Hash[config.map { |(k, v)| [k.to_sym, v] }] - domain = api_domain options - fail InvalidApiNamespace, 'Api namespace must supply an API domain' if domain.nil? - self.class.base_uri "https://#{domain}" + self.class.base_uri base_url(options) self.class.headers client_headers(config) extend Auth0::Api::AuthenticationEndpoints @client_id = options[:client_id] - initialize_v2(options) if api_v2?(options) - initialize_v1(options) if api_v1?(options) - fail InvalidCredentials, 'Must supply a valid API token' if @token.nil? - self.class.headers 'Authorization' => "Bearer #{@token}" + initialize_api(options) end # including initializer in top of klass @@ -28,6 +23,18 @@ def self.included(klass) private + def initialize_api(options) + api_v1?(options) ? initialize_v1(options) : initialize_v2(options) + fail InvalidCredentials, 'Must supply a valid API token' if @token.nil? + self.class.headers 'Authorization' => "Bearer #{@token}" + end + + def base_url(options) + domain = options[:domain] || options[:namespace] + fail InvalidApiNamespace, 'Api namespace must supply an API domain' if domain.nil? + "https://#{domain}" + end + def client_headers(config) client_info = JSON.dump(name: 'ruby-auth0', version: Auth0::VERSION) @@ -43,10 +50,6 @@ def client_headers(config) headers end - def api_domain(options) - options[:domain] || options[:namespace] - end - def initialize_v2(options) extend Auth0::Api::V2 @token = options[:access_token] || options[:token] @@ -59,10 +62,6 @@ def initialize_v1(options) @token = obtain_access_token end - def api_v2?(options) - options[:protocols].to_s.include?('v2') || options[:api_version] == 2 - end - def api_v1?(options) version = options[:api_version] || 1 protocol = options[:protocols].to_s diff --git a/lib/auth0/version.rb b/lib/auth0/version.rb index d5accbfb..1693e7fa 100644 --- a/lib/auth0/version.rb +++ b/lib/auth0/version.rb @@ -1,4 +1,4 @@ +# current version of gem module Auth0 - # current version of gem VERSION = '3.6.1' end diff --git a/spec/integration/lib/auth0/api/v1/api_users_spec.rb b/spec/integration/lib/auth0/api/v1/api_users_spec.rb index 2e177c23..5ab58b09 100644 --- a/spec/integration/lib/auth0/api/v1/api_users_spec.rb +++ b/spec/integration/lib/auth0/api/v1/api_users_spec.rb @@ -5,10 +5,7 @@ let(:email) { "#{entity_suffix}#{Faker::Internet.safe_email(username)}" } let(:password) { Faker::Internet.password } let(:connection) { 'Username-Password-Authentication' } - let!(:user) do - client.create_user(email, password, connection, 'username' => username, - 'email_verified' => false) - end + let!(:user) { client.create_user(email, password, connection, 'username' => username, 'email_verified' => false) } describe '.users' do let(:users) { client.users } diff --git a/spec/integration/lib/auth0/api/v2/api_clients_spec.rb b/spec/integration/lib/auth0/api/v2/api_clients_spec.rb index fd2fe5e3..c19d4470 100644 --- a/spec/integration/lib/auth0/api/v2/api_clients_spec.rb +++ b/spec/integration/lib/auth0/api/v2/api_clients_spec.rb @@ -6,9 +6,18 @@ it { expect(client.clients).to_not be_empty } - it { expect(client.create_client(client_name, custom_login_page_on: false)).to include('name' => client_name, 'custom_login_page_on' => false) } + it do + expect(client.create_client(client_name, custom_login_page_on: false)).to( + include('name' => client_name, 'custom_login_page_on' => false)) + end - it { expect(client.patch_client(existing_client['client_id'], custom_login_page_on: false, sso: true)).to include('custom_login_page_on' => false, 'sso' => true) } + it do + expect( + client.patch_client( + existing_client['client_id'], + custom_login_page_on: false, + sso: true)).to(include('custom_login_page_on' => false, 'sso' => true)) + end it { expect { client.delete_client(existing_client['client_id']) }.to_not raise_error } diff --git a/spec/integration/lib/auth0/api/v2/api_connections_spec.rb b/spec/integration/lib/auth0/api/v2/api_connections_spec.rb index c2a5a32e..87519eaf 100644 --- a/spec/integration/lib/auth0/api/v2/api_connections_spec.rb +++ b/spec/integration/lib/auth0/api/v2/api_connections_spec.rb @@ -23,7 +23,10 @@ context '#filters' do it { expect(client.connections(strategy: strategy).size).to be > 0 } it { expect(client.connections(strategy: strategy, fields: [:name].join(',')).first).to include('name') } - it { expect(client.connections(strategy: strategy, fields: [:name].join(','), include_fields: false).first).to_not include('name') } + it do + expect(client.connections(strategy: strategy, fields: [:name].join(','), include_fields: false).first).to_not( + include('name')) + end end end @@ -34,7 +37,10 @@ context '#filters' do it { expect(client.connection(connection['id'], fields: [:name, :id].join(','))).to include('id', 'name') } - it { expect(client.connection(connection['id'], fields: [:name, :id].join(','), include_fields: false)).to_not include('id', 'name') } + it do + expect(client.connection(connection['id'], fields: [:name, :id].join(','), include_fields: false)).to_not( + include('id', 'name')) + end end end @@ -42,21 +48,16 @@ let(:subject) { connection } it { should include('id', 'name') } - it do - should include( - 'name' => connection['name'] - ) - end + it { should include('name' => connection['name']) } end describe '.delete_connection' do it { expect { client.delete_connection connection['id'] }.to_not raise_error } - it { expect { client.delete_connection '' }.to raise_error(Auth0::MissingConnectionId) } end describe '.update_connection' do new_name = SecureRandom.uuid[0..25] - it { expect(client.update_connection(connection['id'], { 'name' => new_name })).to include('name' => new_name) } + it { expect(client.update_connection(connection['id'], 'name' => new_name)).to include('name' => new_name) } end end diff --git a/spec/integration/lib/auth0/api/v2/api_users_spec.rb b/spec/integration/lib/auth0/api/v2/api_users_spec.rb index cd38bb45..9071804c 100644 --- a/spec/integration/lib/auth0/api/v2/api_users_spec.rb +++ b/spec/integration/lib/auth0/api/v2/api_users_spec.rb @@ -6,11 +6,11 @@ let(:password) { Faker::Internet.password } let(:connection) { 'Username-Password-Authentication' } let!(:user) do - client.create_user(username, 'email' => email, - 'password' => password, - 'email_verified' => false, - 'connection' => connection, - 'app_metadata' => {}) + client.create_user(username, 'email' => email, + 'password' => password, + 'email_verified' => false, + 'connection' => connection, + 'app_metadata' => {}) end describe '.users' do @@ -20,7 +20,10 @@ context '#filters' do it { expect(client.users(per_page: 1).size).to be 1 } - it { expect(client.users(per_page: 1, fields: [:picture, :email, :user_id].join(',')).first).to include('email', 'user_id', 'picture') } + it do + expect(client.users(per_page: 1, fields: [:picture, :email, :user_id].join(',')).first).to( + include('email', 'user_id', 'picture')) + end it { expect(client.users(per_page: 1, fields: [:email].join(',')).first).to_not include('user_id', 'picture') } end end @@ -31,7 +34,10 @@ it { should include('email' => email, 'name' => username) } context '#filters' do - it { expect(client.user(user['user_id'], fields: [:picture, :email, :user_id].join(','))).to include('email', 'user_id', 'picture') } + it do + expect(client.user(user['user_id'], fields: [:picture, :email, :user_id].join(','))).to( + include('email', 'user_id', 'picture')) + end it { expect(client.user(user['user_id'], fields: [:email].join(','))).to_not include('user_id', 'picture') } end end @@ -40,21 +46,15 @@ let(:subject) { user } it { should include('user_id', 'identities') } - it do - should include( - 'email' => email, - 'email_verified' => false - ) - end + it { expect(client.patch_user(user['user_id'], 'email_verified' => true)).to include('email_verified' => true) } end describe '.delete_user' do it { expect { client.delete_user user['user_id'] }.to_not raise_error } - it { expect { client.delete_user '' }.to raise_error(Auth0::MissingUserId) } end describe '.patch_user' do - it { expect(client.patch_user(user['user_id'], 'email_verified' => true)).to include('email_verified' => true) } + it { expect(client.patch_user(user['user_id'], 'email_verified' => true)).to(include('email_verified' => true)) } end end diff --git a/spec/integration/lib/auth0/auth0_client_spec.rb b/spec/integration/lib/auth0/auth0_client_spec.rb index 693b515c..10eb1e19 100644 --- a/spec/integration/lib/auth0/auth0_client_spec.rb +++ b/spec/integration/lib/auth0/auth0_client_spec.rb @@ -11,16 +11,25 @@ end it_should_behave_like 'invalid credentials', { namespace: 'samples.auth0.com' }, Auth0::InvalidCredentials - it_should_behave_like 'invalid credentials', { namespace: 'samples.auth0.com', client_id: 'client_id' }, Auth0::InvalidCredentials - it_should_behave_like 'invalid credentials', { namespace: 'samples.auth0.com', client_secret: 'secret' }, Auth0::InvalidCredentials - it_should_behave_like 'invalid credentials', { namespace: 'samples.auth0.com', api_version: 2 }, Auth0::InvalidCredentials + it_should_behave_like 'invalid credentials', { + namespace: 'samples.auth0.com', client_id: 'client_id' }, Auth0::InvalidCredentials + it_should_behave_like 'invalid credentials', { + namespace: 'samples.auth0.com', client_secret: 'secret' }, Auth0::InvalidCredentials + it_should_behave_like 'invalid credentials', { + namespace: 'samples.auth0.com', api_version: 2 }, Auth0::InvalidCredentials it_should_behave_like 'invalid credentials', {} it_should_behave_like 'invalid credentials', api_version: 2 it_should_behave_like 'invalid credentials', api_version: 1 - it_should_behave_like 'invalid credentials', { client_id: 'client_id', client_secret: 'secret' }, Auth0::InvalidApiNamespace - it_should_behave_like 'invalid credentials', { api_version: 2, token: 'token' }, Auth0::InvalidApiNamespace + it_should_behave_like 'invalid credentials', { + client_id: 'client_id', client_secret: 'secret' }, Auth0::InvalidApiNamespace + it_should_behave_like 'invalid credentials', { + api_version: 2, token: 'token' }, Auth0::InvalidApiNamespace - let(:valid_v1_credentials) { { client_id: ENV['CLIENT_ID'], client_secret: ENV['CLIENT_SECRET'], domain: ENV['DOMAIN'] } } + let(:valid_v1_credentials) do + { client_id: ENV['CLIENT_ID'], + client_secret: ENV['CLIENT_SECRET'], + domain: ENV['DOMAIN'] } + end let(:token) { ENV['MASTER_JWT'] } let(:v2_credentials) { { domain: ENV['DOMAIN'], api_version: 2 } } @@ -50,7 +59,7 @@ end it 'has the correct headers present' do - expect(headers.keys.sort).to eql ['Auth0-Client', 'Authorization', 'Content-Type', 'User-Agent'] + expect(headers.keys.sort).to eql(['Auth0-Client', 'Authorization', 'Content-Type', 'User-Agent']) end it 'uses the correct access token' do diff --git a/spec/lib/auth0/api/authentication_endpoints_spec.rb b/spec/lib/auth0/api/authentication_endpoints_spec.rb index 2405f93e..6f8a45ac 100644 --- a/spec/lib/auth0/api/authentication_endpoints_spec.rb +++ b/spec/lib/auth0/api/authentication_endpoints_spec.rb @@ -9,9 +9,11 @@ context '.obtain_access_token' do it { expect(@instance).to respond_to(:obtain_access_token) } it "is expected to make post request to '/oauth/token'" do - allow(@instance).to receive(:post).with('/oauth/token', client_id: nil, client_secret: nil, grant_type: 'client_credentials') + allow(@instance).to receive(:post).with( + '/oauth/token', client_id: nil, client_secret: nil, grant_type: 'client_credentials') .and_return('access_token' => 'AccessToken') - expect(@instance).to receive(:post).with('/oauth/token', client_id: nil, client_secret: nil, grant_type: 'client_credentials') + expect(@instance).to receive(:post).with( + '/oauth/token', client_id: nil, client_secret: nil, grant_type: 'client_credentials') expect(@instance.obtain_access_token).to eql 'AccessToken' end end @@ -19,20 +21,36 @@ context '.delegation' do it { expect(@instance).to respond_to(:delegation) } it "is expected to make post request to '/delegation'" do - expect(@instance).to receive(:post).with('/delegation', client_id: nil, grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', id_token: 'token', target: 'target', scope: '', api_type: 'app') + expect(@instance).to receive(:post).with( + '/delegation', + client_id: nil, + grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', + id_token: 'token', + target: 'target', + scope: '', + api_type: 'app') @instance.delegation('token', 'target', '') end - it "is expected to make post request to '/delegation' with specified api_type" do - expect(@instance).to receive(:post).with('/delegation', client_id: nil, grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', - id_token: '', target: '', scope: '', - api_type: 'salesforce_api') + it "is expected to make post request to '/delegation' + with specified api_type" do + expect(@instance).to receive(:post).with( + '/delegation', + client_id: nil, + grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', + id_token: '', target: '', scope: '', + api_type: 'salesforce_api') @instance.delegation('', '', '', 'salesforce_api') end it 'allows to pass extra parameters' do - expect(@instance).to receive(:post).with('/delegation', client_id: nil, grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', - id_token: '', target: '', scope: '', api_type: '', - community_name: 'test-community', community_url: 'test-url') - @instance.delegation('', '', '', '', community_name: 'test-community', community_url: 'test-url') + expect(@instance).to receive(:post).with( + '/delegation', + client_id: nil, + grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', + id_token: '', target: '', scope: '', api_type: '', + community_name: 'test-community', community_url: 'test-url') + @instance.delegation( + '', '', '', '', + community_name: 'test-community', community_url: 'test-url') end end @@ -43,7 +61,13 @@ it { expect(@instance).to respond_to(:impersonate) } it "is expected to make post request to '/users/{user_id}/impersonate'" do - expect(@instance).to receive(:post).with("/users/#{user_id}/impersonate", protocol: 'oauth2', impersonator_id: impersonator_id, client_id: app_client_id, ttl: 120, additionalParameters: { response_type: 'code', state: '', scope: 'openid', callback_url: '' }) + expect(@instance).to receive(:post).with( + "/users/#{user_id}/impersonate", + protocol: 'oauth2', + impersonator_id: impersonator_id, client_id: app_client_id, ttl: 120, + additionalParameters: { + response_type: 'code', state: '', + scope: 'openid', callback_url: '' }) @instance.impersonate(user_id, app_client_id, impersonator_id, {}) end end @@ -51,21 +75,31 @@ context '.login' do it { expect(@instance).to respond_to(:signup) } it 'is expected to make post to /oauth/ro' do - expect(@instance).to receive(:post).with('/oauth/ro', client_id: nil, username: 'test@test.com', password: 'password', connection: 'Username-Password-Authentication', scope: 'openid', grant_type: 'password', id_token: nil) + expect(@instance).to receive(:post).with( + '/oauth/ro', + client_id: nil, username: 'test@test.com', + password: 'password', connection: 'Username-Password-Authentication', + scope: 'openid', grant_type: 'password', id_token: nil) @instance.login('test@test.com', 'password') end end context '.signup' do it { expect(@instance).to respond_to(:signup) } it 'is expected to make post to /dbconnections/signup' do - expect(@instance).to receive(:post).with('/dbconnections/signup', client_id: nil, email: 'test@test.com', password: 'password', connection: 'User') + expect(@instance).to receive(:post).with( + '/dbconnections/signup', + client_id: nil, email: 'test@test.com', + password: 'password', connection: 'User') @instance.signup('test@test.com', 'password', 'User') end end context '.change_password' do it { expect(@instance).to respond_to(:change_password) } it 'is expected to make post to /dbconnections/change_password' do - expect(@instance).to receive(:post).with('/dbconnections/change_password', client_id: nil, email: 'test@test.com', password: 'password', connection: 'User') + expect(@instance).to receive(:post).with( + '/dbconnections/change_password', + client_id: nil, email: 'test@test.com', + password: 'password', connection: 'User') @instance.change_password('test@test.com', 'password', 'User') end end diff --git a/spec/lib/auth0/api/v1/clients_spec.rb b/spec/lib/auth0/api/v1/clients_spec.rb index 5c361d60..c321f974 100644 --- a/spec/lib/auth0/api/v1/clients_spec.rb +++ b/spec/lib/auth0/api/v1/clients_spec.rb @@ -19,7 +19,7 @@ it 'is expected to send post request to /api/clients' do client_name = 'ClientRandomName' callbacks = 'Some absolutely random stuff here' - expect(@instance).to receive(:post).with('/api/clients', { name: client_name, callbacks: callbacks }) + expect(@instance).to receive(:post).with('/api/clients', name: client_name, callbacks: callbacks) expect { @instance.create_client(client_name, callbacks) }.not_to raise_error end end @@ -29,7 +29,7 @@ it 'is expected to send post request to /api/clients' do client_name = 'Some random name' callbacks = 'Some random stuff' - expect(@instance).to receive(:post).with('/api/clients', { name: client_name, callbacks: callbacks }) + expect(@instance).to receive(:post).with('/api/clients', name: client_name, callbacks: callbacks) expect { @instance.create_client(client_name, callbacks) }.not_to raise_error end end diff --git a/spec/lib/auth0/api/v1/connections_spec.rb b/spec/lib/auth0/api/v1/connections_spec.rb index 6aa37711..850295a8 100644 --- a/spec/lib/auth0/api/v1/connections_spec.rb +++ b/spec/lib/auth0/api/v1/connections_spec.rb @@ -43,7 +43,14 @@ } } expect(@instance).to receive(:post).with('/api/connections', params) - expect { @instance.create_connection('Some Test name', 'Unpredictable', 'google.com', 'test.google.com,auth0.com') }.not_to raise_error + expect do + @instance.create_connection( + 'Some Test name', + 'Unpredictable', + 'google.com', + 'test.google.com,auth0.com' + ) + end.not_to raise_error end end context '.update_connection' do diff --git a/spec/lib/auth0/api/v1/logs_spec.rb b/spec/lib/auth0/api/v1/logs_spec.rb index 95a2a090..6ae4370a 100644 --- a/spec/lib/auth0/api/v1/logs_spec.rb +++ b/spec/lib/auth0/api/v1/logs_spec.rb @@ -20,7 +20,9 @@ end it 'should raise warn if any garbage params are passed' do - error_message = 'random_stuff is not in acceptable params list: [:take, :from, :search_criteria, :page, :per_page, :sort, :fields, :exclude_fields]' + error_message = 'random_stuff is not in acceptable params list: '\ + '[:take, :from, :search_criteria, :page, :per_page, :sort, :fields, '\ + ':exclude_fields]' expect(@instance).to receive(:warn).with(error_message) @instance.logs('per_page' => 500, page: 3, random_stuff: 7) end diff --git a/spec/lib/auth0/api/v1/rules_spec.rb b/spec/lib/auth0/api/v1/rules_spec.rb index 734181db..a1274a82 100644 --- a/spec/lib/auth0/api/v1/rules_spec.rb +++ b/spec/lib/auth0/api/v1/rules_spec.rb @@ -18,7 +18,9 @@ context '.create_rule' do it { expect(@instance).to respond_to(:create_rule) } it 'is expected to call post /api/rules' do - expect(@instance).to receive(:post).with('/api/rules', name: 'test', script: 'script', order: 'order', status: 'status') + expect(@instance).to receive(:post).with( + '/api/rules', + name: 'test', script: 'script', order: 'order', status: 'status') expect { @instance.create_rule('test', 'script', 'order', 'status') }.not_to raise_error end end diff --git a/spec/lib/auth0/api/v1/users_spec.rb b/spec/lib/auth0/api/v1/users_spec.rb index 808d5996..b1fad0db 100644 --- a/spec/lib/auth0/api/v1/users_spec.rb +++ b/spec/lib/auth0/api/v1/users_spec.rb @@ -46,7 +46,8 @@ expect { @instance.connection_users('CONNECTION_ID') }.not_to raise_error end - it 'is expected to call /api/connections/CONNECTION_ID/users?search=search_criteria when search is search_criteria' do + it 'is expected to call + /api/connections/CONNECTION_ID/users?search=search_criteria when search is search_criteria' do expect(@instance).to receive(:get).with('/api/connections/CONNECTION_ID/users?search=search_criteria') expect { @instance.connection_users('CONNECTION_ID', 'search_criteria') }.not_to raise_error end @@ -96,7 +97,11 @@ context '.create_user' do it { expect(@instance).to respond_to(:create_user) } it 'is expected to call post to /api/users' do - expect(@instance).to receive(:post).with('/api/users', { email: 'test@test.com', password: 'password', connection: 'conn' }) + expect(@instance).to receive(:post).with( + '/api/users', + email: 'test@test.com', + password: 'password', + connection: 'conn') @instance.create_user('test@test.com', 'password', 'conn') end end @@ -113,7 +118,9 @@ it { expect(@instance).to respond_to(:change_password_ticket) } it 'is expected to call post to /api/users/USERID/change_password_ticket' do password = SecureRandom.hex - expect(@instance).to receive(:post).with('/api/users/USERID/change_password_ticket', 'newPassword' => password, 'resultUrl' => nil) + expect(@instance).to receive(:post).with( + '/api/users/USERID/change_password_ticket', + 'newPassword' => password, 'resultUrl' => nil) @instance.change_password_ticket 'USERID', password end end @@ -121,11 +128,16 @@ context '.verification_ticket' do it { expect(@instance).to respond_to(:verification_ticket) } it 'is expected to call post to /api/users/userId/verification_ticket if resulturl is set' do - expect(@instance).to receive(:post).with('/api/users/auth0|tdasfasdfasdfa/verification_ticket', 'resultUrl' => 'google.com') + expect(@instance).to receive(:post).with( + '/api/users/auth0|tdasfasdfasdfa/verification_ticket', + 'resultUrl' => 'google.com') @instance.verification_ticket('auth0|tdasfasdfasdfa', 'google.com') end - it 'is expected to call post to /api/users/userId/verification_ticket if result url is empty' do - expect(@instance).to receive(:post).with('/api/users/auth0|tdasfasdfasdfa/verification_ticket', 'resultUrl' => nil) + it 'is expected to call post to /api/users/userId/verification_ticket + if result url is empty' do + expect(@instance).to receive(:post).with( + '/api/users/auth0|tdasfasdfasdfa/verification_ticket', + 'resultUrl' => nil) @instance.verification_ticket('auth0|tdasfasdfasdfa') end end @@ -133,7 +145,9 @@ context '.create_public_key' do it { expect(@instance).to respond_to(:create_public_key) } it 'is expected to call post to /api/users/userId/public_key' do - expect(@instance).to receive(:post).with('/api/users/auth0|tdasfasdfasdfa/public_key', device: 'device22', public_key: 'SuperSecurePK') + expect(@instance).to receive(:post).with( + '/api/users/auth0|tdasfasdfasdfa/public_key', + device: 'device22', public_key: 'SuperSecurePK') @instance.create_public_key('auth0|tdasfasdfasdfa', 'device22', 'SuperSecurePK') end end @@ -157,7 +171,10 @@ context '.update_user_password' do it { expect(@instance).to respond_to(:update_user_password) } it 'is expected to call put to /api/users/auth0|tdasfasdfasdfa/password' do - expect(@instance).to receive(:put).with('/api/users/auth0|tdasfasdfasdfa/password', { password: 'password', verify: true }) + expect(@instance).to receive(:put).with( + '/api/users/auth0|tdasfasdfasdfa/password', + password: 'password', + verify: true) @instance.update_user_password('auth0|tdasfasdfasdfa', 'password') end end @@ -165,8 +182,14 @@ context '.update_user_password_using_email' do it { expect(@instance).to respond_to(:update_user_password_using_email) } it 'is expected to call put to /api/users/email@email.com/password' do - expect(@instance).to receive(:put).with('/api/users/email@email.com/password', email: 'email@email.com', password: 'password', connection: 'Con', verify: true) - @instance.update_user_password_using_email('email@email.com', 'password', 'Con') + expect(@instance).to receive(:put).with( + '/api/users/email@email.com/password', + email: 'email@email.com', + password: 'password', + connection: 'Con', + verify: true) + @instance.update_user_password_using_email( + 'email@email.com', 'password', 'Con') end end diff --git a/spec/lib/auth0/api/v2/clients_spec.rb b/spec/lib/auth0/api/v2/clients_spec.rb index 87b4fd5b..e5dd9c87 100644 --- a/spec/lib/auth0/api/v2/clients_spec.rb +++ b/spec/lib/auth0/api/v2/clients_spec.rb @@ -12,8 +12,9 @@ expect(@instance).to receive(:get).with('/api/v2/clients', {}) expect { @instance.clients }.not_to raise_error end - it 'is expected to send get request to /api/v2/clients?fields=name&exclude_fields=false' do - expect(@instance).to receive(:get).with('/api/v2/clients', { exclude_fields: false, fields: [:name] }) + it 'is expected to send get request to + /api/v2/clients?fields=name&exclude_fields=false' do + expect(@instance).to receive(:get).with('/api/v2/clients', exclude_fields: false, fields: [:name]) expect { @instance.clients(exclude_fields: false, fields: [:name]) }.not_to raise_error end end @@ -23,7 +24,8 @@ expect(@instance).to receive(:get).with('/api/v2/clients/1', {}) expect { @instance.client(1) }.not_to raise_error end - it 'is expected to send get request to /api/v2/clients?fields=name&exclude_fields=false' do + it 'is expected to send get request to + /api/v2/clients?fields=name&exclude_fields=false' do expect(@instance).to receive(:get).with('/api/v2/clients/1', exclude_fields: false, fields: [:name]) expect { @instance.client(1, exclude_fields: false, fields: [:name]) }.not_to raise_error end diff --git a/spec/lib/auth0/api/v2/connections_spec.rb b/spec/lib/auth0/api/v2/connections_spec.rb index f606c541..f3824ceb 100644 --- a/spec/lib/auth0/api/v2/connections_spec.rb +++ b/spec/lib/auth0/api/v2/connections_spec.rb @@ -11,10 +11,11 @@ it { expect(@instance).to respond_to(:get_connections) } it 'is expected to call /api/v2/connections' do - expect(@instance).to receive(:get).with('/api/v2/connections', strategy: nil, - fields: nil, - include_fields: true) - + expect(@instance).to receive(:get).with( + '/api/v2/connections', + strategy: nil, + fields: nil, + include_fields: true) expect { @instance.connections }.not_to raise_error end end @@ -46,7 +47,8 @@ @instance.delete_connection('connectionId') end - it 'is expected not to call delete to /api/v2/connections if connection_id is blank' do + it 'is expected not to call delete to /api/v2/connections + if connection_id is blank' do expect(@instance).not_to receive(:delete) expect { @instance.delete_connection('') }.to raise_exception(Auth0::MissingConnectionId) end @@ -60,7 +62,8 @@ @instance.update_connection('connectionId', body) end - it 'is expected not to call delete to /api/v2/connections if connection_id is blank' do + it 'is expected not to call delete to /api/v2/connections + if connection_id is blank' do expect(@instance).not_to receive(:patch) expect { @instance.delete_connection('') }.to raise_exception(Auth0::MissingConnectionId) end diff --git a/spec/lib/auth0/api/v2/stats_spec.rb b/spec/lib/auth0/api/v2/stats_spec.rb index 0c4da311..cccba25e 100644 --- a/spec/lib/auth0/api/v2/stats_spec.rb +++ b/spec/lib/auth0/api/v2/stats_spec.rb @@ -15,7 +15,7 @@ context '.daily_stats' do it { expect(@instance).to respond_to(:daily_stats) } it 'expect client to send get to /api/v2/stats/daily' do - expect(@instance).to receive(:get).with('/api/v2/stats/daily', { from: '20120222', to: '20151222' }) + expect(@instance).to receive(:get).with('/api/v2/stats/daily', from: '20120222', to: '20151222') expect { @instance.daily_stats('20120222', '20151222') }.not_to raise_error end end diff --git a/spec/lib/auth0/api/v2/users_spec.rb b/spec/lib/auth0/api/v2/users_spec.rb index 9e22c7fb..fcf345ca 100644 --- a/spec/lib/auth0/api/v2/users_spec.rb +++ b/spec/lib/auth0/api/v2/users_spec.rb @@ -10,7 +10,15 @@ it { expect(@instance).to respond_to(:users) } it { expect(@instance).to respond_to(:get_users) } it 'is expected to call /api/v2/users' do - expect(@instance).to receive(:get).with('/api/v2/users', per_page: nil, page: nil, include_totals: nil, sort: nil, connection: nil, fields: nil, q: nil) + expect(@instance).to receive(:get).with( + '/api/v2/users', + per_page: nil, + page: nil, + include_totals: nil, + sort: nil, + connection: nil, + fields: nil, + q: nil) expect { @instance.users }.not_to raise_error end end @@ -18,7 +26,7 @@ context '.user' do it { expect(@instance).to respond_to(:user) } it 'is expected to call get request to /api/v2/users/USER_ID' do - expect(@instance).to receive(:get).with('/api/v2/users/USER_ID', { fields: nil }) + expect(@instance).to receive(:get).with('/api/v2/users/USER_ID', fields: nil) expect { @instance.user('USER_ID') }.not_to raise_error end end @@ -26,8 +34,17 @@ context '.create_user' do it { expect(@instance).to respond_to(:create_user) } it 'is expected to call post to /api/v2/users' do - expect(@instance).to receive(:post).with('/api/v2/users', email: 'test@test.com', password: 'password', connection: 'conn', name: 'name') - @instance.create_user('name', email: 'test@test.com', password: 'password', connection: 'conn') + expect(@instance).to receive(:post).with( + '/api/v2/users', + email: 'test@test.com', + password: 'password', + connection: 'conn', + name: 'name') + @instance.create_user( + 'name', + email: 'test@test.com', + password: 'password', + connection: 'conn') end end @@ -48,7 +65,8 @@ it 'is expected not to call delete to /api/v2/users if user_id is blank' do expect(@instance).not_to receive(:delete) - expect { @instance.delete_user('') }.to raise_exception(Auth0::MissingUserId) + expect { @instance.delete_user('') }.to raise_exception( + Auth0::MissingUserId) end end @@ -63,8 +81,18 @@ context '.patch_user' do it { expect(@instance).to respond_to(:patch_user) } it 'is expected to call patch to /api/v2/users/userID' do - expect(@instance).to receive(:patch).with('/api/v2/users/UserID', email: 'test@test.com', password: 'password', connection: 'conn', name: 'name') - @instance.patch_user('UserID', email: 'test@test.com', password: 'password', connection: 'conn', name: 'name') + expect(@instance).to receive(:patch).with( + '/api/v2/users/UserID', + email: 'test@test.com', + password: 'password', + connection: 'conn', + name: 'name') + @instance.patch_user( + 'UserID', + email: 'test@test.com', + password: 'password', + connection: 'conn', + name: 'name') end end end diff --git a/spec/lib/auth0/client_spec.rb b/spec/lib/auth0/client_spec.rb index 18a8ee87..eb2a11c0 100644 --- a/spec/lib/auth0/client_spec.rb +++ b/spec/lib/auth0/client_spec.rb @@ -47,22 +47,31 @@ end context 'with namespace' do - let(:subject) { Auth0::Client.new(client_id: 'client_id', client_secret: 'client_secret', namespace: 'samples.auth0.com') } + let(:subject) do + Auth0::Client.new(client_id: 'client_id', client_secret: 'client_secret', namespace: 'samples.auth0.com') + end it_should_behave_like 'v1 API client' it_should_behave_like 'authentication API client' end context 'with domain' do - let(:subject) { Auth0::Client.new(client_id: 'client_id', client_secret: 'client_secret', domain: 'samples.auth0.com') } + let(:subject) do + Auth0::Client.new(client_id: 'client_id', client_secret: 'client_secret', domain: 'samples.auth0.com') + end it_should_behave_like 'v1 API client' it_should_behave_like 'authentication API client' end context 'with version' do - let(:subject) { Auth0::Client.new(client_id: 'client_id', client_secret: 'client_secret', domain: 'samples.auth0.com', api_version: 1) } - + let(:subject) do + Auth0::Client.new( + client_id: 'client_id', + client_secret: 'client_secret', + domain: 'samples.auth0.com', + api_version: 1) + end it_should_behave_like 'v1 API client' it_should_behave_like 'authentication API client' end @@ -75,21 +84,18 @@ context 'with namespace' do let(:subject) { Auth0::Client.new(protocols: 'v2', access_token: 'access_token', namespace: 'samples.auth0.com') } - it_should_behave_like 'v2 API client' it_should_behave_like 'authentication API client' end context 'with domain' do let(:subject) { Auth0::Client.new(protocols: 'v2', access_token: 'access_token', domain: 'samples.auth0.com') } - it_should_behave_like 'v2 API client' it_should_behave_like 'authentication API client' end context 'with version' do let(:subject) { Auth0::Client.new(api_version: 2, access_token: 'access_token', domain: 'samples.auth0.com') } - it_should_behave_like 'v2 API client' it_should_behave_like 'authentication API client' end diff --git a/spec/lib/auth0/mixins/httparty_proxy_spec.rb b/spec/lib/auth0/mixins/httparty_proxy_spec.rb index a1b143d9..ab9fd99a 100644 --- a/spec/lib/auth0/mixins/httparty_proxy_spec.rb +++ b/spec/lib/auth0/mixins/httparty_proxy_spec.rb @@ -11,54 +11,70 @@ it { expect(@instance).to respond_to(http_method.to_sym) } it "should call send http #{http_method} method to path defined through HTTParty" do allow(DummyClassForProxy).to receive(http_method).with('http://login.auth0.com/test', query: {}) - expect(DummyClassForProxy).to receive(http_method).with('/test', query: {}).and_return(StubResponse.new({}, true, 200)) + expect(DummyClassForProxy).to receive(http_method).with('/test', query: {}) + .and_return(StubResponse.new({}, true, 200)) expect { @instance.send(http_method, '/test') }.not_to raise_error end - it 'should not raise exception if data returned not in json format(should be fixed in v2)' do - allow(DummyClassForProxy).to receive(http_method).with('/test', query: {}).and_return(StubResponse.new('Some random text here', true, 200)) + it 'should not raise exception if data returned not in json format (should be fixed in v2)' do + allow(DummyClassForProxy).to receive(http_method).with('/test', query: {}) + .and_return(StubResponse.new('Some random text here', true, 200)) expect { @instance.send(http_method, '/test') }.not_to raise_error - expect(@instance.send(http_method, '/test')).to eql 'Some random text here' + expect(@instance.send(http_method, '/test')).to eql('Some random text here') end - it "should raise Auth0::Unauthorized on send http #{http_method} method to path defined through HTTParty when 401 status received" do + it "should raise Auth0::Unauthorized on send http #{http_method} + method to path defined through HTTParty when 401 status received" do allow(DummyClassForProxy).to receive(http_method).with('http://login.auth0.com/test', query: {}) - expect(DummyClassForProxy).to receive(http_method).with('/test', query: {}).and_return(StubResponse.new({}, false, 401)) + expect(DummyClassForProxy).to receive(http_method).with('/test', query: {}) + .and_return(StubResponse.new({}, false, 401)) expect { @instance.send(http_method, '/test') }.to raise_error(Auth0::Unauthorized) end - it "should raise Auth0::NotFound on send http #{http_method} method to path defined through HTTParty when 404 status received" do + it "should raise Auth0::NotFound on send http #{http_method} method + to path defined through HTTParty when 404 status received" do allow(DummyClassForProxy).to receive(http_method).with('http://login.auth0.com/test', query: {}) - expect(DummyClassForProxy).to receive(http_method).with('/test', query: {}).and_return(StubResponse.new({}, false, 404)) + expect(DummyClassForProxy).to receive(http_method).with('/test', query: {}) + .and_return(StubResponse.new({}, false, 404)) expect { @instance.send(http_method, '/test') }.to raise_error(Auth0::NotFound) end - it "should raise Auth0::Unsupported on send http #{http_method} method to path defined through HTTParty when 418 or other unknown status received" do + it "should raise Auth0::Unsupported on send http #{http_method} method + to path defined through HTTParty when 418 or other unknown status received" do allow(DummyClassForProxy).to receive(http_method).with('http://login.auth0.com/test', query: {}) - expect(DummyClassForProxy).to receive(http_method).with('/test', query: {}).and_return(StubResponse.new({}, false, 418)) + expect(DummyClassForProxy).to receive(http_method).with('/test', query: {}) + .and_return(StubResponse.new({}, false, 418)) expect { @instance.send(http_method, '/test') }.to raise_error(Auth0::Unsupported) end - it "should raise Auth0::BadRequest on send http #{http_method} method to path defined through HTTParty when 400 or other unknown status received" do + it "should raise Auth0::BadRequest on send http #{http_method} method + to path defined through HTTParty when 400 or other unknown status received" do allow(DummyClassForProxy).to receive(http_method).with('http://login.auth0.com/test', query: {}) - expect(DummyClassForProxy).to receive(http_method).with('/test', query: {}).and_return(StubResponse.new({}, false, 400)) - expect { @instance.send(http_method, '/test') }.to raise_error(Auth0::BadRequest) + expect(DummyClassForProxy).to receive(http_method).with('/test', query: {}) + .and_return(StubResponse.new({}, false, 400)) + expect { @instance.send(http_method, '/test') }.to raise_error( + Auth0::BadRequest) end - it "should raise Auth0::AccessDenied on send http #{http_method} method to path defined through HTTParty when 403" do + it "should raise Auth0::AccessDenied on send http #{http_method} method + to path defined through HTTParty when 403" do allow(DummyClassForProxy).to receive(http_method).with('http://login.auth0.com/test', query: {}) - expect(DummyClassForProxy).to receive(http_method).with('/test', query: {}).and_return(StubResponse.new({}, false, 403)) + expect(DummyClassForProxy).to receive(http_method).with('/test', query: {}) + .and_return(StubResponse.new({}, false, 403)) expect { @instance.send(http_method, '/test') }.to raise_error(Auth0::AccessDenied) end - it "should raise Auth0::ServerError on send http #{http_method} method to path defined through HTTParty when 500 received" do + it "should raise Auth0::ServerError on send http #{http_method} method + to path defined through HTTParty when 500 received" do allow(DummyClassForProxy).to receive(http_method).with('http://login.auth0.com/test', query: {}) - expect(DummyClassForProxy).to receive(http_method).with('/test', query: {}).and_return(StubResponse.new({}, false, 500)) + expect(DummyClassForProxy).to receive(http_method).with('/test', query: {}) + .and_return(StubResponse.new({}, false, 500)) expect { @instance.send(http_method, '/test') }.to raise_error(Auth0::ServerError) end it 'should escape path with URI.escape' do allow(DummyClassForProxy).to receive(http_method).with('http://login.auth0.com/te%20st', query: {}) - expect(DummyClassForProxy).to receive(http_method).with('/te%20st', query: {}).and_return(StubResponse.new({}, true, 200)) + expect(DummyClassForProxy).to receive(http_method).with('/te%20st', query: {}) + .and_return(StubResponse.new({}, true, 200)) expect { @instance.send(http_method, '/te st') }.not_to raise_error end end @@ -69,49 +85,62 @@ it { expect(@instance).to respond_to(http_method.to_sym) } it "should call send http #{http_method} method to path defined through HTTParty" do allow(DummyClassForProxy).to receive(http_method).with('http://login.auth0.com/test', body: '{}') - expect(DummyClassForProxy).to receive(http_method).with('/test', body: '{}').and_return(StubResponse.new('{}', true, 200)) + expect(DummyClassForProxy).to receive(http_method).with('/test', body: '{}') + .and_return(StubResponse.new('{}', true, 200)) expect { @instance.send(http_method, '/test') }.not_to raise_error end - it 'should not raise exception if data returned not in json format(should be fixed in v2)' do - allow(DummyClassForProxy).to receive(http_method).with('/test', body: '{}').and_return(StubResponse.new('Some random text here', true, 200)) + it 'should not raise exception if data returned not in json format (should be fixed in v2)' do + allow(DummyClassForProxy).to receive(http_method).with('/test', body: '{}') + .and_return(StubResponse.new('Some random text here', true, 200)) expect { @instance.send(http_method, '/test') }.not_to raise_error - expect(@instance.send(http_method, '/test')).to eql 'Some random text here' + expect(@instance.send(http_method, '/test')).to eql('Some random text here') end - it "should raise Auth0::Unauthorized on send http #{http_method} method to path defined through HTTParty when 401 status received" do + it "should raise Auth0::Unauthorized on send http #{http_method} method + to path defined through HTTParty when 401 status received" do allow(DummyClassForProxy).to receive(http_method).with('http://login.auth0.com/test', body: '{}') - expect(DummyClassForProxy).to receive(http_method).with('/test', body: '{}').and_return(StubResponse.new('{}', false, 401)) + expect(DummyClassForProxy).to receive(http_method).with('/test', body: '{}') + .and_return(StubResponse.new('{}', false, 401)) expect { @instance.send(http_method, '/test') }.to raise_error(Auth0::Unauthorized) end - it "should raise Auth0::NotFound on send http #{http_method} method to path defined through HTTParty when 404 status received" do + it "should raise Auth0::NotFound on send http #{http_method} method + to path defined through HTTParty when 404 status received" do allow(DummyClassForProxy).to receive(http_method).with('http://login.auth0.com/test', body: '{}') - expect(DummyClassForProxy).to receive(http_method).with('/test', body: '{}').and_return(StubResponse.new('{}', false, 404)) + expect(DummyClassForProxy).to receive(http_method).with('/test', body: '{}') + .and_return(StubResponse.new('{}', false, 404)) expect { @instance.send(http_method, '/test') }.to raise_error(Auth0::NotFound) end - it "should raise Auth0::Unsupported on send http #{http_method} method to path defined through HTTParty when 418 or other unknown status received" do + it "should raise Auth0::Unsupported on send http #{http_method} method + to path defined through HTTParty when 418 or other unknown status received" do allow(DummyClassForProxy).to receive(http_method).with('http://login.auth0.com/test', body: '{}') - expect(DummyClassForProxy).to receive(http_method).with('/test', body: '{}').and_return(StubResponse.new('{}', false, 418)) + expect(DummyClassForProxy).to receive(http_method).with('/test', body: '{}') + .and_return(StubResponse.new('{}', false, 418)) expect { @instance.send(http_method, '/test') }.to raise_error(Auth0::Unsupported) end - it "should raise Auth0::BadRequest on send http #{http_method} method to path defined through HTTParty when 400 or other unknown status received" do + it "should raise Auth0::BadRequest on send http #{http_method} method + to path defined through HTTParty when 400 or other unknown status received" do allow(DummyClassForProxy).to receive(http_method).with('http://login.auth0.com/test', body: '{}') - expect(DummyClassForProxy).to receive(http_method).with('/test', body: '{}').and_return(StubResponse.new('{}', false, 400)) + expect(DummyClassForProxy).to receive(http_method).with('/test', body: '{}') + .and_return(StubResponse.new('{}', false, 400)) expect { @instance.send(http_method, '/test') }.to raise_error(Auth0::BadRequest) end - it "should raise Auth0::ServerError on send http #{http_method} method to path defined through HTTParty when 500 received" do + it "should raise Auth0::ServerError on send http #{http_method} method + to path defined through HTTParty when 500 received" do allow(DummyClassForProxy).to receive(http_method).with('http://login.auth0.com/test', body: '{}') - expect(DummyClassForProxy).to receive(http_method).with('/test', body: '{}').and_return(StubResponse.new('{}', false, 500)) + expect(DummyClassForProxy).to receive(http_method).with('/test', body: '{}') + .and_return(StubResponse.new('{}', false, 500)) expect { @instance.send(http_method, '/test') }.to raise_error(Auth0::ServerError) end it 'should escape path with URI.escape' do allow(DummyClassForProxy).to receive(http_method).with('http://login.auth0.com/te%20st', body: '{}') - expect(DummyClassForProxy).to receive(http_method).with('/te%20st', body: '{}').and_return(StubResponse.new('{}', true, 200)) + expect(DummyClassForProxy).to receive(http_method).with('/te%20st', body: '{}') + .and_return(StubResponse.new('{}', true, 200)) expect { @instance.send(http_method, '/te st') }.not_to raise_error end end diff --git a/spec/spec_helper_full.rb b/spec/spec_helper_full.rb index 44ff8c9f..55d97164 100644 --- a/spec/spec_helper_full.rb +++ b/spec/spec_helper_full.rb @@ -32,7 +32,8 @@ def entity_suffix config.include Credentials config.after(:suite) do puts "Cleaning up for #{entity_suffix}" - v2_client = Auth0Client.new({ token: ENV['MASTER_JWT'], api_version: 2, domain: ENV['DOMAIN'] }) + v2_client = Auth0Client.new( + token: ENV['MASTER_JWT'], api_version: 2, domain: ENV['DOMAIN']) v2_client .clients .select { |client| client['name'] != 'DefaultApp' && !client['global'] && client['name'].include?(entity_suffix) } diff --git a/spec/support/credentials.rb b/spec/support/credentials.rb index 6d7b4515..30b783a0 100644 --- a/spec/support/credentials.rb +++ b/spec/support/credentials.rb @@ -1,13 +1,19 @@ module Credentials def v1_creds - { client_id: ENV['CLIENT_ID'], client_secret: ENV['CLIENT_SECRET'], domain: ENV['DOMAIN'] } + { client_id: ENV['CLIENT_ID'], + client_secret: ENV['CLIENT_SECRET'], + domain: ENV['DOMAIN'] } end def v1_global_creds - { client_id: ENV['GLOBAL_CLIENT_ID'], client_secret: ENV['GLOBAL_CLIENT_SECRET'], domain: ENV['DOMAIN'] } + { client_id: ENV['GLOBAL_CLIENT_ID'], + client_secret: ENV['GLOBAL_CLIENT_SECRET'], + domain: ENV['DOMAIN'] } end def v2_creds - { token: ENV['MASTER_JWT'], api_version: 2, domain: ENV['DOMAIN'] } + { token: ENV['MASTER_JWT'], + api_version: 2, + domain: ENV['DOMAIN'] } end end diff --git a/spec/support/stub_response.rb b/spec/support/stub_response.rb index 4766aa97..676c71ca 100644 --- a/spec/support/stub_response.rb +++ b/spec/support/stub_response.rb @@ -1,2 +1 @@ -class StubResponse < Struct.new(:body, :success?, :code) -end +StubResponse = Struct.new(:body, :success?, :code)