完善域名服务器项目归属与数据库关联约束
保存资源项目、所有者和操作人,接口从会话记录归属,普通更新保留创建信息。 新增三库第十一版迁移、同项目绑定约束和 SQLite 触发器保护,补充迁移回滚重试、仓储、接口及备份恢复测试。 本次整理验证:后端构建、静态检查、关联改造针对性测试和 CLI 包测试通过。 范围说明:项目成员权限及其他资源归属尚未完成;本次未重新执行外部数据库实库、独立程序及生产部署验收。
This commit is contained in:
1 parent
059e6d7b67
commit
7284958819
22 files changed
+815
-27
No files matched your search
@@ -0,0 +1,25 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/domain"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 归属由服务端会话填写,不能从请求体接受操作者或项目授权声明。
|
||||
func (s *Server) newResourceScope(c *gin.Context) domain.ResourceScope {
|
||||
scope := domain.ResourceScope{ProjectID: domain.DefaultProjectID}
|
||||
if session, ok := s.currentAuthSession(c); ok {
|
||||
scope.OwnerID = session.UserID
|
||||
scope.CreatedBy = session.UserID
|
||||
scope.UpdatedBy = session.UserID
|
||||
}
|
||||
return scope
|
||||
}
|
||||
|
||||
func (s *Server) updatedResourceScope(c *gin.Context, existing domain.ResourceScope) domain.ResourceScope {
|
||||
existing.UpdatedBy = ""
|
||||
if session, ok := s.currentAuthSession(c); ok {
|
||||
existing.UpdatedBy = session.UserID
|
||||
}
|
||||
return existing.Normalize()
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/database"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/domain"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/repository"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestDomainResourceScopeAPI(t *testing.T) {
|
||||
cfg := readyTestConfig(t.TempDir())
|
||||
db, err := database.Open(t.Context(), cfg.Database)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
if err := database.Migrate(t.Context(), db.DB, "sqlite"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo := repository.NewSQLDomainRepository(db.DB)
|
||||
server := New(Dependencies{Config: cfg, Repository: repo})
|
||||
request := func(method, path, body, user string) *httptest.ResponseRecorder {
|
||||
req := httptest.NewRequest(method, path, strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.AddCookie(httpUserSessionCookie(server, user, repository.UserRoleAdmin))
|
||||
response := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(response, req)
|
||||
return response
|
||||
}
|
||||
for _, field := range []string{"project_id", "owner_id", "created_by", "updated_by"} {
|
||||
body := fmt.Sprintf(`{"name":"forged.example.test","certificate_type":"single","%s":"forged"}`, field)
|
||||
response := request(http.MethodPost, "/api/v1/domains", body, "creator")
|
||||
if response.Code != http.StatusBadRequest {
|
||||
t.Fatalf("客户端不应提供 %s:%d", field, response.Code)
|
||||
}
|
||||
}
|
||||
response := request(http.MethodPost, "/api/v1/domains", `{"name":"scope.example.test","certificate_type":"single"}`, "creator")
|
||||
if response.Code != http.StatusCreated {
|
||||
t.Fatalf("创建失败:%d,%s", response.Code, response.Body.String())
|
||||
}
|
||||
var created domain.Domain
|
||||
if err := json.Unmarshal(response.Body.Bytes(), &created); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := domain.ResourceScope{ProjectID: "default", OwnerID: "creator", CreatedBy: "creator", UpdatedBy: "creator"}
|
||||
if created.ResourceScope != want {
|
||||
t.Fatalf("创建响应归属错误:%+v", created.ResourceScope)
|
||||
}
|
||||
response = request(http.MethodPut, "/api/v1/domains/"+created.ID, `{"name":"edited.example.test","certificate_type":"single"}`, "editor")
|
||||
if response.Code != http.StatusOK {
|
||||
t.Fatalf("更新失败:%d,%s", response.Code, response.Body.String())
|
||||
}
|
||||
loaded, err := repo.GetByID(t.Context(), created.ID)
|
||||
want.UpdatedBy = "editor"
|
||||
if err != nil || loaded.ResourceScope != want {
|
||||
t.Fatalf("更新后归属错误:%+v,%v", loaded.ResourceScope, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceScopePreservesProjectAndCreator(t *testing.T) {
|
||||
server := New(Dependencies{Config: readyTestConfig(t.TempDir())})
|
||||
recorder := httptest.NewRecorder()
|
||||
ctx, _ := gin.CreateTestContext(recorder)
|
||||
ctx.Request = httptest.NewRequest(http.MethodPut, "/", nil)
|
||||
ctx.Request.AddCookie(httpUserSessionCookie(server, "editor", repository.UserRoleAdmin))
|
||||
existing := domain.ResourceScope{ProjectID: "project-a", OwnerID: "owner", CreatedBy: "creator", UpdatedBy: "previous"}
|
||||
updated := server.updatedResourceScope(ctx, existing)
|
||||
if updated.ProjectID != existing.ProjectID || updated.OwnerID != existing.OwnerID || updated.CreatedBy != existing.CreatedBy || updated.UpdatedBy != "editor" {
|
||||
t.Fatalf("更新操作者时丢失原资源归属:%+v", updated)
|
||||
}
|
||||
}
|
||||
@@ -546,6 +546,7 @@ func (s *Server) createDomain(c *gin.Context) {
|
||||
s.writeError(c, http.StatusBadRequest, err.Error(), err)
|
||||
return
|
||||
}
|
||||
item.ResourceScope = s.newResourceScope(c)
|
||||
if err := s.repository.Create(c.Request.Context(), item); err != nil {
|
||||
if repository.IsConflict(err) {
|
||||
s.writeError(c, http.StatusConflict, "域名已经存在", err)
|
||||
@@ -585,6 +586,7 @@ func (s *Server) updateDomain(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
item.ID = existing.ID
|
||||
item.ResourceScope = s.updatedResourceScope(c, existing.ResourceScope)
|
||||
item.CreatedAt = existing.CreatedAt
|
||||
item.Status = "pending"
|
||||
item.ExpiresAt = nil
|
||||
@@ -702,6 +704,7 @@ func (s *Server) createServer(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
item := serverFromRequest(request)
|
||||
item.ResourceScope = s.newResourceScope(c)
|
||||
item = item.Normalize()
|
||||
now := time.Now().UTC()
|
||||
item.CreatedAt = now
|
||||
@@ -754,6 +757,7 @@ func (s *Server) updateServer(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
item.CreatedAt = existing.CreatedAt
|
||||
item.ResourceScope = s.updatedResourceScope(c, existing.ResourceScope)
|
||||
item.UpdatedAt = time.Now().UTC()
|
||||
item, cleanup, err := s.prepareServerCredential(c.Request.Context(), item, request.Password, &existing)
|
||||
if err != nil {
|
||||
|
||||
@@ -342,6 +342,9 @@ func TestServerAndDomainAssociationAPI(t *testing.T) {
|
||||
t.Fatalf("服务器 API 未使用默认远程证书路径模板:%+v", created)
|
||||
}
|
||||
|
||||
if created.ProjectID != "default" || created.OwnerID != "test-admin" || created.CreatedBy != "test-admin" || created.UpdatedBy != "test-admin" {
|
||||
t.Fatalf("服务器未记录服务端会话归属:%+v", created.ResourceScope)
|
||||
}
|
||||
updateRequest := httptest.NewRequest(http.MethodPut, "/api/v1/servers/"+created.ID, strings.NewReader(`{"name":"updated-server","address":"192.0.2.50","port":22,"username":"deploy","private_key_ref":"`+key.Reference+`","host_key_fingerprint":"SHA256:test","web_server":"nginx"}`))
|
||||
updateRequest.Header.Set("Content-Type", "application/json")
|
||||
updateResponse := httptest.NewRecorder()
|
||||
@@ -356,6 +359,9 @@ func TestServerAndDomainAssociationAPI(t *testing.T) {
|
||||
if updated.Name != "updated-server" || updated.CreatedAt.IsZero() || updated.UpdatedAt.IsZero() {
|
||||
t.Fatalf("updated server response is incomplete: %+v", updated)
|
||||
}
|
||||
if updated.ResourceScope != created.ResourceScope {
|
||||
t.Fatalf("服务器更新丢失归属:%+v", updated.ResourceScope)
|
||||
}
|
||||
|
||||
associationRequest := httptest.NewRequest(http.MethodPut, "/api/v1/domains/"+item.ID+"/servers", strings.NewReader(`{"server_ids":["`+created.ID+`"]}`))
|
||||
associationRequest.Header.Set("Content-Type", "application/json")
|
||||
|
||||
Reference in new issue
Block a user