// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package user_test

import (
	"testing"

	"code.gitea.io/gitea/models/unittest"
	user_model "code.gitea.io/gitea/models/user"
	"code.gitea.io/gitea/modules/setting"
	"code.gitea.io/gitea/modules/test"

	"github.com/stretchr/testify/require"
)

func TestResetPasswordTimeLimitCodeUsesResetPasswordLifetime(t *testing.T) {
	require.NoError(t, unittest.PrepareTestDatabase())
	defer test.MockVariableValue(&setting.Service.ActiveCodeLives, 0)()
	defer test.MockVariableValue(&setting.Service.ResetPwdCodeLives, 180)()

	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
	opts := &user_model.TimeLimitCodeOptions{Purpose: user_model.TimeLimitCodeResetPassword}
	code := user_model.GenerateUserTimeLimitCode(opts, user)

	require.Equal(t, "000180", code[12:18])
	require.NotNil(t, user_model.VerifyUserTimeLimitCode(t.Context(), opts, code))
}

func TestAdminInviteTimeLimitCodeUsesInvitationLifetime(t *testing.T) {
	require.NoError(t, unittest.PrepareTestDatabase())
	defer test.MockVariableValue(&setting.Service.ActiveCodeLives, 0)()

	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
	opts := &user_model.TimeLimitCodeOptions{Purpose: user_model.TimeLimitCodeAdminInvite}
	code := user_model.GenerateUserTimeLimitCode(opts, user)

	require.Equal(t, "001440", code[12:18])
	require.NotNil(t, user_model.VerifyUserTimeLimitCode(t.Context(), opts, code))
}
