github.com/rajatvaryani/mattermost-server@v5.11.1+incompatible/model/license_test.go (about) 1 // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package model 5 6 import ( 7 "strings" 8 "testing" 9 ) 10 11 func TestLicenseFeaturesToMap(t *testing.T) { 12 f := Features{} 13 f.SetDefaults() 14 15 m := f.ToMap() 16 17 CheckTrue(t, m["ldap"].(bool)) 18 CheckTrue(t, m["ldap_groups"].(bool)) 19 CheckTrue(t, m["mfa"].(bool)) 20 CheckTrue(t, m["google"].(bool)) 21 CheckTrue(t, m["office365"].(bool)) 22 CheckTrue(t, m["compliance"].(bool)) 23 CheckTrue(t, m["cluster"].(bool)) 24 CheckTrue(t, m["metrics"].(bool)) 25 CheckTrue(t, m["mhpns"].(bool)) 26 CheckTrue(t, m["saml"].(bool)) 27 CheckTrue(t, m["elastic_search"].(bool)) 28 CheckTrue(t, m["email_notification_contents"].(bool)) 29 CheckTrue(t, m["data_retention"].(bool)) 30 CheckTrue(t, m["message_export"].(bool)) 31 CheckTrue(t, m["custom_permissions_schemes"].(bool)) 32 CheckTrue(t, m["future"].(bool)) 33 } 34 35 func TestLicenseFeaturesSetDefaults(t *testing.T) { 36 f := Features{} 37 f.SetDefaults() 38 39 CheckInt(t, *f.Users, 0) 40 CheckTrue(t, *f.LDAP) 41 CheckTrue(t, *f.LDAPGroups) 42 CheckTrue(t, *f.MFA) 43 CheckTrue(t, *f.GoogleOAuth) 44 CheckTrue(t, *f.Office365OAuth) 45 CheckTrue(t, *f.Compliance) 46 CheckTrue(t, *f.Cluster) 47 CheckTrue(t, *f.Metrics) 48 CheckTrue(t, *f.MHPNS) 49 CheckTrue(t, *f.SAML) 50 CheckTrue(t, *f.Elasticsearch) 51 CheckTrue(t, *f.EmailNotificationContents) 52 CheckTrue(t, *f.DataRetention) 53 CheckTrue(t, *f.MessageExport) 54 CheckTrue(t, *f.CustomPermissionsSchemes) 55 CheckTrue(t, *f.FutureFeatures) 56 57 f = Features{} 58 f.SetDefaults() 59 60 *f.Users = 300 61 *f.FutureFeatures = false 62 *f.LDAP = true 63 *f.LDAPGroups = true 64 *f.MFA = true 65 *f.GoogleOAuth = true 66 *f.Office365OAuth = true 67 *f.Compliance = true 68 *f.Cluster = true 69 *f.Metrics = true 70 *f.MHPNS = true 71 *f.SAML = true 72 *f.Elasticsearch = true 73 *f.DataRetention = true 74 *f.MessageExport = true 75 *f.CustomPermissionsSchemes = true 76 *f.EmailNotificationContents = true 77 78 f.SetDefaults() 79 80 CheckInt(t, *f.Users, 300) 81 CheckTrue(t, *f.LDAP) 82 CheckTrue(t, *f.LDAPGroups) 83 CheckTrue(t, *f.MFA) 84 CheckTrue(t, *f.GoogleOAuth) 85 CheckTrue(t, *f.Office365OAuth) 86 CheckTrue(t, *f.Compliance) 87 CheckTrue(t, *f.Cluster) 88 CheckTrue(t, *f.Metrics) 89 CheckTrue(t, *f.MHPNS) 90 CheckTrue(t, *f.SAML) 91 CheckTrue(t, *f.Elasticsearch) 92 CheckTrue(t, *f.EmailNotificationContents) 93 CheckTrue(t, *f.DataRetention) 94 CheckTrue(t, *f.MessageExport) 95 CheckTrue(t, *f.CustomPermissionsSchemes) 96 CheckFalse(t, *f.FutureFeatures) 97 } 98 99 func TestLicenseIsExpired(t *testing.T) { 100 l1 := License{} 101 l1.ExpiresAt = GetMillis() - 1000 102 if !l1.IsExpired() { 103 t.Fatal("license should be expired") 104 } 105 106 l1.ExpiresAt = GetMillis() + 10000 107 if l1.IsExpired() { 108 t.Fatal("license should not be expired") 109 } 110 } 111 112 func TestLicenseIsStarted(t *testing.T) { 113 l1 := License{} 114 l1.StartsAt = GetMillis() - 1000 115 if !l1.IsStarted() { 116 t.Fatal("license should be started") 117 } 118 119 l1.StartsAt = GetMillis() + 10000 120 if l1.IsStarted() { 121 t.Fatal("license should not be started") 122 } 123 } 124 125 func TestLicenseToFromJson(t *testing.T) { 126 f := Features{} 127 f.SetDefaults() 128 129 l := License{ 130 Id: NewId(), 131 IssuedAt: GetMillis(), 132 StartsAt: GetMillis(), 133 ExpiresAt: GetMillis(), 134 Customer: &Customer{ 135 Id: NewId(), 136 Name: NewId(), 137 Email: NewId(), 138 Company: NewId(), 139 PhoneNumber: NewId(), 140 }, 141 Features: &f, 142 } 143 144 j := l.ToJson() 145 146 l1 := LicenseFromJson(strings.NewReader(j)) 147 if l1 == nil { 148 t.Fatalf("Decoding failed but should have passed.") 149 } 150 151 CheckString(t, l1.Id, l.Id) 152 CheckInt64(t, l1.IssuedAt, l.IssuedAt) 153 CheckInt64(t, l1.StartsAt, l.StartsAt) 154 CheckInt64(t, l1.ExpiresAt, l.ExpiresAt) 155 156 CheckString(t, l1.Customer.Id, l.Customer.Id) 157 CheckString(t, l1.Customer.Name, l.Customer.Name) 158 CheckString(t, l1.Customer.Email, l.Customer.Email) 159 CheckString(t, l1.Customer.Company, l.Customer.Company) 160 CheckString(t, l1.Customer.PhoneNumber, l.Customer.PhoneNumber) 161 162 f1 := l1.Features 163 164 CheckInt(t, *f1.Users, *f.Users) 165 CheckBool(t, *f1.LDAP, *f.LDAP) 166 CheckBool(t, *f1.LDAPGroups, *f.LDAPGroups) 167 CheckBool(t, *f1.MFA, *f.MFA) 168 CheckBool(t, *f1.GoogleOAuth, *f.GoogleOAuth) 169 CheckBool(t, *f1.Office365OAuth, *f.Office365OAuth) 170 CheckBool(t, *f1.Compliance, *f.Compliance) 171 CheckBool(t, *f1.Cluster, *f.Cluster) 172 CheckBool(t, *f1.Metrics, *f.Metrics) 173 CheckBool(t, *f1.MHPNS, *f.MHPNS) 174 CheckBool(t, *f1.SAML, *f.SAML) 175 CheckBool(t, *f1.Elasticsearch, *f.Elasticsearch) 176 CheckBool(t, *f1.DataRetention, *f.DataRetention) 177 CheckBool(t, *f1.MessageExport, *f.MessageExport) 178 CheckBool(t, *f1.CustomPermissionsSchemes, *f.CustomPermissionsSchemes) 179 CheckBool(t, *f1.FutureFeatures, *f.FutureFeatures) 180 181 invalid := `{"asdf` 182 l2 := LicenseFromJson(strings.NewReader(invalid)) 183 if l2 != nil { 184 t.Fatalf("Should have failed but didn't") 185 } 186 } 187 188 func TestLicenseRecordIsValid(t *testing.T) { 189 lr := LicenseRecord{ 190 CreateAt: GetMillis(), 191 Bytes: "asdfghjkl;", 192 } 193 194 if err := lr.IsValid(); err == nil { 195 t.Fatalf("Should have been invalid") 196 } 197 198 lr.Id = NewId() 199 lr.CreateAt = 0 200 if err := lr.IsValid(); err == nil { 201 t.Fatalf("Should have been invalid") 202 } 203 204 lr.CreateAt = GetMillis() 205 lr.Bytes = "" 206 if err := lr.IsValid(); err == nil { 207 t.Fatalf("Should have been invalid") 208 } 209 210 lr.Bytes = strings.Repeat("0123456789", 1001) 211 if err := lr.IsValid(); err == nil { 212 t.Fatalf("Should have been invalid") 213 } 214 215 lr.Bytes = "ASDFGHJKL;" 216 if err := lr.IsValid(); err != nil { 217 t.Fatal(err) 218 } 219 } 220 221 func TestLicenseRecordPreSave(t *testing.T) { 222 lr := LicenseRecord{} 223 lr.PreSave() 224 225 if lr.CreateAt == 0 { 226 t.Fatal("CreateAt should not be zero") 227 } 228 }