Files
mostlymatter/api/channel_test.go
Corey Hulen 2e5617c29b PLT-2057 User as a first class object (#2648)
* Adding TeamMember to system

* Fixing all unit tests on the backend

* Fixing merge conflicts

* Fixing merge conflict

* Adding javascript unit tests

* Adding TeamMember to system

* Fixing all unit tests on the backend

* Fixing merge conflicts

* Fixing merge conflict

* Adding javascript unit tests

* Adding client side unit test

* Cleaning up the clint side tests

* Fixing msg

* Adding more client side unit tests

* Adding more using tests

* Adding last bit of client side unit tests and adding make cmd

* Fixing bad merge

* Fixing libraries

* Updating to new client side API

* Fixing borken unit test

* Fixing unit tests

* ugg...trying to beat gofmt

* ugg...trying to beat gofmt

* Cleaning up remainder of the server side routes

* Adding inital load api

* Increased coverage of webhook unit tests (#2660)

* Adding loading ... to root html

* Fixing bad merge

* Removing explicit content type so superagent will guess corectly (#2685)

* Fixing merge and unit tests

* Adding create team UI

* Fixing signup flows

* Adding LDAP unit tests and enterprise unit test helper (#2702)

* Add the ability to reset MFA from the commandline (#2706)

* Fixing compliance unit tests

* Fixing client side tests

* Adding open server to system console

* Moving websocket connection

* Fixing unit test

* Fixing unit tests

* Fixing unit tests

* Adding nickname and more LDAP unit tests (#2717)

* Adding join open teams

* Cleaning up all TODOs in the code

* Fixing web sockets

* Removing unused webockets file

* PLT-2533 Add the ability to reset a user's MFA from the system console (#2715)

* Add the ability to reset a user's MFA from the system console

* Add client side unit test for adminResetMfa

* Reorganizing authentication to fix LDAP error message (#2723)

* Fixing failing unit test

* Initial upgrade db code

* Adding upgrade script

* Fixing upgrade script after running on core

* Update OAuth and Claim routes to work with user model changes (#2739)

* Fixing perminant deletion. Adding ability to delete all user and the entire database (#2740)

* Fixing team invite ldap login call (#2741)

* Fixing bluebar and some img stuff

* Fix all the different file upload web utils (#2743)

* Fixing invalid session redirect (#2744)

* Redirect on bad channel name (#2746)

* Fixing a bunch of issue and removing dead code

* Patch to fix error message on leave channel (#2747)

* Setting EnableOpenServer to false by default

* Fixing config

* Fixing upgrade

* Fixing reported bugs

* Bug fixes for PLT-2057

* PLT-2563 Redo password recovery to use a database table (#2745)

* Redo password recovery to use a database table

* Update reset password audits

* Split out admin and user reset password APIs to be separate

* Delete password recovery when user is permanently deleted

* Consolidate password resetting into a single function

* Removed private channels as an option for outgoing webhooks (#2752)

* PLT-2577/PLT-2552 Fixes for backstage (#2753)

* Added URL to incoming webhook list

* Fixed client functions for adding/removing integrations

* Disallowed slash commands without trigger words

* Fixed clientside handling of errors on AddCommand page

* Minor auth cleanup (#2758)

* Changed EditPostModal to just close if you save without making any changes (#2759)

* Renamed client -> Client in async_client.jsx and fixed eslint warnings (#2756)

* Fixed url in channel info modal (#2755)

* Fixing reported issues

* Moving to version 3 of the apis

* Fixing command unit tests (#2760)

* Adding team admins

* Fixing DM issue

* Fixing eslint error

* Properly set EditPostModal's originalText state in all cases (#2762)

* Update client config check to assume features is defined if server is licensed (#2772)

* Fixing url link

* Fixing issue with websocket crashing when sending messages to different teams
2016-04-21 22:37:01 -07:00

909 строки
29 KiB
Go

// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package api
import (
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/store"
"github.com/mattermost/platform/utils"
"net/http"
"strings"
"testing"
"time"
)
func TestCreateChannel(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := th.BasicTeam
Client.Must(Client.Logout())
team2 := th.CreateTeam(th.BasicClient)
th.LoginBasic()
th.BasicClient.SetTeamId(team.Id)
channel := model.Channel{DisplayName: "Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
rchannel, err := Client.CreateChannel(&channel)
if err != nil {
t.Fatal(err)
}
if rchannel.Data.(*model.Channel).Name != channel.Name {
t.Fatal("full name didn't match")
}
rget := Client.Must(Client.GetChannels("")).Data.(*model.ChannelList)
nameMatch := false
for _, c := range rget.Channels {
if c.Name == channel.Name {
nameMatch = true
}
}
if !nameMatch {
t.Fatal("Did not create channel with correct name")
}
if _, err := Client.CreateChannel(rchannel.Data.(*model.Channel)); err == nil {
t.Fatal("Cannot create an existing")
}
savedId := rchannel.Data.(*model.Channel).Id
rchannel.Data.(*model.Channel).Id = ""
if _, err := Client.CreateChannel(rchannel.Data.(*model.Channel)); err != nil {
if err.Message != "A channel with that URL already exists" {
t.Fatal(err)
}
}
if _, err := Client.DoApiPost(Client.GetTeamRoute()+"/channels/create", "garbage"); err == nil {
t.Fatal("should have been an error")
}
Client.DeleteChannel(savedId)
if _, err := Client.CreateChannel(rchannel.Data.(*model.Channel)); err != nil {
if err.Message != "A channel with that URL was previously created" {
t.Fatal(err)
}
}
channel = model.Channel{DisplayName: "Channel on Different Team", Name: "aaaa" + model.NewId() + "abbb", Type: model.CHANNEL_OPEN, TeamId: team2.Id}
if _, err := Client.CreateChannel(&channel); err.StatusCode != http.StatusForbidden {
t.Fatal(err)
}
channel = model.Channel{DisplayName: "Test API Name", Name: model.NewId() + "__" + model.NewId(), Type: model.CHANNEL_OPEN, TeamId: team.Id}
if _, err := Client.CreateChannel(&channel); err == nil {
t.Fatal("Should have errored out on invalid '__' character")
}
channel = model.Channel{DisplayName: "Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_DIRECT, TeamId: team.Id}
if _, err := Client.CreateChannel(&channel); err == nil {
t.Fatal("Should have errored out on direct channel type")
}
}
func TestCreateDirectChannel(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
user := th.BasicUser
user2 := th.BasicUser2
rchannel, err := Client.CreateDirectChannel(th.BasicUser2.Id)
if err != nil {
t.Fatal(err)
}
channelName := ""
if user2.Id > user.Id {
channelName = user.Id + "__" + user2.Id
} else {
channelName = user2.Id + "__" + user.Id
}
if rchannel.Data.(*model.Channel).Name != channelName {
t.Fatal("channel name didn't match")
}
if rchannel.Data.(*model.Channel).Type != model.CHANNEL_DIRECT {
t.Fatal("channel type was not direct")
}
if _, err := Client.CreateDirectChannel(th.BasicUser2.Id); err == nil {
t.Fatal("channel already exists and should have failed")
}
if _, err := Client.CreateDirectChannel("junk"); err == nil {
t.Fatal("should have failed with bad user id")
}
if _, err := Client.CreateDirectChannel("12345678901234567890123456"); err == nil {
t.Fatal("should have failed with non-existent user")
}
}
func TestUpdateChannel(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := th.BasicTeam
user := th.BasicUser
user2 := th.CreateUser(th.BasicClient)
channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
Client.AddChannelMember(channel1.Id, user.Id)
header := "a" + model.NewId() + "a"
purpose := "a" + model.NewId() + "a"
upChannel1 := &model.Channel{Id: channel1.Id, Header: header, Purpose: purpose}
upChannel1 = Client.Must(Client.UpdateChannel(upChannel1)).Data.(*model.Channel)
if upChannel1.Header != header {
t.Fatal("Channel admin failed to update header")
}
if upChannel1.Purpose != purpose {
t.Fatal("Channel admin failed to update purpose")
}
if upChannel1.DisplayName != channel1.DisplayName {
t.Fatal("Channel admin failed to skip displayName")
}
rget := Client.Must(Client.GetChannels(""))
data := rget.Data.(*model.ChannelList)
for _, c := range data.Channels {
if c.Name == model.DEFAULT_CHANNEL {
c.Header = "new header"
c.Name = "pseudo-square"
if _, err := Client.UpdateChannel(c); err == nil {
t.Fatal("should have errored on updating default channel name")
}
break
}
}
Client.LoginByEmail(team.Name, user2.Email, user2.Password)
if _, err := Client.UpdateChannel(upChannel1); err == nil {
t.Fatal("Standard User should have failed to update")
}
}
func TestUpdateChannelHeader(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := th.BasicTeam
channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
data := make(map[string]string)
data["channel_id"] = channel1.Id
data["channel_header"] = "new header"
var upChannel1 *model.Channel
if result, err := Client.UpdateChannelHeader(data); err != nil {
t.Fatal(err)
} else {
upChannel1 = result.Data.(*model.Channel)
}
if upChannel1.Header != data["channel_header"] {
t.Fatal("Failed to update header")
}
data["channel_id"] = "junk"
if _, err := Client.UpdateChannelHeader(data); err == nil {
t.Fatal("should have errored on junk channel id")
}
data["channel_id"] = "12345678901234567890123456"
if _, err := Client.UpdateChannelHeader(data); err == nil {
t.Fatal("should have errored on non-existent channel id")
}
data["channel_id"] = channel1.Id
data["channel_header"] = strings.Repeat("a", 1050)
if _, err := Client.UpdateChannelHeader(data); err == nil {
t.Fatal("should have errored on bad channel header")
}
th.LoginBasic2()
data["channel_id"] = channel1.Id
data["channel_header"] = "new header"
if _, err := Client.UpdateChannelHeader(data); err == nil {
t.Fatal("should have errored non-channel member trying to update header")
}
}
func TestUpdateChannelPurpose(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := th.BasicTeam
channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
data := make(map[string]string)
data["channel_id"] = channel1.Id
data["channel_purpose"] = "new purpose"
var upChannel1 *model.Channel
if result, err := Client.UpdateChannelPurpose(data); err != nil {
t.Fatal(err)
} else {
upChannel1 = result.Data.(*model.Channel)
}
if upChannel1.Purpose != data["channel_purpose"] {
t.Fatal("Failed to update purpose")
}
data["channel_id"] = "junk"
if _, err := Client.UpdateChannelPurpose(data); err == nil {
t.Fatal("should have errored on junk channel id")
}
data["channel_id"] = "12345678901234567890123456"
if _, err := Client.UpdateChannelPurpose(data); err == nil {
t.Fatal("should have errored on non-existent channel id")
}
data["channel_id"] = channel1.Id
data["channel_purpose"] = strings.Repeat("a", 150)
if _, err := Client.UpdateChannelPurpose(data); err == nil {
t.Fatal("should have errored on bad channel purpose")
}
th.LoginBasic2()
data["channel_id"] = channel1.Id
data["channel_purpose"] = "new purpose"
if _, err := Client.UpdateChannelPurpose(data); err == nil {
t.Fatal("should have errored non-channel member trying to update purpose")
}
}
func TestGetChannel(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := th.BasicTeam
channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
channel2 := &model.Channel{DisplayName: "B Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel)
rget := Client.Must(Client.GetChannels(""))
data := rget.Data.(*model.ChannelList)
if data.Channels[0].DisplayName != channel1.DisplayName {
t.Fatal("full name didn't match")
}
if data.Channels[1].DisplayName != channel2.DisplayName {
t.Fatal("full name didn't match")
}
// test etag caching
if cache_result, err := Client.GetChannels(rget.Etag); err != nil {
t.Fatal(err)
} else if cache_result.Data.(*model.ChannelList) != nil {
t.Log(cache_result.Data)
t.Fatal("cache should be empty")
}
if _, err := Client.UpdateLastViewedAt(channel2.Id); err != nil {
t.Fatal(err)
}
if resp, err := Client.GetChannel(channel1.Id, ""); err != nil {
t.Fatal(err)
} else {
data := resp.Data.(*model.ChannelData)
if data.Channel.DisplayName != channel1.DisplayName {
t.Fatal("name didn't match")
}
// test etag caching
if cache_result, err := Client.GetChannel(channel1.Id, resp.Etag); err != nil {
t.Fatal(err)
} else if cache_result.Data.(*model.ChannelData) != nil {
t.Log(cache_result.Data)
t.Fatal("cache should be empty")
}
}
if _, err := Client.GetChannel("junk", ""); err == nil {
t.Fatal("should have failed - bad channel id")
}
}
func TestGetMoreChannel(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := th.BasicTeam
channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
channel2 := &model.Channel{DisplayName: "B Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel)
th.LoginBasic2()
rget := Client.Must(Client.GetMoreChannels(""))
data := rget.Data.(*model.ChannelList)
if data.Channels[0].DisplayName != channel1.DisplayName {
t.Fatal("full name didn't match")
}
if data.Channels[1].DisplayName != channel2.DisplayName {
t.Fatal("full name didn't match")
}
// test etag caching
if cache_result, err := Client.GetMoreChannels(rget.Etag); err != nil {
t.Fatal(err)
} else if cache_result.Data.(*model.ChannelList) != nil {
t.Log(cache_result.Data)
t.Fatal("cache should be empty")
}
}
func TestGetChannelCounts(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := th.BasicTeam
channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
channel2 := &model.Channel{DisplayName: "B Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel)
if result, err := Client.GetChannelCounts(""); err != nil {
t.Fatal(err)
} else {
counts := result.Data.(*model.ChannelCounts)
if len(counts.Counts) != 5 {
t.Fatal("wrong number of channel counts")
}
if len(counts.UpdateTimes) != 5 {
t.Fatal("wrong number of channel update times")
}
if cache_result, err := Client.GetChannelCounts(result.Etag); err != nil {
t.Fatal(err)
} else if cache_result.Data.(*model.ChannelCounts) != nil {
t.Log(cache_result.Data)
t.Fatal("result data should be empty")
}
}
}
func TestJoinChannel(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := th.BasicTeam
channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
channel3 := &model.Channel{DisplayName: "B Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_PRIVATE, TeamId: team.Id}
channel3 = Client.Must(Client.CreateChannel(channel3)).Data.(*model.Channel)
th.LoginBasic2()
Client.Must(Client.JoinChannel(channel1.Id))
if _, err := Client.JoinChannel(channel3.Id); err == nil {
t.Fatal("shouldn't be able to join secret group")
}
rchannel := Client.Must(Client.CreateDirectChannel(th.BasicUser.Id)).Data.(*model.Channel)
user3 := th.CreateUser(th.BasicClient)
LinkUserToTeam(user3, team)
Client.LoginByEmail(team.Name, user3.Email, "pwd")
if _, err := Client.JoinChannel(rchannel.Id); err == nil {
t.Fatal("shoudn't be able to join direct channel")
}
}
func TestLeaveChannel(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := th.BasicTeam
channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
channel3 := &model.Channel{DisplayName: "B Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_PRIVATE, TeamId: team.Id}
channel3 = Client.Must(Client.CreateChannel(channel3)).Data.(*model.Channel)
th.LoginBasic2()
Client.Must(Client.JoinChannel(channel1.Id))
// No error if you leave a channel you cannot see
Client.Must(Client.LeaveChannel(channel3.Id))
rchannel := Client.Must(Client.CreateDirectChannel(th.BasicUser.Id)).Data.(*model.Channel)
if _, err := Client.LeaveChannel(rchannel.Id); err == nil {
t.Fatal("should have errored, cannot leave direct channel")
}
rget := Client.Must(Client.GetChannels(""))
cdata := rget.Data.(*model.ChannelList)
for _, c := range cdata.Channels {
if c.Name == model.DEFAULT_CHANNEL {
if _, err := Client.LeaveChannel(c.Id); err == nil {
t.Fatal("should have errored on leaving default channel")
}
break
}
}
}
func TestDeleteChannel(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := th.BasicTeam
userTeamAdmin := th.BasicUser
th.LoginBasic2()
channelMadeByCA := &model.Channel{DisplayName: "C Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channelMadeByCA = Client.Must(Client.CreateChannel(channelMadeByCA)).Data.(*model.Channel)
Client.AddChannelMember(channelMadeByCA.Id, userTeamAdmin.Id)
Client.LoginByEmail(team.Name, userTeamAdmin.Email, "pwd")
channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
channel2 := &model.Channel{DisplayName: "B Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel)
if _, err := Client.DeleteChannel(channel1.Id); err != nil {
t.Fatal(err)
}
if _, err := Client.DeleteChannel(channelMadeByCA.Id); err != nil {
t.Fatal("Team admin failed to delete Channel Admin's channel")
}
post1 := &model.Post{ChannelId: channel1.Id, Message: "a" + model.NewId() + "a"}
if _, err := Client.CreatePost(post1); err == nil {
t.Fatal("should have failed to post to deleted channel")
}
userStd := th.CreateUser(th.BasicClient)
LinkUserToTeam(userStd, team)
Client.LoginByEmail(team.Name, userStd.Email, userStd.Password)
if _, err := Client.JoinChannel(channel1.Id); err == nil {
t.Fatal("should have failed to join deleted channel")
}
Client.Must(Client.JoinChannel(channel2.Id))
if _, err := Client.DeleteChannel(channel2.Id); err == nil {
t.Fatal("should have failed to delete channel you're not an admin of")
}
rget := Client.Must(Client.GetChannels(""))
cdata := rget.Data.(*model.ChannelList)
for _, c := range cdata.Channels {
if c.Name == model.DEFAULT_CHANNEL {
if _, err := Client.DeleteChannel(c.Id); err == nil {
t.Fatal("should have errored on deleting default channel")
}
break
}
}
}
func TestGetChannelExtraInfo(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := th.BasicTeam
channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
rget := Client.Must(Client.GetChannelExtraInfo(channel1.Id, -1, ""))
data := rget.Data.(*model.ChannelExtra)
if data.Id != channel1.Id {
t.Fatal("couldnt't get extra info")
} else if len(data.Members) != 1 {
t.Fatal("got incorrect members")
} else if data.MemberCount != 1 {
t.Fatal("got incorrect member count")
}
//
// Testing etag caching
//
currentEtag := rget.Etag
if cache_result, err := Client.GetChannelExtraInfo(channel1.Id, -1, currentEtag); err != nil {
t.Fatal(err)
} else if cache_result.Data.(*model.ChannelExtra) != nil {
t.Log(cache_result.Data)
t.Fatal("response should be empty")
} else {
currentEtag = cache_result.Etag
}
Client2 := model.NewClient("http://localhost" + utils.Cfg.ServiceSettings.ListenAddress)
user2 := &model.User{Email: "success+" + model.NewId() + "@simulator.amazonses.com", Nickname: "Tester 2", Password: "pwd"}
user2 = Client2.Must(Client2.CreateUser(user2, "")).Data.(*model.User)
LinkUserToTeam(user2, team)
Client2.SetTeamId(team.Id)
store.Must(Srv.Store.User().VerifyEmail(user2.Id))
Client2.LoginByEmail(team.Name, user2.Email, "pwd")
Client2.Must(Client2.JoinChannel(channel1.Id))
if cache_result, err := Client.GetChannelExtraInfo(channel1.Id, -1, currentEtag); err != nil {
t.Fatal(err)
} else if cache_result.Data.(*model.ChannelExtra) == nil {
t.Log(cache_result.Data)
t.Fatal("response should not be empty")
} else {
currentEtag = cache_result.Etag
}
if cache_result, err := Client.GetChannelExtraInfo(channel1.Id, -1, currentEtag); err != nil {
t.Fatal(err)
} else if cache_result.Data.(*model.ChannelExtra) != nil {
t.Log(cache_result.Data)
t.Fatal("response should be empty")
} else {
currentEtag = cache_result.Etag
}
Client2.Must(Client2.LeaveChannel(channel1.Id))
if cache_result, err := Client.GetChannelExtraInfo(channel1.Id, -1, currentEtag); err != nil {
t.Fatal(err)
} else if cache_result.Data.(*model.ChannelExtra) == nil {
t.Log(cache_result.Data)
t.Fatal("response should not be empty")
} else {
currentEtag = cache_result.Etag
}
if cache_result, err := Client.GetChannelExtraInfo(channel1.Id, -1, currentEtag); err != nil {
t.Fatal(err)
} else if cache_result.Data.(*model.ChannelExtra) != nil {
t.Log(cache_result.Data)
t.Fatal("response should be empty")
} else {
currentEtag = cache_result.Etag
}
Client2.Must(Client2.JoinChannel(channel1.Id))
if cache_result, err := Client.GetChannelExtraInfo(channel1.Id, 2, currentEtag); err != nil {
t.Fatal(err)
} else if extra := cache_result.Data.(*model.ChannelExtra); extra == nil {
t.Fatal("response should not be empty")
} else if len(extra.Members) != 2 {
t.Fatal("should've returned 2 members")
} else if extra.MemberCount != 2 {
t.Fatal("should've returned member count of 2")
} else {
currentEtag = cache_result.Etag
}
if cache_result, err := Client.GetChannelExtraInfo(channel1.Id, 1, currentEtag); err != nil {
t.Fatal(err)
} else if extra := cache_result.Data.(*model.ChannelExtra); extra == nil {
t.Fatal("response should not be empty")
} else if len(extra.Members) != 1 {
t.Fatal("should've returned only 1 member")
} else if extra.MemberCount != 2 {
t.Fatal("should've returned member count of 2")
} else {
currentEtag = cache_result.Etag
}
if cache_result, err := Client.GetChannelExtraInfo(channel1.Id, 1, currentEtag); err != nil {
t.Fatal(err)
} else if cache_result.Data.(*model.ChannelExtra) != nil {
t.Log(cache_result.Data)
t.Fatal("response should be empty")
} else {
currentEtag = cache_result.Etag
}
}
func TestAddChannelMember(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := th.BasicTeam
user2 := th.BasicUser2
channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
if _, err := Client.AddChannelMember(channel1.Id, user2.Id); err != nil {
t.Fatal(err)
}
if _, err := Client.AddChannelMember(channel1.Id, "dsgsdg"); err == nil {
t.Fatal("Should have errored, bad user id")
}
if _, err := Client.AddChannelMember(channel1.Id, "12345678901234567890123456"); err == nil {
t.Fatal("Should have errored, bad user id")
}
if _, err := Client.AddChannelMember(channel1.Id, user2.Id); err == nil {
t.Fatal("Should have errored, user already a member")
}
if _, err := Client.AddChannelMember("sgdsgsdg", user2.Id); err == nil {
t.Fatal("Should have errored, bad channel id")
}
channel2 := &model.Channel{DisplayName: "A Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel)
th.LoginBasic2()
if _, err := Client.AddChannelMember(channel2.Id, user2.Id); err == nil {
t.Fatal("Should have errored, user not in channel")
}
th.LoginBasic()
Client.Must(Client.DeleteChannel(channel2.Id))
if _, err := Client.AddChannelMember(channel2.Id, user2.Id); err == nil {
t.Fatal("Should have errored, channel deleted")
}
}
func TestRemoveChannelMember(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := th.BasicTeam
user2 := th.BasicUser2
UpdateUserToTeamAdmin(user2, team)
channelMadeByCA := &model.Channel{DisplayName: "A Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channelMadeByCA = Client.Must(Client.CreateChannel(channelMadeByCA)).Data.(*model.Channel)
Client.Must(Client.AddChannelMember(channelMadeByCA.Id, user2.Id))
th.LoginBasic2()
channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
userStd := th.CreateUser(th.BasicClient)
LinkUserToTeam(userStd, team)
Client.Must(Client.AddChannelMember(channel1.Id, userStd.Id))
Client.Must(Client.AddChannelMember(channelMadeByCA.Id, userStd.Id))
if _, err := Client.RemoveChannelMember(channel1.Id, "dsgsdg"); err == nil {
t.Fatal("Should have errored, bad user id")
}
if _, err := Client.RemoveChannelMember("sgdsgsdg", userStd.Id); err == nil {
t.Fatal("Should have errored, bad channel id")
}
if _, err := Client.RemoveChannelMember(channel1.Id, userStd.Id); err != nil {
t.Fatal(err)
}
if _, err := Client.RemoveChannelMember(channelMadeByCA.Id, userStd.Id); err != nil {
t.Fatal("Team Admin failed to remove member from Channel Admin's channel")
}
channel2 := &model.Channel{DisplayName: "A Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel)
Client.LoginByEmail(team.Name, userStd.Email, userStd.Password)
if _, err := Client.RemoveChannelMember(channel2.Id, userStd.Id); err == nil {
t.Fatal("Should have errored, user not channel admin")
}
th.LoginBasic2()
Client.Must(Client.AddChannelMember(channel2.Id, userStd.Id))
Client.Must(Client.DeleteChannel(channel2.Id))
if _, err := Client.RemoveChannelMember(channel2.Id, userStd.Id); err == nil {
t.Fatal("Should have errored, channel deleted")
}
}
func TestUpdateNotifyProps(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := th.BasicTeam
user := th.BasicUser
user2 := th.BasicUser2
channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
data := make(map[string]string)
data["channel_id"] = channel1.Id
data["user_id"] = user.Id
data["desktop"] = model.CHANNEL_NOTIFY_MENTION
timeBeforeUpdate := model.GetMillis()
time.Sleep(100 * time.Millisecond)
// test updating desktop
if result, err := Client.UpdateNotifyProps(data); err != nil {
t.Fatal(err)
} else if notifyProps := result.Data.(map[string]string); notifyProps["desktop"] != model.CHANNEL_NOTIFY_MENTION {
t.Fatal("NotifyProps[\"desktop\"] did not update properly")
} else if notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_ALL {
t.Fatalf("NotifyProps[\"mark_unread\"] changed to %v", notifyProps["mark_unread"])
}
rget := Client.Must(Client.GetChannels(""))
rdata := rget.Data.(*model.ChannelList)
if len(rdata.Members) == 0 || rdata.Members[channel1.Id].NotifyProps["desktop"] != data["desktop"] {
t.Fatal("NotifyProps[\"desktop\"] did not update properly")
} else if rdata.Members[channel1.Id].LastUpdateAt <= timeBeforeUpdate {
t.Fatal("LastUpdateAt did not update")
}
// test an empty update
delete(data, "desktop")
if result, err := Client.UpdateNotifyProps(data); err != nil {
t.Fatal(err)
} else if notifyProps := result.Data.(map[string]string); notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_ALL {
t.Fatalf("NotifyProps[\"mark_unread\"] changed to %v", notifyProps["mark_unread"])
} else if notifyProps["desktop"] != model.CHANNEL_NOTIFY_MENTION {
t.Fatalf("NotifyProps[\"desktop\"] changed to %v", notifyProps["desktop"])
}
// test updating mark unread
data["mark_unread"] = model.CHANNEL_MARK_UNREAD_MENTION
if result, err := Client.UpdateNotifyProps(data); err != nil {
t.Fatal(err)
} else if notifyProps := result.Data.(map[string]string); notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_MENTION {
t.Fatal("NotifyProps[\"mark_unread\"] did not update properly")
} else if notifyProps["desktop"] != model.CHANNEL_NOTIFY_MENTION {
t.Fatalf("NotifyProps[\"desktop\"] changed to %v", notifyProps["desktop"])
}
// test updating both
data["desktop"] = model.CHANNEL_NOTIFY_NONE
data["mark_unread"] = model.CHANNEL_MARK_UNREAD_MENTION
if result, err := Client.UpdateNotifyProps(data); err != nil {
t.Fatal(err)
} else if notifyProps := result.Data.(map[string]string); notifyProps["desktop"] != model.CHANNEL_NOTIFY_NONE {
t.Fatal("NotifyProps[\"desktop\"] did not update properly")
} else if notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_MENTION {
t.Fatal("NotifyProps[\"mark_unread\"] did not update properly")
}
// test error cases
data["user_id"] = "junk"
if _, err := Client.UpdateNotifyProps(data); err == nil {
t.Fatal("Should have errored - bad user id")
}
data["user_id"] = "12345678901234567890123456"
if _, err := Client.UpdateNotifyProps(data); err == nil {
t.Fatal("Should have errored - bad user id")
}
data["user_id"] = user.Id
data["channel_id"] = "junk"
if _, err := Client.UpdateNotifyProps(data); err == nil {
t.Fatal("Should have errored - bad channel id")
}
data["channel_id"] = "12345678901234567890123456"
if _, err := Client.UpdateNotifyProps(data); err == nil {
t.Fatal("Should have errored - bad channel id")
}
data["desktop"] = "junk"
data["mark_unread"] = model.CHANNEL_MARK_UNREAD_ALL
if _, err := Client.UpdateNotifyProps(data); err == nil {
t.Fatal("Should have errored - bad desktop notify level")
}
data["desktop"] = model.CHANNEL_NOTIFY_ALL
data["mark_unread"] = "junk"
if _, err := Client.UpdateNotifyProps(data); err == nil {
t.Fatal("Should have errored - bad mark unread level")
}
th.LoginBasic2()
data["channel_id"] = channel1.Id
data["user_id"] = user2.Id
data["desktop"] = model.CHANNEL_NOTIFY_MENTION
data["mark_unread"] = model.CHANNEL_MARK_UNREAD_MENTION
if _, err := Client.UpdateNotifyProps(data); err == nil {
t.Fatal("Should have errored - user not in channel")
}
}
func TestFuzzyChannel(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := th.BasicTeam
// Strings that should pass as acceptable channel names
var fuzzyStringsPass = []string{
"*", "?", ".", "}{][)(><", "{}[]()<>",
"qahwah ( قهوة)",
"שָׁלוֹם עֲלֵיכֶם",
"Ramen チャーシュー chāshū",
"言而无信",
"Ṫ͌ó̍ ̍͂̓̍̍̀i̊ͯ͒",
"&amp; &lt; &qu",
"' or '1'='1' -- ",
"' or '1'='1' ({ ",
"' or '1'='1' /* ",
"1;DROP TABLE users",
"<b><i><u><strong><em>",
"sue@thatmightbe",
"sue@thatmightbe.",
"sue@thatmightbe.c",
"sue@thatmightbe.co",
"su+san@thatmightbe.com",
"a@b.中国",
"1@2.am",
"a@b.co.uk",
"a@b.cancerresearch",
"local@[127.0.0.1]",
}
for i := 0; i < len(fuzzyStringsPass); i++ {
channel := model.Channel{DisplayName: fuzzyStringsPass[i], Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
_, err := Client.CreateChannel(&channel)
if err != nil {
t.Fatal(err)
}
}
}