summaryrefslogtreecommitdiffstats
path: root/model/preference_test.go
blob: df7fe612da774a63265efcf4c990732e6986813a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package model

import (
	"encoding/json"
	"strings"
	"testing"
)

func TestPreferenceIsValid(t *testing.T) {
	preference := Preference{
		UserId:   "1234garbage",
		Category: PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW,
		Name:     NewId(),
	}

	if err := preference.IsValid(); err == nil {
		t.Fatal()
	}

	preference.UserId = NewId()
	if err := preference.IsValid(); err != nil {
		t.Fatal(err)
	}

	preference.Category = strings.Repeat("01234567890", 20)
	if err := preference.IsValid(); err == nil {
		t.Fatal()
	}

	preference.Category = PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW
	if err := preference.IsValid(); err != nil {
		t.Fatal(err)
	}

	preference.Name = strings.Repeat("01234567890", 20)
	if err := preference.IsValid(); err == nil {
		t.Fatal()
	}

	preference.Name = NewId()
	if err := preference.IsValid(); err != nil {
		t.Fatal(err)
	}

	preference.Value = strings.Repeat("01234567890", 201)
	if err := preference.IsValid(); err == nil {
		t.Fatal()
	}

	preference.Value = "1234garbage"
	if err := preference.IsValid(); err != nil {
		t.Fatal(err)
	}

	preference.Category = PREFERENCE_CATEGORY_THEME
	if err := preference.IsValid(); err == nil {
		t.Fatal()
	}

	preference.Value = `{"color": "#ff0000", "color2": "#faf"}`
	if err := preference.IsValid(); err != nil {
		t.Fatal(err)
	}
}

func TestPreferencePreUpdate(t *testing.T) {
	preference := Preference{
		Category: PREFERENCE_CATEGORY_THEME,
		Value:    `{"color": "#ff0000", "color2": "#faf", "codeTheme": "github", "invalid": "invalid"}`,
	}

	preference.PreUpdate()

	var props map[string]string
	if err := json.NewDecoder(strings.NewReader(preference.Value)).Decode(&props); err != nil {
		t.Fatal(err)
	}

	if props["color"] != "#ff0000" || props["color2"] != "#faf" || props["codeTheme"] != "github" {
		t.Fatal("shouldn't have changed valid props")
	}

	if props["invalid"] == "invalid" {
		t.Fatal("should have changed invalid prop")
	}
}