Skip to content

Commit

Permalink
Replace 'read-only' calls to .configure_crypt_ident with #cryptid_config
Browse files Browse the repository at this point in the history
This reflects that the configuration is no longer a class variable, but a memoised instance variable on the class that includes the `CryptIdent` module. This is more in line with standard practice elsewhere and thus with user (client developer) expectations.

Also eliminate no-longer-needed internal notes.

[Closes #18]

81 tests, 110 assertions, 0 failures, 0 errors, 0 skips
Coverage: 918 / 918 LOC (100.0%) covered.
RuboCop: 11 files inspected, no offenses detected
Flay: Total score 0
Flog: Total 329.8; method average 4.6; max 9.8 (CryptIdent::SignUp#create_result)
Reek: 0 total warnings
Inch: Nothing to suggest
jdickey committed Dec 18, 2018
1 parent d194fe3 commit e4568b9
Showing 12 changed files with 47 additions and 42 deletions.
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ GEM
coderay (1.1.2)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
concurrent-ruby (1.1.3)
concurrent-ruby (1.1.4)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
docile (1.3.1)
@@ -160,8 +160,8 @@ GEM
rainbow (3.0.0)
rake (12.3.2)
rb-fsevent (0.10.3)
rb-inotify (0.9.10)
ffi (>= 0.5.0, < 2)
rb-inotify (0.10.0)
ffi (~> 1.0)
redcarpet (3.4.0)
reek (5.2.0)
codeclimate-engine-rb (~> 0.4.0)
2 changes: 1 addition & 1 deletion lib/crypt_ident.rb
Original file line number Diff line number Diff line change
@@ -457,7 +457,7 @@ def sign_out(current_user:)
# - Registered User
# - Repository
def change_password(user_in, current_password, new_password, repo: nil)
new_params = { config: CryptIdent.configure_crypt_ident, repo: repo,
new_params = { config: CryptIdent.cryptid_config, repo: repo,
user: user_in }
call_params = [current_password, new_password]
ChangePassword.new(new_params).call(*call_params) { |result| yield result }
7 changes: 3 additions & 4 deletions lib/crypt_ident/generate_reset_token.rb
Original file line number Diff line number Diff line change
@@ -52,7 +52,7 @@ def init_ivars(user_name, current_user, repo)
end

def new_token
token_length = CryptIdent.configure_crypt_ident.token_bytes
token_length = CryptIdent.cryptid_config.token_bytes
clear_text_token = SecureRandom.alphanumeric(token_length)
Base64.strict_encode64(clear_text_token)
end
@@ -62,8 +62,7 @@ def update_repo(user)
end

def updated_attribs
# FIXME: Using config-default reset expiry
prea = Time.now + CryptIdent.configure_crypt_ident.reset_expiry
prea = Time.now + CryptIdent.cryptid_config.reset_expiry
{ token: new_token, password_reset_expires_at: prea }
end

@@ -92,7 +91,7 @@ def user_not_found_error

# Reek sees a :reek:ControlParameter in `repo`. Ignoring.
def repo_from(repo)
repo || CryptIdent.configure_crypt_ident.repository
repo || CryptIdent.cryptid_config.repository
end

def validate_current_user
2 changes: 1 addition & 1 deletion lib/crypt_ident/sign_in.rb
Original file line number Diff line number Diff line change
@@ -53,7 +53,7 @@ def same_user?
def set_ivars(user, password, current)
@user = user
@password = password
@current_user = current || CryptIdent.configure_crypt_ident.guest_user
@current_user = current || CryptIdent.cryptid_config.guest_user
end

def validate_user_and_current_user
2 changes: 1 addition & 1 deletion lib/crypt_ident/sign_out.rb
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ class SignOut
# with that of the (reworked) `#sign_up` and `#sign_in` methods.
def call(current_user:)
_ = current_user # presently ignored
Success(config: CryptIdent.configure_crypt_ident)
Success(config: CryptIdent.cryptid_config)
end
end
end
6 changes: 3 additions & 3 deletions lib/crypt_ident/sign_up.rb
Original file line number Diff line number Diff line change
@@ -39,9 +39,9 @@ def all_attribs(attribs)
end

def config_with_repo(repo)
config_hash = CryptIdent.configure_crypt_ident.to_h
config_hash[:repository] = repo if repo
Config.new config_hash
CryptIdent.configure_crypt_ident do |config|
config.repository = repo if repo
end
end

# XXX: This has a Flog score of 9.8. Truly simplifying PRs welcome.
4 changes: 2 additions & 2 deletions test/crypt_ident/change_password_test.rb
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@
end

before do
dummy_repo = repo || CryptIdent.configure_crypt_ident.repository
dummy_repo = repo || CryptIdent.cryptid_config.repository
dummy_repo.clear
_ = target_user
end
@@ -91,7 +91,7 @@
end # describe 'Successfully change password using'

describe 'Fail to change password because the specified' do
let(:repo) { CryptIdent.configure_crypt_ident.repository }
let(:repo) { CryptIdent.cryptid_config.repository }
let(:result_from_failure) do
lambda do |user, current, new_password|
change_password(user, current, new_password, repo: repo) do |result|
16 changes: 8 additions & 8 deletions test/crypt_ident/generate_reset_token_test.rb
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
let(:created_user) do
password_hash = BCrypt::Password.create(password)
user = User.new name: user_name, password_hash: password_hash
our_repo = repo || CryptIdent.configure_crypt_ident.repository
our_repo = repo || CryptIdent.cryptid_config.repository
our_repo.create(user)
end
let(:other_params) { { repo: repo, current_user: nil } }
@@ -26,12 +26,13 @@
let(:user_name) { 'J Random Someone' }

before do
our_repo = repo || CryptIdent.configure_crypt_ident.repository
CryptIdent.reset_crypt_ident_config
our_repo = repo || CryptIdent.cryptid_config.repository
our_repo.clear
end

describe 'using an explicitly-supplied Repository' do
let(:repo) { CryptIdent.configure_crypt_ident.repository }
let(:repo) { CryptIdent.cryptid_config.repository }
let(:result_from_failure) do
lambda do |user_name, current_user|
the_code = the_current_user = the_name = :unassigned
@@ -67,7 +68,7 @@
expect(actual.password_reset_expires_at).must_be :nil?
actual = result_from_success.call
remaining = actual.password_reset_expires_at - Time.now
reset_expiry = CryptIdent.configure_crypt_ident.reset_expiry
reset_expiry = CryptIdent.cryptid_config.reset_expiry
expect(reset_expiry - remaining).must_be :<, 5 # seconds
end
end # describe 'it passes a User Entity to the result.success block with'
@@ -181,16 +182,15 @@
expect(actual.password_reset_expires_at).must_be :nil?
actual = result_from_success.call
remaining = actual.password_reset_expires_at - Time.now
# FIXME: Relies on default config?
reset_expiry = CryptIdent.configure_crypt_ident.reset_expiry
reset_expiry = CryptIdent.cryptid_config.reset_expiry
expect(reset_expiry - remaining).must_be :<, 5 # seconds
end
end # describe 'it passes a User Entity to the result.success block with'

it 'persists the updated Entity to the Repository' do
_ = created_user
entity = result_from_success.call
repo = CryptIdent.configure_crypt_ident.repository
repo = CryptIdent.cryptid_config.repository
expect(repo.first).must_equal entity
expect(repo.all.count).must_equal 1
end
@@ -200,7 +200,7 @@
_ = created_user
@first = result_from_success.call
@second = result_from_success.call
@persisted = CryptIdent.configure_crypt_ident.repository
@persisted = CryptIdent.cryptid_config.repository
.find(@first.id)
end

9 changes: 4 additions & 5 deletions test/crypt_ident/reset_password_test.rb
Original file line number Diff line number Diff line change
@@ -11,8 +11,7 @@
password_hash = BCrypt::Password.create(password)
user = User.new name: user_name, password_hash: password_hash,
token: token, password_reset_expires_at: expires_at
our_repo = repo || CryptIdent.configure_crypt_ident.repository
our_repo.clear # XXX: WTAF?!?
our_repo = repo || CryptIdent.cryptid_config.repository
our_repo.create(user)
end
let(:new_password) { 'New Sufficiently Entropic Passphrase' }
@@ -21,9 +20,9 @@
let(:token) { SecureRandom.alphanumeric(24) }
let(:user_name) { 'J Random Someone' }

after do
repo&.clear
CryptIdent.configure_crypt_ident.repository.clear
before do
our_repo = repo || CryptIdent.cryptid_config.repository
our_repo.clear
end

describe 'using an explicitly-supplied Repository' do
10 changes: 7 additions & 3 deletions test/crypt_ident/sign_in_test.rb
Original file line number Diff line number Diff line change
@@ -5,18 +5,22 @@
include CryptIdent

describe 'CryptIdent#sign_in' do
let(:guest_user) { CryptIdent.configure_crypt_ident.guest_user }
let(:guest_user) { CryptIdent.cryptid_config.guest_user }
let(:password) { 'Suitably Entropic Password' }
let(:repo) { UserRepository.new }
let(:user) do
password_hash = BCrypt::Password.create(password)
user = User.new name: user_name, password_hash: password_hash
our_repo = repo || CryptIdent.configure_crypt_ident.repository
our_repo.clear # XXX: WTAF?!?
our_repo = repo || CryptIdent.cryptid_config.repository
our_repo.create(user)
end
let(:user_name) { 'J Random User' }

before do
our_repo = repo || CryptIdent.cryptid_config.repository
our_repo.clear
end

describe 'when no Authenticated User is Signed In' do
describe 'when the correct password is supplied' do
it 'returns the same User Entity used for Authentication' do
23 changes: 13 additions & 10 deletions test/crypt_ident/sign_out_test.rb
Original file line number Diff line number Diff line change
@@ -5,8 +5,8 @@
include CryptIdent

describe 'CryptIdent#sign_out' do
let(:guest_user) { CryptIdent.configure_crypt_ident.guest_user }
let(:long_ago) { Hanami::Utils::Kernel.Time(0) }
let(:guest_user) { CryptIdent.cryptid_config.guest_user }
let(:far_future) { Time.now + 100 * 365 * 24 * 3600 } # 100 years should do...
let(:session) { Hash[] }

describe 'when an Authenticated User is Signed In' do
@@ -16,12 +16,13 @@
password = 'Anything'
password_hash = BCrypt::Password.create(password)
user = User.new name: user_name, password_hash: password_hash
our_repo = repo || CryptIdent.configure_crypt_ident.repository
our_repo.clear # XXX: WTAF?!?
our_repo = repo || CryptIdent.cryptid_config.repository
our_repo.create(user)
end

before do
our_repo = repo || CryptIdent.cryptid_config.repository
our_repo.clear
session[:current_user] = user
session[:start_time] = Time.now - 60 # 1 minute ago
end
@@ -30,14 +31,17 @@
sign_out(current_user: session[:current_user]) do |result|
result.success do
session[:current_user] = guest_user
session[:start_time] = long_ago
session[:start_time] = far_future
end

result.failure { next }
end
# TODO: Consider calling `#session_expired?` once implemented.
# We had been considering calling `#session_expired?` here, as in
# `expect(session_expired(session)).must_equal true` *but...* that API
# has changed since when we first put the 'todo' note in, which suggests
# that it's an SRP-level Bad Idea. Removing the note; taking the cannoli.
expect(session[:current_user]).must_equal guest_user
expect(session[:start_time]).must_equal long_ago
expect(session[:start_time]).must_equal far_future
end

it 'and session-data items are deleted' do
@@ -62,14 +66,13 @@
sign_out(current_user: nil) do |result|
result.success do
session[:current_user] = guest_user
session[:start_time] = long_ago
session[:start_time] = far_future
end

result.failure { next }
end
# TODO: Consider calling `#session_expired?` once implemented.
expect(session[:current_user]).must_equal guest_user
expect(session[:start_time]).must_equal long_ago
expect(session[:start_time]).must_equal far_future
end

it 'and session-data items are deleted' do
2 changes: 1 addition & 1 deletion test/crypt_ident/sign_up_test.rb
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@
end
result.failure { fail 'Oops' }
end
expect(saved[:conf]).must_equal CryptIdent.configure_crypt_ident
expect(saved[:conf]).must_equal CryptIdent.cryptid_config
expect(saved[:user]).must_equal repo.last
end

0 comments on commit e4568b9

Please sign in to comment.