Skip to content

Commit

Permalink
tests: correctly reset app config in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stonith404 committed Oct 25, 2024
1 parent 0b0a678 commit 3350398
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 44 deletions.
5 changes: 5 additions & 0 deletions backend/internal/controller/test_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,10 @@ func (tc *TestController) resetAndSeedHandler(c *gin.Context) {
return
}

if err := tc.TestService.ResetAppConfig(); err != nil {
utils.ControllerError(c, err)
return
}

c.Status(http.StatusNoContent)
}
11 changes: 6 additions & 5 deletions backend/internal/model/app_config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package model

type AppConfigVariable struct {
Key string `gorm:"primaryKey;not null"`
Type string
IsPublic bool
IsInternal bool
Value string
Key string `gorm:"primaryKey;not null"`
Type string
IsPublic bool
IsInternal bool
Value string
DefaultValue string
}

type AppConfig struct {
Expand Down
73 changes: 43 additions & 30 deletions backend/internal/service/app_config_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,43 @@ func NewAppConfigService(db *gorm.DB) *AppConfigService {

var defaultDbConfig = model.AppConfig{
AppName: model.AppConfigVariable{
Key: "appName",
Type: "string",
IsPublic: true,
Value: "Pocket ID",
Key: "appName",
Type: "string",
IsPublic: true,
DefaultValue: "Pocket ID",
},
SessionDuration: model.AppConfigVariable{
Key: "sessionDuration",
Type: "number",
Value: "60",
Key: "sessionDuration",
Type: "number",
DefaultValue: "60",
},
EmailsVerified: model.AppConfigVariable{
Key: "emailsVerified",
Type: "bool",
Value: "false",
Key: "emailsVerified",
Type: "bool",
DefaultValue: "false",
},
BackgroundImageType: model.AppConfigVariable{
Key: "backgroundImageType",
Type: "string",
IsInternal: true,
Value: "jpg",
Key: "backgroundImageType",
Type: "string",
IsInternal: true,
DefaultValue: "jpg",
},
LogoLightImageType: model.AppConfigVariable{
Key: "logoLightImageType",
Type: "string",
IsInternal: true,
Value: "svg",
Key: "logoLightImageType",
Type: "string",
IsInternal: true,
DefaultValue: "svg",
},
LogoDarkImageType: model.AppConfigVariable{
Key: "logoDarkImageType",
Type: "string",
IsInternal: true,
Value: "svg",
Key: "logoDarkImageType",
Type: "string",
IsInternal: true,
DefaultValue: "svg",
},
EmailEnabled: model.AppConfigVariable{
Key: "emailEnabled",
Type: "bool",
Value: "false",
Key: "emailEnabled",
Type: "bool",
DefaultValue: "false",
},
SmtpHost: model.AppConfigVariable{
Key: "smtpHost",
Expand Down Expand Up @@ -120,7 +120,7 @@ func (s *AppConfigService) UpdateAppConfig(input dto.AppConfigUpdateDto) ([]mode

tx.Commit()

if err := s.loadDbConfigFromDb(); err != nil {
if err := s.LoadDbConfigFromDb(); err != nil {
return nil, err
}

Expand All @@ -134,7 +134,7 @@ func (s *AppConfigService) UpdateImageType(imageName string, fileType string) er
return err
}

return s.loadDbConfigFromDb()
return s.LoadDbConfigFromDb()
}

func (s *AppConfigService) ListAppConfig(showAll bool) ([]model.AppConfigVariable, error) {
Expand All @@ -151,6 +151,13 @@ func (s *AppConfigService) ListAppConfig(showAll bool) ([]model.AppConfigVariabl
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
}

Expand Down Expand Up @@ -206,10 +213,11 @@ func (s *AppConfigService) InitDbConfig() error {
}

// 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.IsPublic = defaultConfigVar.IsPublic
storedConfigVar.IsInternal = defaultConfigVar.IsInternal
storedConfigVar.DefaultValue = defaultConfigVar.DefaultValue
if err := s.db.Save(&storedConfigVar).Error; err != nil {
return err
}
Expand All @@ -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()

for i := 0; i < dbConfigReflectValue.NumField(); i++ {
Expand All @@ -243,6 +252,10 @@ func (s *AppConfigService) loadDbConfigFromDb() error {
return err
}

if storedConfigVar.Value == "" && storedConfigVar.DefaultValue != "" {
storedConfigVar.Value = storedConfigVar.DefaultValue
}

dbConfigField.Set(reflect.ValueOf(storedConfigVar))
}

Expand Down
28 changes: 21 additions & 7 deletions backend/internal/service/test_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ func (s *TestService) SeedDatabase() error {
return err
}

publicKey1, err := getCborPublicKey("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEwcOo5KV169KR67QEHrcYkeXE3CCxv2BgwnSq4VYTQxyLtdmKxegexa8JdwFKhKXa2BMI9xaN15BoL6wSCRFJhg==")
publicKey2, err := getCborPublicKey("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAESq/wR8QbBu3dKnpaw/v0mDxFFDwnJ/L5XHSg2tAmq5x1BpSMmIr3+DxCbybVvGRmWGh8kKhy7SMnK91M6rFHTA==")
publicKey1, err := s.getCborPublicKey("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEwcOo5KV169KR67QEHrcYkeXE3CCxv2BgwnSq4VYTQxyLtdmKxegexa8JdwFKhKXa2BMI9xaN15BoL6wSCRFJhg==")
publicKey2, err := s.getCborPublicKey("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAESq/wR8QbBu3dKnpaw/v0mDxFFDwnJ/L5XHSg2tAmq5x1BpSMmIr3+DxCbybVvGRmWGh8kKhy7SMnK91M6rFHTA==")
if err != nil {
return err
}
Expand Down Expand Up @@ -187,17 +187,16 @@ func (s *TestService) ResetDatabase() error {
return err
}

// Delete all rows from all tables
for _, table := range tables {
if err := tx.Exec("DELETE FROM " + table).Error; err != nil {
return err
}
}

return nil
})
if err != nil {
return err
}
err = s.appConfigService.InitDbConfig()

return err
}

Expand All @@ -215,8 +214,23 @@ func (s *TestService) ResetApplicationImages() error {
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
func getCborPublicKey(base64PublicKey string) ([]byte, error) {
func (s *TestService) getCborPublicKey(base64PublicKey string) ([]byte, error) {
decodedKey, err := base64.StdEncoding.DecodeString(base64PublicKey)
if err != nil {
return nil, fmt.Errorf("failed to decode base64 key: %w", err)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE app_config_variables DROP COLUMN default_value;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE app_config_variables ADD COLUMN default_value TEXT;
4 changes: 2 additions & 2 deletions frontend/src/lib/services/app-config-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ export default class AppConfigService extends APIService {
return true;
} else if (value === 'false') {
return false;
} else if (!isNaN(Number(value))) {
return Number(value);
} else if (!isNaN(parseFloat(value))) {
return parseFloat(value);
} else {
return value;
}
Expand Down

0 comments on commit 3350398

Please sign in to comment.