github.com/ashishbhate/mattermost-server@v5.11.1+incompatible/utils/license_test.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package utils
     5  
     6  import (
     7  	"io/ioutil"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestValidateLicense(t *testing.T) {
    16  	b1 := []byte("junk")
    17  	if ok, _ := ValidateLicense(b1); ok {
    18  		t.Fatal("should have failed - bad license")
    19  	}
    20  
    21  	b2 := []byte("junkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunk")
    22  	if ok, _ := ValidateLicense(b2); ok {
    23  		t.Fatal("should have failed - bad license")
    24  	}
    25  }
    26  
    27  func TestGetLicenseFileLocation(t *testing.T) {
    28  	fileName := GetLicenseFileLocation("")
    29  	if len(fileName) == 0 {
    30  		t.Fatal("invalid default file name")
    31  	}
    32  
    33  	fileName = GetLicenseFileLocation("mattermost.mattermost-license")
    34  	if fileName != "mattermost.mattermost-license" {
    35  		t.Fatal("invalid file name")
    36  	}
    37  }
    38  
    39  func TestGetLicenseFileFromDisk(t *testing.T) {
    40  	t.Run("missing file", func(t *testing.T) {
    41  		fileBytes := GetLicenseFileFromDisk("thisfileshouldnotexist.mattermost-license")
    42  		assert.Empty(t, fileBytes, "invalid bytes")
    43  	})
    44  
    45  	t.Run("not a license file", func(t *testing.T) {
    46  		f, err := ioutil.TempFile("", "TestGetLicenseFileFromDisk")
    47  		require.NoError(t, err)
    48  		defer os.Remove(f.Name())
    49  		ioutil.WriteFile(f.Name(), []byte("not a license"), 0777)
    50  
    51  		fileBytes := GetLicenseFileFromDisk(f.Name())
    52  		require.NotEmpty(t, fileBytes, "should have read the file")
    53  
    54  		success, _ := ValidateLicense(fileBytes)
    55  		assert.False(t, success, "should have been an invalid file")
    56  	})
    57  }