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