summaryrefslogtreecommitdiffstats
path: root/model/license_test.go
blob: 8b65d0700cb7e41e340b5a017751f0d67bbc3f4c (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package model

import (
	"strings"
	"testing"
)

func TestLicenseFeaturesToMap(t *testing.T) {
	f := Features{}
	f.SetDefaults()

	m := f.ToMap()

	CheckTrue(t, m["ldap"].(bool))
	CheckTrue(t, m["mfa"].(bool))
	CheckTrue(t, m["google"].(bool))
	CheckTrue(t, m["office365"].(bool))
	CheckTrue(t, m["compliance"].(bool))
	CheckTrue(t, m["cluster"].(bool))
	CheckTrue(t, m["metrics"].(bool))
	CheckTrue(t, m["custom_brand"].(bool))
	CheckTrue(t, m["mhpns"].(bool))
	CheckTrue(t, m["saml"].(bool))
	CheckTrue(t, m["password"].(bool))
	CheckTrue(t, m["elastic_search"].(bool))
	CheckTrue(t, m["future"].(bool))
}

func TestLicenseFeaturesSetDefaults(t *testing.T) {
	f := Features{}
	f.SetDefaults()

	CheckInt(t, *f.Users, 0)
	CheckTrue(t, *f.LDAP)
	CheckTrue(t, *f.MFA)
	CheckTrue(t, *f.GoogleOAuth)
	CheckTrue(t, *f.Office365OAuth)
	CheckTrue(t, *f.Compliance)
	CheckTrue(t, *f.Cluster)
	CheckTrue(t, *f.Metrics)
	CheckTrue(t, *f.CustomBrand)
	CheckTrue(t, *f.MHPNS)
	CheckTrue(t, *f.SAML)
	CheckTrue(t, *f.PasswordRequirements)
	CheckTrue(t, *f.ElasticSearch)
	CheckTrue(t, *f.FutureFeatures)

	f = Features{}
	f.SetDefaults()

	*f.Users = 300
	*f.FutureFeatures = false
	*f.LDAP = true
	*f.MFA = true
	*f.GoogleOAuth = true
	*f.Office365OAuth = true
	*f.Compliance = true
	*f.Cluster = true
	*f.Metrics = true
	*f.CustomBrand = true
	*f.MHPNS = true
	*f.SAML = true
	*f.PasswordRequirements = true
	*f.ElasticSearch = true

	f.SetDefaults()

	CheckInt(t, *f.Users, 300)
	CheckTrue(t, *f.LDAP)
	CheckTrue(t, *f.MFA)
	CheckTrue(t, *f.GoogleOAuth)
	CheckTrue(t, *f.Office365OAuth)
	CheckTrue(t, *f.Compliance)
	CheckTrue(t, *f.Cluster)
	CheckTrue(t, *f.Metrics)
	CheckTrue(t, *f.CustomBrand)
	CheckTrue(t, *f.MHPNS)
	CheckTrue(t, *f.SAML)
	CheckTrue(t, *f.PasswordRequirements)
	CheckTrue(t, *f.ElasticSearch)
	CheckFalse(t, *f.FutureFeatures)
}

func TestLicenseIsExpired(t *testing.T) {
	l1 := License{}
	l1.ExpiresAt = GetMillis() - 1000
	if !l1.IsExpired() {
		t.Fatal("license should be expired")
	}

	l1.ExpiresAt = GetMillis() + 10000
	if l1.IsExpired() {
		t.Fatal("license should not be expired")
	}
}

func TestLicenseIsStarted(t *testing.T) {
	l1 := License{}
	l1.StartsAt = GetMillis() - 1000
	if !l1.IsStarted() {
		t.Fatal("license should be started")
	}

	l1.StartsAt = GetMillis() + 10000
	if l1.IsStarted() {
		t.Fatal("license should not be started")
	}
}

func TestLicenseToFromJson(t *testing.T) {
	f := Features{}
	f.SetDefaults()

	l := License{
		Id:        NewId(),
		IssuedAt:  GetMillis(),
		StartsAt:  GetMillis(),
		ExpiresAt: GetMillis(),
		Customer: &Customer{
			Id:          NewId(),
			Name:        NewId(),
			Email:       NewId(),
			Company:     NewId(),
			PhoneNumber: NewId(),
		},
		Features: &f,
	}

	j := l.ToJson()

	l1 := LicenseFromJson(strings.NewReader(j))
	if l1 == nil {
		t.Fatalf("Decoding failed but should have passed.")
	}

	CheckString(t, l1.Id, l.Id)
	CheckInt64(t, l1.IssuedAt, l.IssuedAt)
	CheckInt64(t, l1.StartsAt, l.StartsAt)
	CheckInt64(t, l1.ExpiresAt, l.ExpiresAt)

	CheckString(t, l1.Customer.Id, l.Customer.Id)
	CheckString(t, l1.Customer.Name, l.Customer.Name)
	CheckString(t, l1.Customer.Email, l.Customer.Email)
	CheckString(t, l1.Customer.Company, l.Customer.Company)
	CheckString(t, l1.Customer.PhoneNumber, l.Customer.PhoneNumber)

	f1 := l1.Features

	CheckInt(t, *f1.Users, *f.Users)
	CheckBool(t, *f1.LDAP, *f.LDAP)
	CheckBool(t, *f1.MFA, *f.MFA)
	CheckBool(t, *f1.GoogleOAuth, *f.GoogleOAuth)
	CheckBool(t, *f1.Office365OAuth, *f.Office365OAuth)
	CheckBool(t, *f1.Compliance, *f.Compliance)
	CheckBool(t, *f1.Cluster, *f.Cluster)
	CheckBool(t, *f1.Metrics, *f.Metrics)
	CheckBool(t, *f1.CustomBrand, *f.CustomBrand)
	CheckBool(t, *f1.MHPNS, *f.MHPNS)
	CheckBool(t, *f1.SAML, *f.SAML)
	CheckBool(t, *f1.PasswordRequirements, *f.PasswordRequirements)
	CheckBool(t, *f1.ElasticSearch, *f.ElasticSearch)
	CheckBool(t, *f1.FutureFeatures, *f.FutureFeatures)

	invalid := `{"asdf`
	l2 := LicenseFromJson(strings.NewReader(invalid))
	if l2 != nil {
		t.Fatalf("Should have failed but didn't")
	}
}

func TestLicenseRecordIsValid(t *testing.T) {
	lr := LicenseRecord{
		CreateAt: GetMillis(),
		Bytes:    "asdfghjkl;",
	}

	if err := lr.IsValid(); err == nil {
		t.Fatalf("Should have been invalid")
	}

	lr.Id = NewId()
	lr.CreateAt = 0
	if err := lr.IsValid(); err == nil {
		t.Fatalf("Should have been invalid")
	}

	lr.CreateAt = GetMillis()
	lr.Bytes = ""
	if err := lr.IsValid(); err == nil {
		t.Fatalf("Should have been invalid")
	}

	lr.Bytes = strings.Repeat("0123456789", 1001)
	if err := lr.IsValid(); err == nil {
		t.Fatalf("Should have been invalid")
	}

	lr.Bytes = "ASDFGHJKL;"
	if err := lr.IsValid(); err != nil {
		t.Fatal(err)
	}
}

func TestLicenseRecordPreSave(t *testing.T) {
	lr := LicenseRecord{}
	lr.PreSave()

	if lr.CreateAt == 0 {
		t.Fatal("CreateAt should not be zero")
	}
}