github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/model/license_test.go (about)

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