forked from linaproai/official-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_contract_test.go
More file actions
320 lines (292 loc) · 9.83 KB
/
Copy pathapi_contract_test.go
File metadata and controls
320 lines (292 loc) · 9.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// This file verifies source-plugin API response DTO boundaries.
package linaplugins
import (
"bytes"
"encoding/json"
"go/ast"
"go/parser"
"go/token"
"io/fs"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
noticev1 "lina-plugin-content-notice/backend/api/notice/v1"
loginlogv1 "lina-plugin-monitor-loginlog/backend/api/loginlog/v1"
operlogv1 "lina-plugin-monitor-operlog/backend/api/operlog/v1"
authv1 "lina-plugin-multi-tenant/backend/api/auth/v1"
platformv1 "lina-plugin-multi-tenant/backend/api/platform/v1"
tenantv1 "lina-plugin-multi-tenant/backend/api/tenant/v1"
deptv1 "lina-plugin-org-center/backend/api/dept/v1"
postv1 "lina-plugin-org-center/backend/api/post/v1"
)
// TestPluginAPIsDoNotDependOnGeneratedEntities ensures API contracts are independent from database entities.
func TestPluginAPIsDoNotDependOnGeneratedEntities(t *testing.T) {
root := pluginWorkspaceRoot(t)
files := collectPluginAPIFiles(t, root)
for _, file := range files {
parsed, err := parser.ParseFile(token.NewFileSet(), file, nil, parser.ImportsOnly)
if err != nil {
t.Fatalf("parse %s: %v", file, err)
}
for _, imported := range parsed.Imports {
path := strings.Trim(imported.Path.Value, `"`)
if strings.Contains(path, "/internal/model/entity") {
t.Fatalf("plugin API file %s imports generated entity package %s", slashPath(root, file), path)
}
}
}
}
// TestPluginAPIDTOsDoNotUseEntityNaming ensures API response DTOs are not named like database entities.
func TestPluginAPIDTOsDoNotUseEntityNaming(t *testing.T) {
root := pluginWorkspaceRoot(t)
files := collectPluginAPIFiles(t, root)
for _, file := range files {
parsed, err := parser.ParseFile(token.NewFileSet(), file, nil, parser.ParseComments)
if err != nil {
t.Fatalf("parse %s: %v", file, err)
}
ast.Inspect(parsed, func(node ast.Node) bool {
spec, ok := node.(*ast.TypeSpec)
if !ok {
return true
}
if strings.HasSuffix(spec.Name.Name, "Entity") {
t.Fatalf("plugin API type %s in %s must use response DTO naming instead of Entity", spec.Name.Name, slashPath(root, file))
}
return true
})
}
}
// TestPluginAPIDTOFilePlacement keeps shared response DTOs in each API package main source file.
func TestPluginAPIDTOFilePlacement(t *testing.T) {
root := pluginWorkspaceRoot(t)
expected := map[string]string{
"content-notice/backend/api/notice/v1/NoticeItem": "notice.go",
"monitor-loginlog/backend/api/loginlog/v1/LoginLogItem": "loginlog.go",
"monitor-operlog/backend/api/operlog/v1/OperLogListItem": "operlog.go",
"monitor-operlog/backend/api/operlog/v1/OperLogDetailItem": "operlog.go",
"multi-tenant/backend/api/auth/v1/LoginTenantItem": "auth.go",
"multi-tenant/backend/api/platform/v1/TenantItem": "platform.go",
"multi-tenant/backend/api/tenant/v1/TenantPluginItem": "tenant.go",
"org-center/backend/api/dept/v1/DeptItem": "dept.go",
"org-center/backend/api/post/v1/PostItem": "post.go",
}
seen := make(map[string]string, len(expected))
files := collectPluginAPIFiles(t, root)
for _, file := range files {
parsed, err := parser.ParseFile(token.NewFileSet(), file, nil, parser.ParseComments)
if err != nil {
t.Fatalf("parse %s: %v", file, err)
}
for _, declaration := range parsed.Decls {
general, ok := declaration.(*ast.GenDecl)
if !ok || general.Tok != token.TYPE {
continue
}
for _, spec := range general.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
key := dtoKey(root, file, typeSpec.Name.Name)
if _, ok := expected[key]; ok {
seen[key] = filepath.Base(file)
}
}
}
}
for key, wantFile := range expected {
gotFile, ok := seen[key]
if !ok {
t.Fatalf("expected response DTO %s to exist", key)
}
if gotFile != wantFile {
t.Fatalf("response DTO %s must be defined in %s, got %s", key, wantFile, gotFile)
}
}
}
// TestPluginAPIDTOFilesAvoidLegacyNames rejects legacy DTO-only file naming.
func TestPluginAPIDTOFilesAvoidLegacyNames(t *testing.T) {
root := pluginWorkspaceRoot(t)
files := collectPluginAPIFiles(t, root)
for _, file := range files {
name := filepath.Base(file)
if strings.HasSuffix(name, "_entity.go") || strings.HasSuffix(name, "_dto.go") {
t.Fatalf("plugin API DTO file %s must be folded into the API main source file", slashPath(root, file))
}
}
}
// TestPluginResponseDTOsHideInternalFields verifies sensitive implementation fields stay out of API responses.
func TestPluginResponseDTOsHideInternalFields(t *testing.T) {
bannedFields := []string{"password", "deletedAt", "path", "engine", "hash"}
dtoTypes := []struct {
name string
typ reflect.Type
}{
{name: "notice.NoticeItem", typ: reflect.TypeOf(noticev1.NoticeItem{})},
{name: "dept.DeptItem", typ: reflect.TypeOf(deptv1.DeptItem{})},
{name: "post.PostItem", typ: reflect.TypeOf(postv1.PostItem{})},
{name: "loginlog.LoginLogItem", typ: reflect.TypeOf(loginlogv1.LoginLogItem{})},
{name: "operlog.OperLogListItem", typ: reflect.TypeOf(operlogv1.OperLogListItem{})},
{name: "operlog.OperLogDetailItem", typ: reflect.TypeOf(operlogv1.OperLogDetailItem{})},
{name: "auth.LoginTenantItem", typ: reflect.TypeOf(authv1.LoginTenantItem{})},
{name: "platform.TenantItem", typ: reflect.TypeOf(platformv1.TenantItem{})},
{name: "tenant.TenantPluginItem", typ: reflect.TypeOf(tenantv1.TenantPluginItem{})},
}
for _, dto := range dtoTypes {
fields := jsonFieldSet(dto.typ)
for _, field := range bannedFields {
if fields[field] {
t.Fatalf("%s exposes internal field %q", dto.name, field)
}
}
}
}
// TestPluginOperLogListDTOExcludesPayloads keeps large audited payloads out of list responses.
func TestPluginOperLogListDTOExcludesPayloads(t *testing.T) {
fields := jsonFieldSet(reflect.TypeOf(operlogv1.OperLogListItem{}))
for _, field := range []string{"operParam", "jsonResult"} {
if fields[field] {
t.Fatalf("operlog.OperLogListItem exposes detail-only field %q", field)
}
}
detailFields := jsonFieldSet(reflect.TypeOf(operlogv1.OperLogDetailItem{}))
for _, field := range []string{"operParam", "jsonResult"} {
if !detailFields[field] {
t.Fatalf("operlog.OperLogDetailItem must expose audited payload field %q", field)
}
}
}
// TestPluginAPIDocI18NDoesNotReferenceRemovedDTOFields keeps apidoc translations aligned with response DTOs.
func TestPluginAPIDocI18NDoesNotReferenceRemovedDTOFields(t *testing.T) {
root := pluginWorkspaceRoot(t)
var files []string
if err := filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if entry.IsDir() {
if entry.Name() == "vendor" {
return filepath.SkipDir
}
return nil
}
rel := slashPath(root, path)
if strings.Contains(rel, "/manifest/i18n/") && strings.Contains(rel, "/apidoc/") && strings.HasSuffix(rel, ".json") {
files = append(files, path)
}
return nil
}); err != nil {
t.Fatalf("walk plugin apidoc i18n files: %v", err)
}
banned := [][]byte{
[]byte("NoticeEntity"),
[]byte("DeptEntity"),
[]byte("PostEntity"),
[]byte("LoginLogEntity"),
[]byte("OperLogEntity"),
[]byte("TenantEntity"),
[]byte("LoginTenantEntity"),
[]byte("TenantPluginEntity"),
[]byte("deletedAt"),
}
for _, file := range files {
content, err := os.ReadFile(file)
if err != nil {
t.Fatalf("read %s: %v", file, err)
}
if !json.Valid(content) {
t.Fatalf("plugin apidoc i18n file %s is not valid JSON", slashPath(root, file))
}
for _, token := range banned {
if bytes.Contains(content, token) {
t.Fatalf("plugin apidoc i18n file %s still references removed DTO token %q", slashPath(root, file), token)
}
}
}
}
// pluginWorkspaceRoot returns the lina-plugins module root for path-based contract checks.
func pluginWorkspaceRoot(t *testing.T) string {
t.Helper()
_, file, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("resolve current test file path")
}
return filepath.Dir(file)
}
// collectPluginAPIFiles lists Go source files under plugin backend API directories.
func collectPluginAPIFiles(t *testing.T, root string) []string {
t.Helper()
var files []string
if err := filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if entry.IsDir() {
if entry.Name() == "vendor" {
return filepath.SkipDir
}
return nil
}
rel := slashPath(root, path)
if !strings.HasSuffix(rel, ".go") || strings.HasSuffix(rel, "_test.go") {
return nil
}
if strings.Contains(rel, "/backend/api/") {
files = append(files, path)
}
return nil
}); err != nil {
t.Fatalf("walk plugin API files: %v", err)
}
return files
}
// dtoKey builds a stable key for a type declaration in a plugin API package.
func dtoKey(root string, file string, typeName string) string {
rel := slashPath(root, file)
dir := strings.TrimSuffix(rel, "/"+filepath.Base(file))
return dir + "/" + typeName
}
// slashPath returns a stable slash-separated path relative to root.
func slashPath(root string, path string) string {
rel, err := filepath.Rel(root, path)
if err != nil {
return filepath.ToSlash(path)
}
return filepath.ToSlash(rel)
}
// jsonFieldSet collects JSON field names from a DTO, including embedded structs.
func jsonFieldSet(typ reflect.Type) map[string]bool {
fields := make(map[string]bool)
collectJSONFields(typ, fields)
return fields
}
// collectJSONFields recursively records JSON field names for a struct type.
func collectJSONFields(typ reflect.Type, fields map[string]bool) {
for typ.Kind() == reflect.Pointer {
typ = typ.Elem()
}
if typ.Kind() != reflect.Struct {
return
}
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
name := strings.Split(field.Tag.Get("json"), ",")[0]
if field.Anonymous {
collectJSONFields(field.Type, fields)
if name == "" {
continue
}
}
if name == "-" {
continue
}
if name == "" {
name = field.Name
}
fields[name] = true
}
}