Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/find user by reset token interactor #57

Merged
merged 4 commits into from
Jan 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions app/interactors/find_user_by_reset_token.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
class FindUserByResetToken
include Interactor
class FindUserByResetToken < StandardInteraction
def validate_input
context.fail!(errors: "invalid input") unless context.reset_token
end

def execute
context.user = User.find_by(reset_digest: digest_token)
end

def validate_output
context.fail!(errors: "invalid output") unless context.user
end

private

def call
def digest_token
Encryptor.digest_token(context.reset_token)
end
end
57 changes: 57 additions & 0 deletions spec/interactors/find_user_by_reset_token_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
RSpec.describe FindUserByResetToken do
describe ".call" do
let(:token) { Encryptor.generate_token }

context "when successful" do
let!(:user) { create(:confirmed_user, reset_digest: token[0]) }

subject do
described_class.call(reset_token: token[1])
end

it "is a success" do
is_expected.to be_a_success
end

it "find the user" do
expect(subject.user).to eq(user)
end
end

context "when reset token not provided" do
before do
create(:confirmed_user, reset_digest: token[0])
end

subject do
described_class.call(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

context "when user not found" do
before do
create(:confirmed_user, reset_digest: "differenttoken")
end

subject do
described_class.call(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 output")
end
end
end
end