MM-44509 - subscription change not reflected after trial (#20352)
* MM-44509 - subscription change not reflected after trial * fix the tests * receive the subscription id as param, defer the cache invalidation * validate business email with body request * update api request to accept json body payload * fix unit tests * fix tests after merge conflict fixes Co-authored-by: Pablo Velez Vidal <pablo.velez@mattermost.com> Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
f1ddd3ad38
Коммит
2b8d63b606
@@ -138,12 +138,6 @@ func requestCloudTrial(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
currentSubscription, appErr := c.App.Cloud().GetSubscription(c.AppContext.Session().UserId)
|
||||
if appErr != nil {
|
||||
c.Err = model.NewAppError("Api4.requestCloudTrial", "api.cloud.app_error", nil, appErr.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// check if the email needs to be set
|
||||
bodyBytes, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
@@ -152,13 +146,13 @@ func requestCloudTrial(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// this value will not be empty when both emails (user admin and CWS customer) are not business email and
|
||||
// we need to request a new email from the user via the request business email modal
|
||||
var newValidBusinessEmail *model.ValidateBusinessEmailRequest
|
||||
if err = json.Unmarshal(bodyBytes, &newValidBusinessEmail); err != nil {
|
||||
var startTrialRequest *model.StartCloudTrialRequest
|
||||
if err = json.Unmarshal(bodyBytes, &startTrialRequest); err != nil {
|
||||
c.Err = model.NewAppError("Api4.requestCloudTrial", "api.cloud.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
changedSub, err := c.App.Cloud().RequestCloudTrial(c.AppContext.Session().UserId, currentSubscription.ID, newValidBusinessEmail.Email)
|
||||
changedSub, err := c.App.Cloud().RequestCloudTrial(c.AppContext.Session().UserId, startTrialRequest.SubscriptionID, startTrialRequest.Email)
|
||||
if err != nil {
|
||||
c.Err = model.NewAppError("Api4.requestCloudTrial", "api.cloud.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@@ -170,6 +164,8 @@ func requestCloudTrial(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
defer c.App.Srv().Cloud.InvalidateCaches()
|
||||
|
||||
w.Write(json)
|
||||
}
|
||||
|
||||
@@ -190,7 +186,30 @@ func validateBusinessEmail(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// validate current userAdmin email
|
||||
// if an email was sent as a body param, validate it and return wether is valid or not
|
||||
bodyBytes, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
c.Err = model.NewAppError("Api4.requestCloudTrial", "api.cloud.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var emailToValidate *model.ValidateBusinessEmailRequest
|
||||
if err := json.Unmarshal(bodyBytes, &emailToValidate); err != nil {
|
||||
c.Err = model.NewAppError("Api4.requestCloudTrial", "api.cloud.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if emailToValidate.Email != "" {
|
||||
errValidatingEmail := c.App.Cloud().ValidateBusinessEmail(user.Id, emailToValidate.Email)
|
||||
if errValidatingEmail != nil {
|
||||
c.Err = model.NewAppError("Api4.valiateBusinessEmail", "api.cloud.request_error", nil, errValidatingEmail.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
ReturnStatusOK(w)
|
||||
return
|
||||
}
|
||||
|
||||
// If no email was sent as body param, then validate current userAdmin email
|
||||
errValidatingAdminEmail := c.App.Cloud().ValidateBusinessEmail(user.Id, user.Email)
|
||||
|
||||
// if the current admin email is not a valid email
|
||||
@@ -211,7 +230,7 @@ func validateBusinessEmail(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// if the email is valid, return ok
|
||||
// if any of the emails is valid, return ok
|
||||
ReturnStatusOK(w)
|
||||
}
|
||||
|
||||
|
||||
@@ -125,7 +125,7 @@ func Test_requestTrial(t *testing.T) {
|
||||
IsPaidTier: "false",
|
||||
}
|
||||
|
||||
newValidBusinessEmail := model.ValidateBusinessEmailRequest{Email: ""}
|
||||
newValidBusinessEmail := model.StartCloudTrialRequest{Email: ""}
|
||||
|
||||
t.Run("NON Admin users are UNABLE to request the trial", func(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
@@ -139,6 +139,7 @@ func Test_requestTrial(t *testing.T) {
|
||||
|
||||
cloud.Mock.On("GetSubscription", mock.Anything).Return(subscription, nil)
|
||||
cloud.Mock.On("RequestCloudTrial", mock.Anything, mock.Anything, "").Return(subscription, nil)
|
||||
cloud.Mock.On("InvalidateCaches").Return(nil)
|
||||
|
||||
cloudImpl := th.App.Srv().Cloud
|
||||
defer func() {
|
||||
@@ -164,6 +165,7 @@ func Test_requestTrial(t *testing.T) {
|
||||
|
||||
cloud.Mock.On("GetSubscription", mock.Anything).Return(subscription, nil)
|
||||
cloud.Mock.On("RequestCloudTrial", mock.Anything, mock.Anything, "").Return(subscription, nil)
|
||||
cloud.Mock.On("InvalidateCaches").Return(nil)
|
||||
|
||||
cloudImpl := th.App.Srv().Cloud
|
||||
defer func() {
|
||||
@@ -193,6 +195,7 @@ func Test_requestTrial(t *testing.T) {
|
||||
|
||||
cloud.Mock.On("GetSubscription", mock.Anything).Return(subscription, nil)
|
||||
cloud.Mock.On("RequestCloudTrial", mock.Anything, mock.Anything, "valid.email@mattermost.com").Return(subscription, nil)
|
||||
cloud.Mock.On("InvalidateCaches").Return(nil)
|
||||
|
||||
cloudImpl := th.App.Srv().Cloud
|
||||
defer func() {
|
||||
@@ -215,6 +218,8 @@ func Test_validateBusinessEmail(t *testing.T) {
|
||||
|
||||
th.Client.Login(th.BasicUser.Email, th.BasicUser.Password)
|
||||
|
||||
validateBusinessEmail := model.ValidateBusinessEmailRequest{Email: ""}
|
||||
|
||||
th.App.Srv().SetLicense(model.NewTestLicense("cloud"))
|
||||
|
||||
cloud := mocks.CloudInterface{}
|
||||
@@ -229,7 +234,7 @@ func Test_validateBusinessEmail(t *testing.T) {
|
||||
}()
|
||||
th.App.Srv().Cloud = &cloud
|
||||
|
||||
_, err := th.Client.ValidateBusinessEmail()
|
||||
_, err := th.Client.ValidateBusinessEmail(&validateBusinessEmail)
|
||||
require.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -7838,7 +7838,7 @@ func (c *Client4) ConfirmCustomerPayment(confirmRequest *ConfirmPaymentMethodReq
|
||||
return BuildResponse(r), nil
|
||||
}
|
||||
|
||||
func (c *Client4) RequestCloudTrial(email *ValidateBusinessEmailRequest) (*Subscription, *Response, error) {
|
||||
func (c *Client4) RequestCloudTrial(email *StartCloudTrialRequest) (*Subscription, *Response, error) {
|
||||
payload, _ := json.Marshal(email)
|
||||
r, err := c.DoAPIPutBytes(c.cloudRoute()+"/request-trial", payload)
|
||||
if err != nil {
|
||||
@@ -7852,8 +7852,9 @@ func (c *Client4) RequestCloudTrial(email *ValidateBusinessEmailRequest) (*Subsc
|
||||
return subscription, BuildResponse(r), nil
|
||||
}
|
||||
|
||||
func (c *Client4) ValidateBusinessEmail() (*Response, error) {
|
||||
r, err := c.DoAPIPost(c.cloudRoute()+"/validate-business-email", "")
|
||||
func (c *Client4) ValidateBusinessEmail(email *ValidateBusinessEmailRequest) (*Response, error) {
|
||||
payload, _ := json.Marshal(email)
|
||||
r, err := c.DoAPIPostBytes(c.cloudRoute()+"/validate-business-email", payload)
|
||||
if err != nil {
|
||||
return BuildResponse(r), err
|
||||
}
|
||||
|
||||
@@ -84,6 +84,11 @@ type CloudCustomer struct {
|
||||
PaymentMethod *PaymentMethod `json:"payment_method"`
|
||||
}
|
||||
|
||||
type StartCloudTrialRequest struct {
|
||||
Email string `json:"email"`
|
||||
SubscriptionID string `json:"subscription_id"`
|
||||
}
|
||||
|
||||
type ValidateBusinessEmailRequest struct {
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user