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

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/mattermost/mattermost-server/model"
    10  	"github.com/mattermost/mattermost-server/store"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestPermanentDeleteChannel(t *testing.T) {
    15  	th := Setup().InitBasic()
    16  	defer th.TearDown()
    17  
    18  	th.App.UpdateConfig(func(cfg *model.Config) {
    19  		cfg.ServiceSettings.EnableIncomingWebhooks = true
    20  		cfg.ServiceSettings.EnableOutgoingWebhooks = true
    21  	})
    22  
    23  	channel, err := th.App.CreateChannel(&model.Channel{DisplayName: "deletion-test", Name: "deletion-test", Type: model.CHANNEL_OPEN, TeamId: th.BasicTeam.Id}, false)
    24  	if err != nil {
    25  		t.Fatal(err.Error())
    26  	}
    27  	defer func() {
    28  		th.App.PermanentDeleteChannel(channel)
    29  	}()
    30  
    31  	incoming, err := th.App.CreateIncomingWebhookForChannel(th.BasicUser.Id, channel, &model.IncomingWebhook{ChannelId: channel.Id})
    32  	if err != nil {
    33  		t.Fatal(err.Error())
    34  	}
    35  	defer th.App.DeleteIncomingWebhook(incoming.Id)
    36  
    37  	if incoming, err = th.App.GetIncomingWebhook(incoming.Id); incoming == nil || err != nil {
    38  		t.Fatal("unable to get new incoming webhook")
    39  	}
    40  
    41  	outgoing, err := th.App.CreateOutgoingWebhook(&model.OutgoingWebhook{
    42  		ChannelId:    channel.Id,
    43  		TeamId:       channel.TeamId,
    44  		CreatorId:    th.BasicUser.Id,
    45  		CallbackURLs: []string{"http://foo"},
    46  	})
    47  	if err != nil {
    48  		t.Fatal(err.Error())
    49  	}
    50  	defer th.App.DeleteOutgoingWebhook(outgoing.Id)
    51  
    52  	if outgoing, err = th.App.GetOutgoingWebhook(outgoing.Id); outgoing == nil || err != nil {
    53  		t.Fatal("unable to get new outgoing webhook")
    54  	}
    55  
    56  	if err := th.App.PermanentDeleteChannel(channel); err != nil {
    57  		t.Fatal(err.Error())
    58  	}
    59  
    60  	if incoming, err = th.App.GetIncomingWebhook(incoming.Id); incoming != nil || err == nil {
    61  		t.Error("incoming webhook wasn't deleted")
    62  	}
    63  
    64  	if outgoing, err = th.App.GetOutgoingWebhook(outgoing.Id); outgoing != nil || err == nil {
    65  		t.Error("outgoing webhook wasn't deleted")
    66  	}
    67  }
    68  
    69  func TestMoveChannel(t *testing.T) {
    70  	th := Setup().InitBasic()
    71  	defer th.TearDown()
    72  
    73  	sourceTeam := th.CreateTeam()
    74  	targetTeam := th.CreateTeam()
    75  	channel1 := th.CreateChannel(sourceTeam)
    76  	defer func() {
    77  		th.App.PermanentDeleteChannel(channel1)
    78  		th.App.PermanentDeleteTeam(sourceTeam)
    79  		th.App.PermanentDeleteTeam(targetTeam)
    80  	}()
    81  
    82  	if _, err := th.App.AddUserToTeam(sourceTeam.Id, th.BasicUser.Id, ""); err != nil {
    83  		t.Fatal(err)
    84  	}
    85  	if _, err := th.App.AddUserToTeam(sourceTeam.Id, th.BasicUser2.Id, ""); err != nil {
    86  		t.Fatal(err)
    87  	}
    88  
    89  	if _, err := th.App.AddUserToTeam(targetTeam.Id, th.BasicUser.Id, ""); err != nil {
    90  		t.Fatal(err)
    91  	}
    92  
    93  	if _, err := th.App.AddUserToChannel(th.BasicUser, channel1); err != nil {
    94  		t.Fatal(err)
    95  	}
    96  	if _, err := th.App.AddUserToChannel(th.BasicUser2, channel1); err != nil {
    97  		t.Fatal(err)
    98  	}
    99  
   100  	if err := th.App.MoveChannel(targetTeam, channel1); err == nil {
   101  		t.Fatal("Should have failed due to mismatched members.")
   102  	}
   103  
   104  	if _, err := th.App.AddUserToTeam(targetTeam.Id, th.BasicUser2.Id, ""); err != nil {
   105  		t.Fatal(err)
   106  	}
   107  
   108  	if err := th.App.MoveChannel(targetTeam, channel1); err != nil {
   109  		t.Fatal(err)
   110  	}
   111  }
   112  
   113  func TestJoinDefaultChannelsTownSquare(t *testing.T) {
   114  	th := Setup().InitBasic()
   115  	defer th.TearDown()
   116  
   117  	// figure out the initial number of users in town square
   118  	townSquareChannelId := store.Must(th.App.Srv.Store.Channel().GetByName(th.BasicTeam.Id, "town-square", true)).(*model.Channel).Id
   119  	initialNumTownSquareUsers := len(store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, townSquareChannelId)).([]*model.ChannelMemberHistory))
   120  
   121  	// create a new user that joins the default channels
   122  	user := th.CreateUser()
   123  	th.App.JoinDefaultChannels(th.BasicTeam.Id, user, model.CHANNEL_USER_ROLE_ID, "")
   124  
   125  	// there should be a ChannelMemberHistory record for the user
   126  	histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, townSquareChannelId)).([]*model.ChannelMemberHistory)
   127  	assert.Len(t, histories, initialNumTownSquareUsers+1)
   128  
   129  	found := false
   130  	for _, history := range histories {
   131  		if user.Id == history.UserId && townSquareChannelId == history.ChannelId {
   132  			found = true
   133  			break
   134  		}
   135  	}
   136  	assert.True(t, found)
   137  }
   138  
   139  func TestJoinDefaultChannelsOffTopic(t *testing.T) {
   140  	th := Setup().InitBasic()
   141  	defer th.TearDown()
   142  
   143  	// figure out the initial number of users in off-topic
   144  	offTopicChannelId := store.Must(th.App.Srv.Store.Channel().GetByName(th.BasicTeam.Id, "off-topic", true)).(*model.Channel).Id
   145  	initialNumTownSquareUsers := len(store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, offTopicChannelId)).([]*model.ChannelMemberHistory))
   146  
   147  	// create a new user that joins the default channels
   148  	user := th.CreateUser()
   149  	th.App.JoinDefaultChannels(th.BasicTeam.Id, user, model.CHANNEL_USER_ROLE_ID, "")
   150  
   151  	// there should be a ChannelMemberHistory record for the user
   152  	histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, offTopicChannelId)).([]*model.ChannelMemberHistory)
   153  	assert.Len(t, histories, initialNumTownSquareUsers+1)
   154  
   155  	found := false
   156  	for _, history := range histories {
   157  		if user.Id == history.UserId && offTopicChannelId == history.ChannelId {
   158  			found = true
   159  			break
   160  		}
   161  	}
   162  	assert.True(t, found)
   163  }
   164  
   165  func TestCreateChannelPublic(t *testing.T) {
   166  	th := Setup().InitBasic()
   167  	defer th.TearDown()
   168  
   169  	// creates a public channel and adds basic user to it
   170  	publicChannel := th.createChannel(th.BasicTeam, model.CHANNEL_OPEN)
   171  
   172  	// there should be a ChannelMemberHistory record for the user
   173  	histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, publicChannel.Id)).([]*model.ChannelMemberHistory)
   174  	assert.Len(t, histories, 1)
   175  	assert.Equal(t, th.BasicUser.Id, histories[0].UserId)
   176  	assert.Equal(t, publicChannel.Id, histories[0].ChannelId)
   177  }
   178  
   179  func TestCreateChannelPrivate(t *testing.T) {
   180  	th := Setup().InitBasic()
   181  	defer th.TearDown()
   182  
   183  	// creates a private channel and adds basic user to it
   184  	privateChannel := th.createChannel(th.BasicTeam, model.CHANNEL_PRIVATE)
   185  
   186  	// there should be a ChannelMemberHistory record for the user
   187  	histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, privateChannel.Id)).([]*model.ChannelMemberHistory)
   188  	assert.Len(t, histories, 1)
   189  	assert.Equal(t, th.BasicUser.Id, histories[0].UserId)
   190  	assert.Equal(t, privateChannel.Id, histories[0].ChannelId)
   191  }
   192  
   193  func TestUpdateChannelPrivacy(t *testing.T) {
   194  	th := Setup().InitBasic()
   195  	defer th.TearDown()
   196  
   197  	privateChannel := th.createChannel(th.BasicTeam, model.CHANNEL_PRIVATE)
   198  	privateChannel.Type = model.CHANNEL_OPEN
   199  
   200  	if publicChannel, err := th.App.UpdateChannelPrivacy(privateChannel, th.BasicUser); err != nil {
   201  		t.Fatal("Failed to update channel privacy. Error: " + err.Error())
   202  	} else {
   203  		assert.Equal(t, publicChannel.Id, privateChannel.Id)
   204  		assert.Equal(t, publicChannel.Type, model.CHANNEL_OPEN)
   205  	}
   206  }
   207  
   208  func TestCreateGroupChannel(t *testing.T) {
   209  	th := Setup().InitBasic()
   210  	defer th.TearDown()
   211  
   212  	user1 := th.CreateUser()
   213  	user2 := th.CreateUser()
   214  
   215  	groupUserIds := make([]string, 0)
   216  	groupUserIds = append(groupUserIds, user1.Id)
   217  	groupUserIds = append(groupUserIds, user2.Id)
   218  	groupUserIds = append(groupUserIds, th.BasicUser.Id)
   219  
   220  	if channel, err := th.App.CreateGroupChannel(groupUserIds, th.BasicUser.Id); err != nil {
   221  		t.Fatal("Failed to create group channel. Error: " + err.Message)
   222  	} else {
   223  		// there should be a ChannelMemberHistory record for each user
   224  		histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id)).([]*model.ChannelMemberHistory)
   225  		assert.Len(t, histories, 3)
   226  
   227  		channelMemberHistoryUserIds := make([]string, 0)
   228  		for _, history := range histories {
   229  			assert.Equal(t, channel.Id, history.ChannelId)
   230  			channelMemberHistoryUserIds = append(channelMemberHistoryUserIds, history.UserId)
   231  		}
   232  		assert.Equal(t, groupUserIds, channelMemberHistoryUserIds)
   233  	}
   234  }
   235  
   236  func TestAddUserToChannel(t *testing.T) {
   237  	th := Setup().InitBasic()
   238  	defer th.TearDown()
   239  
   240  	// create a user and add it to a channel
   241  	user := th.CreateUser()
   242  	if _, err := th.App.AddTeamMember(th.BasicTeam.Id, user.Id); err != nil {
   243  		t.Fatal("Failed to add user to team. Error: " + err.Message)
   244  	}
   245  
   246  	groupUserIds := make([]string, 0)
   247  	groupUserIds = append(groupUserIds, th.BasicUser.Id)
   248  	groupUserIds = append(groupUserIds, user.Id)
   249  
   250  	channel := th.createChannel(th.BasicTeam, model.CHANNEL_OPEN)
   251  	if _, err := th.App.AddUserToChannel(user, channel); err != nil {
   252  		t.Fatal("Failed to add user to channel. Error: " + err.Message)
   253  	}
   254  
   255  	// there should be a ChannelMemberHistory record for the user
   256  	histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id)).([]*model.ChannelMemberHistory)
   257  	assert.Len(t, histories, 2)
   258  	channelMemberHistoryUserIds := make([]string, 0)
   259  	for _, history := range histories {
   260  		assert.Equal(t, channel.Id, history.ChannelId)
   261  		channelMemberHistoryUserIds = append(channelMemberHistoryUserIds, history.UserId)
   262  	}
   263  	assert.Equal(t, groupUserIds, channelMemberHistoryUserIds)
   264  }
   265  
   266  func TestRemoveUserFromChannel(t *testing.T) {
   267  	th := Setup().InitBasic()
   268  	defer th.TearDown()
   269  
   270  	// a user creates a channel
   271  	publicChannel := th.createChannel(th.BasicTeam, model.CHANNEL_OPEN)
   272  	histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, publicChannel.Id)).([]*model.ChannelMemberHistory)
   273  	assert.Len(t, histories, 1)
   274  	assert.Equal(t, th.BasicUser.Id, histories[0].UserId)
   275  	assert.Equal(t, publicChannel.Id, histories[0].ChannelId)
   276  	assert.Nil(t, histories[0].LeaveTime)
   277  
   278  	// the user leaves that channel
   279  	if err := th.App.LeaveChannel(publicChannel.Id, th.BasicUser.Id); err != nil {
   280  		t.Fatal("Failed to remove user from channel. Error: " + err.Message)
   281  	}
   282  	histories = store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, publicChannel.Id)).([]*model.ChannelMemberHistory)
   283  	assert.Len(t, histories, 1)
   284  	assert.Equal(t, th.BasicUser.Id, histories[0].UserId)
   285  	assert.Equal(t, publicChannel.Id, histories[0].ChannelId)
   286  	assert.NotNil(t, histories[0].LeaveTime)
   287  }