Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor service object and add unit test
- Loading branch information
Showing
3 changed files
with
49 additions
and
5 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 |
---|---|---|
@@ -1,11 +1,22 @@ | ||
class AuthenticateUser | ||
|
||
def self.call(user, params) | ||
if user && user.authenticate(params[:password]) | ||
true | ||
def initialize(user, params) | ||
@user = user | ||
@params = params | ||
@result = false | ||
end | ||
|
||
def call | ||
if @user && @user.authenticate(@params[:password]) | ||
@result = true | ||
else | ||
false | ||
@result = false | ||
end | ||
self | ||
end | ||
|
||
def success? | ||
@result | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe AuthenticateUser do | ||
|
||
subject { AuthenticateUser.new(user, params).call } | ||
|
||
describe 'for unknown user' do | ||
let(:user) { nil } | ||
let(:params) { {} } | ||
it 'is unsuccessful' do | ||
expect(subject.success?).to be false | ||
end | ||
end | ||
|
||
describe 'for known user' do | ||
let(:user) { Fabricate.build(:user) } | ||
|
||
context 'with correct password' do | ||
let(:params) { { password: user.password } } | ||
it 'is successful' do | ||
expect(subject.success?).to be true | ||
end | ||
end | ||
|
||
context 'with incorrect password' do | ||
let(:params) { { password: 'wrongpassword' } } | ||
it 'is unsuccessful' do | ||
expect(subject.success?).to be false | ||
end | ||
end | ||
end | ||
end |