-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
6 changed files
with
130 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,19 @@ | ||
class SendPasswordResetEmail | ||
include Interactor | ||
class SendPasswordResetEmail < StandardInteraction | ||
def validate_input | ||
context.fail!(errors: "invalid input") unless valid_input | ||
end | ||
|
||
def execute | ||
context.fail!(errors: "delivery failed") unless send_reset_email | ||
end | ||
|
||
private | ||
|
||
def send_reset_email | ||
UserMailer.password_reset(context.user).deliver_now | ||
end | ||
|
||
def call | ||
def valid_input | ||
context.user && context.user.reset_digest && context.user.reset_token | ||
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,11 @@ | ||
class UserMailer < ActionMailer::Base | ||
default from: "admin@meetmeeples.com" | ||
|
||
def password_reset(user) | ||
@user = user | ||
@first_name = user.first_name | ||
|
||
mail(to: "#{@user.email}", | ||
subject: "Meet Meeples Password Reset") | ||
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
class User < ActiveRecord::Base | ||
attr_accessor :reset_token | ||
|
||
has_secure_password | ||
|
||
has_many :user_group_memberships | ||
|
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,2 @@ | ||
<h1>Hi <%= @first_name %>,</h1> | ||
<p>Please click the link below to reset your password</p> |
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,73 @@ | ||
RSpec.describe SendPasswordResetEmail do | ||
describe ".call" do | ||
let(:token) { Encryptor.generate_token } | ||
|
||
let(:user) do | ||
create(:confirmed_user, reset_digest: token[0], | ||
reset_token: token[1]) | ||
end | ||
|
||
subject(:send_email) do | ||
described_class.call(user: user) | ||
end | ||
|
||
before do | ||
ActionMailer::Base.deliveries.clear | ||
end | ||
|
||
context "when successful" do | ||
it "is a success" do | ||
is_expected.to be_a_success | ||
end | ||
|
||
it "sends an email" do | ||
send_email | ||
expect(ActionMailer::Base.deliveries.count).to eq(1) | ||
end | ||
end | ||
|
||
context "when user not provided" do | ||
subject(:send_email) do | ||
described_class.call(user: nil) | ||
end | ||
|
||
it "fails" do | ||
is_expected.to be_a_failure | ||
end | ||
|
||
it "adds an error to errors" do | ||
expect(subject.errors).to eq("invalid input") | ||
end | ||
end | ||
|
||
context "when user does not have a valid reset_digest" do | ||
let(:user) do | ||
create(:confirmed_user, reset_digest: nil, | ||
reset_token: token[1]) | ||
end | ||
|
||
it "fails" do | ||
is_expected.to be_a_failure | ||
end | ||
|
||
it "adds an error to errors" do | ||
expect(subject.errors).to eq("invalid input") | ||
end | ||
end | ||
|
||
context "when user does not have a valid reset_token" do | ||
let(:user) do | ||
create(:confirmed_user, reset_digest: token[0], | ||
reset_token: nil) | ||
end | ||
|
||
it "fails" do | ||
is_expected.to be_a_failure | ||
end | ||
|
||
it "adds an error to errors" do | ||
expect(subject.errors).to eq("invalid input") | ||
end | ||
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,26 @@ | ||
RSpec.describe UserMailer do | ||
describe ".password_reset" do | ||
let(:token) { Encryptor.generate_token } | ||
let(:user) { create(:confirmed_user, reset_token: token[1]) } | ||
|
||
let(:mail) do | ||
described_class.password_reset(user) | ||
end | ||
|
||
it "sets the subject" do | ||
expect(mail.subject).to eq("Meet Meeples Password Reset") | ||
end | ||
|
||
it "sets the receiver email" do | ||
expect(mail.to).to eq([user.email]) | ||
end | ||
|
||
it "sets the sender email" do | ||
expect(mail.from).to eq(["admin@meetmeeples.com"]) | ||
end | ||
|
||
it "assigns @first_name" do | ||
expect(mail.body.encoded).to match(user.first_name) | ||
end | ||
end | ||
end |