github.com/spreadshirt/mattermost-server@v5.3.2-0.20180927191755-a257d501df3d+incompatible/app/license_test.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/mattermost/mattermost-server/model"
    10  )
    11  
    12  func TestLoadLicense(t *testing.T) {
    13  	th := Setup()
    14  	defer th.TearDown()
    15  
    16  	th.App.LoadLicense()
    17  	if th.App.License() != nil {
    18  		t.Fatal("shouldn't have a valid license")
    19  	}
    20  }
    21  
    22  func TestSaveLicense(t *testing.T) {
    23  	th := Setup()
    24  	defer th.TearDown()
    25  
    26  	b1 := []byte("junk")
    27  
    28  	if _, err := th.App.SaveLicense(b1); err == nil {
    29  		t.Fatal("shouldn't have saved license")
    30  	}
    31  }
    32  
    33  func TestRemoveLicense(t *testing.T) {
    34  	th := Setup()
    35  	defer th.TearDown()
    36  
    37  	if err := th.App.RemoveLicense(); err != nil {
    38  		t.Fatal("should have removed license")
    39  	}
    40  }
    41  
    42  func TestSetLicense(t *testing.T) {
    43  	th := Setup()
    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  	if ok := th.App.SetLicense(l1); !ok {
    52  		t.Fatal("license should have worked")
    53  	}
    54  
    55  	l2 := &model.License{}
    56  	l2.Features = &model.Features{}
    57  	l2.Customer = &model.Customer{}
    58  	l2.StartsAt = model.GetMillis() - 1000
    59  	l2.ExpiresAt = model.GetMillis() - 100
    60  	if ok := th.App.SetLicense(l2); ok {
    61  		t.Fatal("license should have failed")
    62  	}
    63  
    64  	l3 := &model.License{}
    65  	l3.Features = &model.Features{}
    66  	l3.Customer = &model.Customer{}
    67  	l3.StartsAt = model.GetMillis() + 10000
    68  	l3.ExpiresAt = model.GetMillis() + 100000
    69  	if ok := th.App.SetLicense(l3); !ok {
    70  		t.Fatal("license should have passed")
    71  	}
    72  }
    73  
    74  func TestClientLicenseEtag(t *testing.T) {
    75  	th := Setup()
    76  	defer th.TearDown()
    77  
    78  	etag1 := th.App.GetClientLicenseEtag(false)
    79  
    80  	th.App.SetClientLicense(map[string]string{"SomeFeature": "true", "IsLicensed": "true"})
    81  
    82  	etag2 := th.App.GetClientLicenseEtag(false)
    83  	if etag1 == etag2 {
    84  		t.Fatal("etags should not match")
    85  	}
    86  
    87  	th.App.SetClientLicense(map[string]string{"SomeFeature": "true", "IsLicensed": "false"})
    88  
    89  	etag3 := th.App.GetClientLicenseEtag(false)
    90  	if etag2 == etag3 {
    91  		t.Fatal("etags should not match")
    92  	}
    93  }
    94  
    95  func TestGetSanitizedClientLicense(t *testing.T) {
    96  	th := Setup()
    97  	defer th.TearDown()
    98  
    99  	l1 := &model.License{}
   100  	l1.Features = &model.Features{}
   101  	l1.Customer = &model.Customer{}
   102  	l1.Customer.Name = "TestName"
   103  	l1.StartsAt = model.GetMillis() - 1000
   104  	l1.ExpiresAt = model.GetMillis() + 100000
   105  	th.App.SetLicense(l1)
   106  
   107  	m := th.App.GetSanitizedClientLicense()
   108  
   109  	if _, ok := m["Name"]; ok {
   110  		t.Fatal("should have been sanatized")
   111  	}
   112  }