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