github.com/psyb0t/mattermost-server@v4.6.1-0.20180125161845-5503a1351abf+incompatible/api/command_leave_test.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/mattermost/mattermost-server/model"
    11  )
    12  
    13  func TestLeaveCommands(t *testing.T) {
    14  	th := Setup().InitBasic()
    15  	defer th.TearDown()
    16  
    17  	Client := th.BasicClient
    18  	team := th.BasicTeam
    19  	user2 := th.BasicUser2
    20  
    21  	channel1 := &model.Channel{DisplayName: "AA", Name: "aa" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
    22  	channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
    23  	Client.Must(Client.JoinChannel(channel1.Id))
    24  
    25  	channel2 := &model.Channel{DisplayName: "BB", Name: "bb" + model.NewId() + "a", Type: model.CHANNEL_PRIVATE, TeamId: team.Id}
    26  	channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel)
    27  	Client.Must(Client.JoinChannel(channel2.Id))
    28  	Client.Must(Client.AddChannelMember(channel2.Id, user2.Id))
    29  
    30  	channel3 := Client.Must(Client.CreateDirectChannel(user2.Id)).Data.(*model.Channel)
    31  
    32  	rs1 := Client.Must(Client.Command(channel1.Id, "/leave")).Data.(*model.CommandResponse)
    33  	if !strings.HasSuffix(rs1.GotoLocation, "/"+team.Name+"/channels/"+model.DEFAULT_CHANNEL) {
    34  		t.Fatal("failed to leave open channel 1")
    35  	}
    36  
    37  	rs2 := Client.Must(Client.Command(channel2.Id, "/leave")).Data.(*model.CommandResponse)
    38  	if !strings.HasSuffix(rs2.GotoLocation, "/"+team.Name+"/channels/"+model.DEFAULT_CHANNEL) {
    39  		t.Fatal("failed to leave private channel 1")
    40  	}
    41  
    42  	rs3 := Client.Must(Client.Command(channel3.Id, "/leave")).Data.(*model.CommandResponse)
    43  	if strings.HasSuffix(rs3.GotoLocation, "/"+team.Name+"/channels/"+model.DEFAULT_CHANNEL) {
    44  		t.Fatal("should not have left direct message channel")
    45  	}
    46  
    47  	cdata := Client.Must(Client.GetChannels("")).Data.(*model.ChannelList)
    48  
    49  	found := false
    50  	for _, c := range *cdata {
    51  		if c.Id == channel1.Id || c.Id == channel2.Id {
    52  			found = true
    53  		}
    54  	}
    55  
    56  	if found {
    57  		t.Fatal("did not leave right channels")
    58  	}
    59  
    60  	for _, c := range *cdata {
    61  		if c.Name == model.DEFAULT_CHANNEL {
    62  			if _, err := Client.LeaveChannel(c.Id); err == nil {
    63  				t.Fatal("should have errored on leaving default channel")
    64  			}
    65  			break
    66  		}
    67  	}
    68  }