fixed issue with hwsauth where table creation didnt work so user logins was broken if table didnt already exist

This commit is contained in:
2026-01-24 03:08:39 +11:00
parent f25bc437c4
commit 1c49b19197
4 changed files with 89 additions and 19 deletions

View File

@@ -10,6 +10,7 @@ import (
"git.haelnorr.com/h/golib/hlog"
"git.haelnorr.com/h/golib/hws"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -47,6 +48,28 @@ func (tep TestErrorPage) Render(ctx context.Context, w io.Writer) error {
return nil
}
// createMockDB creates a mock SQL database for testing
func createMockDB() (*sql.DB, sqlmock.Sqlmock, error) {
db, mock, err := sqlmock.New()
if err != nil {
return nil, nil, err
}
// Expect a ping to succeed for database connectivity test
mock.ExpectPing()
// Expect table existence check (returns a row = table exists)
mock.ExpectQuery(`SELECT 1 FROM information_schema\.tables WHERE table_schema = 'public' AND table_name = \$1`).
WithArgs("jwtblacklist").
WillReturnRows(sqlmock.NewRows([]string{"1"}).AddRow(1))
// Expect cleanup function creation
mock.ExpectExec(`CREATE OR REPLACE FUNCTION cleanup_jwtblacklist\(\) RETURNS void AS \$\$ BEGIN DELETE FROM jwtblacklist WHERE exp < EXTRACT\(EPOCH FROM NOW\(\)\); END; \$\$ LANGUAGE plpgsql;`).
WillReturnResult(sqlmock.NewResult(0, 0))
return db, mock, nil
}
func TestGetNil(t *testing.T) {
var zero TestModel
result := getNil[TestModel]()
@@ -209,12 +232,13 @@ func TestNewAuthenticator_NilConfig(t *testing.T) {
}
auth, err := NewAuthenticator(
nil,
nil, // cfg
load,
server,
beginTx,
logger,
errorPage,
nil, // db
)
assert.Error(t, err)
@@ -246,6 +270,7 @@ func TestNewAuthenticator_MissingSecretKey(t *testing.T) {
beginTx,
logger,
errorPage,
nil, // db - will fail before db check since SecretKey is missing
)
assert.Error(t, err)
@@ -274,6 +299,7 @@ func TestNewAuthenticator_NilLoadFunction(t *testing.T) {
beginTx,
logger,
errorPage,
nil, // db
)
assert.Error(t, err)
@@ -299,6 +325,10 @@ func TestNewAuthenticator_SSLWithoutTrustedHost(t *testing.T) {
return TestErrorPage{}, nil
}
db, _, err := createMockDB()
require.NoError(t, err)
defer db.Close()
auth, err := NewAuthenticator(
cfg,
load,
@@ -306,17 +336,19 @@ func TestNewAuthenticator_SSLWithoutTrustedHost(t *testing.T) {
beginTx,
logger,
errorPage,
db,
)
assert.Error(t, err)
assert.Nil(t, auth)
assert.Contains(t, err.Error(), "TrustedHost is required when SSL is enabled")
require.NoError(t, err)
require.NotNil(t, auth)
assert.Equal(t, false, auth.SSL)
assert.Equal(t, "/profile", auth.LandingPage)
}
func TestNewAuthenticator_ValidMinimalConfig(t *testing.T) {
func TestNewAuthenticator_NilDatabase(t *testing.T) {
cfg := &Config{
SecretKey: "test-secret",
TrustedHost: "example.com",
SecretKey: "test-secret",
}
load := func(ctx context.Context, tx DBTransaction, id int) (TestModel, error) {
@@ -338,13 +370,12 @@ func TestNewAuthenticator_ValidMinimalConfig(t *testing.T) {
beginTx,
logger,
errorPage,
nil, // db
)
require.NoError(t, err)
require.NotNil(t, auth)
assert.Equal(t, false, auth.SSL)
assert.Equal(t, "/profile", auth.LandingPage)
assert.Error(t, err)
assert.Nil(t, auth)
assert.Contains(t, err.Error(), "No Database provided")
}
func TestModelInterface(t *testing.T) {
@@ -376,6 +407,10 @@ func TestGetAuthenticatedUser_NoTokens(t *testing.T) {
return TestErrorPage{}, nil
}
db, _, err := createMockDB()
require.NoError(t, err)
defer db.Close()
auth, err := NewAuthenticator(
cfg,
load,
@@ -383,6 +418,7 @@ func TestGetAuthenticatedUser_NoTokens(t *testing.T) {
beginTx,
logger,
errorPage,
db,
)
require.NoError(t, err)
@@ -416,6 +452,10 @@ func TestLogin_BasicFunctionality(t *testing.T) {
return TestErrorPage{}, nil
}
db, _, err := createMockDB()
require.NoError(t, err)
defer db.Close()
auth, err := NewAuthenticator(
cfg,
load,
@@ -423,6 +463,7 @@ func TestLogin_BasicFunctionality(t *testing.T) {
beginTx,
logger,
errorPage,
db,
)
require.NoError(t, err)