Skip to content

Commit

Permalink
complete CheckTokenExpiration interactor
Browse files Browse the repository at this point in the history
  • Loading branch information
npauzenga committed Jan 5, 2016
1 parent 82b0cb6 commit efd48ce
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/interactors/check_token_expiration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,21 @@ class CheckTokenExpiration
include Interactor

def call
validate_input
execute
end

private

def validate_input
context.fail!(errors: "invalid token") unless context.sent_at
end

def execute
context.fail!(errors: "token expired") if token_expired?
end

def token_expired?
context.sent_at < 1.day.ago
end
end
43 changes: 43 additions & 0 deletions spec/interactors/check_token_expiration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
RSpec.describe CheckTokenExpiration do
describe ".call" do
context "when successful" do
let(:user) do
create(:confirmed_user, reset_sent_at: 1.hour.ago)
end

subject { described_class.call(sent_at: user.reset_sent_at) }

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

context "when sent_at not provided" do
subject { described_class.call(sent_at: nil) }

it "is a failure" do
is_expected.to be_a_failure
end

it "adds an error to errors" do
expect(subject.errors).to eq("invalid token")
end
end

context "when the token was sent more than a day ago" do
let(:user) do
create(:confirmed_user, reset_sent_at: (1.day + 1.hour).ago)
end

subject { described_class.call(sent_at: user.reset_sent_at) }

it "is a failure" do
is_expected.to be_a_failure
end

it "adds an error to errors" do
expect(subject.errors).to eq("token expired")
end
end
end
end
37 changes: 37 additions & 0 deletions spec/interactors/create_password_reset_token_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
RSpec.describe CreatePasswordResetToken do
describe ".call" do
let(:user) { create(:confirmed_user) }

context "when successful" do
subject { described_class.call(user: user) }

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

it "sets the reset digest" do
expect { subject }.to change(subject.user.reset_digest)
end
end

context "when input is invalid" do
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 output is invalid" do
it "fails" do
is_expected.to be_a_failure
end

it "adds an error to errors" do
expect(subject.errors).to eq("internal server error")
end
end
end
end

0 comments on commit efd48ce

Please sign in to comment.