Mm 47648 check license expandable (#21770)

* [MM-47648] Handler for check if a subscription is expandable

* Mock cloud regenerate

* PR Webapp test purpose only

* [MM-47648] Refactor and tests for expand stats for a subscription

* Fix typos

* style fix

* has to be revert

* CWS URL to Spinwick

* new spinwick url

* target portal test

* Generate token instead of send licenseId

* Rename

* Rename methods

* Revert public key and cws url

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Said Atrahouch
2022-12-27 12:08:11 +01:00
коммит произвёл GitHub
родитель dc533e59b8
Коммит b9501bc535
6 изменённых файлов: 122 добавлений и 0 удалений

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

@@ -40,6 +40,7 @@ func (api *API) InitCloud() {
api.BaseRoutes.Cloud.Handle("/subscription", api.APISessionRequired(getSubscription)).Methods("GET")
api.BaseRoutes.Cloud.Handle("/subscription/invoices", api.APISessionRequired(getInvoicesForSubscription)).Methods("GET")
api.BaseRoutes.Cloud.Handle("/subscription/invoices/{invoice_id:[A-Za-z0-9]+}/pdf", api.APISessionRequired(getSubscriptionInvoicePDF)).Methods("GET")
api.BaseRoutes.Cloud.Handle("/subscription/expand", api.APISessionRequired(GetLicenseExpandStatus)).Methods("GET")
api.BaseRoutes.Cloud.Handle("/subscription", api.APISessionRequired(changeSubscription)).Methods("PUT")
// GET /api/v4/cloud/request-trial
@@ -413,6 +414,34 @@ func getCloudCustomer(c *Context, w http.ResponseWriter, r *http.Request) {
w.Write(json)
}
func GetLicenseExpandStatus(c *Context, w http.ResponseWriter, r *http.Request) {
if !c.App.SessionHasPermissionTo(*c.AppContext.Session(), model.PermissionManageLicenseInformation) {
c.SetPermissionError(model.PermissionManageLicenseInformation)
return
}
_, token, err := c.App.Srv().GenerateLicenseRenewalLink()
if err != nil {
c.Err = err
return
}
res, cloudErr := c.App.Cloud().GetLicenseExpandStatus(c.AppContext.Session().UserId, token)
if cloudErr != nil {
c.Err = model.NewAppError("Api4.GetLicenseExpandStatusForSubscription", "api.cloud.request_error", nil, "", http.StatusInternalServerError).Wrap(cloudErr)
return
}
json, jsonErr := json.Marshal(res)
if jsonErr != nil {
c.Err = model.NewAppError("Api4.GetLicenseExpandStatusForSubscription", "api.cloud.app_error", nil, "", http.StatusInternalServerError).Wrap(jsonErr)
return
}
w.Write(json)
}
func updateCloudCustomer(c *Context, w http.ResponseWriter, r *http.Request) {
if !c.App.Channels().License().IsCloud() {
c.Err = model.NewAppError("Api4.updateCloudCustomer", "api.cloud.license_error", nil, "", http.StatusForbidden)

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

@@ -649,6 +649,58 @@ func TestGetCloudProducts(t *testing.T) {
})
}
func Test_GetExpandStatsForSubscription(t *testing.T) {
isExpandable := &model.SubscriptionExpandStatus{
IsExpandable: true,
}
licenseId := "licenseID"
t.Run("NON Admin users are UNABLE to request expand stats for the subscription", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
th.Client.Login(th.BasicUser.Email, th.BasicUser.Password)
cloud := mocks.CloudInterface{}
cloud.Mock.On("GetLicenseExpandStatus", mock.Anything).Return(isExpandable, nil)
cloudImpl := th.App.Srv().Cloud
defer func() {
th.App.Srv().Cloud = cloudImpl
}()
th.App.Srv().Cloud = &cloud
subscriptionExpandable, r, err := th.Client.GetExpandStats(licenseId)
require.Error(t, err)
require.Nil(t, subscriptionExpandable)
require.Equal(t, http.StatusForbidden, r.StatusCode, "403 Forbidden")
})
t.Run("Admin users are UNABLE to request licenses is expendable due missing the id", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
th.Client.Login(th.SystemAdminUser.Email, th.SystemAdminUser.Password)
cloud := mocks.CloudInterface{}
cloud.Mock.On("GetLicenseExpandStatus", mock.Anything).Return(isExpandable, nil)
cloudImpl := th.App.Srv().Cloud
defer func() {
th.App.Srv().Cloud = cloudImpl
}()
th.App.Srv().Cloud = &cloud
subscriptionExpandable, r, err := th.Client.GetExpandStats("")
require.Error(t, err)
require.Nil(t, subscriptionExpandable)
require.Equal(t, http.StatusBadRequest, r.StatusCode, "400 Bad Request")
})
}
func TestGetSelfHostedProducts(t *testing.T) {
products := []*model.Product{
{