github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/config/client_test.go (about)

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