github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/model/channel_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package model 5 6 import ( 7 "strings" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestChannelJson(t *testing.T) { 14 o := Channel{Id: NewId(), Name: NewId()} 15 json := o.ToJson() 16 ro := ChannelFromJson(strings.NewReader(json)) 17 18 require.Equal(t, o.Id, ro.Id) 19 20 p := ChannelPatch{Name: new(string)} 21 *p.Name = NewId() 22 json = p.ToJson() 23 rp := ChannelPatchFromJson(strings.NewReader(json)) 24 25 require.Equal(t, *p.Name, *rp.Name) 26 } 27 28 func TestChannelCopy(t *testing.T) { 29 o := Channel{Id: NewId(), Name: NewId()} 30 ro := o.DeepCopy() 31 32 require.Equal(t, o.Id, ro.Id, "Ids do not match") 33 } 34 35 func TestChannelPatch(t *testing.T) { 36 p := &ChannelPatch{Name: new(string), DisplayName: new(string), Header: new(string), Purpose: new(string), GroupConstrained: new(bool)} 37 *p.Name = NewId() 38 *p.DisplayName = NewId() 39 *p.Header = NewId() 40 *p.Purpose = NewId() 41 *p.GroupConstrained = true 42 43 o := Channel{Id: NewId(), Name: NewId()} 44 o.Patch(p) 45 46 require.Equal(t, *p.Name, o.Name) 47 require.Equal(t, *p.DisplayName, o.DisplayName) 48 require.Equal(t, *p.Header, o.Header) 49 require.Equal(t, *p.Purpose, o.Purpose) 50 require.Equal(t, *p.GroupConstrained, *o.GroupConstrained) 51 } 52 53 func TestChannelIsValid(t *testing.T) { 54 o := Channel{} 55 56 require.Error(t, o.IsValid()) 57 58 o.Id = NewId() 59 require.Error(t, o.IsValid()) 60 61 o.CreateAt = GetMillis() 62 require.Error(t, o.IsValid()) 63 64 o.UpdateAt = GetMillis() 65 require.Error(t, o.IsValid()) 66 67 o.DisplayName = strings.Repeat("01234567890", 20) 68 require.Error(t, o.IsValid()) 69 70 o.DisplayName = "1234" 71 o.Name = "ZZZZZZZ" 72 require.Error(t, o.IsValid()) 73 74 o.Name = "zzzzz" 75 require.Error(t, o.IsValid()) 76 77 o.Type = "U" 78 require.Error(t, o.IsValid()) 79 80 o.Type = "P" 81 require.Error(t, o.IsValid()) 82 83 o.Header = strings.Repeat("01234567890", 100) 84 require.Error(t, o.IsValid()) 85 86 o.Header = "1234" 87 require.Nil(t, o.IsValid()) 88 89 o.Purpose = strings.Repeat("01234567890", 30) 90 require.Error(t, o.IsValid()) 91 92 o.Purpose = "1234" 93 require.Nil(t, o.IsValid()) 94 95 o.Purpose = strings.Repeat("0123456789", 25) 96 require.Nil(t, o.IsValid()) 97 } 98 99 func TestChannelPreSave(t *testing.T) { 100 o := Channel{Name: "test"} 101 o.PreSave() 102 o.Etag() 103 } 104 105 func TestChannelPreUpdate(t *testing.T) { 106 o := Channel{Name: "test"} 107 o.PreUpdate() 108 } 109 110 func TestGetGroupDisplayNameFromUsers(t *testing.T) { 111 users := make([]*User, 4) 112 users[0] = &User{Username: NewId()} 113 users[1] = &User{Username: NewId()} 114 users[2] = &User{Username: NewId()} 115 users[3] = &User{Username: NewId()} 116 117 name := GetGroupDisplayNameFromUsers(users, true) 118 require.LessOrEqual(t, len(name), CHANNEL_NAME_MAX_LENGTH) 119 } 120 121 func TestGetGroupNameFromUserIds(t *testing.T) { 122 name := GetGroupNameFromUserIds([]string{NewId(), NewId(), NewId(), NewId(), NewId()}) 123 124 require.LessOrEqual(t, len(name), CHANNEL_NAME_MAX_LENGTH) 125 }