channel_test.go 3.31 KB
Newer Older
1
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
=Corey Hulen's avatar
=Corey Hulen committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// See License.txt for license information.

package model

import (
	"strings"
	"testing"
)

func TestChannelJson(t *testing.T) {
	o := Channel{Id: NewId(), Name: NewId()}
	json := o.ToJson()
	ro := ChannelFromJson(strings.NewReader(json))

	if o.Id != ro.Id {
		t.Fatal("Ids do not match")
	}
19 20 21 22 23 24 25 26 27 28 29

	p := ChannelPatch{Name: new(string)}
	*p.Name = NewId()
	json = p.ToJson()
	rp := ChannelPatchFromJson(strings.NewReader(json))

	if *p.Name != *rp.Name {
		t.Fatal("names do not match")
	}
}

30 31 32 33 34 35 36 37 38
func TestChannelCopy(t *testing.T) {
	o := Channel{Id: NewId(), Name: NewId()}
	ro := o.DeepCopy()

	if o.Id != ro.Id {
		t.Fatal("Ids do not match")
	}
}

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
func TestChannelPatch(t *testing.T) {
	p := &ChannelPatch{Name: new(string), DisplayName: new(string), Header: new(string), Purpose: new(string)}
	*p.Name = NewId()
	*p.DisplayName = NewId()
	*p.Header = NewId()
	*p.Purpose = NewId()

	o := Channel{Id: NewId(), Name: NewId()}
	o.Patch(p)

	if *p.Name != o.Name {
		t.Fatal("do not match")
	}
	if *p.DisplayName != o.DisplayName {
		t.Fatal("do not match")
	}
	if *p.Header != o.Header {
		t.Fatal("do not match")
	}
	if *p.Purpose != o.Purpose {
		t.Fatal("do not match")
	}
=Corey Hulen's avatar
=Corey Hulen committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
}

func TestChannelIsValid(t *testing.T) {
	o := Channel{}

	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.Id = NewId()
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.CreateAt = GetMillis()
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.UpdateAt = GetMillis()
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.DisplayName = strings.Repeat("01234567890", 20)
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.DisplayName = "1234"
	o.Name = "ZZZZZZZ"
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.Name = "zzzzz"

	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.Type = "U"
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.Type = "P"

	if err := o.IsValid(); err != nil {
		t.Fatal(err)
	}
hmhealey's avatar
hmhealey committed
112 113 114 115 116 117 118 119 120 121 122

	o.Header = strings.Repeat("01234567890", 100)
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.Header = "1234"
	if err := o.IsValid(); err != nil {
		t.Fatal(err)
	}

123
	o.Purpose = strings.Repeat("01234567890", 30)
hmhealey's avatar
hmhealey committed
124 125 126 127 128 129 130 131
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.Purpose = "1234"
	if err := o.IsValid(); err != nil {
		t.Fatal(err)
	}
132 133 134 135 136

	o.Purpose = strings.Repeat("0123456789", 25)
	if err := o.IsValid(); err != nil {
		t.Fatal(err)
	}
=Corey Hulen's avatar
=Corey Hulen committed
137 138 139 140 141 142 143 144 145 146 147 148
}

func TestChannelPreSave(t *testing.T) {
	o := Channel{Name: "test"}
	o.PreSave()
	o.Etag()
}

func TestChannelPreUpdate(t *testing.T) {
	o := Channel{Name: "test"}
	o.PreUpdate()
}
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

func TestGetGroupDisplayNameFromUsers(t *testing.T) {
	users := make([]*User, 4)
	users[0] = &User{Username: NewId()}
	users[1] = &User{Username: NewId()}
	users[2] = &User{Username: NewId()}
	users[3] = &User{Username: NewId()}

	name := GetGroupDisplayNameFromUsers(users, true)
	if len(name) > CHANNEL_NAME_MAX_LENGTH {
		t.Fatal("name too long")
	}
}

func TestGetGroupNameFromUserIds(t *testing.T) {
	name := GetGroupNameFromUserIds([]string{NewId(), NewId(), NewId(), NewId(), NewId()})

	if len(name) > CHANNEL_NAME_MAX_LENGTH {
		t.Fatal("name too long")
	}
}