Skip to content

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

2.7 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 model

import (
	"net/http"
	"net/http/httptest"
	"slices"
	"testing"

	"github.com/gin-gonic/gin"
	"github.com/siyuan-note/siyuan/kernel/sql"
	"github.com/siyuan-note/siyuan/kernel/util"
)

func TestFilterBookmarkLabelsByPublishAccess(t *testing.T) {
	const (
		boxID             = "20260729000000-boxid01"
		publicDocID       = "20260729000001-public1"
		protectedDocID    = "20260729000002-protect"
		hiddenDocID       = "20260729000003-hidden1"
		disabledDocID     = "20260729000004-disable"
		protectedPassword = "password"
	)
	publishAccess := PublishAccess{
		{ID: protectedDocID, Visible: true, Password: protectedPassword},
		{ID: hiddenDocID, Visible: false},
		{ID: disabledDocID, Visible: true, Disable: true},
	}
	blocks := []*sql.BookmarkLabelBlock{
		{Label: "shared", Box: boxID, Path: "/" + publicDocID + ".sy"},
		{Label: "shared", Box: boxID, Path: "/" + protectedDocID + ".sy"},
		{Label: "protected", Box: boxID, Path: "/" + protectedDocID + ".sy"},
		{Label: "hidden", Box: boxID, Path: "/" + hiddenDocID + ".sy"},
		{Label: "disabled", Box: boxID, Path: "/" + disabledDocID + ".sy"},
		{Label: "", Box: boxID, Path: "/" + publicDocID + ".sy"},
		nil,
	}

	c, _ := gin.CreateTestContext(httptest.NewRecorder())
	c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
	filtered := filterBookmarkLabelsByPublishAccess(c, publishAccess, blocks)
	if !slices.Equal(filtered, []string{"shared"}) {
		t.Fatalf("unexpected unauthenticated bookmark labels: %v", filtered)
	}

	c.Request.AddCookie(&http.Cookie{
		Name:  "publish-auth-" + protectedDocID,
		Value: util.SHA256Hash([]byte(protectedDocID + protectedPassword)),
	})
	filtered = filterBookmarkLabelsByPublishAccess(c, publishAccess, blocks)
	if !slices.Equal(filtered, []string{"protected", "shared"}) {
		t.Fatalf("unexpected authenticated bookmark labels: %v", filtered)
	}
}

func TestFilterBlocksByPublishAccessRejectsDisabledBlocks(t *testing.T) {
	const (
		boxID         = "20260729000100-boxid01"
		publicDocID   = "20260729000101-public1"
		disabledDocID = "20260729000102-disable"
	)
	publishAccess := PublishAccess{
		{ID: disabledDocID, Visible: true, Disable: true},
	}
	blocks := []*Block{
		{ID: publicDocID, Box: boxID, Path: "/" + publicDocID + ".sy"},
		{ID: disabledDocID, Box: boxID, Path: "/" + disabledDocID + ".sy"},
	}

	c, _ := gin.CreateTestContext(httptest.NewRecorder())
	c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
	filtered := FilterBlocksByPublishAccess(c, publishAccess, blocks)
	if len(filtered) != 1 || filtered[0].ID != publicDocID {
		t.Fatalf("unexpected filtered blocks: %+v", filtered)
	}
}