Skip to content

Commit

Permalink
Add token_spec.rb for AuthenticationUseCase
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianoflorentino committed Mar 15, 2024
1 parent 6496ed5 commit 7290c62
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions spec/use_cases/authentication_use_case/token_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe AuthenticationUseCase::Token do
let(:email) { '[email protected]' }
let(:password) { 'password1234' }

describe '#call' do
it 'generates a token with the correct payload' do
customer = create(:customer, email:, password:)

token = described_class.new(email, password).call
decoded_token = JWT.decode(token, Rails.application.credentials.secret_key_base, true, algorithm: 'HS256').first

expect(decoded_token['customer_id']).to eq(customer.id)
expect(decoded_token['email']).to eq(customer.email)
expect(decoded_token['password']).to eq(customer.password)
expect(decoded_token['exp']).to be_within(1.minute).of(1.hour.from_now.to_i)
end

it 'raises an error if the customer is not found' do
expect { described_class.new(email, password).call }.to raise_error(ActiveRecord::RecordNotFound)
end

it 'raises an error if the password is incorrect' do
create(:customer, email:, password: 'password1234')

expect { described_class.new(email, 'wrongpassword').call }.to raise_error(ActiveRecord::RecordInvalid)
end
end
end

0 comments on commit 7290c62

Please sign in to comment.