0
     ˆÆÅÄ554ÃÂÁ *    ‹      Å•_o›0Åßû)<?ìnþ®S
žºiÑÒ$J3MÓ´ƒM°
ÙNhöégB¢°)YS­Û^°1ßß9XïU0.¿ÌGèÃòn‚æŸÞOÆC„€ÏÝ!@°ê=·ÕM1Â‰1Å  ,K·ìºR­`¹€Ädi´Q"2.3“+¯Z#WÈK8evDž&ådœ3þˆdŒ@®�ŒÛ�‹u˜Šè° Lf4ÏY,Rî2žI7Ñn$ea1iGëô ¶e·ß@”P½A·zÚEÃ30fÛoyP—¶0°§ñBÉ¶dûÿ Ùº»Hh¸ãª¦Šx&AšŠUîc#L<‘­�V‘�AD2×¦4pW"Æˆ¦ÆÇ_ÇÃÙ7«“To�¢DñØÇï†þôfæ˜LiÆ= §5wVs‹É„jƒ2ÉD,8;+¾¯Å÷âûùƒZp)Q!ó£¬Å¦ÕH¦º Ökß:HTSUKØSiÐè§0æ·‹ÑtŒu$¬ÚâH÷O>/&sªxnP �ŒTÛ½ÿæuêâæp‹öî”X%„tRùŒHÖùC.Ë¼™ŠMüdÖ&öÒü…¨Óê¼qÚ§ÕAíëAÿ-Bg„¶?|üSôX¦Œ«&ù¹/YÈ’+u=^Îß=ËÿÑ_Îß<.Çù“>ÚN·Uùè]ÿUÏ8Bk¾±g“z|	¿?H6Ø·Uv]ÞƒúWôéà@í    F   ˆl–ßi~”Šj"Tâ‚Æ[¸ØÅ£�þYl-¬ãr2C]ìÿ?À„-¼ÿÅ_�uÐb&=LyZËcõÄÃ 5    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe Token, type: :model do
  subject(:token) { create(:password_reset_token, user: user) }

  let(:user) { create(:user) }

  describe '.check' do
    context 'with name and action matching existing token' do
      it 'returns the tokenâ€™s user' do
        expect(described_class.check(action: token.action, token: token.token)).to eq(token.user)
      end
    end

    context 'with invalid name' do
      it 'returns nil' do
        expect(described_class.check(action: token.action, token: '1NV4L1D')).to be_nil
      end
    end

    context 'with invalid action' do
      it 'returns nil' do
        expect(described_class.check(action: 'PasswordReset_NotExisting', token: token.token)).to be_nil
      end
    end

    describe 'persistence handling' do
      context 'for persistent token' do
        subject(:token) { create(:ical_token, persistent: true, created_at: created_at) }

        context 'at any time' do
          let(:created_at) { 1.month.ago }

          it 'returns the tokenâ€™s user' do
            expect(described_class.check(action: token.action, token: token.token)).to eq(token.user)
          end

          it 'does not delete the token' do
            token  # create token

            expect { described_class.check(action: token.action, token: token.token) }
              .not_to change(described_class, :count)
          end
        end
      end

      context 'for non-persistent token' do
        subject(:token) { create(:password_reset_token, persistent: false, created_at: created_at) }

        context 'less than one day after creation' do
          let(:created_at) { 1.day.ago + 5 }

          it 'returns the tokenâ€™s user' do
            expect(described_class.check(action: token.action, token: token.token)).to eq(token.user)
          end

          it 'does not delete the token' do
            token  # create token

            expect { described_class.check(action: token.action, token: token.token) }
              .not_to change(described_class, :count)
          end
        end

        context 'at least one day after creation' do
          let(:created_at) { 1.day.ago }

          it 'returns nil' do
            expect(described_class.check(action: token.action, token: token.token)).to be_nil
          end

          it 'deletes the token' do
            token  # create token

            expect { described_class.check(action: token.action, token: token.token) }
              .to change(described_class, :count).by(-1)
          end
        end
      end
    end

    describe 'inactive user handling' do
      let(:user) { create(:user, active: false) }

      context 'for persistent token' do
        subject(:token) { create(:ical_token, user: user) }

        it 'returns nil' do
          expect(described_class.check(action: token.action, token: token.token)).to be_nil
        end
      end

      context 'for non-persistent token' do
        subject(:token) { create(:password_reset_token, user: user, persistent: false) }

        it 'returns nil' do
          expect(described_class.check(action: token.action, token: token.token)).to be_nil
        end
      end

      context 'with inactive_user option' do
        it 'returns the tokenâ€™s user' do
          expect(described_class.check(action: token.action, token: tok