Skip to content

Commit

Permalink
Fix Rubocop issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Ignacio Jonas committed Dec 3, 2015
1 parent 50cfb77 commit 8d3de89
Show file tree
Hide file tree
Showing 38 changed files with 387 additions and 209 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
inherit_from: .rubocop_todo.yml
AllCops:
RunRailsCops: true
Exclude:
- bin/**/*
- vendor/**/*
- auth0.gemspec
7 changes: 7 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 15

# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 121
9 changes: 6 additions & 3 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 6 additions & 3 deletions lib/auth0/api/authentication_endpoints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions lib/auth0/api/v1/connections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 3 additions & 9 deletions lib/auth0/api/v1/logs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lib/auth0/api/v1/users.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Auth0
module Api
# rubocop:disable Metrics/ModuleLength
module V1
# {https://auth0.com/docs/api#users}
module Users
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/auth0/api/v2/clients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions lib/auth0/api/v2/connections.rb
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/auth0/api/v2/jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/auth0/api/v2/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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

Expand Down
14 changes: 8 additions & 6 deletions lib/auth0/client.rb
Original file line number Diff line number Diff line change
@@ -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
48 changes: 26 additions & 22 deletions lib/auth0/exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 6 additions & 4 deletions lib/auth0/mixins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions lib/auth0/mixins/httparty_proxy.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
31 changes: 15 additions & 16 deletions lib/auth0/mixins/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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]
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/auth0/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# current version of gem
module Auth0
# current version of gem
VERSION = '3.6.1'
end
5 changes: 1 addition & 4 deletions spec/integration/lib/auth0/api/v1/api_users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Loading

0 comments on commit 8d3de89

Please sign in to comment.