github.com/ashishbhate/mattermost-server@v5.11.1+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  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestPreferenceIsValid(t *testing.T) {
    15  	preference := Preference{
    16  		UserId:   "1234garbage",
    17  		Category: PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW,
    18  		Name:     NewId(),
    19  	}
    20  
    21  	require.NotNil(t, preference.IsValid())
    22  
    23  	preference.UserId = NewId()
    24  	require.Nil(t, preference.IsValid())
    25  
    26  	preference.Category = strings.Repeat("01234567890", 20)
    27  	require.NotNil(t, preference.IsValid())
    28  
    29  	preference.Category = PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW
    30  	require.Nil(t, preference.IsValid())
    31  
    32  	preference.Name = strings.Repeat("01234567890", 20)
    33  	require.NotNil(t, preference.IsValid())
    34  
    35  	preference.Name = NewId()
    36  	require.Nil(t, preference.IsValid())
    37  
    38  	preference.Value = strings.Repeat("01234567890", 201)
    39  	require.NotNil(t, preference.IsValid())
    40  
    41  	preference.Value = "1234garbage"
    42  	require.Nil(t, preference.IsValid())
    43  
    44  	preference.Category = PREFERENCE_CATEGORY_THEME
    45  	require.NotNil(t, preference.IsValid())
    46  
    47  	preference.Value = `{"color": "#ff0000", "color2": "#faf"}`
    48  	require.Nil(t, preference.IsValid())
    49  }
    50  
    51  func TestPreferencePreUpdate(t *testing.T) {
    52  	preference := Preference{
    53  		Category: PREFERENCE_CATEGORY_THEME,
    54  		Value:    `{"color": "#ff0000", "color2": "#faf", "codeTheme": "github", "invalid": "invalid"}`,
    55  	}
    56  
    57  	preference.PreUpdate()
    58  
    59  	var props map[string]string
    60  	if err := json.NewDecoder(strings.NewReader(preference.Value)).Decode(&props); err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	require.Equal(t, "#ff0000", props["color"], "shouldn't have changed valid props")
    65  	require.Equal(t, "#faf", props["color2"], "shouldn't have changed valid props")
    66  	require.Equal(t, "github", props["codeTheme"], "shouldn't have changed valid props")
    67  
    68  	require.NotEqual(t, "invalid", props["invalid"], "should have changed invalid prop")
    69  }