github.com/mattermost/mattermost-server/v5@v5.39.3/config/client_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package config
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	"github.com/mattermost/mattermost-server/v5/model"
    13  )
    14  
    15  func TestGetClientConfig(t *testing.T) {
    16  	t.Parallel()
    17  	testCases := []struct {
    18  		description    string
    19  		config         *model.Config
    20  		telemetryID    string
    21  		license        *model.License
    22  		expectedFields map[string]string
    23  	}{
    24  		{
    25  			"unlicensed",
    26  			&model.Config{
    27  				EmailSettings: model.EmailSettings{
    28  					EmailNotificationContentsType: model.NewString(model.EMAIL_NOTIFICATION_CONTENTS_FULL),
    29  				},
    30  				ThemeSettings: model.ThemeSettings{
    31  					// Ignored, since not licensed.
    32  					AllowCustomThemes: model.NewBool(false),
    33  				},
    34  				ServiceSettings: model.ServiceSettings{
    35  					WebsocketURL:        model.NewString("ws://mattermost.example.com:8065"),
    36  					WebsocketPort:       model.NewInt(80),
    37  					WebsocketSecurePort: model.NewInt(443),
    38  				},
    39  			},
    40  			"",
    41  			nil,
    42  			map[string]string{
    43  				"DiagnosticId":                     "",
    44  				"EmailNotificationContentsType":    "full",
    45  				"AllowCustomThemes":                "true",
    46  				"EnforceMultifactorAuthentication": "false",
    47  				"WebsocketURL":                     "ws://mattermost.example.com:8065",
    48  				"WebsocketPort":                    "80",
    49  				"WebsocketSecurePort":              "443",
    50  			},
    51  		},
    52  		{
    53  			"licensed, but not for theme management",
    54  			&model.Config{
    55  				EmailSettings: model.EmailSettings{
    56  					EmailNotificationContentsType: model.NewString(model.EMAIL_NOTIFICATION_CONTENTS_FULL),
    57  				},
    58  				ThemeSettings: model.ThemeSettings{
    59  					// Ignored, since not licensed.
    60  					AllowCustomThemes: model.NewBool(false),
    61  				},
    62  			},
    63  			"tag1",
    64  			&model.License{
    65  				Features: &model.Features{
    66  					ThemeManagement: model.NewBool(false),
    67  				},
    68  			},
    69  			map[string]string{
    70  				"DiagnosticId":                  "tag1",
    71  				"EmailNotificationContentsType": "full",
    72  				"AllowCustomThemes":             "true",
    73  			},
    74  		},
    75  		{
    76  			"licensed for theme management",
    77  			&model.Config{
    78  				EmailSettings: model.EmailSettings{
    79  					EmailNotificationContentsType: model.NewString(model.EMAIL_NOTIFICATION_CONTENTS_FULL),
    80  				},
    81  				ThemeSettings: model.ThemeSettings{
    82  					AllowCustomThemes: model.NewBool(false),
    83  				},
    84  			},
    85  			"tag2",
    86  			&model.License{
    87  				Features: &model.Features{
    88  					ThemeManagement: model.NewBool(true),
    89  				},
    90  			},
    91  			map[string]string{
    92  				"DiagnosticId":                  "tag2",
    93  				"EmailNotificationContentsType": "full",
    94  				"AllowCustomThemes":             "false",
    95  			},
    96  		},
    97  		{
    98  			"licensed for enforcement",
    99  			&model.Config{
   100  				ServiceSettings: model.ServiceSettings{
   101  					EnforceMultifactorAuthentication: model.NewBool(true),
   102  				},
   103  			},
   104  			"tag1",
   105  			&model.License{
   106  				Features: &model.Features{
   107  					MFA: model.NewBool(true),
   108  				},
   109  			},
   110  			map[string]string{
   111  				"EnforceMultifactorAuthentication": "true",
   112  			},
   113  		},
   114  		{
   115  			"experimental channel organization enabled",
   116  			&model.Config{
   117  				ServiceSettings: model.ServiceSettings{
   118  					ExperimentalChannelOrganization: model.NewBool(true),
   119  				},
   120  			},
   121  			"tag1",
   122  			nil,
   123  			map[string]string{
   124  				"ExperimentalChannelOrganization": "true",
   125  			},
   126  		},
   127  		{
   128  			"experimental channel organization disabled, but experimental group unread channels on",
   129  			&model.Config{
   130  				ServiceSettings: model.ServiceSettings{
   131  					ExperimentalChannelOrganization: model.NewBool(false),
   132  					ExperimentalGroupUnreadChannels: model.NewString(model.GROUP_UNREAD_CHANNELS_DEFAULT_ON),
   133  				},
   134  			},
   135  			"tag1",
   136  			nil,
   137  			map[string]string{
   138  				"ExperimentalChannelOrganization": "true",
   139  			},
   140  		},
   141  		{
   142  			"default marketplace",
   143  			&model.Config{
   144  				PluginSettings: model.PluginSettings{
   145  					MarketplaceUrl: model.NewString(model.PLUGIN_SETTINGS_DEFAULT_MARKETPLACE_URL),
   146  				},
   147  			},
   148  			"tag1",
   149  			nil,
   150  			map[string]string{
   151  				"IsDefaultMarketplace": "true",
   152  			},
   153  		},
   154  		{
   155  			"non-default marketplace",
   156  			&model.Config{
   157  				PluginSettings: model.PluginSettings{
   158  					MarketplaceUrl: model.NewString("http://example.com"),
   159  				},
   160  			},
   161  			"tag1",
   162  			nil,
   163  			map[string]string{
   164  				"IsDefaultMarketplace": "false",
   165  			},
   166  		},
   167  		{
   168  			"enable ShowFullName prop",
   169  			&model.Config{
   170  				PrivacySettings: model.PrivacySettings{
   171  					ShowFullName: model.NewBool(true),
   172  				},
   173  			},
   174  			"tag1",
   175  			nil,
   176  			map[string]string{
   177  				"ShowFullName": "true",
   178  			},
   179  		},
   180  	}
   181  
   182  	for _, testCase := range testCases {
   183  		testCase := testCase
   184  		t.Run(testCase.description, func(t *testing.T) {
   185  			t.Parallel()
   186  
   187  			testCase.config.SetDefaults()
   188  			if testCase.license != nil {
   189  				testCase.license.Features.SetDefaults()
   190  			}
   191  
   192  			configMap := GenerateClientConfig(testCase.config, testCase.telemetryID, testCase.license)
   193  			for expectedField, expectedValue := range testCase.expectedFields {
   194  				actualValue, ok := configMap[expectedField]
   195  				if assert.True(t, ok, fmt.Sprintf("config does not contain %v", expectedField)) {
   196  					assert.Equal(t, expectedValue, actualValue)
   197  				}
   198  			}
   199  		})
   200  	}
   201  }
   202  
   203  func TestGetLimitedClientConfig(t *testing.T) {
   204  	t.Parallel()
   205  	testCases := []struct {
   206  		description    string
   207  		config         *model.Config
   208  		telemetryID    string
   209  		license        *model.License
   210  		expectedFields map[string]string
   211  	}{
   212  		{
   213  			"unlicensed",
   214  			&model.Config{
   215  				EmailSettings: model.EmailSettings{
   216  					EmailNotificationContentsType: model.NewString(model.EMAIL_NOTIFICATION_CONTENTS_FULL),
   217  				},
   218  				ThemeSettings: model.ThemeSettings{
   219  					// Ignored, since not licensed.
   220  					AllowCustomThemes: model.NewBool(false),
   221  				},
   222  				ServiceSettings: model.ServiceSettings{
   223  					WebsocketURL:        model.NewString("ws://mattermost.example.com:8065"),
   224  					WebsocketPort:       model.NewInt(80),
   225  					WebsocketSecurePort: model.NewInt(443),
   226  				},
   227  			},
   228  			"",
   229  			nil,
   230  			map[string]string{
   231  				"DiagnosticId":                     "",
   232  				"EnforceMultifactorAuthentication": "false",
   233  				"WebsocketURL":                     "ws://mattermost.example.com:8065",
   234  				"WebsocketPort":                    "80",
   235  				"WebsocketSecurePort":              "443",
   236  			},
   237  		},
   238  		{
   239  			"password settings",
   240  			&model.Config{
   241  				PasswordSettings: model.PasswordSettings{
   242  					MinimumLength: model.NewInt(15),
   243  					Lowercase:     model.NewBool(true),
   244  					Uppercase:     model.NewBool(true),
   245  					Number:        model.NewBool(true),
   246  					Symbol:        model.NewBool(false),
   247  				},
   248  			},
   249  			"",
   250  			nil,
   251  			map[string]string{
   252  				"PasswordMinimumLength":    "15",
   253  				"PasswordRequireLowercase": "true",
   254  				"PasswordRequireUppercase": "true",
   255  				"PasswordRequireNumber":    "true",
   256  				"PasswordRequireSymbol":    "false",
   257  			},
   258  		},
   259  		{
   260  			"Feature Flags",
   261  			&model.Config{
   262  				FeatureFlags: &model.FeatureFlags{
   263  					TestFeature: "myvalue",
   264  				},
   265  			},
   266  			"",
   267  			nil,
   268  			map[string]string{
   269  				"FeatureFlagTestFeature": "myvalue",
   270  			},
   271  		},
   272  	}
   273  
   274  	for _, testCase := range testCases {
   275  		testCase := testCase
   276  		t.Run(testCase.description, func(t *testing.T) {
   277  			t.Parallel()
   278  
   279  			testCase.config.SetDefaults()
   280  			if testCase.license != nil {
   281  				testCase.license.Features.SetDefaults()
   282  			}
   283  
   284  			configMap := GenerateLimitedClientConfig(testCase.config, testCase.telemetryID, testCase.license)
   285  			for expectedField, expectedValue := range testCase.expectedFields {
   286  				actualValue, ok := configMap[expectedField]
   287  				if assert.True(t, ok, fmt.Sprintf("config does not contain %v", expectedField)) {
   288  					assert.Equal(t, expectedValue, actualValue)
   289  				}
   290  			}
   291  		})
   292  	}
   293  }