Skip to content

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

2.3 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 conf

import "testing"

func TestSyncProviderRequiredFields(t *testing.T) {
	values := map[string]string{"endpoint": "example.com", "accessKey": "key", "secretKey": "secret", "bucket": "notes", "region": "auto"}
	for _, provider := range []int{ProviderS3, ProviderWebDAV, ProviderLocal} {
		if MissingSyncProviderField(provider, values) {
			t.Fatal("complete configuration was rejected")
		}
		for _, key := range SyncProviderRequiredFields(provider) {
			original := values[key]
			values[key] = " \t\n "
			if !MissingSyncProviderField(provider, values) {
				t.Fatalf("provider %d did not enforce advertised field %s", provider, key)
			}
			values[key] = original
		}
	}
	if MissingSyncProviderField(ProviderWebDAV, map[string]string{"endpoint": "example.com"}) {
		t.Fatal("anonymous WebDAV was rejected")
	}
	if len(SyncProviderRequiredFields(ProviderSiYuan)) != 0 {
		t.Fatal("official storage advertises third-party fields")
	}
}

func TestS3EndpointIncludesBucketOrPath(t *testing.T) {
	for _, tc := range []struct {
		endpoint, bucket string
		want             bool
	}{
		{"https://notes.storage.example", "notes", true},
		{" NOTES.storage.example/path ", " Notes ", true},
		{"http(s)://notes.storage.example", "notes", true},
		{"http://notes.storage.example:9000", "notes", true},
		{"https://my.notes.storage.example", "my.notes", true},
		{"https://notebook.storage.example", "notes", false},
		{"https://storage.example/notes", "notes", true},
		{"https://s3.example.com/proxy/", "notes", true},
		{"https://s3.example.com/%6eotes/", "notes", true},
		{"https://s3.example.com", "notes/sub", true},
		{"https://s3.example.com/", "notes", false},
		{"https://s3.example.com", "notes/", false},
		{"https://notes", "notes", false},
		{"https://storage.example?notes.storage.example", "notes", false},
		{"ftp://notes.storage.example", "notes", false},
		{"https://bad host", "bad", false},
		{"https://", "notes", false},
		{"", "notes", false},
		{"https://notes.storage.example", "", false},
	} {
		if got := S3EndpointIncludesBucketOrPath(tc.endpoint, tc.bucket); got != tc.want {
			t.Errorf("%q, %q: got %v, want %v", tc.endpoint, tc.bucket, got, tc.want)
		}
	}
}