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