2026-07-27 13:12:43 +08:00
package initdb
import (
"regexp"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
2026-07-29 13:18:52 +08:00
func TestInitPlatformAccessSeedsRootRole ( t * testing . T ) {
2026-07-27 13:12:43 +08:00
sqlDatabase , mock , err := sqlmock . New ( )
if err != nil {
t . Fatal ( err )
}
t . Cleanup ( func ( ) { _ = sqlDatabase . Close ( ) } )
database , err := gorm . Open ( postgres . New ( postgres . Config { Conn : sqlDatabase } ) , & gorm . Config { } )
if err != nil {
t . Fatal ( err )
}
2026-08-04 15:21:39 +08:00
mock . ExpectQuery ( regexp . QuoteMeta ( ` SELECT * FROM "platform_role" WHERE role_code = $1 AND "platform_role"."deleted_at" IS NULL ORDER BY "platform_role"."id" LIMIT $2 ` ) ) .
2026-07-27 13:12:43 +08:00
WithArgs ( "root" , 1 ) .
2026-07-29 14:25:38 +08:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" , "identity" , "status" , "role_code" , "name" , "location_scope" , "is_system" } ) .
AddRow ( uint64 ( 1 ) , "root-role" , 1 , "root" , "Root" , "precise" , true ) )
2026-07-27 13:12:43 +08:00
if err := InitPlatformAccess ( database ) ; err != nil {
t . Fatal ( err )
}
if err := mock . ExpectationsWereMet ( ) ; err != nil {
t . Fatal ( err )
}
}
2026-07-30 10:01:19 +08:00
func TestPlatformRootPassword ( t * testing . T ) {
t . Run ( "accepts six character environment password" , func ( t * testing . T ) {
t . Setenv ( "HEQI_PLATFORM_ROOT_PASSWORD" , "123456" )
if got := platformRootPassword ( ) ; got != "123456" {
t . Fatalf ( "platformRootPassword() = %q, want environment password" , got )
}
} )
t . Run ( "falls back when environment password is too short" , func ( t * testing . T ) {
t . Setenv ( "HEQI_PLATFORM_ROOT_PASSWORD" , "12345" )
if got := platformRootPassword ( ) ; got != PlatformRootPassword {
t . Fatalf ( "platformRootPassword() = %q, want default password" , got )
}
} )
}