Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor Users#reset_password_create action to reduce complexity
Extract password reset logic in PasswordService class.
- Loading branch information
Showing
3 changed files
with
167 additions
and
39 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,47 @@ | ||
class PasswordService | ||
|
||
attr_accessor :message | ||
|
||
def initialize(user, options={}) | ||
@user = user | ||
@password = options[:password] | ||
@confirm_password = options[:password_confirm] | ||
@email = options[:email] | ||
@message = fetch_message | ||
end | ||
|
||
def invalid? | ||
password_missing? || password_mismatch? || email_missing? | ||
end | ||
|
||
def reset_password | ||
@user.password = @password | ||
@user.save | ||
end | ||
|
||
private | ||
|
||
def password_missing? | ||
@password.blank? | ||
end | ||
|
||
def password_mismatch? | ||
@password != @confirm_password | ||
end | ||
|
||
def email_missing? | ||
@user.email ||= @email | ||
@user.email.blank? | ||
end | ||
|
||
def fetch_message | ||
if password_missing? | ||
"Password must be present" | ||
elsif password_mismatch? | ||
"Passwords do not match" | ||
elsif email_missing? | ||
"Email must be provided" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
require_relative '../test_helper' | ||
|
||
describe PasswordService do | ||
include TestHelper | ||
|
||
let(:user) { Fabricate(:user) } | ||
|
||
describe "#invalid?" do | ||
describe "when password_missing?" do | ||
describe "when password is not present" do | ||
let(:options) { { password: "" } } | ||
let(:password_service) { PasswordService.new(user, options) } | ||
|
||
it "should be invalid?" do | ||
assert_equal true, password_service.invalid? | ||
end | ||
end | ||
|
||
describe "when password is present" do | ||
let(:options) { { password: "password" } } | ||
let(:password_service) { PasswordService.new(user, options) } | ||
|
||
it "should be valid" do | ||
password_service.stubs(:password_mismatch?).returns false | ||
password_service.stubs(:email_missing?).returns false | ||
assert_equal false, password_service.invalid? | ||
end | ||
end | ||
end | ||
|
||
describe "when password_mismatch?" do | ||
describe "when password and confirm pasword don't match" do | ||
let(:options) { { password: "password", password_confirm: "somepassword" } } | ||
let(:password_service) { PasswordService.new(user, options) } | ||
|
||
it "should not be valid" do | ||
assert_equal true, password_service.invalid? | ||
end | ||
end | ||
|
||
describe "when password and confirm pasword match" do | ||
let(:options) { { password: "password", password_confirm: "password" } } | ||
let(:password_service) { PasswordService.new(user, options) } | ||
|
||
it "should be valid" do | ||
password_service.stubs(:password_missing?).returns false | ||
password_service.stubs(:email_missing?).returns false | ||
assert_equal false, password_service.invalid? | ||
end | ||
end | ||
end | ||
|
||
describe "when email_missing?" do | ||
describe "when user email not present" do | ||
let(:user) { Fabricate(:user, email: nil) } | ||
|
||
describe "and email not present in params" do | ||
let(:password_service) { PasswordService.new(user) } | ||
|
||
it "should return true" do | ||
assert_equal true, password_service.invalid? | ||
end | ||
end | ||
|
||
describe "and email is present in params" do | ||
let(:password_service) { PasswordService.new(user, { email: "some@email.com"}) } | ||
|
||
it "should return false" do | ||
password_service.stubs(:password_missing?).returns false | ||
password_service.stubs(:password_mismatch?).returns false | ||
|
||
assert_equal false, password_service.invalid? | ||
end | ||
|
||
it "should set the params email as the use email" do | ||
password_service.stubs(:password_missing?).returns false | ||
password_service.stubs(:password_mismatch?).returns false | ||
|
||
assert_equal false, password_service.invalid? | ||
end | ||
end | ||
end | ||
|
||
describe "when user email is present" do | ||
let(:user) { Fabricate(:user, email: "some@example.com") } | ||
let(:password_service) { PasswordService.new(user) } | ||
|
||
it "should NOT say email is missing" do | ||
password_service.stubs(:password_missing?).returns false | ||
password_service.stubs(:password_mismatch?).returns false | ||
|
||
assert_equal false, password_service.invalid? | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe "#reset_password" do | ||
let(:user) { Fabricate(:user, email: "some@example.com") } | ||
let(:password_service) { PasswordService.new(user, password: "password") } | ||
|
||
it "should reset the password successfully" do | ||
assert_equal true, password_service.reset_password | ||
end | ||
|
||
end | ||
|
||
end |