client_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
  2. // See LICENSE.txt for license information.
  3. package config_test
  4. import (
  5. "fmt"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/mattermost/mattermost-server/v5/config"
  9. "github.com/mattermost/mattermost-server/v5/model"
  10. )
  11. func TestGetClientConfig(t *testing.T) {
  12. t.Parallel()
  13. testCases := []struct {
  14. description string
  15. config *model.Config
  16. diagnosticID string
  17. license *model.License
  18. expectedFields map[string]string
  19. }{
  20. {
  21. "unlicensed",
  22. &model.Config{
  23. EmailSettings: model.EmailSettings{
  24. EmailNotificationContentsType: sToP(model.EMAIL_NOTIFICATION_CONTENTS_FULL),
  25. },
  26. ThemeSettings: model.ThemeSettings{
  27. // Ignored, since not licensed.
  28. AllowCustomThemes: bToP(false),
  29. },
  30. ServiceSettings: model.ServiceSettings{
  31. WebsocketURL: sToP("ws://mattermost.example.com:8065"),
  32. WebsocketPort: iToP(80),
  33. WebsocketSecurePort: iToP(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: sToP(model.EMAIL_NOTIFICATION_CONTENTS_FULL),
  53. },
  54. ThemeSettings: model.ThemeSettings{
  55. // Ignored, since not licensed.
  56. AllowCustomThemes: bToP(false),
  57. },
  58. },
  59. "tag1",
  60. &model.License{
  61. Features: &model.Features{
  62. ThemeManagement: bToP(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: sToP(model.EMAIL_NOTIFICATION_CONTENTS_FULL),
  76. },
  77. ThemeSettings: model.ThemeSettings{
  78. AllowCustomThemes: bToP(false),
  79. },
  80. },
  81. "tag2",
  82. &model.License{
  83. Features: &model.Features{
  84. ThemeManagement: bToP(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: bToP(true),
  98. },
  99. },
  100. "tag1",
  101. &model.License{
  102. Features: &model.Features{
  103. MFA: bToP(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: bToP(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: bToP(false),
  128. ExperimentalGroupUnreadChannels: sToP(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: sToP(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: sToP("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: bToP(true),
  168. },
  169. },
  170. "tag1",
  171. nil,
  172. map[string]string{
  173. "ShowFullName": "true",
  174. },
  175. },
  176. }
  177. for _, testCase := range testCases {
  178. testCase := testCase
  179. t.Run(testCase.description, func(t *testing.T) {
  180. t.Parallel()
  181. testCase.config.SetDefaults()
  182. if testCase.license != nil {
  183. testCase.license.Features.SetDefaults()
  184. }
  185. configMap := config.GenerateClientConfig(testCase.config, testCase.diagnosticID, testCase.license)
  186. for expectedField, expectedValue := range testCase.expectedFields {
  187. actualValue, ok := configMap[expectedField]
  188. if assert.True(t, ok, fmt.Sprintf("config does not contain %v", expectedField)) {
  189. assert.Equal(t, expectedValue, actualValue)
  190. }
  191. }
  192. })
  193. }
  194. }
  195. func TestGetLimitedClientConfig(t *testing.T) {
  196. t.Parallel()
  197. testCases := []struct {
  198. description string
  199. config *model.Config
  200. diagnosticID string
  201. license *model.License
  202. expectedFields map[string]string
  203. }{
  204. {
  205. "unlicensed",
  206. &model.Config{
  207. EmailSettings: model.EmailSettings{
  208. EmailNotificationContentsType: sToP(model.EMAIL_NOTIFICATION_CONTENTS_FULL),
  209. },
  210. ThemeSettings: model.ThemeSettings{
  211. // Ignored, since not licensed.
  212. AllowCustomThemes: bToP(false),
  213. },
  214. ServiceSettings: model.ServiceSettings{
  215. WebsocketURL: sToP("ws://mattermost.example.com:8065"),
  216. WebsocketPort: iToP(80),
  217. WebsocketSecurePort: iToP(443),
  218. },
  219. },
  220. "",
  221. nil,
  222. map[string]string{
  223. "DiagnosticId": "",
  224. "EnforceMultifactorAuthentication": "false",
  225. "WebsocketURL": "ws://mattermost.example.com:8065",
  226. "WebsocketPort": "80",
  227. "WebsocketSecurePort": "443",
  228. },
  229. },
  230. {
  231. "password settings",
  232. &model.Config{
  233. PasswordSettings: model.PasswordSettings{
  234. MinimumLength: iToP(15),
  235. Lowercase: bToP(true),
  236. Uppercase: bToP(true),
  237. Number: bToP(true),
  238. Symbol: bToP(false),
  239. },
  240. },
  241. "",
  242. nil,
  243. map[string]string{
  244. "PasswordMinimumLength": "15",
  245. "PasswordRequireLowercase": "true",
  246. "PasswordRequireUppercase": "true",
  247. "PasswordRequireNumber": "true",
  248. "PasswordRequireSymbol": "false",
  249. },
  250. },
  251. }
  252. for _, testCase := range testCases {
  253. testCase := testCase
  254. t.Run(testCase.description, func(t *testing.T) {
  255. t.Parallel()
  256. testCase.config.SetDefaults()
  257. if testCase.license != nil {
  258. testCase.license.Features.SetDefaults()
  259. }
  260. configMap := config.GenerateLimitedClientConfig(testCase.config, testCase.diagnosticID, testCase.license)
  261. for expectedField, expectedValue := range testCase.expectedFields {
  262. actualValue, ok := configMap[expectedField]
  263. if assert.True(t, ok, fmt.Sprintf("config does not contain %v", expectedField)) {
  264. assert.Equal(t, expectedValue, actualValue)
  265. }
  266. }
  267. })
  268. }
  269. }
  270. func sToP(s string) *string {
  271. return &s
  272. }
  273. func bToP(b bool) *bool {
  274. return &b
  275. }
  276. func iToP(i int) *int {
  277. return &i
  278. }