github.com/larkox/mattermost-server@v5.11.1+incompatible/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/mattermost/mattermost-server/config" 13 "github.com/mattermost/mattermost-server/model" 14 ) 15 16 func TestGetClientConfig(t *testing.T) { 17 t.Parallel() 18 testCases := []struct { 19 description string 20 config *model.Config 21 diagnosticId 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 144 for _, testCase := range testCases { 145 testCase := testCase 146 t.Run(testCase.description, func(t *testing.T) { 147 t.Parallel() 148 149 testCase.config.SetDefaults() 150 if testCase.license != nil { 151 testCase.license.Features.SetDefaults() 152 } 153 154 configMap := config.GenerateClientConfig(testCase.config, testCase.diagnosticId, testCase.license) 155 for expectedField, expectedValue := range testCase.expectedFields { 156 actualValue, ok := configMap[expectedField] 157 if assert.True(t, ok, fmt.Sprintf("config does not contain %v", expectedField)) { 158 assert.Equal(t, expectedValue, actualValue) 159 } 160 } 161 }) 162 } 163 } 164 165 func TestGetLimitedClientConfig(t *testing.T) { 166 t.Parallel() 167 testCases := []struct { 168 description string 169 config *model.Config 170 diagnosticId string 171 license *model.License 172 expectedFields map[string]string 173 }{ 174 { 175 "unlicensed", 176 &model.Config{ 177 EmailSettings: model.EmailSettings{ 178 EmailNotificationContentsType: sToP(model.EMAIL_NOTIFICATION_CONTENTS_FULL), 179 }, 180 ThemeSettings: model.ThemeSettings{ 181 // Ignored, since not licensed. 182 AllowCustomThemes: bToP(false), 183 }, 184 ServiceSettings: model.ServiceSettings{ 185 WebsocketURL: sToP("ws://mattermost.example.com:8065"), 186 WebsocketPort: iToP(80), 187 WebsocketSecurePort: iToP(443), 188 }, 189 }, 190 "", 191 nil, 192 map[string]string{ 193 "DiagnosticId": "", 194 "EnforceMultifactorAuthentication": "false", 195 "WebsocketURL": "ws://mattermost.example.com:8065", 196 "WebsocketPort": "80", 197 "WebsocketSecurePort": "443", 198 }, 199 }, 200 } 201 202 for _, testCase := range testCases { 203 testCase := testCase 204 t.Run(testCase.description, func(t *testing.T) { 205 t.Parallel() 206 207 testCase.config.SetDefaults() 208 if testCase.license != nil { 209 testCase.license.Features.SetDefaults() 210 } 211 212 configMap := config.GenerateLimitedClientConfig(testCase.config, testCase.diagnosticId, testCase.license) 213 for expectedField, expectedValue := range testCase.expectedFields { 214 actualValue, ok := configMap[expectedField] 215 if assert.True(t, ok, fmt.Sprintf("config does not contain %v", expectedField)) { 216 assert.Equal(t, expectedValue, actualValue) 217 } 218 } 219 }) 220 } 221 } 222 223 func sToP(s string) *string { 224 return &s 225 } 226 227 func bToP(b bool) *bool { 228 return &b 229 } 230 231 func iToP(i int) *int { 232 return &i 233 }