Simplified the database layer by removing custom interface wrappers and using standard library *sql.DB and *sql.Tx types directly. Changes: - Removed DBConnection and DBTransaction interfaces from database.go - Removed NewDBConnection() wrapper function - Updated TokenGenerator to use *sql.DB instead of DBConnection - Updated all validation and revocation methods to accept *sql.Tx - Updated TableManager to work with *sql.DB directly - Updated all tests to use db.Begin() instead of custom wrappers - Fixed GeneratorConfig.DB field (was DBConn) - Updated documentation in doc.go with correct API usage Benefits: - Simpler API with fewer abstractions - Works directly with database/sql standard library - Compatible with GORM (via gormDB.DB()) and Bun (share same *sql.DB) - Easier to understand and maintain - No unnecessary wrapper layers Breaking changes: - GeneratorConfig.DBConn renamed to GeneratorConfig.DB - Removed NewDBConnection() function - pass *sql.DB directly - ValidateAccess/ValidateRefresh now accept *sql.Tx instead of DBTransaction - Token.Revoke/CheckNotRevoked now accept *sql.Tx instead of DBTransaction 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
41 lines
949 B
Go
41 lines
949 B
Go
package jwt
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func newTestGenerator(t *testing.T) *TokenGenerator {
|
|
gen, err := CreateGenerator(GeneratorConfig{
|
|
AccessExpireAfter: 15,
|
|
RefreshExpireAfter: 60,
|
|
FreshExpireAfter: 5,
|
|
TrustedHost: "example.com",
|
|
SecretKey: "supersecret",
|
|
DB: nil,
|
|
DBType: DatabaseType{Type: DatabasePostgreSQL, Version: "15"},
|
|
TableConfig: DefaultTableConfig(),
|
|
})
|
|
require.NoError(t, err)
|
|
return gen
|
|
}
|
|
|
|
func TestNewAccessToken(t *testing.T) {
|
|
gen := newTestGenerator(t)
|
|
|
|
tokenStr, exp, err := gen.NewAccess(123, true, false)
|
|
require.NoError(t, err)
|
|
require.NotEmpty(t, tokenStr)
|
|
require.Greater(t, exp, int64(0))
|
|
}
|
|
|
|
func TestNewRefreshToken(t *testing.T) {
|
|
gen := newTestGenerator(t)
|
|
|
|
tokenStr, exp, err := gen.NewRefresh(123, true)
|
|
require.NoError(t, err)
|
|
require.NotEmpty(t, tokenStr)
|
|
require.Greater(t, exp, int64(0))
|
|
}
|