fixed transaction issues

This commit is contained in:
2026-01-01 22:44:39 +11:00
parent c4574e32c7
commit 05aad5f11b
5 changed files with 57 additions and 69 deletions

View File

@@ -2,6 +2,7 @@ package jwt
import (
"context"
"database/sql"
"testing"
"time"
@@ -31,14 +32,15 @@ func TestNoDBFail(t *testing.T) {
token := AccessToken{
JTI: jti,
EXP: exp,
gen: &TokenGenerator{},
}
// Revoke should fail due to no DB
err := token.Revoke(context.Background())
err := token.Revoke(&sql.Tx{})
require.Error(t, err)
// CheckNotRevoked should fail
_, err = token.CheckNotRevoked(context.Background())
_, err = token.CheckNotRevoked(&sql.Tx{})
require.Error(t, err)
}
@@ -52,7 +54,7 @@ func TestRevokeAndCheckNotRevoked(t *testing.T) {
token := AccessToken{
JTI: jti,
EXP: exp,
db: gen.dbConn,
gen: gen,
}
// Revoke expectations
@@ -60,21 +62,22 @@ func TestRevokeAndCheckNotRevoked(t *testing.T) {
mock.ExpectExec(`INSERT INTO jwtblacklist`).
WithArgs(jti, exp).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
err := token.Revoke(context.Background())
require.NoError(t, err)
// CheckNotRevoked expectations (now revoked)
mock.ExpectBegin()
mock.ExpectQuery(`SELECT 1 FROM jwtblacklist`).
WithArgs(jti).
WillReturnRows(sqlmock.NewRows([]string{"1"}).AddRow(1))
mock.ExpectCommit()
valid, err := token.CheckNotRevoked(context.Background())
tx, err := gen.dbConn.BeginTx(context.Background(), nil)
defer tx.Rollback()
require.NoError(t, err)
err = token.Revoke(tx)
require.NoError(t, err)
valid, err := token.CheckNotRevoked(tx)
require.NoError(t, err)
require.False(t, valid)
require.NoError(t, tx.Commit())
require.NoError(t, mock.ExpectationsWereMet())
}