github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/model/preference_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  	"encoding/json"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestPreferenceIsValid(t *testing.T) {
    13  	preference := Preference{
    14  		UserId:   "1234garbage",
    15  		Category: PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW,
    16  		Name:     NewId(),
    17  	}
    18  
    19  	if err := preference.IsValid(); err == nil {
    20  		t.Fatal()
    21  	}
    22  
    23  	preference.UserId = NewId()
    24  	if err := preference.IsValid(); err != nil {
    25  		t.Fatal(err)
    26  	}
    27  
    28  	preference.Category = strings.Repeat("01234567890", 20)
    29  	if err := preference.IsValid(); err == nil {
    30  		t.Fatal()
    31  	}
    32  
    33  	preference.Category = PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW
    34  	if err := preference.IsValid(); err != nil {
    35  		t.Fatal(err)
    36  	}
    37  
    38  	preference.Name = strings.Repeat("01234567890", 20)
    39  	if err := preference.IsValid(); err == nil {
    40  		t.Fatal()
    41  	}
    42  
    43  	preference.Name = NewId()
    44  	if err := preference.IsValid(); err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	preference.Value = strings.Repeat("01234567890", 201)
    49  	if err := preference.IsValid(); err == nil {
    50  		t.Fatal()
    51  	}
    52  
    53  	preference.Value = "1234garbage"
    54  	if err := preference.IsValid(); err != nil {
    55  		t.Fatal(err)
    56  	}
    57  
    58  	preference.Category = PREFERENCE_CATEGORY_THEME
    59  	if err := preference.IsValid(); err == nil {
    60  		t.Fatal()
    61  	}
    62  
    63  	preference.Value = `{"color": "#ff0000", "color2": "#faf"}`
    64  	if err := preference.IsValid(); err != nil {
    65  		t.Fatal(err)
    66  	}
    67  }
    68  
    69  func TestPreferencePreUpdate(t *testing.T) {
    70  	preference := Preference{
    71  		Category: PREFERENCE_CATEGORY_THEME,
    72  		Value:    `{"color": "#ff0000", "color2": "#faf", "codeTheme": "github", "invalid": "invalid"}`,
    73  	}
    74  
    75  	preference.PreUpdate()
    76  
    77  	var props map[string]string
    78  	if err := json.NewDecoder(strings.NewReader(preference.Value)).Decode(&props); err != nil {
    79  		t.Fatal(err)
    80  	}
    81  
    82  	if props["color"] != "#ff0000" || props["color2"] != "#faf" || props["codeTheme"] != "github" {
    83  		t.Fatal("shouldn't have changed valid props")
    84  	}
    85  
    86  	if props["invalid"] == "invalid" {
    87  		t.Fatal("should have changed invalid prop")
    88  	}
    89  }