github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/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  
    11  func TestChannelJson(t *testing.T) {
    12  	o := Channel{Id: NewId(), Name: NewId()}
    13  	json := o.ToJson()
    14  	ro := ChannelFromJson(strings.NewReader(json))
    15  
    16  	if o.Id != ro.Id {
    17  		t.Fatal("Ids do not match")
    18  	}
    19  
    20  	p := ChannelPatch{Name: new(string)}
    21  	*p.Name = NewId()
    22  	json = p.ToJson()
    23  	rp := ChannelPatchFromJson(strings.NewReader(json))
    24  
    25  	if *p.Name != *rp.Name {
    26  		t.Fatal("names do not match")
    27  	}
    28  }
    29  
    30  func TestChannelCopy(t *testing.T) {
    31  	o := Channel{Id: NewId(), Name: NewId()}
    32  	ro := o.DeepCopy()
    33  
    34  	if o.Id != ro.Id {
    35  		t.Fatal("Ids do not match")
    36  	}
    37  }
    38  
    39  func TestChannelPatch(t *testing.T) {
    40  	p := &ChannelPatch{Name: new(string), DisplayName: new(string), Header: new(string), Purpose: new(string)}
    41  	*p.Name = NewId()
    42  	*p.DisplayName = NewId()
    43  	*p.Header = NewId()
    44  	*p.Purpose = NewId()
    45  
    46  	o := Channel{Id: NewId(), Name: NewId()}
    47  	o.Patch(p)
    48  
    49  	if *p.Name != o.Name {
    50  		t.Fatal("do not match")
    51  	}
    52  	if *p.DisplayName != o.DisplayName {
    53  		t.Fatal("do not match")
    54  	}
    55  	if *p.Header != o.Header {
    56  		t.Fatal("do not match")
    57  	}
    58  	if *p.Purpose != o.Purpose {
    59  		t.Fatal("do not match")
    60  	}
    61  }
    62  
    63  func TestChannelIsValid(t *testing.T) {
    64  	o := Channel{}
    65  
    66  	if err := o.IsValid(); err == nil {
    67  		t.Fatal("should be invalid")
    68  	}
    69  
    70  	o.Id = NewId()
    71  	if err := o.IsValid(); err == nil {
    72  		t.Fatal("should be invalid")
    73  	}
    74  
    75  	o.CreateAt = GetMillis()
    76  	if err := o.IsValid(); err == nil {
    77  		t.Fatal("should be invalid")
    78  	}
    79  
    80  	o.UpdateAt = GetMillis()
    81  	if err := o.IsValid(); err == nil {
    82  		t.Fatal("should be invalid")
    83  	}
    84  
    85  	o.DisplayName = strings.Repeat("01234567890", 20)
    86  	if err := o.IsValid(); err == nil {
    87  		t.Fatal("should be invalid")
    88  	}
    89  
    90  	o.DisplayName = "1234"
    91  	o.Name = "ZZZZZZZ"
    92  	if err := o.IsValid(); err == nil {
    93  		t.Fatal("should be invalid")
    94  	}
    95  
    96  	o.Name = "zzzzz"
    97  
    98  	if err := o.IsValid(); err == nil {
    99  		t.Fatal("should be invalid")
   100  	}
   101  
   102  	o.Type = "U"
   103  	if err := o.IsValid(); err == nil {
   104  		t.Fatal("should be invalid")
   105  	}
   106  
   107  	o.Type = "P"
   108  
   109  	if err := o.IsValid(); err != nil {
   110  		t.Fatal(err)
   111  	}
   112  
   113  	o.Header = strings.Repeat("01234567890", 100)
   114  	if err := o.IsValid(); err == nil {
   115  		t.Fatal("should be invalid")
   116  	}
   117  
   118  	o.Header = "1234"
   119  	if err := o.IsValid(); err != nil {
   120  		t.Fatal(err)
   121  	}
   122  
   123  	o.Purpose = strings.Repeat("01234567890", 30)
   124  	if err := o.IsValid(); err == nil {
   125  		t.Fatal("should be invalid")
   126  	}
   127  
   128  	o.Purpose = "1234"
   129  	if err := o.IsValid(); err != nil {
   130  		t.Fatal(err)
   131  	}
   132  
   133  	o.Purpose = strings.Repeat("0123456789", 25)
   134  	if err := o.IsValid(); err != nil {
   135  		t.Fatal(err)
   136  	}
   137  }
   138  
   139  func TestChannelPreSave(t *testing.T) {
   140  	o := Channel{Name: "test"}
   141  	o.PreSave()
   142  	o.Etag()
   143  }
   144  
   145  func TestChannelPreUpdate(t *testing.T) {
   146  	o := Channel{Name: "test"}
   147  	o.PreUpdate()
   148  }
   149  
   150  func TestGetGroupDisplayNameFromUsers(t *testing.T) {
   151  	users := make([]*User, 4)
   152  	users[0] = &User{Username: NewId()}
   153  	users[1] = &User{Username: NewId()}
   154  	users[2] = &User{Username: NewId()}
   155  	users[3] = &User{Username: NewId()}
   156  
   157  	name := GetGroupDisplayNameFromUsers(users, true)
   158  	if len(name) > CHANNEL_NAME_MAX_LENGTH {
   159  		t.Fatal("name too long")
   160  	}
   161  }
   162  
   163  func TestGetGroupNameFromUserIds(t *testing.T) {
   164  	name := GetGroupNameFromUserIds([]string{NewId(), NewId(), NewId(), NewId(), NewId()})
   165  
   166  	if len(name) > CHANNEL_NAME_MAX_LENGTH {
   167  		t.Fatal("name too long")
   168  	}
   169  }