Skip to content

Commit fb9c583

Browse files
authored
[MM-64445] api4/channels_test: Add tests cases for guest user private channels (#31319) (#33827)
Automatic Merge
1 parent ef99a9f commit fb9c583

File tree

2 files changed

+106
-5
lines changed

2 files changed

+106
-5
lines changed

server/channels/api4/channel.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,9 +1831,22 @@ func addChannelMember(c *Context, w http.ResponseWriter, r *http.Request) {
18311831

18321832
// Security check: if the user is a guest, they must have access to the channel
18331833
// to view its members
1834-
if c.AppContext.Session().IsGuest() && !c.App.SessionHasPermissionToChannel(c.AppContext, *c.AppContext.Session(), c.Params.ChannelId, model.PermissionReadChannel) {
1835-
c.SetPermissionError(model.PermissionReadChannel)
1836-
return
1834+
if c.AppContext.Session().IsGuest() {
1835+
if !c.App.SessionHasPermissionToChannel(c.AppContext, *c.AppContext.Session(), c.Params.ChannelId, model.PermissionReadChannel) {
1836+
c.SetPermissionError(model.PermissionReadChannel)
1837+
return
1838+
}
1839+
for _, userId := range userIds {
1840+
allowed, appErr := c.App.UserCanSeeOtherUser(c.AppContext, c.AppContext.Session().UserId, userId)
1841+
if appErr != nil {
1842+
c.Err = appErr
1843+
return
1844+
}
1845+
if !allowed {
1846+
c.SetPermissionError(model.PermissionInviteUser)
1847+
return
1848+
}
1849+
}
18371850
}
18381851

18391852
if channel.Type == model.ChannelTypeDirect || channel.Type == model.ChannelTypeGroup {

server/channels/api4/channel_test.go

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,106 @@ func TestCreateChannel(t *testing.T) {
149149
t.Run("Test create channel with missing team id", func(t *testing.T) {
150150
channel := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.ChannelTypeOpen, TeamId: ""}
151151

152-
_, resp, err := client.CreateChannel(context.Background(), channel)
152+
_, resp, err = client.CreateChannel(context.Background(), channel)
153153
CheckErrorID(t, err, "api.context.invalid_body_param.app_error")
154154
CheckBadRequestStatus(t, resp)
155155
})
156156

157157
t.Run("Test create channel with missing display name", func(t *testing.T) {
158158
channel := &model.Channel{DisplayName: "", Name: GenerateTestChannelName(), Type: model.ChannelTypeOpen, TeamId: team.Id}
159159

160-
_, resp, err := client.CreateChannel(context.Background(), channel)
160+
_, resp, err = client.CreateChannel(context.Background(), channel)
161161
CheckErrorID(t, err, "api.context.invalid_body_param.app_error")
162162
CheckBadRequestStatus(t, resp)
163163
})
164+
165+
t.Run("Guest users", func(t *testing.T) {
166+
th.App.Srv().SetLicense(model.NewTestLicenseSKU(model.LicenseShortSkuEnterprise))
167+
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.Enable = true })
168+
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.AllowEmailAccounts = true })
169+
170+
guestUser := th.CreateUser()
171+
appErr := th.App.VerifyUserEmail(guestUser.Id, guestUser.Email)
172+
require.Nil(t, appErr)
173+
174+
appErr = th.App.DemoteUserToGuest(th.Context, guestUser)
175+
require.Nil(t, appErr)
176+
177+
_, _, appErr = th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, guestUser.Id, "")
178+
require.Nil(t, appErr)
179+
180+
guestClient := th.CreateClient()
181+
_, _, err := guestClient.Login(context.Background(), guestUser.Username, guestUser.Password)
182+
require.NoError(t, err)
183+
t.Cleanup(func() {
184+
_, lErr := guestClient.Logout(context.Background())
185+
require.NoError(t, lErr)
186+
})
187+
188+
userOutsideOfChannels := th.CreateUser()
189+
_, _, err = th.Client.AddTeamMember(context.Background(), team.Id, userOutsideOfChannels.Id)
190+
require.NoError(t, err)
191+
192+
public := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.ChannelTypeOpen, TeamId: team.Id}
193+
private := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.ChannelTypePrivate, TeamId: team.Id}
194+
195+
t.Run("Guest user should not be able to create channels", func(t *testing.T) {
196+
_, resp, err = guestClient.CreateChannel(context.Background(), public)
197+
require.Error(t, err)
198+
CheckForbiddenStatus(t, resp)
199+
200+
private.Name = GenerateTestChannelName()
201+
_, resp, err = guestClient.CreateChannel(context.Background(), private)
202+
require.Error(t, err)
203+
CheckForbiddenStatus(t, resp)
204+
})
205+
206+
t.Run("Guest user should not be able to add channel members if they have no common channels", func(t *testing.T) {
207+
// Now actually create the channels with the main client
208+
public, _, err = th.Client.CreateChannel(context.Background(), public)
209+
require.NoError(t, err)
210+
private, _, err = th.Client.CreateChannel(context.Background(), private)
211+
require.NoError(t, err)
212+
213+
// Add the guest user to the private channel
214+
_, _, err = th.Client.AddChannelMember(context.Background(), private.Id, guestUser.Id)
215+
require.NoError(t, err)
216+
217+
// Verify that the guest user can access the private channel they were added to
218+
_, _, err = guestClient.GetChannel(context.Background(), private.Id, "")
219+
require.NoError(t, err)
220+
221+
// Verify that the guest user cannot add members to the private channel
222+
_, resp, err = guestClient.AddChannelMember(context.Background(), private.Id, userOutsideOfChannels.Id)
223+
require.Error(t, err)
224+
CheckForbiddenStatus(t, resp)
225+
226+
// Add the guest user to the public channel
227+
_, _, err = th.Client.AddChannelMember(context.Background(), public.Id, guestUser.Id)
228+
require.NoError(t, err)
229+
230+
// Verify that the guest user can access the public channel they were added to
231+
_, _, err = guestClient.GetChannel(context.Background(), public.Id, "")
232+
require.NoError(t, err)
233+
234+
// Verify that the guest user cannot add members to the public channel
235+
_, resp, err = guestClient.AddChannelMember(context.Background(), public.Id, userOutsideOfChannels.Id)
236+
require.Error(t, err)
237+
CheckForbiddenStatus(t, resp)
238+
239+
// Update team guest permissions to allow creating private channels
240+
th.AddPermissionToRole(model.PermissionCreatePrivateChannel.Id, model.TeamGuestRoleId)
241+
privateGuest := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.ChannelTypePrivate, TeamId: team.Id}
242+
privateGuest, resp, err = guestClient.CreateChannel(context.Background(), privateGuest)
243+
require.NoError(t, err)
244+
CheckCreatedStatus(t, resp)
245+
246+
// Verify that the guest user can't add users they have no visibility to
247+
_, resp, err = guestClient.AddChannelMember(context.Background(), privateGuest.Id, userOutsideOfChannels.Id)
248+
require.Error(t, err)
249+
CheckForbiddenStatus(t, resp)
250+
})
251+
})
164252
}
165253

166254
func TestUpdateChannel(t *testing.T) {

0 commit comments

Comments
 (0)