github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/app/license_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/mattermost/mattermost-server/v5/model"
    14  )
    15  
    16  func TestLoadLicense(t *testing.T) {
    17  	th := Setup(t)
    18  	defer th.TearDown()
    19  
    20  	th.App.Srv().LoadLicense()
    21  	require.Nil(t, th.App.Srv().License(), "shouldn't have a valid license")
    22  }
    23  
    24  func TestSaveLicense(t *testing.T) {
    25  	th := Setup(t)
    26  	defer th.TearDown()
    27  
    28  	b1 := []byte("junk")
    29  
    30  	_, err := th.App.Srv().SaveLicense(b1)
    31  	require.NotNil(t, err, "shouldn't have saved license")
    32  }
    33  
    34  func TestRemoveLicense(t *testing.T) {
    35  	th := Setup(t)
    36  	defer th.TearDown()
    37  
    38  	err := th.App.Srv().RemoveLicense()
    39  	require.Nil(t, err, "should have removed license")
    40  }
    41  
    42  func TestSetLicense(t *testing.T) {
    43  	th := Setup(t)
    44  	defer th.TearDown()
    45  
    46  	l1 := &model.License{}
    47  	l1.Features = &model.Features{}
    48  	l1.Customer = &model.Customer{}
    49  	l1.StartsAt = model.GetMillis() - 1000
    50  	l1.ExpiresAt = model.GetMillis() + 100000
    51  	ok := th.App.Srv().SetLicense(l1)
    52  	require.True(t, ok, "license should have worked")
    53  
    54  	l3 := &model.License{}
    55  	l3.Features = &model.Features{}
    56  	l3.Customer = &model.Customer{}
    57  	l3.StartsAt = model.GetMillis() + 10000
    58  	l3.ExpiresAt = model.GetMillis() + 100000
    59  	ok = th.App.Srv().SetLicense(l3)
    60  	require.True(t, ok, "license should have passed")
    61  }
    62  
    63  func TestGetSanitizedClientLicense(t *testing.T) {
    64  	th := Setup(t)
    65  	defer th.TearDown()
    66  
    67  	setLicense(th, nil)
    68  
    69  	m := th.App.Srv().GetSanitizedClientLicense()
    70  
    71  	_, ok := m["Name"]
    72  	assert.False(t, ok)
    73  	_, ok = m["SkuName"]
    74  	assert.False(t, ok)
    75  	_, ok = m["SkuShortName"]
    76  	assert.False(t, ok)
    77  }
    78  
    79  func TestGenerateRenewalToken(t *testing.T) {
    80  	th := Setup(t)
    81  	defer th.TearDown()
    82  
    83  	t.Run("renewal token generated correctly", func(t *testing.T) {
    84  		setLicense(th, nil)
    85  		token, appErr := th.App.Srv().GenerateRenewalToken(JWTDefaultTokenExpiration)
    86  		require.Nil(t, appErr)
    87  		require.NotEmpty(t, token)
    88  		defer th.App.Srv().Store.System().PermanentDeleteByName(model.SYSTEM_LICENSE_RENEWAL_TOKEN)
    89  
    90  		customerEmail := th.App.Srv().License().Customer.Email
    91  		validToken, err := th.App.Srv().renewalTokenValid(token, customerEmail)
    92  		require.NoError(t, err)
    93  		require.True(t, validToken)
    94  	})
    95  
    96  	t.Run("only one token should be active", func(t *testing.T) {
    97  		setLicense(th, nil)
    98  		token, appErr := th.App.Srv().GenerateRenewalToken(JWTDefaultTokenExpiration)
    99  		require.Nil(t, appErr)
   100  		require.NotEmpty(t, token)
   101  		defer th.App.Srv().Store.System().PermanentDeleteByName(model.SYSTEM_LICENSE_RENEWAL_TOKEN)
   102  
   103  		newToken, appErr := th.App.Srv().GenerateRenewalToken(JWTDefaultTokenExpiration)
   104  		require.Nil(t, appErr)
   105  		require.Equal(t, token, newToken)
   106  	})
   107  
   108  	t.Run("return error if there is no active license", func(t *testing.T) {
   109  		th.App.Srv().SetLicense(nil)
   110  		_, appErr := th.App.Srv().GenerateRenewalToken(JWTDefaultTokenExpiration)
   111  		require.NotNil(t, appErr)
   112  	})
   113  
   114  	t.Run("return another token if the license owner change", func(t *testing.T) {
   115  		setLicense(th, nil)
   116  		token, appErr := th.App.Srv().GenerateRenewalToken(JWTDefaultTokenExpiration)
   117  		require.Nil(t, appErr)
   118  		require.NotEmpty(t, token)
   119  		defer th.App.Srv().Store.System().PermanentDeleteByName(model.SYSTEM_LICENSE_RENEWAL_TOKEN)
   120  		setLicense(th, &model.Customer{
   121  			Name:  "another customer",
   122  			Email: "another@example.com",
   123  		})
   124  		newToken, appErr := th.App.Srv().GenerateRenewalToken(JWTDefaultTokenExpiration)
   125  		require.Nil(t, appErr)
   126  		require.NotEqual(t, token, newToken)
   127  	})
   128  
   129  	t.Run("return another token if the active one has expired", func(t *testing.T) {
   130  		setLicense(th, nil)
   131  		token, appErr := th.App.Srv().GenerateRenewalToken(1 * time.Second)
   132  		require.Nil(t, appErr)
   133  		require.NotEmpty(t, token)
   134  		defer th.App.Srv().Store.System().PermanentDeleteByName(model.SYSTEM_LICENSE_RENEWAL_TOKEN)
   135  		// The small time unit for expiration we're using is seconds
   136  		time.Sleep(1 * time.Second)
   137  		newToken, appErr := th.App.Srv().GenerateRenewalToken(JWTDefaultTokenExpiration)
   138  		require.Nil(t, appErr)
   139  		require.NotEqual(t, token, newToken)
   140  	})
   141  
   142  }
   143  
   144  func setLicense(th *TestHelper, customer *model.Customer) {
   145  	l1 := &model.License{}
   146  	l1.Features = &model.Features{}
   147  	if customer != nil {
   148  		l1.Customer = customer
   149  	} else {
   150  		l1.Customer = &model.Customer{}
   151  		l1.Customer.Name = "TestName"
   152  		l1.Customer.Email = "test@example.com"
   153  	}
   154  	l1.SkuName = "SKU NAME"
   155  	l1.SkuShortName = "SKU SHORT NAME"
   156  	l1.StartsAt = model.GetMillis() - 1000
   157  	l1.ExpiresAt = model.GetMillis() + 100000
   158  	th.App.Srv().SetLicense(l1)
   159  }