diff --git a/api4/license_test.go b/api4/license_test.go index 62c01e9847..46d710c872 100644 --- a/api4/license_test.go +++ b/api4/license_test.go @@ -6,17 +6,19 @@ package api4 import ( "encoding/json" "net/http" + "net/http/httptest" "testing" "time" + "github.com/mattermost/mattermost-server/v6/app" "github.com/mattermost/mattermost-server/v6/einterfaces/mocks" + "github.com/mattermost/mattermost-server/v6/model" "github.com/mattermost/mattermost-server/v6/utils" mocks2 "github.com/mattermost/mattermost-server/v6/utils/mocks" "github.com/mattermost/mattermost-server/v6/utils/testutils" + "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" - - "github.com/mattermost/mattermost-server/v6/model" ) func TestGetOldClientLicense(t *testing.T) { @@ -219,8 +221,35 @@ func TestRequestTrialLicense(t *testing.T) { }) t.Run("trial license user count less than current users", func(t *testing.T) { - t.Skip("MM-36695") - resp, err := th.SystemAdminClient.RequestTrialLicense(1) + nUsers := 1 + license := model.NewTestLicense() + license.Features.Users = model.NewInt(nUsers) + licenseStr := license.ToJson() + testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { + res.WriteHeader(http.StatusOK) + response := map[string]string{ + "license": licenseStr, + } + err := json.NewEncoder(res).Encode(response) + require.NoError(t, err) + })) + defer testServer.Close() + + mockLicenseValidator := mocks2.LicenseValidatorIface{} + defer testutils.ResetLicenseValidator() + + mockLicenseValidator.On("ValidateLicense", mock.Anything).Return(true, licenseStr) + utils.LicenseValidator = &mockLicenseValidator + licenseManagerMock := &mocks.LicenseInterface{} + licenseManagerMock.On("CanStartTrial").Return(true, nil).Once() + th.App.Srv().LicenseManager = licenseManagerMock + + defer func(requestTrialURL string) { + app.RequestTrialURL = requestTrialURL + }(app.RequestTrialURL) + app.RequestTrialURL = testServer.URL + + resp, err := th.SystemAdminClient.RequestTrialLicense(nUsers) CheckErrorID(t, err, "api.license.add_license.unique_users.app_error") CheckBadRequestStatus(t, resp) }) diff --git a/app/license.go b/app/license.go index 0dea661fcb..0e7424b1f7 100644 --- a/app/license.go +++ b/app/license.go @@ -20,12 +20,13 @@ import ( ) const ( - requestTrialURL = "https://customers.mattermost.com/api/v1/trials" LicenseEnv = "MM_LICENSE" LicenseRenewalURL = "https://customers.mattermost.com/subscribe/renew" JWTDefaultTokenExpiration = 7 * 24 * time.Hour // 7 days of expiration ) +var RequestTrialURL = "https://customers.mattermost.com/api/v1/trials" + // JWTClaims custom JWT claims with the needed information for the // renewal process type JWTClaims struct { @@ -253,7 +254,7 @@ func (s *Server) GetSanitizedClientLicense() map[string]string { // RequestTrialLicense request a trial license from the mattermost official license server func (s *Server) RequestTrialLicense(trialRequest *model.TrialLicenseRequest) *model.AppError { - resp, err := http.Post(requestTrialURL, "application/json", bytes.NewBuffer([]byte(trialRequest.ToJson()))) + resp, err := http.Post(RequestTrialURL, "application/json", bytes.NewBuffer([]byte(trialRequest.ToJson()))) if err != nil { return model.NewAppError("RequestTrialLicense", "api.license.request_trial_license.app_error", nil, err.Error(), http.StatusBadRequest) }