[MM-42822] Check for and return HTTP 451 for all trial requests (#19873)

* Check for and return HTTP 451 for all trial requests

* Use constant in another spot too

* add a test

* Update test name
Этот коммит содержится в:
Nick Misasi
2022-04-04 09:48:14 -04:00
коммит произвёл GitHub
родитель 7b5ac343f0
Коммит 650ad3b3ae
3 изменённых файлов: 39 добавлений и 0 удалений

Просмотреть файл

@@ -255,6 +255,36 @@ func TestRequestTrialLicense(t *testing.T) {
CheckBadRequestStatus(t, resp)
})
t.Run("returns status 451 when it receives status 451", func(t *testing.T) {
nUsers := 1
license := model.NewTestLicense()
license.Features.Users = model.NewInt(nUsers)
licenseJSON, jsonErr := json.Marshal(license)
require.NoError(t, jsonErr)
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusUnavailableForLegalReasons)
}))
defer testServer.Close()
mockLicenseValidator := mocks2.LicenseValidatorIface{}
defer testutils.ResetLicenseValidator()
mockLicenseValidator.On("ValidateLicense", mock.Anything).Return(true, string(licenseJSON))
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)
require.Error(t, err)
require.Equal(t, resp.StatusCode, 451)
})
th.App.Srv().LicenseManager = nil
t.Run("trial license should fail if LicenseManager is nil", func(t *testing.T) {
resp, err := th.SystemAdminClient.RequestTrialLicense(1)

Просмотреть файл

@@ -340,6 +340,11 @@ func (s *Server) RequestTrialLicense(trialRequest *model.TrialLicenseRequest) *m
}
defer resp.Body.Close()
// CloudFlare sitting in front of the Customer Portal will block this request with a 451 response code in the event that the request originates from a country sanctioned by the U.S. Government.
if resp.StatusCode == http.StatusUnavailableForLegalReasons {
return model.NewAppError("RequestTrialLicense", "api.license.request_trial_license.embargoed", nil, "Request for trial license came from an embargoed country", http.StatusUnavailableForLegalReasons)
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return model.NewAppError("RequestTrialLicense", "api.license.request_trial_license.app_error", nil,
fmt.Sprintf("Unexpected HTTP status code %q returned by server", resp.Status), http.StatusInternalServerError)

Просмотреть файл

@@ -2005,6 +2005,10 @@
"id": "api.license.request_trial_license.app_error",
"translation": "Unable to get a trial license, please try again or contact with support@mattermost.com."
},
{
"id": "api.license.request_trial_license.embargoed",
"translation": "We were unable to process the request due to limitations for embargoed countries. [Learn more in our documentation](https://mattermost.com/pl/limitations-for-embargoed-countries), or reach out to legal@mattermost.com for questions around export limitations."
},
{
"id": "api.license.request_trial_license.fail_get_user_count.app_error",
"translation": "Unable to get a trial license, please try again or contact with support@mattermost.com. Cannot obtain the number of registered users."