github.com/mattermost/mattermost-server/server/v8@v8.0.0-20230610055354-a6d1d38b273d/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/server/public/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.EmailNotificationContentsFull),
    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.EmailNotificationContentsFull),
    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.EmailNotificationContentsFull),
    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  			"default marketplace",
   116  			&model.Config{
   117  				PluginSettings: model.PluginSettings{
   118  					MarketplaceURL: model.NewString(model.PluginSettingsDefaultMarketplaceURL),
   119  				},
   120  			},
   121  			"tag1",
   122  			nil,
   123  			map[string]string{
   124  				"IsDefaultMarketplace": "true",
   125  			},
   126  		},
   127  		{
   128  			"non-default marketplace",
   129  			&model.Config{
   130  				PluginSettings: model.PluginSettings{
   131  					MarketplaceURL: model.NewString("http://example.com"),
   132  				},
   133  			},
   134  			"tag1",
   135  			nil,
   136  			map[string]string{
   137  				"IsDefaultMarketplace": "false",
   138  			},
   139  		},
   140  		{
   141  			"enable ShowFullName prop",
   142  			&model.Config{
   143  				PrivacySettings: model.PrivacySettings{
   144  					ShowFullName: model.NewBool(true),
   145  				},
   146  			},
   147  			"tag1",
   148  			nil,
   149  			map[string]string{
   150  				"ShowFullName": "true",
   151  			},
   152  		},
   153  		{
   154  			"Insights professional license",
   155  			&model.Config{
   156  				FeatureFlags: &model.FeatureFlags{
   157  					InsightsEnabled: true,
   158  				},
   159  			},
   160  			"",
   161  			&model.License{
   162  				Features:     &model.Features{},
   163  				SkuShortName: model.LicenseShortSkuProfessional,
   164  			},
   165  			map[string]string{
   166  				"InsightsEnabled": "true",
   167  			},
   168  		},
   169  		{
   170  			"Insights enterprise license",
   171  			&model.Config{
   172  				FeatureFlags: &model.FeatureFlags{
   173  					InsightsEnabled: true,
   174  				},
   175  			},
   176  			"",
   177  			&model.License{
   178  				Features:     &model.Features{},
   179  				SkuShortName: model.LicenseShortSkuEnterprise,
   180  			},
   181  			map[string]string{
   182  				"InsightsEnabled": "true",
   183  			},
   184  		},
   185  		{
   186  			"Insights other license",
   187  			&model.Config{
   188  				FeatureFlags: &model.FeatureFlags{
   189  					InsightsEnabled: true,
   190  				},
   191  			},
   192  			"",
   193  			&model.License{
   194  				Features:     &model.Features{},
   195  				SkuShortName: "other",
   196  			},
   197  			map[string]string{
   198  				"InsightsEnabled": "true",
   199  			},
   200  		},
   201  		{
   202  			"Insights professional license, feature flag disabled",
   203  			&model.Config{
   204  				FeatureFlags: &model.FeatureFlags{
   205  					InsightsEnabled: false,
   206  				},
   207  			},
   208  			"",
   209  			&model.License{
   210  				Features:     &model.Features{},
   211  				SkuShortName: model.LicenseShortSkuProfessional,
   212  			},
   213  			map[string]string{
   214  				"InsightsEnabled": "false",
   215  			},
   216  		},
   217  		{
   218  			"Custom groups professional license",
   219  			&model.Config{},
   220  			"",
   221  			&model.License{
   222  				Features:     &model.Features{},
   223  				SkuShortName: model.LicenseShortSkuProfessional,
   224  			},
   225  			map[string]string{
   226  				"EnableCustomGroups": "true",
   227  			},
   228  		},
   229  		{
   230  			"Custom groups enterprise license",
   231  			&model.Config{},
   232  			"",
   233  			&model.License{
   234  				Features:     &model.Features{},
   235  				SkuShortName: model.LicenseShortSkuEnterprise,
   236  			},
   237  			map[string]string{
   238  				"EnableCustomGroups": "true",
   239  			},
   240  		},
   241  		{
   242  			"Custom groups other license",
   243  			&model.Config{
   244  				FeatureFlags: &model.FeatureFlags{
   245  					InsightsEnabled: true,
   246  				},
   247  			},
   248  			"",
   249  			&model.License{
   250  				Features:     &model.Features{},
   251  				SkuShortName: "other",
   252  			},
   253  			map[string]string{
   254  				"EnableCustomGroups": "false",
   255  			},
   256  		},
   257  		{
   258  			"Shared channels other license",
   259  			&model.Config{
   260  				ExperimentalSettings: model.ExperimentalSettings{
   261  					EnableSharedChannels: model.NewBool(true),
   262  				},
   263  			},
   264  			"",
   265  			&model.License{
   266  				Features: &model.Features{
   267  					SharedChannels: model.NewBool(false),
   268  				},
   269  				SkuShortName: "other",
   270  			},
   271  			map[string]string{
   272  				"ExperimentalSharedChannels": "false",
   273  			},
   274  		},
   275  		{
   276  			"licensed for shared channels",
   277  			&model.Config{
   278  				ExperimentalSettings: model.ExperimentalSettings{
   279  					EnableSharedChannels: model.NewBool(true),
   280  				},
   281  			},
   282  			"",
   283  			&model.License{
   284  				Features: &model.Features{
   285  					SharedChannels: model.NewBool(true),
   286  				},
   287  				SkuShortName: "other",
   288  			},
   289  			map[string]string{
   290  				"ExperimentalSharedChannels": "true",
   291  			},
   292  		},
   293  		{
   294  			"Shared channels professional license",
   295  			&model.Config{
   296  				ExperimentalSettings: model.ExperimentalSettings{
   297  					EnableSharedChannels: model.NewBool(true),
   298  				},
   299  			},
   300  			"",
   301  			&model.License{
   302  				Features: &model.Features{
   303  					SharedChannels: model.NewBool(false),
   304  				},
   305  				SkuShortName: model.LicenseShortSkuProfessional,
   306  			},
   307  			map[string]string{
   308  				"ExperimentalSharedChannels": "true",
   309  			},
   310  		},
   311  		{
   312  			"Shared channels enterprise license",
   313  			&model.Config{
   314  				ExperimentalSettings: model.ExperimentalSettings{
   315  					EnableSharedChannels: model.NewBool(true),
   316  				},
   317  			},
   318  			"",
   319  			&model.License{
   320  				Features: &model.Features{
   321  					SharedChannels: model.NewBool(false),
   322  				},
   323  				SkuShortName: model.LicenseShortSkuEnterprise,
   324  			},
   325  			map[string]string{
   326  				"ExperimentalSharedChannels": "true",
   327  			},
   328  		},
   329  		{
   330  			"Default Playbooks Enabled",
   331  			&model.Config{
   332  				ProductSettings: model.ProductSettings{},
   333  			},
   334  			"",
   335  			&model.License{
   336  				Features:     &model.Features{},
   337  				SkuShortName: "other",
   338  			},
   339  			map[string]string{
   340  				"EnablePlaybooks": "true",
   341  			},
   342  		},
   343  	}
   344  
   345  	for _, testCase := range testCases {
   346  		testCase := testCase
   347  		t.Run(testCase.description, func(t *testing.T) {
   348  			t.Parallel()
   349  
   350  			testCase.config.SetDefaults()
   351  			if testCase.license != nil {
   352  				testCase.license.Features.SetDefaults()
   353  			}
   354  
   355  			configMap := GenerateClientConfig(testCase.config, testCase.telemetryID, testCase.license)
   356  			for expectedField, expectedValue := range testCase.expectedFields {
   357  				actualValue, ok := configMap[expectedField]
   358  				if assert.True(t, ok, fmt.Sprintf("config does not contain %v", expectedField)) {
   359  					assert.Equal(t, expectedValue, actualValue)
   360  				}
   361  			}
   362  		})
   363  	}
   364  }
   365  
   366  func TestGetLimitedClientConfig(t *testing.T) {
   367  	t.Parallel()
   368  	testCases := []struct {
   369  		description    string
   370  		config         *model.Config
   371  		telemetryID    string
   372  		license        *model.License
   373  		expectedFields map[string]string
   374  	}{
   375  		{
   376  			"unlicensed",
   377  			&model.Config{
   378  				EmailSettings: model.EmailSettings{
   379  					EmailNotificationContentsType: model.NewString(model.EmailNotificationContentsFull),
   380  				},
   381  				ThemeSettings: model.ThemeSettings{
   382  					// Ignored, since not licensed.
   383  					AllowCustomThemes: model.NewBool(false),
   384  				},
   385  				ServiceSettings: model.ServiceSettings{
   386  					WebsocketURL:        model.NewString("ws://mattermost.example.com:8065"),
   387  					WebsocketPort:       model.NewInt(80),
   388  					WebsocketSecurePort: model.NewInt(443),
   389  				},
   390  			},
   391  			"",
   392  			nil,
   393  			map[string]string{
   394  				"DiagnosticId":                     "",
   395  				"EnforceMultifactorAuthentication": "false",
   396  				"WebsocketURL":                     "ws://mattermost.example.com:8065",
   397  				"WebsocketPort":                    "80",
   398  				"WebsocketSecurePort":              "443",
   399  			},
   400  		},
   401  		{
   402  			"password settings",
   403  			&model.Config{
   404  				PasswordSettings: model.PasswordSettings{
   405  					MinimumLength: model.NewInt(15),
   406  					Lowercase:     model.NewBool(true),
   407  					Uppercase:     model.NewBool(true),
   408  					Number:        model.NewBool(true),
   409  					Symbol:        model.NewBool(false),
   410  				},
   411  			},
   412  			"",
   413  			nil,
   414  			map[string]string{
   415  				"PasswordMinimumLength":    "15",
   416  				"PasswordRequireLowercase": "true",
   417  				"PasswordRequireUppercase": "true",
   418  				"PasswordRequireNumber":    "true",
   419  				"PasswordRequireSymbol":    "false",
   420  			},
   421  		},
   422  		{
   423  			"Feature Flags",
   424  			&model.Config{
   425  				FeatureFlags: &model.FeatureFlags{
   426  					TestFeature: "myvalue",
   427  				},
   428  			},
   429  			"",
   430  			nil,
   431  			map[string]string{
   432  				"FeatureFlagTestFeature": "myvalue",
   433  			},
   434  		},
   435  	}
   436  
   437  	for _, testCase := range testCases {
   438  		testCase := testCase
   439  		t.Run(testCase.description, func(t *testing.T) {
   440  			t.Parallel()
   441  
   442  			testCase.config.SetDefaults()
   443  			if testCase.license != nil {
   444  				testCase.license.Features.SetDefaults()
   445  			}
   446  
   447  			configMap := GenerateLimitedClientConfig(testCase.config, testCase.telemetryID, testCase.license)
   448  			for expectedField, expectedValue := range testCase.expectedFields {
   449  				actualValue, ok := configMap[expectedField]
   450  				if assert.True(t, ok, fmt.Sprintf("config does not contain %v", expectedField)) {
   451  					assert.Equal(t, expectedValue, actualValue)
   452  				}
   453  			}
   454  		})
   455  	}
   456  }