Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

1.4 KB
Raw Blame
// SiYuan - https://github.com/siyuan-note/siyuan
// Copyright (c) 2020-present, b3log.org
// SPDX-License-Identifier: AGPL-3.0-or-later

package api

import (
	"os"
	"path/filepath"
	"testing"

	"github.com/siyuan-note/siyuan/kernel/internal/testutil"
	"github.com/siyuan-note/siyuan/kernel/util"
)

func TestIsPathInTemplatesDirJunctionWorkspace(t *testing.T) {
	for _, atWorkspace := range []bool{false, true} {
		t.Run(map[bool]string{false: "ancestor", true: "workspace"}[atWorkspace], func(t *testing.T) {
			alias, _ := testutil.JunctionWorkspace(t, t.TempDir(), atWorkspace)
			previous := util.DataDir
			util.DataDir = filepath.Join(alias, "data")
			t.Cleanup(func() { util.DataDir = previous })
			root := filepath.Join(util.DataDir, "templates")
			if err := os.MkdirAll(root, 0755); err != nil {
				t.Fatal(err)
			}
			file := filepath.Join(root, "test.md")
			if err := os.WriteFile(file, []byte("template"), 0644); err != nil {
				t.Fatal(err)
			}
			if !isPathInTemplatesDir(file) {
				t.Error("valid template below a junction was rejected")
			}
			outside := t.TempDir()
			if err := os.WriteFile(filepath.Join(outside, "test.md"), []byte("outside"), 0644); err != nil {
				t.Fatal(err)
			}
			testutil.Junction(t, filepath.Join(root, "escape"), outside)
			if isPathInTemplatesDir(filepath.Join(root, "escape", "test.md")) {
				t.Error("template escaped its real root")
			}
		})
	}
}