github.com/hahmadia/mattermost-server@v5.11.1+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), GroupConstrained: new(bool)}
    41  	*p.Name = NewId()
    42  	*p.DisplayName = NewId()
    43  	*p.Header = NewId()
    44  	*p.Purpose = NewId()
    45  	*p.GroupConstrained = true
    46  
    47  	o := Channel{Id: NewId(), Name: NewId()}
    48  	o.Patch(p)
    49  
    50  	if *p.Name != o.Name {
    51  		t.Fatal("do not match")
    52  	}
    53  	if *p.DisplayName != o.DisplayName {
    54  		t.Fatal("do not match")
    55  	}
    56  	if *p.Header != o.Header {
    57  		t.Fatal("do not match")
    58  	}
    59  	if *p.Purpose != o.Purpose {
    60  		t.Fatal("do not match")
    61  	}
    62  	if *p.GroupConstrained != *o.GroupConstrained {
    63  		t.Fatalf("expected %v got %v", *p.GroupConstrained, *o.GroupConstrained)
    64  	}
    65  }
    66  
    67  func TestChannelIsValid(t *testing.T) {
    68  	o := Channel{}
    69  
    70  	if err := o.IsValid(); err == nil {
    71  		t.Fatal("should be invalid")
    72  	}
    73  
    74  	o.Id = NewId()
    75  	if err := o.IsValid(); err == nil {
    76  		t.Fatal("should be invalid")
    77  	}
    78  
    79  	o.CreateAt = GetMillis()
    80  	if err := o.IsValid(); err == nil {
    81  		t.Fatal("should be invalid")
    82  	}
    83  
    84  	o.UpdateAt = GetMillis()
    85  	if err := o.IsValid(); err == nil {
    86  		t.Fatal("should be invalid")
    87  	}
    88  
    89  	o.DisplayName = strings.Repeat("01234567890", 20)
    90  	if err := o.IsValid(); err == nil {
    91  		t.Fatal("should be invalid")
    92  	}
    93  
    94  	o.DisplayName = "1234"
    95  	o.Name = "ZZZZZZZ"
    96  	if err := o.IsValid(); err == nil {
    97  		t.Fatal("should be invalid")
    98  	}
    99  
   100  	o.Name = "zzzzz"
   101  
   102  	if err := o.IsValid(); err == nil {
   103  		t.Fatal("should be invalid")
   104  	}
   105  
   106  	o.Type = "U"
   107  	if err := o.IsValid(); err == nil {
   108  		t.Fatal("should be invalid")
   109  	}
   110  
   111  	o.Type = "P"
   112  
   113  	if err := o.IsValid(); err != nil {
   114  		t.Fatal(err)
   115  	}
   116  
   117  	o.Header = strings.Repeat("01234567890", 100)
   118  	if err := o.IsValid(); err == nil {
   119  		t.Fatal("should be invalid")
   120  	}
   121  
   122  	o.Header = "1234"
   123  	if err := o.IsValid(); err != nil {
   124  		t.Fatal(err)
   125  	}
   126  
   127  	o.Purpose = strings.Repeat("01234567890", 30)
   128  	if err := o.IsValid(); err == nil {
   129  		t.Fatal("should be invalid")
   130  	}
   131  
   132  	o.Purpose = "1234"
   133  	if err := o.IsValid(); err != nil {
   134  		t.Fatal(err)
   135  	}
   136  
   137  	o.Purpose = strings.Repeat("0123456789", 25)
   138  	if err := o.IsValid(); err != nil {
   139  		t.Fatal(err)
   140  	}
   141  }
   142  
   143  func TestChannelPreSave(t *testing.T) {
   144  	o := Channel{Name: "test"}
   145  	o.PreSave()
   146  	o.Etag()
   147  }
   148  
   149  func TestChannelPreUpdate(t *testing.T) {
   150  	o := Channel{Name: "test"}
   151  	o.PreUpdate()
   152  }
   153  
   154  func TestGetGroupDisplayNameFromUsers(t *testing.T) {
   155  	users := make([]*User, 4)
   156  	users[0] = &User{Username: NewId()}
   157  	users[1] = &User{Username: NewId()}
   158  	users[2] = &User{Username: NewId()}
   159  	users[3] = &User{Username: NewId()}
   160  
   161  	name := GetGroupDisplayNameFromUsers(users, true)
   162  	if len(name) > CHANNEL_NAME_MAX_LENGTH {
   163  		t.Fatal("name too long")
   164  	}
   165  }
   166  
   167  func TestGetGroupNameFromUserIds(t *testing.T) {
   168  	name := GetGroupNameFromUserIds([]string{NewId(), NewId(), NewId(), NewId(), NewId()})
   169  
   170  	if len(name) > CHANNEL_NAME_MAX_LENGTH {
   171  		t.Fatal("name too long")
   172  	}
   173  }