github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/api4/role_test.go (about) 1 // Copyright (c) 2018-present Xenia, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package api4 5 6 import ( 7 "strings" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 12 "github.com/xzl8028/xenia-server/model" 13 ) 14 15 func TestGetRole(t *testing.T) { 16 th := Setup().InitBasic() 17 defer th.TearDown() 18 19 role := &model.Role{ 20 Name: model.NewId(), 21 DisplayName: model.NewId(), 22 Description: model.NewId(), 23 Permissions: []string{"manage_system", "create_public_channel"}, 24 SchemeManaged: true, 25 } 26 27 role, err := th.App.Srv.Store.Role().Save(role) 28 assert.Nil(t, err) 29 defer th.App.Srv.Store.Job().Delete(role.Id) 30 31 received, resp := th.Client.GetRole(role.Id) 32 CheckNoError(t, resp) 33 34 assert.Equal(t, received.Id, role.Id) 35 assert.Equal(t, received.Name, role.Name) 36 assert.Equal(t, received.DisplayName, role.DisplayName) 37 assert.Equal(t, received.Description, role.Description) 38 assert.EqualValues(t, received.Permissions, role.Permissions) 39 assert.Equal(t, received.SchemeManaged, role.SchemeManaged) 40 41 _, resp = th.SystemAdminClient.GetRole("1234") 42 CheckBadRequestStatus(t, resp) 43 44 _, resp = th.SystemAdminClient.GetRole(model.NewId()) 45 CheckNotFoundStatus(t, resp) 46 } 47 48 func TestGetRoleByName(t *testing.T) { 49 th := Setup().InitBasic() 50 defer th.TearDown() 51 52 role := &model.Role{ 53 Name: model.NewId(), 54 DisplayName: model.NewId(), 55 Description: model.NewId(), 56 Permissions: []string{"manage_system", "create_public_channel"}, 57 SchemeManaged: true, 58 } 59 60 role, err := th.App.Srv.Store.Role().Save(role) 61 assert.Nil(t, err) 62 defer th.App.Srv.Store.Job().Delete(role.Id) 63 64 received, resp := th.Client.GetRoleByName(role.Name) 65 CheckNoError(t, resp) 66 67 assert.Equal(t, received.Id, role.Id) 68 assert.Equal(t, received.Name, role.Name) 69 assert.Equal(t, received.DisplayName, role.DisplayName) 70 assert.Equal(t, received.Description, role.Description) 71 assert.EqualValues(t, received.Permissions, role.Permissions) 72 assert.Equal(t, received.SchemeManaged, role.SchemeManaged) 73 74 _, resp = th.SystemAdminClient.GetRoleByName(strings.Repeat("abcdefghij", 10)) 75 CheckBadRequestStatus(t, resp) 76 77 _, resp = th.SystemAdminClient.GetRoleByName(model.NewId()) 78 CheckNotFoundStatus(t, resp) 79 } 80 81 func TestGetRolesByNames(t *testing.T) { 82 th := Setup().InitBasic() 83 defer th.TearDown() 84 85 role1 := &model.Role{ 86 Name: model.NewId(), 87 DisplayName: model.NewId(), 88 Description: model.NewId(), 89 Permissions: []string{"manage_system", "create_public_channel"}, 90 SchemeManaged: true, 91 } 92 role2 := &model.Role{ 93 Name: model.NewId(), 94 DisplayName: model.NewId(), 95 Description: model.NewId(), 96 Permissions: []string{"manage_system", "delete_private_channel"}, 97 SchemeManaged: true, 98 } 99 role3 := &model.Role{ 100 Name: model.NewId(), 101 DisplayName: model.NewId(), 102 Description: model.NewId(), 103 Permissions: []string{"manage_system", "manage_public_channel_properties"}, 104 SchemeManaged: true, 105 } 106 107 role1, err := th.App.Srv.Store.Role().Save(role1) 108 assert.Nil(t, err) 109 defer th.App.Srv.Store.Job().Delete(role1.Id) 110 111 role2, err = th.App.Srv.Store.Role().Save(role2) 112 assert.Nil(t, err) 113 defer th.App.Srv.Store.Job().Delete(role2.Id) 114 115 role3, err = th.App.Srv.Store.Role().Save(role3) 116 assert.Nil(t, err) 117 defer th.App.Srv.Store.Job().Delete(role3.Id) 118 119 // Check all three roles can be found. 120 received, resp := th.Client.GetRolesByNames([]string{role1.Name, role2.Name, role3.Name}) 121 CheckNoError(t, resp) 122 123 assert.Contains(t, received, role1) 124 assert.Contains(t, received, role2) 125 assert.Contains(t, received, role3) 126 127 // Check a list of non-existent roles. 128 _, resp = th.Client.GetRolesByNames([]string{model.NewId(), model.NewId()}) 129 CheckNoError(t, resp) 130 131 // Empty list should error. 132 _, resp = th.SystemAdminClient.GetRolesByNames([]string{}) 133 CheckBadRequestStatus(t, resp) 134 135 // Invalid role name should error. 136 _, resp = th.Client.GetRolesByNames([]string{model.NewId(), model.NewId(), "!!!!!!"}) 137 CheckBadRequestStatus(t, resp) 138 139 // Empty/whitespace rolenames should be ignored. 140 _, resp = th.Client.GetRolesByNames([]string{model.NewId(), model.NewId(), "", " "}) 141 CheckNoError(t, resp) 142 } 143 144 func TestPatchRole(t *testing.T) { 145 th := Setup().InitBasic() 146 defer th.TearDown() 147 148 role := &model.Role{ 149 Name: model.NewId(), 150 DisplayName: model.NewId(), 151 Description: model.NewId(), 152 Permissions: []string{"manage_system", "create_public_channel", "manage_slash_commands"}, 153 SchemeManaged: true, 154 } 155 156 role, err := th.App.Srv.Store.Role().Save(role) 157 assert.Nil(t, err) 158 defer th.App.Srv.Store.Job().Delete(role.Id) 159 160 patch := &model.RolePatch{ 161 Permissions: &[]string{"manage_system", "create_public_channel", "manage_incoming_webhooks", "manage_outgoing_webhooks"}, 162 } 163 164 received, resp := th.SystemAdminClient.PatchRole(role.Id, patch) 165 CheckNoError(t, resp) 166 167 assert.Equal(t, received.Id, role.Id) 168 assert.Equal(t, received.Name, role.Name) 169 assert.Equal(t, received.DisplayName, role.DisplayName) 170 assert.Equal(t, received.Description, role.Description) 171 assert.EqualValues(t, received.Permissions, []string{"manage_system", "create_public_channel", "manage_incoming_webhooks", "manage_outgoing_webhooks"}) 172 assert.Equal(t, received.SchemeManaged, role.SchemeManaged) 173 174 // Check a no-op patch succeeds. 175 _, resp = th.SystemAdminClient.PatchRole(role.Id, patch) 176 CheckNoError(t, resp) 177 178 _, resp = th.SystemAdminClient.PatchRole("junk", patch) 179 CheckBadRequestStatus(t, resp) 180 181 _, resp = th.Client.PatchRole(model.NewId(), patch) 182 CheckNotFoundStatus(t, resp) 183 184 _, resp = th.Client.PatchRole(role.Id, patch) 185 CheckForbiddenStatus(t, resp) 186 187 // Check a change that the license would not allow. 188 patch = &model.RolePatch{ 189 Permissions: &[]string{"manage_system", "manage_incoming_webhooks", "manage_outgoing_webhooks"}, 190 } 191 192 _, resp = th.SystemAdminClient.PatchRole(role.Id, patch) 193 CheckNotImplementedStatus(t, resp) 194 195 // Add a license. 196 th.App.SetLicense(model.NewTestLicense()) 197 198 // Try again, should succeed 199 received, resp = th.SystemAdminClient.PatchRole(role.Id, patch) 200 CheckNoError(t, resp) 201 202 assert.Equal(t, received.Id, role.Id) 203 assert.Equal(t, received.Name, role.Name) 204 assert.Equal(t, received.DisplayName, role.DisplayName) 205 assert.Equal(t, received.Description, role.Description) 206 assert.EqualValues(t, received.Permissions, []string{"manage_system", "manage_incoming_webhooks", "manage_outgoing_webhooks"}) 207 assert.Equal(t, received.SchemeManaged, role.SchemeManaged) 208 }