feat(audit): cover operational mutations

This commit is contained in:
2026-06-05 10:45:55 +08:00
parent 4597d404fd
commit 2ec5366d7c
8 changed files with 321 additions and 24 deletions
@@ -33,12 +33,13 @@ func (s *AgentCapacityHandlerTestSuite) SetupSuite() {
Logger: logger.Default.LogMode(logger.Silent),
})
s.Require().NoError(err)
s.Require().NoError(db.AutoMigrate(&model.Account{}, &model.User{}, &model.AccountUser{}, &model.Inbox{}, &model.AgentCapacityPolicy{}, &model.InboxCapacityLimit{}))
s.Require().NoError(db.AutoMigrate(&model.Account{}, &model.User{}, &model.AccountUser{}, &model.Inbox{}, &model.AgentCapacityPolicy{}, &model.InboxCapacityLimit{}, &model.Audit{}))
s.db = db
repo := repository.NewAgentCapacityPolicyRepo(db)
svc := service.NewAgentCapacityPolicyService(repo)
s.handler = NewAgentCapacityHandler(svc)
auditSvc := service.NewAuditService(repository.NewAuditRepo(db))
s.handler = NewAgentCapacityHandler(svc).WithAuditService(auditSvc)
s.account = &model.Account{Name: "test-capacity-account"}
s.Require().NoError(db.Create(s.account).Error)
@@ -55,6 +56,10 @@ func TestAgentCapacityHandlerSuite(t *testing.T) {
suite.Run(t, new(AgentCapacityHandlerTestSuite))
}
func (s *AgentCapacityHandlerTestSuite) SetupTest() {
s.Require().NoError(s.db.Exec("DELETE FROM audits").Error)
}
func (s *AgentCapacityHandlerTestSuite) TestList_Success() {
r := gin.New()
r.GET("/api/v1/accounts/:account_id/agent_capacity_policies", s.handler.List)
@@ -208,3 +213,54 @@ func (s *AgentCapacityHandlerTestSuite) TestChatwootPolicyInboxLimitAndUserFlow(
r.ServeHTTP(w, req)
s.Require().Equal(http.StatusNoContent, w.Code)
}
func (s *AgentCapacityHandlerTestSuite) TestMutations_WriteAuditEntries() {
r := gin.New()
r.Use(func(c *gin.Context) {
c.Set("account_id", s.account.ID)
c.Set("user_id", uint(99))
c.Next()
})
api := r.Group("/api/v1/accounts/:account_id")
RegisterAgentCapacityRoutes(api, s.handler)
w := httptest.NewRecorder()
body := `{"agent_capacity_policy":{"name":"Audit capacity","description":"tracked"}}`
req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/agent_capacity_policies/", s.account.ID), bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Request-ID", "capacity-audit-create")
r.ServeHTTP(w, req)
s.Require().Equal(http.StatusOK, w.Code, w.Body.String())
var policy map[string]any
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &policy))
policyID := uint(policy["id"].(float64))
w = httptest.NewRecorder()
body = `{"agent_capacity_policy":{"name":"Audit capacity updated","description":"tracked again"}}`
req, _ = http.NewRequest(http.MethodPut, fmt.Sprintf("/api/v1/accounts/%d/agent_capacity_policies/%d", s.account.ID, policyID), bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
r.ServeHTTP(w, req)
s.Require().Equal(http.StatusOK, w.Code, w.Body.String())
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/v1/accounts/%d/agent_capacity_policies/%d", s.account.ID, policyID), nil)
r.ServeHTTP(w, req)
s.Require().Equal(http.StatusOK, w.Code, w.Body.String())
var audits []model.Audit
s.Require().NoError(s.db.Order("id ASC").Find(&audits).Error)
s.Require().Len(audits, 3)
for _, audit := range audits {
s.Equal(s.account.ID, *audit.AccountID)
s.Equal("Account", audit.AssociatedType)
s.Equal(s.account.ID, *audit.AssociatedID)
s.Equal(uint(99), *audit.UserID)
s.Equal("AgentCapacityPolicy", audit.AuditableType)
s.Equal(policyID, audit.AuditableID)
s.NotEmpty(audit.AuditedChanges)
}
s.Equal("create", audits[0].Action)
s.Equal("capacity-audit-create", audits[0].RequestUUID)
s.Equal("update", audits[1].Action)
s.Equal("destroy", audits[2].Action)
}