2021-11-23 09:45:38 +00:00
package storage
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestShouldReturnErrOnTargetSameAsCurrent ( t * testing . T ) {
assert . EqualError ( t ,
schemaMigrateChecks ( providerSQLite , true , 1 , 1 ) ,
fmt . Sprintf ( ErrFmtMigrateAlreadyOnTargetVersion , 1 , 1 ) )
assert . EqualError ( t ,
schemaMigrateChecks ( providerSQLite , false , 1 , 1 ) ,
fmt . Sprintf ( ErrFmtMigrateAlreadyOnTargetVersion , 1 , 1 ) )
assert . EqualError ( t ,
schemaMigrateChecks ( providerSQLite , false , 2 , 2 ) ,
fmt . Sprintf ( ErrFmtMigrateAlreadyOnTargetVersion , 2 , 2 ) )
assert . EqualError ( t ,
schemaMigrateChecks ( providerMySQL , false , 1 , 1 ) ,
fmt . Sprintf ( ErrFmtMigrateAlreadyOnTargetVersion , 1 , 1 ) )
assert . EqualError ( t ,
schemaMigrateChecks ( providerPostgres , false , 1 , 1 ) ,
fmt . Sprintf ( ErrFmtMigrateAlreadyOnTargetVersion , 1 , 1 ) )
}
2022-11-25 12:44:55 +00:00
func TestShouldReturnErrOnUpMigrationTargetVersionLessThanCurrent ( t * testing . T ) {
2021-11-23 09:45:38 +00:00
assert . EqualError ( t ,
2022-10-20 02:16:36 +00:00
schemaMigrateChecks ( providerPostgres , true , 0 , LatestVersion ) ,
fmt . Sprintf ( ErrFmtMigrateUpTargetLessThanCurrent , 0 , LatestVersion ) )
2021-11-23 09:45:38 +00:00
assert . NoError ( t ,
2022-10-20 02:16:36 +00:00
schemaMigrateChecks ( providerPostgres , true , LatestVersion , 0 ) )
2021-11-23 09:45:38 +00:00
assert . EqualError ( t ,
2022-10-20 02:16:36 +00:00
schemaMigrateChecks ( providerSQLite , true , 0 , LatestVersion ) ,
fmt . Sprintf ( ErrFmtMigrateUpTargetLessThanCurrent , 0 , LatestVersion ) )
2021-11-23 09:45:38 +00:00
assert . NoError ( t ,
2022-10-20 02:16:36 +00:00
schemaMigrateChecks ( providerSQLite , true , LatestVersion , 0 ) )
2021-11-23 09:45:38 +00:00
assert . EqualError ( t ,
2022-10-20 02:16:36 +00:00
schemaMigrateChecks ( providerMySQL , true , 0 , LatestVersion ) ,
fmt . Sprintf ( ErrFmtMigrateUpTargetLessThanCurrent , 0 , LatestVersion ) )
2021-11-23 09:45:38 +00:00
assert . NoError ( t ,
2022-10-20 02:16:36 +00:00
schemaMigrateChecks ( providerMySQL , true , LatestVersion , 0 ) )
2021-11-23 09:45:38 +00:00
}
func TestMigrationUpShouldReturnErrOnAlreadyLatest ( t * testing . T ) {
assert . Equal ( t ,
ErrSchemaAlreadyUpToDate ,
2022-10-20 02:16:36 +00:00
schemaMigrateChecks ( providerPostgres , true , SchemaLatest , LatestVersion ) )
2021-11-23 09:45:38 +00:00
assert . Equal ( t ,
ErrSchemaAlreadyUpToDate ,
2022-10-20 02:16:36 +00:00
schemaMigrateChecks ( providerMySQL , true , SchemaLatest , LatestVersion ) )
2021-11-23 09:45:38 +00:00
assert . Equal ( t ,
ErrSchemaAlreadyUpToDate ,
2022-10-20 02:16:36 +00:00
schemaMigrateChecks ( providerSQLite , true , SchemaLatest , LatestVersion ) )
2021-11-23 09:45:38 +00:00
}
func TestShouldReturnErrOnVersionDoesntExits ( t * testing . T ) {
assert . EqualError ( t ,
2022-10-20 02:16:36 +00:00
schemaMigrateChecks ( providerPostgres , true , SchemaLatest - 1 , LatestVersion ) ,
fmt . Sprintf ( ErrFmtMigrateUpTargetGreaterThanLatest , SchemaLatest - 1 , LatestVersion ) )
2021-11-23 09:45:38 +00:00
assert . EqualError ( t ,
2022-10-20 02:16:36 +00:00
schemaMigrateChecks ( providerMySQL , true , SchemaLatest - 1 , LatestVersion ) ,
fmt . Sprintf ( ErrFmtMigrateUpTargetGreaterThanLatest , SchemaLatest - 1 , LatestVersion ) )
2021-11-23 09:45:38 +00:00
assert . EqualError ( t ,
2022-10-20 02:16:36 +00:00
schemaMigrateChecks ( providerSQLite , true , SchemaLatest - 1 , LatestVersion ) ,
fmt . Sprintf ( ErrFmtMigrateUpTargetGreaterThanLatest , SchemaLatest - 1 , LatestVersion ) )
2021-11-23 09:45:38 +00:00
}
2022-11-25 12:44:55 +00:00
func TestMigrationDownShouldReturnErrOnTargetLessThan1 ( t * testing . T ) {
2021-11-23 09:45:38 +00:00
assert . EqualError ( t ,
2022-10-20 02:16:36 +00:00
schemaMigrateChecks ( providerSQLite , false , - 4 , LatestVersion ) ,
2021-11-23 09:45:38 +00:00
fmt . Sprintf ( ErrFmtMigrateDownTargetLessThanMinimum , - 4 ) )
assert . EqualError ( t ,
2022-10-20 02:16:36 +00:00
schemaMigrateChecks ( providerMySQL , false , - 2 , LatestVersion ) ,
2021-11-23 09:45:38 +00:00
fmt . Sprintf ( ErrFmtMigrateDownTargetLessThanMinimum , - 2 ) )
assert . EqualError ( t ,
2022-10-20 02:16:36 +00:00
schemaMigrateChecks ( providerPostgres , false , - 2 , LatestVersion ) ,
2021-11-23 09:45:38 +00:00
fmt . Sprintf ( ErrFmtMigrateDownTargetLessThanMinimum , - 2 ) )
2022-11-25 12:44:55 +00:00
assert . EqualError ( t ,
schemaMigrateChecks ( providerPostgres , false , - 1 , LatestVersion ) ,
"schema migration down to pre1 is no longer supported: you must use an older version of authelia to perform this migration: you should downgrade to schema version 1 using the current authelia version then use the suggested authelia version to downgrade to pre1: the suggested authelia version is 4.37.2" )
}
func TestMigrationDownShouldReturnErrOnCurrentLessThan0 ( t * testing . T ) {
assert . EqualError ( t ,
schemaMigrateChecks ( providerPostgres , true , LatestVersion , - 1 ) ,
"schema migration up from pre1 is no longer supported: you must use an older version of authelia to perform this migration: the suggested authelia version is 4.37.2" )
2021-11-23 09:45:38 +00:00
}
func TestMigrationDownShouldReturnErrOnTargetVersionGreaterThanCurrent ( t * testing . T ) {
assert . EqualError ( t ,
2022-10-20 02:16:36 +00:00
schemaMigrateChecks ( providerSQLite , false , LatestVersion , 0 ) ,
fmt . Sprintf ( ErrFmtMigrateDownTargetGreaterThanCurrent , LatestVersion , 0 ) )
2021-11-23 09:45:38 +00:00
assert . EqualError ( t ,
2022-10-20 02:16:36 +00:00
schemaMigrateChecks ( providerMySQL , false , LatestVersion , 0 ) ,
fmt . Sprintf ( ErrFmtMigrateDownTargetGreaterThanCurrent , LatestVersion , 0 ) )
2021-11-23 09:45:38 +00:00
assert . EqualError ( t ,
2022-10-20 02:16:36 +00:00
schemaMigrateChecks ( providerPostgres , false , LatestVersion , 0 ) ,
fmt . Sprintf ( ErrFmtMigrateDownTargetGreaterThanCurrent , LatestVersion , 0 ) )
2021-11-23 09:45:38 +00:00
}
func TestShouldReturnErrWhenCurrentIsGreaterThanLatest ( t * testing . T ) {
assert . EqualError ( t ,
schemaMigrateChecks ( providerPostgres , true , SchemaLatest - 4 , SchemaLatest - 5 ) ,
2022-10-20 02:16:36 +00:00
fmt . Sprintf ( errFmtSchemaCurrentGreaterThanLatestKnown , LatestVersion ) )
2021-11-23 09:45:38 +00:00
assert . EqualError ( t ,
schemaMigrateChecks ( providerMySQL , true , SchemaLatest - 4 , SchemaLatest - 5 ) ,
2022-10-20 02:16:36 +00:00
fmt . Sprintf ( errFmtSchemaCurrentGreaterThanLatestKnown , LatestVersion ) )
2021-11-23 09:45:38 +00:00
assert . EqualError ( t ,
schemaMigrateChecks ( providerSQLite , true , SchemaLatest - 4 , SchemaLatest - 5 ) ,
2022-10-20 02:16:36 +00:00
fmt . Sprintf ( errFmtSchemaCurrentGreaterThanLatestKnown , LatestVersion ) )
2021-11-23 09:45:38 +00:00
}
func TestSchemaVersionToString ( t * testing . T ) {
assert . Equal ( t , "unknown" , SchemaVersionToString ( - 2 ) )
assert . Equal ( t , "pre1" , SchemaVersionToString ( - 1 ) )
assert . Equal ( t , "N/A" , SchemaVersionToString ( 0 ) )
assert . Equal ( t , "1" , SchemaVersionToString ( 1 ) )
assert . Equal ( t , "2" , SchemaVersionToString ( 2 ) )
}