github.com/mad-app/mattermost-server@v5.11.1+incompatible/api4/license_test.go (about)

     1  package api4
     2  
     3  import (
     4  	"net/http"
     5  	"testing"
     6  
     7  	"github.com/mattermost/mattermost-server/model"
     8  )
     9  
    10  func TestGetOldClientLicense(t *testing.T) {
    11  	th := Setup().InitBasic()
    12  	defer th.TearDown()
    13  	Client := th.Client
    14  
    15  	license, resp := Client.GetOldClientLicense("")
    16  	CheckNoError(t, resp)
    17  
    18  	if len(license["IsLicensed"]) == 0 {
    19  		t.Fatal("license not returned correctly")
    20  	}
    21  
    22  	Client.Logout()
    23  
    24  	_, resp = Client.GetOldClientLicense("")
    25  	CheckNoError(t, resp)
    26  
    27  	if _, err := Client.DoApiGet("/license/client", ""); err == nil || err.StatusCode != http.StatusNotImplemented {
    28  		t.Fatal("should have errored with 501")
    29  	}
    30  
    31  	if _, err := Client.DoApiGet("/license/client?format=junk", ""); err == nil || err.StatusCode != http.StatusBadRequest {
    32  		t.Fatal("should have errored with 400")
    33  	}
    34  
    35  	license, resp = th.SystemAdminClient.GetOldClientLicense("")
    36  	CheckNoError(t, resp)
    37  
    38  	if len(license["IsLicensed"]) == 0 {
    39  		t.Fatal("license not returned correctly")
    40  	}
    41  }
    42  
    43  func TestUploadLicenseFile(t *testing.T) {
    44  	th := Setup().InitBasic()
    45  	defer th.TearDown()
    46  	Client := th.Client
    47  
    48  	t.Run("as system user", func(t *testing.T) {
    49  		ok, resp := Client.UploadLicenseFile([]byte{})
    50  		CheckForbiddenStatus(t, resp)
    51  		if ok {
    52  			t.Fatal("should fail")
    53  		}
    54  	})
    55  
    56  	t.Run("as system admin user", func(t *testing.T) {
    57  		ok, resp := th.SystemAdminClient.UploadLicenseFile([]byte{})
    58  		CheckBadRequestStatus(t, resp)
    59  		if ok {
    60  			t.Fatal("should fail")
    61  		}
    62  	})
    63  
    64  	t.Run("as restricted system admin user", func(t *testing.T) {
    65  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ExperimentalSettings.RestrictSystemAdmin = true })
    66  
    67  		ok, resp := th.SystemAdminClient.UploadLicenseFile([]byte{})
    68  		CheckForbiddenStatus(t, resp)
    69  		if ok {
    70  			t.Fatal("should fail")
    71  		}
    72  	})
    73  }
    74  
    75  func TestRemoveLicenseFile(t *testing.T) {
    76  	th := Setup().InitBasic()
    77  	defer th.TearDown()
    78  	Client := th.Client
    79  
    80  	t.Run("as system user", func(t *testing.T) {
    81  		ok, resp := Client.RemoveLicenseFile()
    82  		CheckForbiddenStatus(t, resp)
    83  		if ok {
    84  			t.Fatal("should fail")
    85  		}
    86  	})
    87  
    88  	t.Run("as system admin user", func(t *testing.T) {
    89  		ok, resp := th.SystemAdminClient.RemoveLicenseFile()
    90  		CheckNoError(t, resp)
    91  		if !ok {
    92  			t.Fatal("should pass")
    93  		}
    94  	})
    95  
    96  	t.Run("as restricted system admin user", func(t *testing.T) {
    97  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ExperimentalSettings.RestrictSystemAdmin = true })
    98  
    99  		ok, resp := th.SystemAdminClient.RemoveLicenseFile()
   100  		CheckForbiddenStatus(t, resp)
   101  		if ok {
   102  			t.Fatal("should fail")
   103  		}
   104  	})
   105  }