summaryrefslogtreecommitdiffstats
path: root/model/license_test.go
blob: b4547343268a8d3214dfbe85480cb1081ed61f1e (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
215
216
217
218
219
220
221
222
223
// 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["mhpns"].(bool))
	CheckTrue(t, m["saml"].(bool))
	CheckTrue(t, m["elastic_search"].(bool))
	CheckTrue(t, m["email_notification_contents"].(bool))
	CheckTrue(t, m["data_retention"].(bool))
	CheckTrue(t, m["message_export"].(bool))
	CheckTrue(t, m["custom_permissions_schemes"].(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.MHPNS)
	CheckTrue(t, *f.SAML)
	CheckTrue(t, *f.Elasticsearch)
	CheckTrue(t, *f.EmailNotificationContents)
	CheckTrue(t, *f.DataRetention)
	CheckTrue(t, *f.MessageExport)
	CheckTrue(t, *f.CustomPermissionsSchemes)
	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.MHPNS = true
	*f.SAML = true
	*f.Elasticsearch = true
	*f.DataRetention = true
	*f.MessageExport = true
	*f.CustomPermissionsSchemes = true
	*f.EmailNotificationContents = 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.MHPNS)
	CheckTrue(t, *f.SAML)
	CheckTrue(t, *f.Elasticsearch)
	CheckTrue(t, *f.EmailNotificationContents)
	CheckTrue(t, *f.DataRetention)
	CheckTrue(t, *f.MessageExport)
	CheckTrue(t, *f.CustomPermissionsSchemes)
	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.MHPNS, *f.MHPNS)
	CheckBool(t, *f1.SAML, *f.SAML)
	CheckBool(t, *f1.Elasticsearch, *f.Elasticsearch)
	CheckBool(t, *f1.DataRetention, *f.DataRetention)
	CheckBool(t, *f1.MessageExport, *f.MessageExport)
	CheckBool(t, *f1.CustomPermissionsSchemes, *f.CustomPermissionsSchemes)
	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")
	}
}