From 587b79ce54a50be1959df2e432469ecf5ac31be5 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 17 Sep 2024 14:48:12 +0200 Subject: [PATCH] Add a spec file for the JWT Administrate Field --- spec/fields/jwt_field_spec.rb | 53 +++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 spec/fields/jwt_field_spec.rb diff --git a/spec/fields/jwt_field_spec.rb b/spec/fields/jwt_field_spec.rb new file mode 100644 index 000000000..9b615d283 --- /dev/null +++ b/spec/fields/jwt_field_spec.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +describe JwtField do + subject(:jwt_field) { described_class.new(:jwt, token_data, :show) } + + let(:token_data) { nil } + + describe "#to_s" do + context "when there is no token data" do + let(:token_data) { nil } + + it "returns 'Pas de token'" do + expect(jwt_field.to_s).to eq("Pas de token") + end + end + + context "when the token is present but invalid" do + let(:token_data) { "invalid.token.data" } + + it "returns an invalid token message" do + expect(jwt_field.to_s).to match(/^Token invalide : /) + end + end + + context "when the token is valid and has no expiration" do + let(:token_data) { JWT.encode({}, nil, 'none') } + + it "returns a message indicating the token is present without expiration" do + expect(jwt_field.to_s).to eq("Token présent, sans expiration") + end + end + + context "when the token is valid and has a future expiration date" do + let(:expiration_time) { 2.days.from_now.to_i } + let(:token_data) { JWT.encode({ 'exp' => expiration_time }, nil, 'none') } + + it "returns a message indicating the token will expire in the future" do + expected_message = "Token présent, expirera le #{Time.zone.at(expiration_time).strftime('%d/%m/%Y à %H:%M')}" + expect(jwt_field.to_s).to eq(expected_message) + end + end + + context "when the token is valid and has a past expiration date" do + let(:expiration_time) { 2.days.ago.to_i } + let(:token_data) { JWT.encode({ 'exp' => expiration_time }, nil, 'none') } + + it "returns a message indicating the token has expired" do + expected_message = "Token présent, expiré le #{Time.zone.at(expiration_time).strftime('%d/%m/%Y à %H:%M')}" + expect(jwt_field.to_s).to eq(expected_message) + end + end + end +end