Align GoChat with Chatwoot frontend contracts
This commit is contained in:
@@ -148,6 +148,68 @@ func (s *CustomRoleHandlerTestSuite) TestCreate_Success() {
|
||||
s.NotContains(payload, "data")
|
||||
}
|
||||
|
||||
func (s *CustomRoleHandlerTestSuite) TestChatwootPermissionSetCreateUpdateListShowParity() {
|
||||
chatwootPermissions := []string{
|
||||
"conversation_manage",
|
||||
"conversation_unassigned_manage",
|
||||
"conversation_participating_manage",
|
||||
"contact_manage",
|
||||
"report_manage",
|
||||
"knowledge_base_manage",
|
||||
}
|
||||
|
||||
r := gin.New()
|
||||
r.POST("/api/v1/accounts/:account_id/custom_roles", withCustomRoleAdminContext(s.account.ID, s.handler.Create))
|
||||
r.GET("/api/v1/accounts/:account_id/custom_roles", withCustomRoleAdminContext(s.account.ID, s.handler.List))
|
||||
r.GET("/api/v1/accounts/:account_id/custom_roles/:id", withCustomRoleAdminContext(s.account.ID, s.handler.Get))
|
||||
r.PUT("/api/v1/accounts/:account_id/custom_roles/:id", withCustomRoleAdminContext(s.account.ID, s.handler.Update))
|
||||
|
||||
createBody := fmt.Sprintf(`{"custom_role":{"name":"chatwoot-full-permissions","description":"all enterprise permissions","permissions":%s}}`, mustJSONForCustomRoleTest(s.T(), chatwootPermissions))
|
||||
createW := httptest.NewRecorder()
|
||||
createReq, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/custom_roles", s.account.ID), bytes.NewBufferString(createBody))
|
||||
createReq.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(createW, createReq)
|
||||
s.Require().Equal(http.StatusOK, createW.Code)
|
||||
|
||||
var created map[string]any
|
||||
s.Require().NoError(json.Unmarshal(createW.Body.Bytes(), &created))
|
||||
s.Equal("chatwoot-full-permissions", created["name"])
|
||||
s.Equal("all enterprise permissions", created["description"])
|
||||
s.Equal(toInterfaceStrings(chatwootPermissions), created["permissions"])
|
||||
s.NotContains(created, "data")
|
||||
s.NotContains(created, "success")
|
||||
roleID := uint(created["id"].(float64))
|
||||
|
||||
showW := httptest.NewRecorder()
|
||||
showReq, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/custom_roles/%d", s.account.ID, roleID), nil)
|
||||
r.ServeHTTP(showW, showReq)
|
||||
s.Require().Equal(http.StatusOK, showW.Code)
|
||||
var shown map[string]any
|
||||
s.Require().NoError(json.Unmarshal(showW.Body.Bytes(), &shown))
|
||||
s.Equal(toInterfaceStrings(chatwootPermissions), shown["permissions"])
|
||||
|
||||
listW := httptest.NewRecorder()
|
||||
listReq, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/custom_roles", s.account.ID), nil)
|
||||
r.ServeHTTP(listW, listReq)
|
||||
s.Require().Equal(http.StatusOK, listW.Code)
|
||||
var listed []map[string]any
|
||||
s.Require().NoError(json.Unmarshal(listW.Body.Bytes(), &listed))
|
||||
s.Require().Len(listed, 1)
|
||||
s.Equal(toInterfaceStrings(chatwootPermissions), listed[0]["permissions"])
|
||||
|
||||
updateBody := `{"custom_role":{"name":"chatwoot-cleared-permissions","description":"","permissions":[]}}`
|
||||
updateW := httptest.NewRecorder()
|
||||
updateReq, _ := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/v1/accounts/%d/custom_roles/%d", s.account.ID, roleID), bytes.NewBufferString(updateBody))
|
||||
updateReq.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(updateW, updateReq)
|
||||
s.Require().Equal(http.StatusOK, updateW.Code)
|
||||
var updated map[string]any
|
||||
s.Require().NoError(json.Unmarshal(updateW.Body.Bytes(), &updated))
|
||||
s.Equal("chatwoot-cleared-permissions", updated["name"])
|
||||
s.Equal("", updated["description"])
|
||||
s.Empty(updated["permissions"])
|
||||
}
|
||||
|
||||
func (s *CustomRoleHandlerTestSuite) TestGet_Success() {
|
||||
// Create a custom role first
|
||||
role := &model.CustomRole{AccountID: s.account.ID, Name: "get-test-role", Permissions: `["report_manage"]`}
|
||||
@@ -314,3 +376,20 @@ func withCustomRoleAdminContext(accountID uint, h gin.HandlerFunc) gin.HandlerFu
|
||||
h(c)
|
||||
}
|
||||
}
|
||||
|
||||
func mustJSONForCustomRoleTest(t *testing.T, value any) string {
|
||||
t.Helper()
|
||||
data, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal custom role test value: %v", err)
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func toInterfaceStrings(values []string) []any {
|
||||
out := make([]any, 0, len(values))
|
||||
for _, value := range values {
|
||||
out = append(out, value)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user