github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/api4/license_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package api4 5 6 import ( 7 "encoding/json" 8 "net/http" 9 "testing" 10 "time" 11 12 "github.com/masterhung0112/hk_server/v5/einterfaces/mocks" 13 "github.com/masterhung0112/hk_server/v5/utils" 14 mocks2 "github.com/masterhung0112/hk_server/v5/utils/mocks" 15 "github.com/masterhung0112/hk_server/v5/utils/testutils" 16 "github.com/stretchr/testify/mock" 17 "github.com/stretchr/testify/require" 18 19 "github.com/masterhung0112/hk_server/v5/model" 20 ) 21 22 func TestGetOldClientLicense(t *testing.T) { 23 th := Setup(t) 24 defer th.TearDown() 25 Client := th.Client 26 27 license, resp := Client.GetOldClientLicense("") 28 CheckNoError(t, resp) 29 30 require.NotEqual(t, license["IsLicensed"], "", "license not returned correctly") 31 32 Client.Logout() 33 34 _, resp = Client.GetOldClientLicense("") 35 CheckNoError(t, resp) 36 37 _, err := Client.DoApiGet("/license/client", "") 38 require.NotNil(t, err, "get /license/client did not return an error") 39 require.Equal(t, err.StatusCode, http.StatusNotImplemented, 40 "expected 501 Not Implemented") 41 42 _, err = Client.DoApiGet("/license/client?format=junk", "") 43 require.NotNil(t, err, "get /license/client?format=junk did not return an error") 44 require.Equal(t, err.StatusCode, http.StatusBadRequest, 45 "expected 400 Bad Request") 46 47 license, resp = th.SystemAdminClient.GetOldClientLicense("") 48 CheckNoError(t, resp) 49 50 require.NotEmpty(t, license["IsLicensed"], "license not returned correctly") 51 } 52 53 func TestUploadLicenseFile(t *testing.T) { 54 th := Setup(t) 55 defer th.TearDown() 56 Client := th.Client 57 LocalClient := th.LocalClient 58 59 t.Run("as system user", func(t *testing.T) { 60 ok, resp := Client.UploadLicenseFile([]byte{}) 61 CheckForbiddenStatus(t, resp) 62 require.False(t, ok) 63 }) 64 65 th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) { 66 ok, resp := c.UploadLicenseFile([]byte{}) 67 CheckBadRequestStatus(t, resp) 68 require.False(t, ok) 69 }, "as system admin user") 70 71 t.Run("as restricted system admin user", func(t *testing.T) { 72 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ExperimentalSettings.RestrictSystemAdmin = true }) 73 74 ok, resp := th.SystemAdminClient.UploadLicenseFile([]byte{}) 75 CheckForbiddenStatus(t, resp) 76 require.False(t, ok) 77 }) 78 79 t.Run("restricted admin setting not honoured through local client", func(t *testing.T) { 80 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ExperimentalSettings.RestrictSystemAdmin = true }) 81 ok, resp := LocalClient.UploadLicenseFile([]byte{}) 82 CheckBadRequestStatus(t, resp) 83 require.False(t, ok) 84 }) 85 t.Run("server has already gone through trial", func(t *testing.T) { 86 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ExperimentalSettings.RestrictSystemAdmin = false }) 87 mockLicenseValidator := mocks2.LicenseValidatorIface{} 88 defer testutils.ResetLicenseValidator() 89 90 //startTimestamp, err := time.Parse("2 Jan 2006 3:04 pm", "1 Jan 2021 12:00 am") 91 //require.Nil(t, err) 92 93 userCount := 100 94 mills := model.GetMillis() 95 96 license := model.License{ 97 Id: "AAAAAAAAAAAAAAAAAAAAAAAAAA", 98 Features: &model.Features{ 99 Users: &userCount, 100 }, 101 Customer: &model.Customer{ 102 Name: "Test", 103 }, 104 StartsAt: mills + 100, 105 ExpiresAt: mills + 100 + (30*(time.Hour*24) + (time.Hour * 8)).Milliseconds(), 106 } 107 108 mockLicenseValidator.On("LicenseFromBytes", mock.Anything).Return(&license, nil).Once() 109 licenseBytes, _ := json.Marshal(license) 110 licenseStr := string(licenseBytes) 111 112 mockLicenseValidator.On("ValidateLicense", mock.Anything).Return(true, licenseStr) 113 utils.LicenseValidator = &mockLicenseValidator 114 115 licenseManagerMock := &mocks.LicenseInterface{} 116 licenseManagerMock.On("CanStartTrial").Return(false, nil).Once() 117 th.App.Srv().LicenseManager = licenseManagerMock 118 119 ok, resp := th.SystemAdminClient.UploadLicenseFile([]byte("sadasdasdasdasdasdsa")) 120 require.False(t, ok) 121 require.Equal(t, http.StatusBadRequest, resp.StatusCode) 122 require.Equal(t, "api.license.request-trial.can-start-trial.not-allowed", resp.Error.Id) 123 }) 124 125 t.Run("allow uploading sanctioned trials even if server already gone through trial", func(t *testing.T) { 126 mockLicenseValidator := mocks2.LicenseValidatorIface{} 127 defer testutils.ResetLicenseValidator() 128 129 userCount := 100 130 mills := model.GetMillis() 131 132 license := model.License{ 133 Id: "PPPPPPPPPPPPPPPPPPPPPPPPPP", 134 Features: &model.Features{ 135 Users: &userCount, 136 }, 137 Customer: &model.Customer{ 138 Name: "Test", 139 }, 140 IsTrial: true, 141 StartsAt: mills + 100, 142 ExpiresAt: mills + 100 + (29*(time.Hour*24) + (time.Hour * 8)).Milliseconds(), 143 } 144 145 mockLicenseValidator.On("LicenseFromBytes", mock.Anything).Return(&license, nil).Once() 146 147 licenseBytes, _ := json.Marshal(license) 148 licenseStr := string(licenseBytes) 149 150 mockLicenseValidator.On("ValidateLicense", mock.Anything).Return(true, licenseStr) 151 152 utils.LicenseValidator = &mockLicenseValidator 153 154 licenseManagerMock := &mocks.LicenseInterface{} 155 licenseManagerMock.On("CanStartTrial").Return(false, nil).Once() 156 th.App.Srv().LicenseManager = licenseManagerMock 157 158 ok, resp := th.SystemAdminClient.UploadLicenseFile([]byte("sadasdasdasdasdasdsa")) 159 require.False(t, ok) 160 require.Equal(t, http.StatusOK, resp.StatusCode) 161 require.Nil(t, resp.Error) 162 }) 163 } 164 165 func TestRemoveLicenseFile(t *testing.T) { 166 th := Setup(t) 167 defer th.TearDown() 168 Client := th.Client 169 LocalClient := th.LocalClient 170 171 t.Run("as system user", func(t *testing.T) { 172 ok, resp := Client.RemoveLicenseFile() 173 CheckForbiddenStatus(t, resp) 174 require.False(t, ok) 175 }) 176 177 th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) { 178 ok, resp := c.RemoveLicenseFile() 179 CheckNoError(t, resp) 180 require.True(t, ok) 181 }, "as system admin user") 182 183 t.Run("as restricted system admin user", func(t *testing.T) { 184 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ExperimentalSettings.RestrictSystemAdmin = true }) 185 186 ok, resp := th.SystemAdminClient.RemoveLicenseFile() 187 CheckForbiddenStatus(t, resp) 188 require.False(t, ok) 189 }) 190 191 t.Run("restricted admin setting not honoured through local client", func(t *testing.T) { 192 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ExperimentalSettings.RestrictSystemAdmin = true }) 193 194 ok, resp := LocalClient.RemoveLicenseFile() 195 CheckNoError(t, resp) 196 require.True(t, ok) 197 }) 198 } 199 200 func TestRequestTrialLicense(t *testing.T) { 201 th := Setup(t) 202 defer th.TearDown() 203 licenseManagerMock := &mocks.LicenseInterface{} 204 licenseManagerMock.On("CanStartTrial").Return(true, nil) 205 th.App.Srv().LicenseManager = licenseManagerMock 206 207 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SiteURL = "http://localhost:8065/" }) 208 209 t.Run("permission denied", func(t *testing.T) { 210 ok, resp := th.Client.RequestTrialLicense(1000) 211 CheckForbiddenStatus(t, resp) 212 require.False(t, ok) 213 }) 214 215 t.Run("blank site url", func(t *testing.T) { 216 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SiteURL = "" }) 217 defer th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SiteURL = "http://localhost:8065/" }) 218 ok, resp := th.SystemAdminClient.RequestTrialLicense(1000) 219 CheckBadRequestStatus(t, resp) 220 require.Equal(t, "api.license.request_trial_license.no-site-url.app_error", resp.Error.Id) 221 require.False(t, ok) 222 }) 223 224 t.Run("trial license user count less than current users", func(t *testing.T) { 225 ok, resp := th.SystemAdminClient.RequestTrialLicense(1) 226 CheckBadRequestStatus(t, resp) 227 require.Equal(t, "api.license.add_license.unique_users.app_error", resp.Error.Id) 228 require.False(t, ok) 229 }) 230 }