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

Error Documentation #33

Merged
merged 11 commits into from
Apr 19, 2017
9 changes: 0 additions & 9 deletions lib/token_master/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def force_tokenable!(model, key, **params)
# TODO
def set_token!(model, key, token_length = nil)
check_manageable! model.class, key
check_configs_set! key
token_length ||= TokenMaster.config.get_token_length(key.to_sym)
token = generate_token token_length

Expand Down Expand Up @@ -112,10 +111,6 @@ def manageable?(klass, key)
).all? { |attr| column_names.include? attr }
end

def check_configs_set!(key)
raise NotConfigured, 'You have not set the configurations for this tokenable.' unless TokenMaster.config.options_set?(key.to_sym)
end

def check_params!(key, params)
required_params = TokenMaster.config.get_required_params(key.to_sym)
raise MissingRequiredParams, 'You did not pass in the required params for this tokenable' unless required_params.all? do |k|
Expand Down Expand Up @@ -155,10 +150,6 @@ def completed?(model, key)
model.send(completed_at_col(key)).present?
end

def check_completed!(model, key)
raise TokenNotCompleted, "#{key} not completed" unless completed?(model, key)
end

def generate_token(length)
rlength = (length * 3) / 4
SecureRandom.urlsafe_base64(rlength).tr('lIO0', 'sxyz')
Expand Down
25 changes: 9 additions & 16 deletions lib/token_master/error.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
module TokenMaster

# The base class for all errors raised by TokenMaster
class Error < StandardError; end

# TODO
class MissingRequiredParams < Error; end

# Raised when the attributes for a tokenable do not exist.
# This could result from a migration not being run or a spelling error
class NotTokenable < Error; end

# TODO
class NotConfigured < Error; end

# TODO
# Raised when the required parameters for a tokenable are not provided.
# This typically happens with reset and invite tokenables, that might require both `password` and `password_confirmation` fields,
# but only one is provided to the method
class MissingRequiredParams < Error; end

# TODO
# Raised when the tokenable instance is not found
class TokenNotFound < Error; end

# TODO
# Raised when the status of the token is reviewed, but the tokenable action has already been completed
class TokenCompleted < Error; end

# TODO
class TokenNotCompleted < Error; end

# TODO
# Raised when the token has expired based on the tokenable's `token_lifetime`
class TokenExpired < Error; end

# TODO
# Raised when the tokenable instructions have already been sent when calling `send_tokenable_instructions!`
class TokenSent < Error; end

# TODO
# Raised when the tokenable model instance does not have a token set for a tokenable
class TokenNotSet < Error; end
end

85 changes: 23 additions & 62 deletions test/test_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,6 @@ def present?
end
end

describe '#check_configs_set!' do
it 'configs not set' do
assert_raises TokenMaster::NotConfigured do
TM.send(:check_configs_set!, 'foo')
end
end

it 'configs set' do
assert_nil TM.send(:check_configs_set!, 'confirm')
end
end

describe '#token_set?' do
before do
@manageable_model = MockTokenMaster.new
Expand Down Expand Up @@ -284,45 +272,35 @@ def present?
@model = MockTokenMaster.new
end

describe 'when configs not set' do
it 'raises' do
assert_raises TokenMaster::NotConfigured do
TM.set_token!(@model, 'invite')
end
end
it 'sets the token to the configured length' do
TM.set_token! @model, 'confirm'
assert_equal @model.confirm_token.length, TokenMaster.config.get_token_length(:confirm)
end

describe 'when configs set' do
it 'sets the token to the configured length' do
TM.set_token! @model, 'confirm'
assert_equal @model.confirm_token.length, TokenMaster.config.get_token_length(:confirm)
end

it 'sets confirm created at time to now' do
TM.set_token! @model, 'confirm'
assert @model.confirm_created_at, Time.now
end
it 'sets confirm created at time to now' do
TM.set_token! @model, 'confirm'
assert @model.confirm_created_at, Time.now
end

it 'sets confirmed completed at time to nil' do
TM.set_token! @model, 'confirm'
assert_nil @model.confirm_completed_at
end
it 'sets confirmed completed at time to nil' do
TM.set_token! @model, 'confirm'
assert_nil @model.confirm_completed_at
end

it 'sets confirm sent at time to nil' do
TM.set_token! @model, 'confirm'
assert_nil @model.confirm_sent_at
end
it 'sets confirm sent at time to nil' do
TM.set_token! @model, 'confirm'
assert_nil @model.confirm_sent_at
end

it 'returns the token' do
token = TM.set_token! @model, 'confirm'
assert_equal token, @model.confirm_token
end
it 'returns the token' do
token = TM.set_token! @model, 'confirm'
assert_equal token, @model.confirm_token
end

describe 'when token length is provided' do
it 'sets the token to the provided length' do
TM.set_token! @model, 'confirm', 40
assert_equal @model.confirm_token.length, 40
end
describe 'when token length is provided' do
it 'sets the token to the provided length' do
TM.set_token! @model, 'confirm', 40
assert_equal @model.confirm_token.length, 40
end
end
end
Expand Down Expand Up @@ -455,23 +433,6 @@ def present?
end
end

describe '#check_completed!' do
before do
@manageable_model = MockTokenMaster.new
end

it 'when not completed' do
assert_raises TokenMaster::TokenNotCompleted do
TM.send(:check_completed!, @manageable_model, 'confirm')
end
end

it 'when completed' do
@manageable_model.confirm_completed_at = Time.now
assert_nil TM.send(:check_completed!, @manageable_model, 'confirm')
end
end

describe '#send_instructions!' do
describe 'when not manageable' do
before do
Expand Down