Skip to content

Commit

Permalink
Update LuckyRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcsmith committed Apr 20, 2018
1 parent 094e86e commit 1fb8c1e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 20 deletions.
3 changes: 1 addition & 2 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ license: MIT
dependencies:
lucky:
github: luckyframework/lucky
lucky_record:
github: luckyframework/lucky_record
branch: master
habitat:
github: luckyframework/habitat

Expand Down
53 changes: 53 additions & 0 deletions spec/base_sign_in_form_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require "./spec_helper"

private class TestSignInForm < LuckyRecord::VirtualForm
include Authentic::BaseSignInForm

virtual password : String

def initialize(
@authenticatable : FakeAuthenticatable?
)
end

private def validate_allowed_to_sign_in(user : FakeAuthenticatable?)
if user && user.encrypted_password != "right"
password.add_error "Password must be: 'right'"
end
end

private def find_authenticatable
@authenticatable
end
end

describe Authentic::BaseSignInForm do
it "authenticatable is not returned when form is invalid" do
authenticatable = FakeAuthenticatable.new(encrypted_password: "wrong")
form = TestSignInForm.new(authenticatable)

form.submit do |form, user|
form.valid?.should be_false
user.should be_nil
end
end

it "authenticatable is returned when form is valid" do
authenticatable = FakeAuthenticatable.new(encrypted_password: "right")
form = TestSignInForm.new(authenticatable)

form.submit do |form, user|
form.valid?.should be_true
user.should eq authenticatable
end
end

it "authenticatable is not returned when it is not found" do
form = TestSignInForm.new(nil)

form.submit do |form, user|
form.valid?.should be_true
user.should be_nil
end
end
end
4 changes: 2 additions & 2 deletions src/authentic.cr
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ module Authentic
# end
# ```
def self.copy_and_encrypt(
from password_field : LuckyRecord::Field | LuckyRecord::AllowedField,
to encrypted_password_field : LuckyRecord::Field | LuckyRecord::AllowedField
from password_field : LuckyRecord::Field | LuckyRecord::FillableField,
to encrypted_password_field : LuckyRecord::Field | LuckyRecord::FillableField
) : Void
password_field.value.try do |value|
encrypted_password_field.value = generate_encrypted_password(value)
Expand Down
21 changes: 5 additions & 16 deletions src/authentic/base_sign_in_form.cr
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
require "crypto/bcrypt/password"

module Authentic::BaseSignInForm
macro included
def self.submit(params)
new(params).submit do |form, user|
yield form, user
end
end
end

def submit
validate_allowed_to_sign_in(user_from_email)
authenticatable = find_authenticatable
validate_allowed_to_sign_in(authenticatable)
if valid?
yield self, user_from_email
yield self, authenticatable
else
yield self, nil
end
end

abstract def validate_allowed_to_sign_in(user)
abstract def validate_allowed_to_sign_in(authenticatable)

private def user_from_email : User?
email.value.try do |value|
UserQuery.new.email(value).first?
end
end
abstract def find_authenticatable
end

0 comments on commit 1fb8c1e

Please sign in to comment.