-
-
Save pda/c92e555dbc16d95d1edb to your computer and use it in GitHub Desktop.
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
require "bcrypt" | |
module GoTipping | |
class UserAuthenticator | |
def initialize(organisation, username, finder) | |
@organisation = organisation | |
@username = username | |
@finder = finder | |
end | |
def authenticate(password) | |
return false unless user | |
if BCrypt::Password.new(user.password_digest) == password | |
user | |
else | |
false | |
end | |
end | |
private | |
def user | |
@_user ||= @finder.by_organisation_and_username(@organisation, @username) | |
end | |
end | |
class UserFinder | |
def by_organisation_and_username(organisation, username) | |
organization.users.find_by(username: username) | |
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
require_relative "gistfile1" | |
RSpec.describe GoTipping::UserAuthenticator do | |
subject { described_class.new(organisation, username, finder) } | |
let(:username) { "foo" } | |
let(:finder) { instance_double(GoTipping::UserFinder) } | |
let(:organisation) { double("Organization") } | |
let(:user) { double("User", password_digest: digest) } | |
let(:digest) { "$2a$10$jYOHOwYW6PcYGUG6IafkV.WAqFY86sfkzjqOxxmycp7UozxXRr5ia" } | |
before do | |
expect(finder).to receive(:by_organisation_and_username). | |
with(organisation, username). | |
and_return(user) | |
end | |
context "user not found" do | |
let(:user) { nil } | |
it "is false" do | |
expect(subject.authenticate("foo")).to eq(false) | |
end | |
end | |
context "password wrong" do | |
it "is false" do | |
expect(subject.authenticate("wrong")).to eq(false) | |
end | |
end | |
context "password correct" do | |
it "returns the user" do | |
expect(subject.authenticate("right")).to eq(user) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment