tests: correctly reset app config in tests

This commit is contained in:
Elias Schneider
2024-10-26 00:15:31 +02:00
parent 0b0a6781ff
commit 3350398abc
7 changed files with 79 additions and 44 deletions

View File

@@ -33,5 +33,10 @@ func (tc *TestController) resetAndSeedHandler(c *gin.Context) {
return return
} }
if err := tc.TestService.ResetAppConfig(); err != nil {
utils.ControllerError(c, err)
return
}
c.Status(http.StatusNoContent) c.Status(http.StatusNoContent)
} }

View File

@@ -1,11 +1,12 @@
package model package model
type AppConfigVariable struct { type AppConfigVariable struct {
Key string `gorm:"primaryKey;not null"` Key string `gorm:"primaryKey;not null"`
Type string Type string
IsPublic bool IsPublic bool
IsInternal bool IsInternal bool
Value string Value string
DefaultValue string
} }
type AppConfig struct { type AppConfig struct {

View File

@@ -31,43 +31,43 @@ func NewAppConfigService(db *gorm.DB) *AppConfigService {
var defaultDbConfig = model.AppConfig{ var defaultDbConfig = model.AppConfig{
AppName: model.AppConfigVariable{ AppName: model.AppConfigVariable{
Key: "appName", Key: "appName",
Type: "string", Type: "string",
IsPublic: true, IsPublic: true,
Value: "Pocket ID", DefaultValue: "Pocket ID",
}, },
SessionDuration: model.AppConfigVariable{ SessionDuration: model.AppConfigVariable{
Key: "sessionDuration", Key: "sessionDuration",
Type: "number", Type: "number",
Value: "60", DefaultValue: "60",
}, },
EmailsVerified: model.AppConfigVariable{ EmailsVerified: model.AppConfigVariable{
Key: "emailsVerified", Key: "emailsVerified",
Type: "bool", Type: "bool",
Value: "false", DefaultValue: "false",
}, },
BackgroundImageType: model.AppConfigVariable{ BackgroundImageType: model.AppConfigVariable{
Key: "backgroundImageType", Key: "backgroundImageType",
Type: "string", Type: "string",
IsInternal: true, IsInternal: true,
Value: "jpg", DefaultValue: "jpg",
}, },
LogoLightImageType: model.AppConfigVariable{ LogoLightImageType: model.AppConfigVariable{
Key: "logoLightImageType", Key: "logoLightImageType",
Type: "string", Type: "string",
IsInternal: true, IsInternal: true,
Value: "svg", DefaultValue: "svg",
}, },
LogoDarkImageType: model.AppConfigVariable{ LogoDarkImageType: model.AppConfigVariable{
Key: "logoDarkImageType", Key: "logoDarkImageType",
Type: "string", Type: "string",
IsInternal: true, IsInternal: true,
Value: "svg", DefaultValue: "svg",
}, },
EmailEnabled: model.AppConfigVariable{ EmailEnabled: model.AppConfigVariable{
Key: "emailEnabled", Key: "emailEnabled",
Type: "bool", Type: "bool",
Value: "false", DefaultValue: "false",
}, },
SmtpHost: model.AppConfigVariable{ SmtpHost: model.AppConfigVariable{
Key: "smtpHost", Key: "smtpHost",
@@ -120,7 +120,7 @@ func (s *AppConfigService) UpdateAppConfig(input dto.AppConfigUpdateDto) ([]mode
tx.Commit() tx.Commit()
if err := s.loadDbConfigFromDb(); err != nil { if err := s.LoadDbConfigFromDb(); err != nil {
return nil, err return nil, err
} }
@@ -134,7 +134,7 @@ func (s *AppConfigService) UpdateImageType(imageName string, fileType string) er
return err return err
} }
return s.loadDbConfigFromDb() return s.LoadDbConfigFromDb()
} }
func (s *AppConfigService) ListAppConfig(showAll bool) ([]model.AppConfigVariable, error) { func (s *AppConfigService) ListAppConfig(showAll bool) ([]model.AppConfigVariable, error) {
@@ -151,6 +151,13 @@ func (s *AppConfigService) ListAppConfig(showAll bool) ([]model.AppConfigVariabl
return nil, err return nil, err
} }
// Set the value to the default value if it is empty
for i := range configuration {
if configuration[i].Value == "" && configuration[i].DefaultValue != "" {
configuration[i].Value = configuration[i].DefaultValue
}
}
return configuration, nil return configuration, nil
} }
@@ -206,10 +213,11 @@ func (s *AppConfigService) InitDbConfig() error {
} }
// Update existing configuration if it differs from the default // Update existing configuration if it differs from the default
if storedConfigVar.Type != defaultConfigVar.Type || storedConfigVar.IsPublic != defaultConfigVar.IsPublic || storedConfigVar.IsInternal != defaultConfigVar.IsInternal { if storedConfigVar.Type != defaultConfigVar.Type || storedConfigVar.IsPublic != defaultConfigVar.IsPublic || storedConfigVar.IsInternal != defaultConfigVar.IsInternal || storedConfigVar.DefaultValue != defaultConfigVar.DefaultValue {
storedConfigVar.Type = defaultConfigVar.Type storedConfigVar.Type = defaultConfigVar.Type
storedConfigVar.IsPublic = defaultConfigVar.IsPublic storedConfigVar.IsPublic = defaultConfigVar.IsPublic
storedConfigVar.IsInternal = defaultConfigVar.IsInternal storedConfigVar.IsInternal = defaultConfigVar.IsInternal
storedConfigVar.DefaultValue = defaultConfigVar.DefaultValue
if err := s.db.Save(&storedConfigVar).Error; err != nil { if err := s.db.Save(&storedConfigVar).Error; err != nil {
return err return err
} }
@@ -229,10 +237,11 @@ func (s *AppConfigService) InitDbConfig() error {
} }
} }
} }
return s.loadDbConfigFromDb() return s.LoadDbConfigFromDb()
} }
func (s *AppConfigService) loadDbConfigFromDb() error { // LoadDbConfigFromDb loads the configuration values from the database into the DbConfig struct.
func (s *AppConfigService) LoadDbConfigFromDb() error {
dbConfigReflectValue := reflect.ValueOf(s.DbConfig).Elem() dbConfigReflectValue := reflect.ValueOf(s.DbConfig).Elem()
for i := 0; i < dbConfigReflectValue.NumField(); i++ { for i := 0; i < dbConfigReflectValue.NumField(); i++ {
@@ -243,6 +252,10 @@ func (s *AppConfigService) loadDbConfigFromDb() error {
return err return err
} }
if storedConfigVar.Value == "" && storedConfigVar.DefaultValue != "" {
storedConfigVar.Value = storedConfigVar.DefaultValue
}
dbConfigField.Set(reflect.ValueOf(storedConfigVar)) dbConfigField.Set(reflect.ValueOf(storedConfigVar))
} }

View File

@@ -138,8 +138,8 @@ func (s *TestService) SeedDatabase() error {
return err return err
} }
publicKey1, err := getCborPublicKey("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEwcOo5KV169KR67QEHrcYkeXE3CCxv2BgwnSq4VYTQxyLtdmKxegexa8JdwFKhKXa2BMI9xaN15BoL6wSCRFJhg==") publicKey1, err := s.getCborPublicKey("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEwcOo5KV169KR67QEHrcYkeXE3CCxv2BgwnSq4VYTQxyLtdmKxegexa8JdwFKhKXa2BMI9xaN15BoL6wSCRFJhg==")
publicKey2, err := getCborPublicKey("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAESq/wR8QbBu3dKnpaw/v0mDxFFDwnJ/L5XHSg2tAmq5x1BpSMmIr3+DxCbybVvGRmWGh8kKhy7SMnK91M6rFHTA==") publicKey2, err := s.getCborPublicKey("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAESq/wR8QbBu3dKnpaw/v0mDxFFDwnJ/L5XHSg2tAmq5x1BpSMmIr3+DxCbybVvGRmWGh8kKhy7SMnK91M6rFHTA==")
if err != nil { if err != nil {
return err return err
} }
@@ -187,17 +187,16 @@ func (s *TestService) ResetDatabase() error {
return err return err
} }
// Delete all rows from all tables
for _, table := range tables { for _, table := range tables {
if err := tx.Exec("DELETE FROM " + table).Error; err != nil { if err := tx.Exec("DELETE FROM " + table).Error; err != nil {
return err return err
} }
} }
return nil return nil
}) })
if err != nil {
return err
}
err = s.appConfigService.InitDbConfig()
return err return err
} }
@@ -215,8 +214,23 @@ func (s *TestService) ResetApplicationImages() error {
return nil return nil
} }
func (s *TestService) ResetAppConfig() error {
// Reseed the config variables
if err := s.appConfigService.InitDbConfig(); err != nil {
return err
}
// Reset all app config variables to their default values
if err := s.db.Session(&gorm.Session{AllowGlobalUpdate: true}).Model(&model.AppConfigVariable{}).Update("value", "").Error; err != nil {
return err
}
// Reload the app config from the database after resetting the values
return s.appConfigService.LoadDbConfigFromDb()
}
// getCborPublicKey decodes a Base64 encoded public key and returns the CBOR encoded COSE key // getCborPublicKey decodes a Base64 encoded public key and returns the CBOR encoded COSE key
func getCborPublicKey(base64PublicKey string) ([]byte, error) { func (s *TestService) getCborPublicKey(base64PublicKey string) ([]byte, error) {
decodedKey, err := base64.StdEncoding.DecodeString(base64PublicKey) decodedKey, err := base64.StdEncoding.DecodeString(base64PublicKey)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to decode base64 key: %w", err) return nil, fmt.Errorf("failed to decode base64 key: %w", err)

View File

@@ -0,0 +1 @@
ALTER TABLE app_config_variables DROP COLUMN default_value;

View File

@@ -0,0 +1 @@
ALTER TABLE app_config_variables ADD COLUMN default_value TEXT;

View File

@@ -73,8 +73,8 @@ export default class AppConfigService extends APIService {
return true; return true;
} else if (value === 'false') { } else if (value === 'false') {
return false; return false;
} else if (!isNaN(Number(value))) { } else if (!isNaN(parseFloat(value))) {
return Number(value); return parseFloat(value);
} else { } else {
return value; return value;
} }