|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/talesofai/okp/internal/config" |
| 11 | + "github.com/talesofai/okp/internal/model" |
| 12 | + "github.com/talesofai/okp/internal/testutil" |
| 13 | +) |
| 14 | + |
| 15 | +func TestExportDomainStreamsConcepts(t *testing.T) { |
| 16 | + db := testutil.OpenDatabase(t) |
| 17 | + previousConfig := config.C |
| 18 | + config.C = config.Config{ExecutionGrantKey: "test-key"} |
| 19 | + t.Cleanup(func() { config.C = previousConfig }) |
| 20 | + |
| 21 | + if err := db.Create(&model.User{UUID: "admin", AuthType: "test", Role: "admin"}).Error; err != nil { |
| 22 | + t.Fatal(err) |
| 23 | + } |
| 24 | + if err := db.Create(&model.DomainMeta{Domain: "public", Visibility: "public"}).Error; err != nil { |
| 25 | + t.Fatal(err) |
| 26 | + } |
| 27 | + for _, c := range []model.Concept{ |
| 28 | + {ID: "public/Note/zeta", Domain: "public", Type: "Note", Title: "Zeta", Provenance: model.JSONMap{"source": "t", "agent": "t"}}, |
| 29 | + {ID: "public/Guide/alpha", Domain: "public", Type: "Guide", Title: "Alpha", Provenance: model.JSONMap{"source": "t", "agent": "t"}}, |
| 30 | + {ID: "public/Note/beta", Domain: "public", Type: "Note", Title: "Beta", Provenance: model.JSONMap{"source": "t", "agent": "t"}}, |
| 31 | + } { |
| 32 | + if err := db.Create(&c).Error; err != nil { |
| 33 | + t.Fatal(err) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + router := NewRouter() |
| 38 | + token := testExecutionToken(t, "test-key", "admin") |
| 39 | + res := testRequest(t, router, token, http.MethodGet, "/api/v1/domains/public/export", nil) |
| 40 | + assertStatus(t, res, http.StatusOK) |
| 41 | + if got := res.Header().Get("Content-Type"); !strings.HasPrefix(got, "application/x-ndjson") { |
| 42 | + t.Fatalf("content type = %q", got) |
| 43 | + } |
| 44 | + if got := res.Header().Get("X-Total-Count"); got != "3" { |
| 45 | + t.Fatalf("X-Total-Count = %q", got) |
| 46 | + } |
| 47 | + |
| 48 | + var ids []string |
| 49 | + scanner := bufio.NewScanner(res.Body) |
| 50 | + for scanner.Scan() { |
| 51 | + var c model.Concept |
| 52 | + if err := json.Unmarshal(scanner.Bytes(), &c); err != nil { |
| 53 | + t.Fatalf("line %q: %v", scanner.Text(), err) |
| 54 | + } |
| 55 | + ids = append(ids, c.ID) |
| 56 | + } |
| 57 | + if err := scanner.Err(); err != nil { |
| 58 | + t.Fatal(err) |
| 59 | + } |
| 60 | + want := []string{"public/Guide/alpha", "public/Note/beta", "public/Note/zeta"} |
| 61 | + if strings.Join(ids, ",") != strings.Join(want, ",") { |
| 62 | + t.Fatalf("ids = %v, want %v", ids, want) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestExportDomainHidesPrivateAndRejectsEmpty(t *testing.T) { |
| 67 | + db := testutil.OpenDatabase(t) |
| 68 | + previousConfig := config.C |
| 69 | + config.C = config.Config{ExecutionGrantKey: "test-key"} |
| 70 | + t.Cleanup(func() { config.C = previousConfig }) |
| 71 | + |
| 72 | + for _, u := range []model.User{ |
| 73 | + {UUID: "creator", AuthType: "test", Role: "reader"}, |
| 74 | + {UUID: "admin", AuthType: "test", Role: "admin"}, |
| 75 | + } { |
| 76 | + if err := db.Create(&u).Error; err != nil { |
| 77 | + t.Fatal(err) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + router := NewRouter() |
| 82 | + creatorToken := testExecutionToken(t, "test-key", "creator") |
| 83 | + adminToken := testExecutionToken(t, "test-key", "admin") |
| 84 | + |
| 85 | + res := testRequest(t, router, creatorToken, http.MethodPut, "/api/v1/domains/private-team", map[string]any{ |
| 86 | + "readme": "# Private", |
| 87 | + "visibility": "private", |
| 88 | + }) |
| 89 | + assertStatus(t, res, http.StatusCreated) |
| 90 | + res = testRequest(t, router, creatorToken, http.MethodPut, "/api/v1/concepts/private-team/Note/plan", map[string]any{ |
| 91 | + "domain": "private-team", |
| 92 | + "type": "Note", |
| 93 | + "title": "Plan", |
| 94 | + "provenance": map[string]any{"source": "t", "agent": "t"}, |
| 95 | + }) |
| 96 | + assertStatus(t, res, http.StatusOK) |
| 97 | + |
| 98 | + // 非成员(含全局 admin)不可导出私有域 |
| 99 | + assertStatus(t, testRequest(t, router, adminToken, http.MethodGet, "/api/v1/domains/private-team/export", nil), http.StatusNotFound) |
| 100 | + // host 可导出 |
| 101 | + res = testRequest(t, router, creatorToken, http.MethodGet, "/api/v1/domains/private-team/export", nil) |
| 102 | + assertStatus(t, res, http.StatusOK) |
| 103 | + if got := res.Header().Get("X-Total-Count"); got != "1" { |
| 104 | + t.Fatalf("X-Total-Count = %q, want 1", got) |
| 105 | + } |
| 106 | + |
| 107 | + // 可见但空的 domain → 404 |
| 108 | + assertStatus(t, testRequest(t, router, adminToken, http.MethodPut, "/api/v1/domains/empty-public", map[string]any{"readme": "# Empty"}), http.StatusCreated) |
| 109 | + assertStatus(t, testRequest(t, router, adminToken, http.MethodGet, "/api/v1/domains/empty-public/export", nil), http.StatusNotFound) |
| 110 | +} |
0 commit comments