1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package api4
import (
"net/http"
"strconv"
"testing"
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/utils"
)
func TestCreateChannel(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
Client := th.Client
team := th.BasicTeam
channel := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_OPEN, TeamId: team.Id}
private := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_PRIVATE, TeamId: team.Id}
rchannel, resp := Client.CreateChannel(channel)
CheckNoError(t, resp)
if rchannel.Name != channel.Name {
t.Fatal("names did not match")
}
if rchannel.DisplayName != channel.DisplayName {
t.Fatal("display names did not match")
}
if rchannel.TeamId != channel.TeamId {
t.Fatal("team ids did not match")
}
rprivate, resp := Client.CreateChannel(private)
CheckNoError(t, resp)
if rprivate.Name != private.Name {
t.Fatal("names did not match")
}
if rprivate.Type != model.CHANNEL_PRIVATE {
t.Fatal("wrong channel type")
}
if rprivate.CreatorId != th.BasicUser.Id {
t.Fatal("wrong creator id")
}
_, resp = Client.CreateChannel(channel)
CheckErrorMessage(t, resp, "store.sql_channel.save_channel.exists.app_error")
CheckBadRequestStatus(t, resp)
direct := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_DIRECT, TeamId: team.Id}
_, resp = Client.CreateChannel(direct)
CheckErrorMessage(t, resp, "api.channel.create_channel.direct_channel.app_error")
CheckBadRequestStatus(t, resp)
Client.Logout()
_, resp = Client.CreateChannel(channel)
CheckUnauthorizedStatus(t, resp)
userNotOnTeam := th.CreateUser()
Client.Login(userNotOnTeam.Email, userNotOnTeam.Password)
_, resp = Client.CreateChannel(channel)
CheckForbiddenStatus(t, resp)
_, resp = Client.CreateChannel(private)
CheckForbiddenStatus(t, resp)
th.LoginBasic()
// Check permissions with policy config changes
isLicensed := utils.IsLicensed
license := utils.License
restrictPublicChannel := *utils.Cfg.TeamSettings.RestrictPublicChannelManagement
restrictPrivateChannel := *utils.Cfg.TeamSettings.RestrictPrivateChannelManagement
defer func() {
*utils.Cfg.TeamSettings.RestrictPublicChannelManagement = restrictPublicChannel
*utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = restrictPrivateChannel
utils.IsLicensed = isLicensed
utils.License = license
utils.SetDefaultRolesBasedOnConfig()
}()
*utils.Cfg.TeamSettings.RestrictPublicChannelManagement = model.PERMISSIONS_ALL
*utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = model.PERMISSIONS_ALL
utils.SetDefaultRolesBasedOnConfig()
utils.IsLicensed = true
utils.License = &model.License{Features: &model.Features{}}
utils.License.Features.SetDefaults()
channel.Name = GenerateTestChannelName()
_, resp = Client.CreateChannel(channel)
CheckNoError(t, resp)
private.Name = GenerateTestChannelName()
_, resp = Client.CreateChannel(private)
CheckNoError(t, resp)
*utils.Cfg.TeamSettings.RestrictPublicChannelCreation = model.PERMISSIONS_TEAM_ADMIN
*utils.Cfg.TeamSettings.RestrictPrivateChannelCreation = model.PERMISSIONS_TEAM_ADMIN
utils.SetDefaultRolesBasedOnConfig()
_, resp = Client.CreateChannel(channel)
CheckForbiddenStatus(t, resp)
_, resp = Client.CreateChannel(private)
CheckForbiddenStatus(t, resp)
th.LoginTeamAdmin()
channel.Name = GenerateTestChannelName()
_, resp = Client.CreateChannel(channel)
CheckNoError(t, resp)
private.Name = GenerateTestChannelName()
_, resp = Client.CreateChannel(private)
CheckNoError(t, resp)
channel.Name = GenerateTestChannelName()
_, resp = th.SystemAdminClient.CreateChannel(channel)
CheckNoError(t, resp)
private.Name = GenerateTestChannelName()
_, resp = th.SystemAdminClient.CreateChannel(private)
CheckNoError(t, resp)
*utils.Cfg.TeamSettings.RestrictPublicChannelCreation = model.PERMISSIONS_SYSTEM_ADMIN
*utils.Cfg.TeamSettings.RestrictPrivateChannelCreation = model.PERMISSIONS_SYSTEM_ADMIN
utils.SetDefaultRolesBasedOnConfig()
th.LoginBasic()
_, resp = Client.CreateChannel(channel)
CheckForbiddenStatus(t, resp)
_, resp = Client.CreateChannel(private)
CheckForbiddenStatus(t, resp)
th.LoginTeamAdmin()
_, resp = Client.CreateChannel(channel)
CheckForbiddenStatus(t, resp)
_, resp = Client.CreateChannel(private)
CheckForbiddenStatus(t, resp)
channel.Name = GenerateTestChannelName()
_, resp = th.SystemAdminClient.CreateChannel(channel)
CheckNoError(t, resp)
private.Name = GenerateTestChannelName()
_, resp = th.SystemAdminClient.CreateChannel(private)
CheckNoError(t, resp)
if r, err := Client.DoApiPost("/channels", "garbage"); err == nil {
t.Fatal("should have errored")
} else {
if r.StatusCode != http.StatusBadRequest {
t.Log("actual: " + strconv.Itoa(r.StatusCode))
t.Log("expected: " + strconv.Itoa(http.StatusBadRequest))
t.Fatal("wrong status code")
}
}
}
|