scheme_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
  2. // See LICENSE.txt for license information.
  3. package api4
  4. import (
  5. "strings"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. "github.com/mattermost/mattermost-server/v5/model"
  10. )
  11. func TestCreateScheme(t *testing.T) {
  12. th := Setup(t)
  13. defer th.TearDown()
  14. th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
  15. th.App.SetPhase2PermissionsMigrationStatus(true)
  16. // Basic test of creating a team scheme.
  17. scheme1 := &model.Scheme{
  18. DisplayName: model.NewId(),
  19. Name: model.NewId(),
  20. Description: model.NewId(),
  21. Scope: model.SCHEME_SCOPE_TEAM,
  22. }
  23. s1, r1 := th.SystemAdminClient.CreateScheme(scheme1)
  24. CheckNoError(t, r1)
  25. assert.Equal(t, s1.DisplayName, scheme1.DisplayName)
  26. assert.Equal(t, s1.Name, scheme1.Name)
  27. assert.Equal(t, s1.Description, scheme1.Description)
  28. assert.NotZero(t, s1.CreateAt)
  29. assert.Equal(t, s1.CreateAt, s1.UpdateAt)
  30. assert.Zero(t, s1.DeleteAt)
  31. assert.Equal(t, s1.Scope, scheme1.Scope)
  32. assert.NotZero(t, len(s1.DefaultTeamAdminRole))
  33. assert.NotZero(t, len(s1.DefaultTeamUserRole))
  34. assert.NotZero(t, len(s1.DefaultTeamGuestRole))
  35. assert.NotZero(t, len(s1.DefaultChannelAdminRole))
  36. assert.NotZero(t, len(s1.DefaultChannelUserRole))
  37. assert.NotZero(t, len(s1.DefaultChannelGuestRole))
  38. // Check the default roles have been created.
  39. _, roleRes1 := th.SystemAdminClient.GetRoleByName(s1.DefaultTeamAdminRole)
  40. CheckNoError(t, roleRes1)
  41. _, roleRes2 := th.SystemAdminClient.GetRoleByName(s1.DefaultTeamUserRole)
  42. CheckNoError(t, roleRes2)
  43. _, roleRes3 := th.SystemAdminClient.GetRoleByName(s1.DefaultChannelAdminRole)
  44. CheckNoError(t, roleRes3)
  45. _, roleRes4 := th.SystemAdminClient.GetRoleByName(s1.DefaultChannelUserRole)
  46. CheckNoError(t, roleRes4)
  47. _, roleRes5 := th.SystemAdminClient.GetRoleByName(s1.DefaultTeamGuestRole)
  48. CheckNoError(t, roleRes5)
  49. _, roleRes6 := th.SystemAdminClient.GetRoleByName(s1.DefaultChannelGuestRole)
  50. CheckNoError(t, roleRes6)
  51. // Basic Test of a Channel scheme.
  52. scheme2 := &model.Scheme{
  53. DisplayName: model.NewId(),
  54. Name: model.NewId(),
  55. Description: model.NewId(),
  56. Scope: model.SCHEME_SCOPE_CHANNEL,
  57. }
  58. s2, r2 := th.SystemAdminClient.CreateScheme(scheme2)
  59. CheckNoError(t, r2)
  60. assert.Equal(t, s2.DisplayName, scheme2.DisplayName)
  61. assert.Equal(t, s2.Name, scheme2.Name)
  62. assert.Equal(t, s2.Description, scheme2.Description)
  63. assert.NotZero(t, s2.CreateAt)
  64. assert.Equal(t, s2.CreateAt, s2.UpdateAt)
  65. assert.Zero(t, s2.DeleteAt)
  66. assert.Equal(t, s2.Scope, scheme2.Scope)
  67. assert.Zero(t, len(s2.DefaultTeamAdminRole))
  68. assert.Zero(t, len(s2.DefaultTeamUserRole))
  69. assert.Zero(t, len(s2.DefaultTeamGuestRole))
  70. assert.NotZero(t, len(s2.DefaultChannelAdminRole))
  71. assert.NotZero(t, len(s2.DefaultChannelUserRole))
  72. assert.NotZero(t, len(s2.DefaultChannelGuestRole))
  73. // Check the default roles have been created.
  74. _, roleRes7 := th.SystemAdminClient.GetRoleByName(s2.DefaultChannelAdminRole)
  75. CheckNoError(t, roleRes7)
  76. _, roleRes8 := th.SystemAdminClient.GetRoleByName(s2.DefaultChannelUserRole)
  77. CheckNoError(t, roleRes8)
  78. _, roleRes9 := th.SystemAdminClient.GetRoleByName(s2.DefaultChannelGuestRole)
  79. CheckNoError(t, roleRes9)
  80. // Try and create a scheme with an invalid scope.
  81. scheme3 := &model.Scheme{
  82. DisplayName: model.NewId(),
  83. Name: model.NewId(),
  84. Description: model.NewId(),
  85. Scope: model.NewId(),
  86. }
  87. _, r3 := th.SystemAdminClient.CreateScheme(scheme3)
  88. CheckBadRequestStatus(t, r3)
  89. // Try and create a scheme with an invalid display name.
  90. scheme4 := &model.Scheme{
  91. DisplayName: strings.Repeat(model.NewId(), 100),
  92. Name: "Name",
  93. Description: model.NewId(),
  94. Scope: model.NewId(),
  95. }
  96. _, r4 := th.SystemAdminClient.CreateScheme(scheme4)
  97. CheckBadRequestStatus(t, r4)
  98. // Try and create a scheme with an invalid name.
  99. scheme8 := &model.Scheme{
  100. DisplayName: "DisplayName",
  101. Name: strings.Repeat(model.NewId(), 100),
  102. Description: model.NewId(),
  103. Scope: model.NewId(),
  104. }
  105. _, r8 := th.SystemAdminClient.CreateScheme(scheme8)
  106. CheckBadRequestStatus(t, r8)
  107. // Try and create a scheme without the appropriate permissions.
  108. scheme5 := &model.Scheme{
  109. DisplayName: model.NewId(),
  110. Name: model.NewId(),
  111. Description: model.NewId(),
  112. Scope: model.SCHEME_SCOPE_TEAM,
  113. }
  114. _, r5 := th.Client.CreateScheme(scheme5)
  115. CheckForbiddenStatus(t, r5)
  116. // Try and create a scheme without a license.
  117. th.App.Srv().SetLicense(nil)
  118. scheme6 := &model.Scheme{
  119. DisplayName: model.NewId(),
  120. Name: model.NewId(),
  121. Description: model.NewId(),
  122. Scope: model.SCHEME_SCOPE_TEAM,
  123. }
  124. _, r6 := th.SystemAdminClient.CreateScheme(scheme6)
  125. CheckNotImplementedStatus(t, r6)
  126. th.App.SetPhase2PermissionsMigrationStatus(false)
  127. th.LoginSystemAdmin()
  128. th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
  129. scheme7 := &model.Scheme{
  130. DisplayName: model.NewId(),
  131. Name: model.NewId(),
  132. Description: model.NewId(),
  133. Scope: model.SCHEME_SCOPE_TEAM,
  134. }
  135. _, r7 := th.SystemAdminClient.CreateScheme(scheme7)
  136. CheckNotImplementedStatus(t, r7)
  137. }
  138. func TestGetScheme(t *testing.T) {
  139. th := Setup(t).InitBasic()
  140. defer th.TearDown()
  141. th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
  142. // Basic test of creating a team scheme.
  143. scheme1 := &model.Scheme{
  144. DisplayName: model.NewId(),
  145. Name: model.NewId(),
  146. Description: model.NewId(),
  147. Scope: model.SCHEME_SCOPE_TEAM,
  148. }
  149. th.App.SetPhase2PermissionsMigrationStatus(true)
  150. s1, r1 := th.SystemAdminClient.CreateScheme(scheme1)
  151. CheckNoError(t, r1)
  152. assert.Equal(t, s1.DisplayName, scheme1.DisplayName)
  153. assert.Equal(t, s1.Name, scheme1.Name)
  154. assert.Equal(t, s1.Description, scheme1.Description)
  155. assert.NotZero(t, s1.CreateAt)
  156. assert.Equal(t, s1.CreateAt, s1.UpdateAt)
  157. assert.Zero(t, s1.DeleteAt)
  158. assert.Equal(t, s1.Scope, scheme1.Scope)
  159. assert.NotZero(t, len(s1.DefaultTeamAdminRole))
  160. assert.NotZero(t, len(s1.DefaultTeamUserRole))
  161. assert.NotZero(t, len(s1.DefaultTeamGuestRole))
  162. assert.NotZero(t, len(s1.DefaultChannelAdminRole))
  163. assert.NotZero(t, len(s1.DefaultChannelUserRole))
  164. assert.NotZero(t, len(s1.DefaultChannelGuestRole))
  165. s2, r2 := th.SystemAdminClient.GetScheme(s1.Id)
  166. CheckNoError(t, r2)
  167. assert.Equal(t, s1, s2)
  168. _, r3 := th.SystemAdminClient.GetScheme(model.NewId())
  169. CheckNotFoundStatus(t, r3)
  170. _, r4 := th.SystemAdminClient.GetScheme("12345")
  171. CheckBadRequestStatus(t, r4)
  172. th.SystemAdminClient.Logout()
  173. _, r5 := th.SystemAdminClient.GetScheme(s1.Id)
  174. CheckUnauthorizedStatus(t, r5)
  175. th.SystemAdminClient.Login(th.SystemAdminUser.Username, th.SystemAdminUser.Password)
  176. th.App.Srv().SetLicense(nil)
  177. _, r6 := th.SystemAdminClient.GetScheme(s1.Id)
  178. CheckNoError(t, r6)
  179. _, r7 := th.Client.GetScheme(s1.Id)
  180. CheckForbiddenStatus(t, r7)
  181. th.App.SetPhase2PermissionsMigrationStatus(false)
  182. _, r8 := th.SystemAdminClient.GetScheme(s1.Id)
  183. CheckNotImplementedStatus(t, r8)
  184. }
  185. func TestGetSchemes(t *testing.T) {
  186. th := Setup(t).InitBasic()
  187. defer th.TearDown()
  188. th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
  189. scheme1 := &model.Scheme{
  190. DisplayName: model.NewId(),
  191. Name: model.NewId(),
  192. Description: model.NewId(),
  193. Scope: model.SCHEME_SCOPE_TEAM,
  194. }
  195. scheme2 := &model.Scheme{
  196. DisplayName: model.NewId(),
  197. Name: model.NewId(),
  198. Description: model.NewId(),
  199. Scope: model.SCHEME_SCOPE_CHANNEL,
  200. }
  201. th.App.SetPhase2PermissionsMigrationStatus(true)
  202. _, r1 := th.SystemAdminClient.CreateScheme(scheme1)
  203. CheckNoError(t, r1)
  204. _, r2 := th.SystemAdminClient.CreateScheme(scheme2)
  205. CheckNoError(t, r2)
  206. l3, r3 := th.SystemAdminClient.GetSchemes("", 0, 100)
  207. CheckNoError(t, r3)
  208. assert.NotZero(t, len(l3))
  209. l4, r4 := th.SystemAdminClient.GetSchemes("team", 0, 100)
  210. CheckNoError(t, r4)
  211. for _, s := range l4 {
  212. assert.Equal(t, "team", s.Scope)
  213. }
  214. l5, r5 := th.SystemAdminClient.GetSchemes("channel", 0, 100)
  215. CheckNoError(t, r5)
  216. for _, s := range l5 {
  217. assert.Equal(t, "channel", s.Scope)
  218. }
  219. _, r6 := th.SystemAdminClient.GetSchemes("asdf", 0, 100)
  220. CheckBadRequestStatus(t, r6)
  221. th.Client.Logout()
  222. _, r7 := th.Client.GetSchemes("", 0, 100)
  223. CheckUnauthorizedStatus(t, r7)
  224. th.Client.Login(th.BasicUser.Username, th.BasicUser.Password)
  225. _, r8 := th.Client.GetSchemes("", 0, 100)
  226. CheckForbiddenStatus(t, r8)
  227. th.App.SetPhase2PermissionsMigrationStatus(false)
  228. _, r9 := th.SystemAdminClient.GetSchemes("", 0, 100)
  229. CheckNotImplementedStatus(t, r9)
  230. }
  231. func TestGetTeamsForScheme(t *testing.T) {
  232. th := Setup(t).InitBasic()
  233. defer th.TearDown()
  234. th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
  235. th.App.SetPhase2PermissionsMigrationStatus(true)
  236. scheme1 := &model.Scheme{
  237. DisplayName: model.NewId(),
  238. Name: model.NewId(),
  239. Description: model.NewId(),
  240. Scope: model.SCHEME_SCOPE_TEAM,
  241. }
  242. scheme1, r1 := th.SystemAdminClient.CreateScheme(scheme1)
  243. CheckNoError(t, r1)
  244. team1 := &model.Team{
  245. Name: GenerateTestUsername(),
  246. DisplayName: "A Test Team",
  247. Type: model.TEAM_OPEN,
  248. }
  249. team1, err := th.App.Srv().Store.Team().Save(team1)
  250. require.Nil(t, err)
  251. l2, r2 := th.SystemAdminClient.GetTeamsForScheme(scheme1.Id, 0, 100)
  252. CheckNoError(t, r2)
  253. assert.Zero(t, len(l2))
  254. team1.SchemeId = &scheme1.Id
  255. team1, err = th.App.Srv().Store.Team().Update(team1)
  256. assert.Nil(t, err)
  257. l3, r3 := th.SystemAdminClient.GetTeamsForScheme(scheme1.Id, 0, 100)
  258. CheckNoError(t, r3)
  259. assert.Len(t, l3, 1)
  260. assert.Equal(t, team1.Id, l3[0].Id)
  261. team2 := &model.Team{
  262. Name: GenerateTestUsername(),
  263. DisplayName: "B Test Team",
  264. Type: model.TEAM_OPEN,
  265. SchemeId: &scheme1.Id,
  266. }
  267. team2, err = th.App.Srv().Store.Team().Save(team2)
  268. require.Nil(t, err)
  269. l4, r4 := th.SystemAdminClient.GetTeamsForScheme(scheme1.Id, 0, 100)
  270. CheckNoError(t, r4)
  271. assert.Len(t, l4, 2)
  272. assert.Equal(t, team1.Id, l4[0].Id)
  273. assert.Equal(t, team2.Id, l4[1].Id)
  274. l5, r5 := th.SystemAdminClient.GetTeamsForScheme(scheme1.Id, 1, 1)
  275. CheckNoError(t, r5)
  276. assert.Len(t, l5, 1)
  277. assert.Equal(t, team2.Id, l5[0].Id)
  278. // Check various error cases.
  279. _, ri1 := th.SystemAdminClient.GetTeamsForScheme(model.NewId(), 0, 100)
  280. CheckNotFoundStatus(t, ri1)
  281. _, ri2 := th.SystemAdminClient.GetTeamsForScheme("", 0, 100)
  282. CheckBadRequestStatus(t, ri2)
  283. th.Client.Logout()
  284. _, ri3 := th.Client.GetTeamsForScheme(model.NewId(), 0, 100)
  285. CheckUnauthorizedStatus(t, ri3)
  286. th.Client.Login(th.BasicUser.Username, th.BasicUser.Password)
  287. _, ri4 := th.Client.GetTeamsForScheme(model.NewId(), 0, 100)
  288. CheckForbiddenStatus(t, ri4)
  289. scheme2 := &model.Scheme{
  290. DisplayName: model.NewId(),
  291. Name: model.NewId(),
  292. Description: model.NewId(),
  293. Scope: model.SCHEME_SCOPE_CHANNEL,
  294. }
  295. scheme2, rs2 := th.SystemAdminClient.CreateScheme(scheme2)
  296. CheckNoError(t, rs2)
  297. _, ri5 := th.SystemAdminClient.GetTeamsForScheme(scheme2.Id, 0, 100)
  298. CheckBadRequestStatus(t, ri5)
  299. th.App.SetPhase2PermissionsMigrationStatus(false)
  300. _, ri6 := th.SystemAdminClient.GetTeamsForScheme(scheme1.Id, 0, 100)
  301. CheckNotImplementedStatus(t, ri6)
  302. }
  303. func TestGetChannelsForScheme(t *testing.T) {
  304. th := Setup(t).InitBasic()
  305. defer th.TearDown()
  306. th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
  307. th.App.SetPhase2PermissionsMigrationStatus(true)
  308. scheme1 := &model.Scheme{
  309. DisplayName: model.NewId(),
  310. Name: model.NewId(),
  311. Description: model.NewId(),
  312. Scope: model.SCHEME_SCOPE_CHANNEL,
  313. }
  314. scheme1, r1 := th.SystemAdminClient.CreateScheme(scheme1)
  315. CheckNoError(t, r1)
  316. channel1 := &model.Channel{
  317. TeamId: model.NewId(),
  318. DisplayName: "A Name",
  319. Name: model.NewId(),
  320. Type: model.CHANNEL_OPEN,
  321. }
  322. channel1, errCh := th.App.Srv().Store.Channel().Save(channel1, 1000000)
  323. assert.Nil(t, errCh)
  324. l2, r2 := th.SystemAdminClient.GetChannelsForScheme(scheme1.Id, 0, 100)
  325. CheckNoError(t, r2)
  326. assert.Zero(t, len(l2))
  327. channel1.SchemeId = &scheme1.Id
  328. channel1, err := th.App.Srv().Store.Channel().Update(channel1)
  329. assert.Nil(t, err)
  330. l3, r3 := th.SystemAdminClient.GetChannelsForScheme(scheme1.Id, 0, 100)
  331. CheckNoError(t, r3)
  332. assert.Len(t, l3, 1)
  333. assert.Equal(t, channel1.Id, l3[0].Id)
  334. channel2 := &model.Channel{
  335. TeamId: model.NewId(),
  336. DisplayName: "B Name",
  337. Name: model.NewId(),
  338. Type: model.CHANNEL_OPEN,
  339. SchemeId: &scheme1.Id,
  340. }
  341. channel2, nErr := th.App.Srv().Store.Channel().Save(channel2, 1000000)
  342. assert.Nil(t, nErr)
  343. l4, r4 := th.SystemAdminClient.GetChannelsForScheme(scheme1.Id, 0, 100)
  344. CheckNoError(t, r4)
  345. assert.Len(t, l4, 2)
  346. assert.Equal(t, channel1.Id, l4[0].Id)
  347. assert.Equal(t, channel2.Id, l4[1].Id)
  348. l5, r5 := th.SystemAdminClient.GetChannelsForScheme(scheme1.Id, 1, 1)
  349. CheckNoError(t, r5)
  350. assert.Len(t, l5, 1)
  351. assert.Equal(t, channel2.Id, l5[0].Id)
  352. // Check various error cases.
  353. _, ri1 := th.SystemAdminClient.GetChannelsForScheme(model.NewId(), 0, 100)
  354. CheckNotFoundStatus(t, ri1)
  355. _, ri2 := th.SystemAdminClient.GetChannelsForScheme("", 0, 100)
  356. CheckBadRequestStatus(t, ri2)
  357. th.Client.Logout()
  358. _, ri3 := th.Client.GetChannelsForScheme(model.NewId(), 0, 100)
  359. CheckUnauthorizedStatus(t, ri3)
  360. th.Client.Login(th.BasicUser.Username, th.BasicUser.Password)
  361. _, ri4 := th.Client.GetChannelsForScheme(model.NewId(), 0, 100)
  362. CheckForbiddenStatus(t, ri4)
  363. scheme2 := &model.Scheme{
  364. DisplayName: model.NewId(),
  365. Name: model.NewId(),
  366. Description: model.NewId(),
  367. Scope: model.SCHEME_SCOPE_TEAM,
  368. }
  369. scheme2, rs2 := th.SystemAdminClient.CreateScheme(scheme2)
  370. CheckNoError(t, rs2)
  371. _, ri5 := th.SystemAdminClient.GetChannelsForScheme(scheme2.Id, 0, 100)
  372. CheckBadRequestStatus(t, ri5)
  373. th.App.SetPhase2PermissionsMigrationStatus(false)
  374. _, ri6 := th.SystemAdminClient.GetChannelsForScheme(scheme1.Id, 0, 100)
  375. CheckNotImplementedStatus(t, ri6)
  376. }
  377. func TestPatchScheme(t *testing.T) {
  378. th := Setup(t)
  379. defer th.TearDown()
  380. th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
  381. th.App.SetPhase2PermissionsMigrationStatus(true)
  382. // Basic test of creating a team scheme.
  383. scheme1 := &model.Scheme{
  384. DisplayName: model.NewId(),
  385. Name: model.NewId(),
  386. Description: model.NewId(),
  387. Scope: model.SCHEME_SCOPE_TEAM,
  388. }
  389. s1, r1 := th.SystemAdminClient.CreateScheme(scheme1)
  390. CheckNoError(t, r1)
  391. assert.Equal(t, s1.DisplayName, scheme1.DisplayName)
  392. assert.Equal(t, s1.Name, scheme1.Name)
  393. assert.Equal(t, s1.Description, scheme1.Description)
  394. assert.NotZero(t, s1.CreateAt)
  395. assert.Equal(t, s1.CreateAt, s1.UpdateAt)
  396. assert.Zero(t, s1.DeleteAt)
  397. assert.Equal(t, s1.Scope, scheme1.Scope)
  398. assert.NotZero(t, len(s1.DefaultTeamAdminRole))
  399. assert.NotZero(t, len(s1.DefaultTeamUserRole))
  400. assert.NotZero(t, len(s1.DefaultTeamGuestRole))
  401. assert.NotZero(t, len(s1.DefaultChannelAdminRole))
  402. assert.NotZero(t, len(s1.DefaultChannelUserRole))
  403. assert.NotZero(t, len(s1.DefaultChannelGuestRole))
  404. s2, r2 := th.SystemAdminClient.GetScheme(s1.Id)
  405. CheckNoError(t, r2)
  406. assert.Equal(t, s1, s2)
  407. // Test with a valid patch.
  408. schemePatch := &model.SchemePatch{
  409. DisplayName: new(string),
  410. Name: new(string),
  411. Description: new(string),
  412. }
  413. *schemePatch.DisplayName = model.NewId()
  414. *schemePatch.Name = model.NewId()
  415. *schemePatch.Description = model.NewId()
  416. s3, r3 := th.SystemAdminClient.PatchScheme(s2.Id, schemePatch)
  417. CheckNoError(t, r3)
  418. assert.Equal(t, s3.Id, s2.Id)
  419. assert.Equal(t, s3.DisplayName, *schemePatch.DisplayName)
  420. assert.Equal(t, s3.Name, *schemePatch.Name)
  421. assert.Equal(t, s3.Description, *schemePatch.Description)
  422. s4, r4 := th.SystemAdminClient.GetScheme(s3.Id)
  423. CheckNoError(t, r4)
  424. assert.Equal(t, s3, s4)
  425. // Test with a partial patch.
  426. *schemePatch.Name = model.NewId()
  427. *schemePatch.DisplayName = model.NewId()
  428. schemePatch.Description = nil
  429. s5, r5 := th.SystemAdminClient.PatchScheme(s4.Id, schemePatch)
  430. CheckNoError(t, r5)
  431. assert.Equal(t, s5.Id, s4.Id)
  432. assert.Equal(t, s5.DisplayName, *schemePatch.DisplayName)
  433. assert.Equal(t, s5.Name, *schemePatch.Name)
  434. assert.Equal(t, s5.Description, s4.Description)
  435. s6, r6 := th.SystemAdminClient.GetScheme(s5.Id)
  436. CheckNoError(t, r6)
  437. assert.Equal(t, s5, s6)
  438. // Test with invalid patch.
  439. *schemePatch.Name = strings.Repeat(model.NewId(), 20)
  440. _, r7 := th.SystemAdminClient.PatchScheme(s6.Id, schemePatch)
  441. CheckBadRequestStatus(t, r7)
  442. // Test with unknown ID.
  443. *schemePatch.Name = model.NewId()
  444. _, r8 := th.SystemAdminClient.PatchScheme(model.NewId(), schemePatch)
  445. CheckNotFoundStatus(t, r8)
  446. // Test with invalid ID.
  447. _, r9 := th.SystemAdminClient.PatchScheme("12345", schemePatch)
  448. CheckBadRequestStatus(t, r9)
  449. // Test without required permissions.
  450. _, r10 := th.Client.PatchScheme(s6.Id, schemePatch)
  451. CheckForbiddenStatus(t, r10)
  452. // Test without license.
  453. th.App.Srv().SetLicense(nil)
  454. _, r11 := th.SystemAdminClient.PatchScheme(s6.Id, schemePatch)
  455. CheckNotImplementedStatus(t, r11)
  456. th.App.SetPhase2PermissionsMigrationStatus(false)
  457. th.LoginSystemAdmin()
  458. th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
  459. _, r12 := th.SystemAdminClient.PatchScheme(s6.Id, schemePatch)
  460. CheckNotImplementedStatus(t, r12)
  461. }
  462. func TestDeleteScheme(t *testing.T) {
  463. th := Setup(t)
  464. defer th.TearDown()
  465. t.Run("ValidTeamScheme", func(t *testing.T) {
  466. th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
  467. th.App.SetPhase2PermissionsMigrationStatus(true)
  468. // Create a team scheme.
  469. scheme1 := &model.Scheme{
  470. DisplayName: model.NewId(),
  471. Name: model.NewId(),
  472. Description: model.NewId(),
  473. Scope: model.SCHEME_SCOPE_TEAM,
  474. }
  475. s1, r1 := th.SystemAdminClient.CreateScheme(scheme1)
  476. CheckNoError(t, r1)
  477. // Retrieve the roles and check they are not deleted.
  478. role1, roleRes1 := th.SystemAdminClient.GetRoleByName(s1.DefaultTeamAdminRole)
  479. CheckNoError(t, roleRes1)
  480. role2, roleRes2 := th.SystemAdminClient.GetRoleByName(s1.DefaultTeamUserRole)
  481. CheckNoError(t, roleRes2)
  482. role3, roleRes3 := th.SystemAdminClient.GetRoleByName(s1.DefaultChannelAdminRole)
  483. CheckNoError(t, roleRes3)
  484. role4, roleRes4 := th.SystemAdminClient.GetRoleByName(s1.DefaultChannelUserRole)
  485. CheckNoError(t, roleRes4)
  486. role5, roleRes5 := th.SystemAdminClient.GetRoleByName(s1.DefaultTeamGuestRole)
  487. CheckNoError(t, roleRes5)
  488. role6, roleRes6 := th.SystemAdminClient.GetRoleByName(s1.DefaultChannelGuestRole)
  489. CheckNoError(t, roleRes6)
  490. assert.Zero(t, role1.DeleteAt)
  491. assert.Zero(t, role2.DeleteAt)
  492. assert.Zero(t, role3.DeleteAt)
  493. assert.Zero(t, role4.DeleteAt)
  494. assert.Zero(t, role5.DeleteAt)
  495. assert.Zero(t, role6.DeleteAt)
  496. // Make sure this scheme is in use by a team.
  497. team, err := th.App.Srv().Store.Team().Save(&model.Team{
  498. Name: "zz" + model.NewId(),
  499. DisplayName: model.NewId(),
  500. Email: model.NewId() + "@nowhere.com",
  501. Type: model.TEAM_OPEN,
  502. SchemeId: &s1.Id,
  503. })
  504. require.Nil(t, err)
  505. // Delete the Scheme.
  506. _, r3 := th.SystemAdminClient.DeleteScheme(s1.Id)
  507. CheckNoError(t, r3)
  508. // Check the roles were deleted.
  509. role1, roleRes1 = th.SystemAdminClient.GetRoleByName(s1.DefaultTeamAdminRole)
  510. CheckNoError(t, roleRes1)
  511. role2, roleRes2 = th.SystemAdminClient.GetRoleByName(s1.DefaultTeamUserRole)
  512. CheckNoError(t, roleRes2)
  513. role3, roleRes3 = th.SystemAdminClient.GetRoleByName(s1.DefaultChannelAdminRole)
  514. CheckNoError(t, roleRes3)
  515. role4, roleRes4 = th.SystemAdminClient.GetRoleByName(s1.DefaultChannelUserRole)
  516. CheckNoError(t, roleRes4)
  517. role5, roleRes5 = th.SystemAdminClient.GetRoleByName(s1.DefaultTeamGuestRole)
  518. CheckNoError(t, roleRes5)
  519. role6, roleRes6 = th.SystemAdminClient.GetRoleByName(s1.DefaultChannelGuestRole)
  520. CheckNoError(t, roleRes6)
  521. assert.NotZero(t, role1.DeleteAt)
  522. assert.NotZero(t, role2.DeleteAt)
  523. assert.NotZero(t, role3.DeleteAt)
  524. assert.NotZero(t, role4.DeleteAt)
  525. assert.NotZero(t, role5.DeleteAt)
  526. assert.NotZero(t, role6.DeleteAt)
  527. // Check the team now uses the default scheme
  528. c2, resp := th.SystemAdminClient.GetTeam(team.Id, "")
  529. CheckNoError(t, resp)
  530. assert.Equal(t, "", *c2.SchemeId)
  531. })
  532. t.Run("ValidChannelScheme", func(t *testing.T) {
  533. th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
  534. th.App.SetPhase2PermissionsMigrationStatus(true)
  535. // Create a channel scheme.
  536. scheme1 := &model.Scheme{
  537. DisplayName: model.NewId(),
  538. Name: model.NewId(),
  539. Description: model.NewId(),
  540. Scope: model.SCHEME_SCOPE_CHANNEL,
  541. }
  542. s1, r1 := th.SystemAdminClient.CreateScheme(scheme1)
  543. CheckNoError(t, r1)
  544. // Retrieve the roles and check they are not deleted.
  545. role3, roleRes3 := th.SystemAdminClient.GetRoleByName(s1.DefaultChannelAdminRole)
  546. CheckNoError(t, roleRes3)
  547. role4, roleRes4 := th.SystemAdminClient.GetRoleByName(s1.DefaultChannelUserRole)
  548. CheckNoError(t, roleRes4)
  549. role6, roleRes6 := th.SystemAdminClient.GetRoleByName(s1.DefaultChannelGuestRole)
  550. CheckNoError(t, roleRes6)
  551. assert.Zero(t, role3.DeleteAt)
  552. assert.Zero(t, role4.DeleteAt)
  553. assert.Zero(t, role6.DeleteAt)
  554. // Make sure this scheme is in use by a team.
  555. channel, err := th.App.Srv().Store.Channel().Save(&model.Channel{
  556. TeamId: model.NewId(),
  557. DisplayName: model.NewId(),
  558. Name: model.NewId(),
  559. Type: model.CHANNEL_OPEN,
  560. SchemeId: &s1.Id,
  561. }, -1)
  562. assert.Nil(t, err)
  563. // Delete the Scheme.
  564. _, r3 := th.SystemAdminClient.DeleteScheme(s1.Id)
  565. CheckNoError(t, r3)
  566. // Check the roles were deleted.
  567. role3, roleRes3 = th.SystemAdminClient.GetRoleByName(s1.DefaultChannelAdminRole)
  568. CheckNoError(t, roleRes3)
  569. role4, roleRes4 = th.SystemAdminClient.GetRoleByName(s1.DefaultChannelUserRole)
  570. CheckNoError(t, roleRes4)
  571. role6, roleRes6 = th.SystemAdminClient.GetRoleByName(s1.DefaultChannelGuestRole)
  572. CheckNoError(t, roleRes6)
  573. assert.NotZero(t, role3.DeleteAt)
  574. assert.NotZero(t, role4.DeleteAt)
  575. assert.NotZero(t, role6.DeleteAt)
  576. // Check the channel now uses the default scheme
  577. c2, resp := th.SystemAdminClient.GetChannelByName(channel.Name, channel.TeamId, "")
  578. CheckNoError(t, resp)
  579. assert.Equal(t, "", *c2.SchemeId)
  580. })
  581. t.Run("FailureCases", func(t *testing.T) {
  582. th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
  583. th.App.SetPhase2PermissionsMigrationStatus(true)
  584. scheme1 := &model.Scheme{
  585. DisplayName: model.NewId(),
  586. Name: model.NewId(),
  587. Description: model.NewId(),
  588. Scope: model.SCHEME_SCOPE_CHANNEL,
  589. }
  590. s1, r1 := th.SystemAdminClient.CreateScheme(scheme1)
  591. CheckNoError(t, r1)
  592. // Test with unknown ID.
  593. _, r2 := th.SystemAdminClient.DeleteScheme(model.NewId())
  594. CheckNotFoundStatus(t, r2)
  595. // Test with invalid ID.
  596. _, r3 := th.SystemAdminClient.DeleteScheme("12345")
  597. CheckBadRequestStatus(t, r3)
  598. // Test without required permissions.
  599. _, r4 := th.Client.DeleteScheme(s1.Id)
  600. CheckForbiddenStatus(t, r4)
  601. // Test without license.
  602. th.App.Srv().SetLicense(nil)
  603. _, r5 := th.SystemAdminClient.DeleteScheme(s1.Id)
  604. CheckNotImplementedStatus(t, r5)
  605. th.App.SetPhase2PermissionsMigrationStatus(false)
  606. th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
  607. _, r6 := th.SystemAdminClient.DeleteScheme(s1.Id)
  608. CheckNotImplementedStatus(t, r6)
  609. })
  610. }