github.com/wgh-/mattermost-server@v4.8.0-rc2+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  	if err := o.IsValid(); err != nil {
    63  		t.Fatal(err)
    64  	}
    65  }
    66  
    67  func TestTeamPreSave(t *testing.T) {
    68  	o := Team{DisplayName: "test"}
    69  	o.PreSave()
    70  	o.Etag()
    71  }
    72  
    73  func TestTeamPreUpdate(t *testing.T) {
    74  	o := Team{DisplayName: "test"}
    75  	o.PreUpdate()
    76  }
    77  
    78  var domains = []struct {
    79  	value    string
    80  	expected bool
    81  }{
    82  	{"spin-punch", true},
    83  	{"-spin-punch", false},
    84  	{"spin-punch-", false},
    85  	{"spin_punch", false},
    86  	{"a", false},
    87  	{"aa", true},
    88  	{"aaa", true},
    89  	{"aaa-999b", true},
    90  	{"b00b", true},
    91  	{"b)", false},
    92  	{"test", true},
    93  }
    94  
    95  func TestValidTeamName(t *testing.T) {
    96  	for _, v := range domains {
    97  		if IsValidTeamName(v.value) != v.expected {
    98  			t.Errorf("expect %v as %v", v.value, v.expected)
    99  		}
   100  	}
   101  }
   102  
   103  var tReservedDomains = []struct {
   104  	value    string
   105  	expected bool
   106  }{
   107  	{"admin", true},
   108  	{"Admin-punch", true},
   109  	{"spin-punch-admin", false},
   110  }
   111  
   112  func TestReservedTeamName(t *testing.T) {
   113  	for _, v := range tReservedDomains {
   114  		if IsReservedTeamName(v.value) != v.expected {
   115  			t.Errorf("expect %v as %v", v.value, v.expected)
   116  		}
   117  	}
   118  }
   119  
   120  func TestCleanTeamName(t *testing.T) {
   121  	if CleanTeamName("Jimbo's Admin") != "jimbos-admin" {
   122  		t.Fatal("didn't clean name properly")
   123  	}
   124  
   125  	if CleanTeamName("Admin Really cool") != "really-cool" {
   126  		t.Fatal("didn't clean name properly")
   127  	}
   128  
   129  	if CleanTeamName("super-duper-guys") != "super-duper-guys" {
   130  		t.Fatal("didn't clean name properly")
   131  	}
   132  }