-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
094e86e
commit 1fb8c1e
Showing
4 changed files
with
61 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |