2019-02-12 13:19:01 -05:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
2019-11-29 06:59:40 -05:00
// See LICENSE.txt for license information.
2019-02-12 13:19:01 -05:00
package config
import (
"testing"
2021-01-07 12:12:43 -05:00
"github.com/stretchr/testify/assert"
2021-05-19 07:30:26 -04:00
"github.com/stretchr/testify/require"
2021-01-07 12:12:43 -05:00
2023-06-11 01:24:35 -04:00
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/v8/channels/utils"
2019-02-12 13:19:01 -05:00
)
func TestDesanitize ( t * testing . T ) {
actual := & model . Config { }
actual . SetDefaults ( )
// These setting should be ignored
2024-08-05 23:45:00 -04:00
actual . LdapSettings . Enable = model . NewPointer ( false )
actual . FileSettings . DriverName = model . NewPointer ( "s3" )
2019-02-12 13:19:01 -05:00
// These settings should be desanitized into target.
2024-08-05 23:45:00 -04:00
actual . LdapSettings . BindPassword = model . NewPointer ( "bind_password" )
actual . FileSettings . PublicLinkSalt = model . NewPointer ( "public_link_salt" )
actual . FileSettings . AmazonS3SecretAccessKey = model . NewPointer ( "amazon_s3_secret_access_key" )
actual . EmailSettings . SMTPPassword = model . NewPointer ( "smtp_password" )
actual . GitLabSettings . Secret = model . NewPointer ( "secret" )
actual . OpenIdSettings . Secret = model . NewPointer ( "secret" )
actual . SqlSettings . DataSource = model . NewPointer ( "data_source" )
actual . SqlSettings . AtRestEncryptKey = model . NewPointer ( "at_rest_encrypt_key" )
actual . ElasticsearchSettings . Password = model . NewPointer ( "password" )
2019-02-12 13:19:01 -05:00
actual . SqlSettings . DataSourceReplicas = append ( actual . SqlSettings . DataSourceReplicas , "replica0" )
actual . SqlSettings . DataSourceReplicas = append ( actual . SqlSettings . DataSourceReplicas , "replica1" )
actual . SqlSettings . DataSourceSearchReplicas = append ( actual . SqlSettings . DataSourceSearchReplicas , "search_replica0" )
actual . SqlSettings . DataSourceSearchReplicas = append ( actual . SqlSettings . DataSourceSearchReplicas , "search_replica1" )
2024-11-11 05:43:26 -05:00
actual . PluginSettings . Plugins = map [ string ] map [ string ] any {
"plugin1" : {
"secret" : "value1" ,
"no_secret" : "value2" ,
} ,
}
2019-02-12 13:19:01 -05:00
target := & model . Config { }
target . SetDefaults ( )
// These setting should be ignored
2024-08-05 23:45:00 -04:00
target . LdapSettings . Enable = model . NewPointer ( true )
target . FileSettings . DriverName = model . NewPointer ( "file" )
2019-02-12 13:19:01 -05:00
// These settings should be updated from actual
2024-08-05 23:45:00 -04:00
target . LdapSettings . BindPassword = model . NewPointer ( model . FakeSetting )
target . FileSettings . PublicLinkSalt = model . NewPointer ( model . FakeSetting )
target . FileSettings . AmazonS3SecretAccessKey = model . NewPointer ( model . FakeSetting )
target . EmailSettings . SMTPPassword = model . NewPointer ( model . FakeSetting )
target . GitLabSettings . Secret = model . NewPointer ( model . FakeSetting )
target . OpenIdSettings . Secret = model . NewPointer ( model . FakeSetting )
target . SqlSettings . DataSource = model . NewPointer ( model . FakeSetting )
target . SqlSettings . AtRestEncryptKey = model . NewPointer ( model . FakeSetting )
target . ElasticsearchSettings . Password = model . NewPointer ( model . FakeSetting )
2021-07-12 14:05:36 -04:00
target . SqlSettings . DataSourceReplicas = [ ] string { model . FakeSetting , model . FakeSetting }
target . SqlSettings . DataSourceSearchReplicas = [ ] string { model . FakeSetting , model . FakeSetting }
2024-11-11 05:43:26 -05:00
target . PluginSettings . Plugins = map [ string ] map [ string ] any {
"plugin1" : {
"secret" : model . FakeSetting ,
"no_secret" : "value2" ,
} ,
}
2019-02-12 13:19:01 -05:00
2020-01-07 11:00:56 -05:00
actualClone := actual . Clone ( )
2019-02-12 13:19:01 -05:00
desanitize ( actual , target )
2020-01-07 11:00:56 -05:00
assert . Equal ( t , actualClone , actual , "actual should not have been changed" )
2019-02-12 13:19:01 -05:00
// Verify the settings that should have been left untouched in target
assert . True ( t , * target . LdapSettings . Enable , "LdapSettings.Enable should not have changed" )
assert . Equal ( t , "file" , * target . FileSettings . DriverName , "FileSettings.DriverName should not have been changed" )
// Verify the settings that should have been desanitized into target
assert . Equal ( t , * actual . LdapSettings . BindPassword , * target . LdapSettings . BindPassword )
assert . Equal ( t , * actual . FileSettings . PublicLinkSalt , * target . FileSettings . PublicLinkSalt )
assert . Equal ( t , * actual . FileSettings . AmazonS3SecretAccessKey , * target . FileSettings . AmazonS3SecretAccessKey )
assert . Equal ( t , * actual . EmailSettings . SMTPPassword , * target . EmailSettings . SMTPPassword )
assert . Equal ( t , * actual . GitLabSettings . Secret , * target . GitLabSettings . Secret )
2020-12-08 21:58:37 -05:00
assert . Equal ( t , * actual . OpenIdSettings . Secret , * target . OpenIdSettings . Secret )
2019-02-12 13:19:01 -05:00
assert . Equal ( t , * actual . SqlSettings . DataSource , * target . SqlSettings . DataSource )
assert . Equal ( t , * actual . SqlSettings . AtRestEncryptKey , * target . SqlSettings . AtRestEncryptKey )
assert . Equal ( t , * actual . ElasticsearchSettings . Password , * target . ElasticsearchSettings . Password )
assert . Equal ( t , actual . SqlSettings . DataSourceReplicas , target . SqlSettings . DataSourceReplicas )
assert . Equal ( t , actual . SqlSettings . DataSourceSearchReplicas , target . SqlSettings . DataSourceSearchReplicas )
2020-10-29 18:54:39 -04:00
assert . Equal ( t , actual . ServiceSettings . SplitKey , target . ServiceSettings . SplitKey )
2024-11-11 05:43:26 -05:00
assert . Equal ( t , actual . PluginSettings . Plugins , target . PluginSettings . Plugins )
2019-02-12 13:19:01 -05:00
}
func TestFixInvalidLocales ( t * testing . T ) {
2024-04-29 05:23:01 -04:00
// utils.TranslationsPreInit errors when TestFixInvalidLocales is run as part of testing the package,
// but doesn't error when the test is run individually.
_ = utils . TranslationsPreInit ( )
2019-02-12 13:19:01 -05:00
cfg := & model . Config { }
cfg . SetDefaults ( )
* cfg . LocalizationSettings . DefaultServerLocale = "en"
* cfg . LocalizationSettings . DefaultClientLocale = "en"
* cfg . LocalizationSettings . AvailableLocales = ""
2024-04-29 05:23:01 -04:00
changed := fixInvalidLocales ( cfg )
2019-02-12 13:19:01 -05:00
assert . False ( t , changed )
* cfg . LocalizationSettings . DefaultServerLocale = "junk"
2024-04-29 05:23:01 -04:00
changed = fixInvalidLocales ( cfg )
2019-02-12 13:19:01 -05:00
assert . True ( t , changed )
assert . Equal ( t , "en" , * cfg . LocalizationSettings . DefaultServerLocale )
* cfg . LocalizationSettings . DefaultServerLocale = ""
2024-04-29 05:23:01 -04:00
changed = fixInvalidLocales ( cfg )
2019-02-12 13:19:01 -05:00
assert . True ( t , changed )
assert . Equal ( t , "en" , * cfg . LocalizationSettings . DefaultServerLocale )
* cfg . LocalizationSettings . AvailableLocales = "en"
* cfg . LocalizationSettings . DefaultServerLocale = "de"
2024-04-29 05:23:01 -04:00
changed = fixInvalidLocales ( cfg )
2019-02-12 13:19:01 -05:00
assert . False ( t , changed )
assert . NotContains ( t , * cfg . LocalizationSettings . AvailableLocales , * cfg . LocalizationSettings . DefaultServerLocale , "DefaultServerLocale should not be added to AvailableLocales" )
* cfg . LocalizationSettings . AvailableLocales = ""
* cfg . LocalizationSettings . DefaultClientLocale = "junk"
2024-04-29 05:23:01 -04:00
changed = fixInvalidLocales ( cfg )
2019-02-12 13:19:01 -05:00
assert . True ( t , changed )
assert . Equal ( t , "en" , * cfg . LocalizationSettings . DefaultClientLocale )
* cfg . LocalizationSettings . DefaultClientLocale = ""
2024-04-29 05:23:01 -04:00
changed = fixInvalidLocales ( cfg )
2019-02-12 13:19:01 -05:00
assert . True ( t , changed )
assert . Equal ( t , "en" , * cfg . LocalizationSettings . DefaultClientLocale )
* cfg . LocalizationSettings . AvailableLocales = "en"
* cfg . LocalizationSettings . DefaultClientLocale = "de"
2024-04-29 05:23:01 -04:00
changed = fixInvalidLocales ( cfg )
2019-02-12 13:19:01 -05:00
assert . True ( t , changed )
assert . Contains ( t , * cfg . LocalizationSettings . AvailableLocales , * cfg . LocalizationSettings . DefaultServerLocale , "DefaultClientLocale should have been added to AvailableLocales" )
// validate AvailableLocales
* cfg . LocalizationSettings . DefaultServerLocale = "en"
* cfg . LocalizationSettings . DefaultClientLocale = "en"
* cfg . LocalizationSettings . AvailableLocales = "junk"
2024-04-29 05:23:01 -04:00
changed = fixInvalidLocales ( cfg )
2019-02-12 13:19:01 -05:00
assert . True ( t , changed )
assert . Equal ( t , "" , * cfg . LocalizationSettings . AvailableLocales )
* cfg . LocalizationSettings . AvailableLocales = "en,de,junk"
2024-04-29 05:23:01 -04:00
changed = fixInvalidLocales ( cfg )
2019-02-12 13:19:01 -05:00
assert . True ( t , changed )
assert . Equal ( t , "" , * cfg . LocalizationSettings . AvailableLocales )
* cfg . LocalizationSettings . DefaultServerLocale = "fr"
* cfg . LocalizationSettings . DefaultClientLocale = "de"
* cfg . LocalizationSettings . AvailableLocales = "en"
2024-04-29 05:23:01 -04:00
changed = fixInvalidLocales ( cfg )
2019-02-12 13:19:01 -05:00
assert . True ( t , changed )
assert . NotContains ( t , * cfg . LocalizationSettings . AvailableLocales , * cfg . LocalizationSettings . DefaultServerLocale , "DefaultServerLocale should not be added to AvailableLocales" )
assert . Contains ( t , * cfg . LocalizationSettings . AvailableLocales , * cfg . LocalizationSettings . DefaultClientLocale , "DefaultClientLocale should have been added to AvailableLocales" )
}
2020-12-16 14:45:17 -05:00
func TestIsDatabaseDSN ( t * testing . T ) {
testCases := [ ] struct {
Name string
DSN string
Expected bool
} {
{
2022-01-12 07:23:56 -05:00
Name : "Postgresql 'postgres' DSN" ,
2021-02-11 15:21:21 -05:00
DSN : "postgres://localhost" ,
2020-12-16 14:45:17 -05:00
Expected : true ,
} ,
2022-01-12 07:23:56 -05:00
{
Name : "Postgresql 'postgresql' DSN" ,
DSN : "postgresql://localhost" ,
Expected : true ,
} ,
2020-12-16 14:45:17 -05:00
{
Name : "Empty DSN" ,
DSN : "" ,
Expected : false ,
} ,
{
Name : "Default file DSN" ,
DSN : "config.json" ,
Expected : false ,
} ,
{
Name : "Relative path DSN" ,
DSN : "configuration/config.json" ,
Expected : false ,
} ,
{
Name : "Absolute path DSN" ,
DSN : "/opt/mattermost/configuration/config.json" ,
Expected : false ,
} ,
}
for _ , tc := range testCases {
t . Run ( tc . Name , func ( t * testing . T ) {
assert . Equal ( t , tc . Expected , IsDatabaseDSN ( tc . DSN ) )
} )
}
}
2021-05-19 07:30:26 -04:00
func TestIsJSONMap ( t * testing . T ) {
2020-07-15 14:40:36 -04:00
tests := [ ] struct {
name string
data string
want bool
} {
{ name : "good json" , data : ` { "local_tcp" : {
"Type" : "tcp" , "Format" : "json" , "Levels" : [
{ "ID" : 5 , "Name" : "debug" , "Stacktrace" : false }
] ,
"Options" : { "ip" : "localhost" , "port" : 18065 } ,
"MaxQueueSize" : 1000 } }
` , want : true ,
} ,
{ name : "empty json" , data : "{}" , want : true } ,
{ name : "string json" , data : ` "test" ` , want : false } ,
{ name : "array json" , data : ` ["test1", "test2"] ` , want : false } ,
{ name : "bad json" , data : ` { huh?} ` , want : false } ,
{ name : "filename" , data : "/tmp/logger.conf" , want : false } ,
{ name : "postgres dsn" , data : "postgres://mmuser:passwordlocalhost:5432/mattermost?sslmode=disable&connect_timeout=10" , want : false } ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
2023-05-15 10:37:48 -04:00
if got := isJSONMap ( [ ] byte ( tt . data ) ) ; got != tt . want {
2021-05-19 07:30:26 -04:00
t . Errorf ( "isJSONMap() = %v, want %v" , got , tt . want )
2020-07-15 14:40:36 -04:00
}
} )
}
}
2021-05-19 07:30:26 -04:00
func TestEqual ( t * testing . T ) {
t . Run ( "nil" , func ( t * testing . T ) {
diff , err := equal ( nil , nil )
require . NoError ( t , err )
require . False ( t , diff )
} )
t . Run ( "no diff" , func ( t * testing . T ) {
old := minimalConfig . Clone ( )
2023-06-13 04:38:36 -04:00
n := minimalConfig . Clone ( )
diff , err := equal ( old , n )
2021-05-19 07:30:26 -04:00
require . NoError ( t , err )
require . False ( t , diff )
} )
t . Run ( "diff" , func ( t * testing . T ) {
old := minimalConfig . Clone ( )
2023-06-13 04:38:36 -04:00
n := minimalConfig . Clone ( )
n . SqlSettings = model . SqlSettings { }
diff , err := equal ( old , n )
2021-05-19 07:30:26 -04:00
require . NoError ( t , err )
require . True ( t , diff )
} )
}