-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add token_spec.rb for AuthenticationUseCase
- Loading branch information
1 parent
6496ed5
commit 7290c62
Showing
1 changed file
with
32 additions
and
0 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 |
---|---|---|
@@ -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 |