github.com/RajatVaryani/mattermost-server@v5.11.1+incompatible/model/team_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 TestTeamJson(t *testing.T) { 12 o := Team{Id: NewId(), DisplayName: NewId()} 13 json := o.ToJson() 14 ro := TeamFromJson(strings.NewReader(json)) 15 16 if o.Id != ro.Id { 17 t.Fatal("Ids do not match") 18 } 19 } 20 21 func TestTeamIsValid(t *testing.T) { 22 o := Team{} 23 24 if err := o.IsValid(); err == nil { 25 t.Fatal("should be invalid") 26 } 27 28 o.Id = NewId() 29 if err := o.IsValid(); err == nil { 30 t.Fatal("should be invalid") 31 } 32 33 o.CreateAt = GetMillis() 34 if err := o.IsValid(); err == nil { 35 t.Fatal("should be invalid") 36 } 37 38 o.UpdateAt = GetMillis() 39 if err := o.IsValid(); err == nil { 40 t.Fatal("should be invalid") 41 } 42 43 o.Email = strings.Repeat("01234567890", 20) 44 if err := o.IsValid(); err == nil { 45 t.Fatal("should be invalid") 46 } 47 48 o.Email = "corey+test@hulen.com" 49 o.DisplayName = strings.Repeat("01234567890", 20) 50 if err := o.IsValid(); err == nil { 51 t.Fatal("should be invalid") 52 } 53 54 o.DisplayName = "1234" 55 o.Name = "ZZZZZZZ" 56 if err := o.IsValid(); err == nil { 57 t.Fatal("should be invalid") 58 } 59 60 o.Name = "zzzzz" 61 o.Type = TEAM_OPEN 62 o.InviteId = NewId() 63 if err := o.IsValid(); err != nil { 64 t.Fatal(err) 65 } 66 } 67 68 func TestTeamPreSave(t *testing.T) { 69 o := Team{DisplayName: "test"} 70 o.PreSave() 71 o.Etag() 72 } 73 74 func TestTeamPreUpdate(t *testing.T) { 75 o := Team{DisplayName: "test"} 76 o.PreUpdate() 77 } 78 79 var domains = []struct { 80 value string 81 expected bool 82 }{ 83 {"spin-punch", true}, 84 {"-spin-punch", false}, 85 {"spin-punch-", false}, 86 {"spin_punch", false}, 87 {"a", false}, 88 {"aa", true}, 89 {"aaa", true}, 90 {"aaa-999b", true}, 91 {"b00b", true}, 92 {"b)", false}, 93 {"test", true}, 94 } 95 96 func TestValidTeamName(t *testing.T) { 97 for _, v := range domains { 98 if IsValidTeamName(v.value) != v.expected { 99 t.Errorf("expect %v as %v", v.value, v.expected) 100 } 101 } 102 } 103 104 var tReservedDomains = []struct { 105 value string 106 expected bool 107 }{ 108 {"admin", true}, 109 {"Admin-punch", true}, 110 {"spin-punch-admin", false}, 111 } 112 113 func TestReservedTeamName(t *testing.T) { 114 for _, v := range tReservedDomains { 115 if IsReservedTeamName(v.value) != v.expected { 116 t.Errorf("expect %v as %v", v.value, v.expected) 117 } 118 } 119 } 120 121 func TestCleanTeamName(t *testing.T) { 122 if CleanTeamName("Jimbo's Admin") != "jimbos-admin" { 123 t.Fatal("didn't clean name properly") 124 } 125 126 if CleanTeamName("Admin Really cool") != "really-cool" { 127 t.Fatal("didn't clean name properly") 128 } 129 130 if CleanTeamName("super-duper-guys") != "super-duper-guys" { 131 t.Fatal("didn't clean name properly") 132 } 133 } 134 135 func TestTeamPatch(t *testing.T) { 136 p := &TeamPatch{ 137 DisplayName: new(string), 138 Description: new(string), 139 CompanyName: new(string), 140 AllowedDomains: new(string), 141 AllowOpenInvite: new(bool), 142 GroupConstrained: new(bool), 143 } 144 145 *p.DisplayName = NewId() 146 *p.Description = NewId() 147 *p.CompanyName = NewId() 148 *p.AllowedDomains = NewId() 149 *p.AllowOpenInvite = true 150 *p.GroupConstrained = true 151 152 o := Team{Id: NewId()} 153 o.Patch(p) 154 155 if *p.DisplayName != o.DisplayName { 156 t.Fatal("DisplayName did not update") 157 } 158 if *p.Description != o.Description { 159 t.Fatal("Description did not update") 160 } 161 if *p.CompanyName != o.CompanyName { 162 t.Fatal("CompanyName did not update") 163 } 164 if *p.AllowedDomains != o.AllowedDomains { 165 t.Fatal("AllowedDomains did not update") 166 } 167 if *p.AllowOpenInvite != o.AllowOpenInvite { 168 t.Fatal("AllowOpenInvite did not update") 169 } 170 if *p.GroupConstrained != *o.GroupConstrained { 171 t.Fatalf("expected %v got %v", *p.GroupConstrained, *o.GroupConstrained) 172 } 173 }