aboutsummaryrefslogtreecommitdiff
path: root/internal/api/server_thin_test.go
blob: 8b9dfac1f2a11b42f03d701f2e81c75b0b166ca1 (plain) (blame)
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
package api

import (
	"context"
	"crypto/ed25519"
	"crypto/rand"
	"encoding/base64"
	"encoding/json"
	"github.com/Fuwn/plutia/internal/config"
	"github.com/Fuwn/plutia/internal/ingest"
	"github.com/Fuwn/plutia/internal/storage"
	"github.com/Fuwn/plutia/internal/types"
	"io"
	"net/http"
	"net/http/httptest"
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"
)

func TestThinModeResolveCompatibilityAndProofNotImplemented(t *testing.T) {
	did := "did:plc:thin-http"
	upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/"+did+"/log" {
			w.WriteHeader(http.StatusNotFound)
			return
		}

		w.Header().Set("Content-Type", "application/json")
		_ = json.NewEncoder(w).Encode(thinHTTPOperationLog(t, did))
	}))
	defer upstream.Close()

	tmp := t.TempDir()
	dataDir := filepath.Join(tmp, "data")
	if err := os.MkdirAll(dataDir, 0o755); err != nil {
		t.Fatalf("mkdir: %v", err)
	}

	store, err := storage.OpenPebble(dataDir)
	if err != nil {
		t.Fatalf("open pebble: %v", err)
	}
	defer store.Close()

	if err := store.SetMode(config.ModeThin); err != nil {
		t.Fatalf("set mode: %v", err)
	}

	cfg := config.Default()
	cfg.Mode = config.ModeThin
	cfg.DataDir = dataDir
	cfg.PLCSource = upstream.URL
	cfg.ThinCacheTTL = 24 * time.Hour
	cfg.ThinCacheMaxEntries = 1000

	svc := ingest.NewService(cfg, store, ingest.NewClient(upstream.URL), nil, nil)
	defer svc.Close()

	ts := httptest.NewServer(NewServer(cfg, store, svc, nil).Handler())
	defer ts.Close()

	resp, err := http.Get(ts.URL + "/" + did)
	if err != nil {
		t.Fatalf("compat resolve: %v", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		t.Fatalf("compat resolve status: got %d want 200", resp.StatusCode)
	}

	if got := resp.Header.Get("Content-Type"); !strings.Contains(got, "application/did+ld+json") {
		t.Fatalf("compat resolve content-type mismatch: %s", got)
	}

	var doc map[string]any
	if err := json.NewDecoder(resp.Body).Decode(&doc); err != nil {
		t.Fatalf("decode did doc: %v", err)
	}

	if got, _ := doc["id"].(string); got != did {
		t.Fatalf("did doc id mismatch: got %q want %q", got, did)
	}

	proofResp, err := http.Get(ts.URL + "/did/" + did + "/proof")
	if err != nil {
		t.Fatalf("thin proof request: %v", err)
	}
	defer proofResp.Body.Close()

	if proofResp.StatusCode != http.StatusNotImplemented {
		t.Fatalf("thin proof status: got %d want 501", proofResp.StatusCode)
	}

	var statusBody map[string]any
	statusResp, err := http.Get(ts.URL + "/status")
	if err != nil {
		t.Fatalf("status request: %v", err)
	}
	defer statusResp.Body.Close()

	statusBytes, err := io.ReadAll(statusResp.Body)
	if err != nil {
		t.Fatalf("read status body: %v", err)
	}

	t.Logf("thin_status=%s", strings.TrimSpace(string(statusBytes)))

	if err := json.Unmarshal(statusBytes, &statusBody); err != nil {
		t.Fatalf("decode status: %v", err)
	}

	if mode, _ := statusBody["mode"].(string); mode != config.ModeThin {
		t.Fatalf("status mode mismatch: got %q want %q", mode, config.ModeThin)
	}

	if err := svc.VerifyDID(context.Background(), did); err != nil {
		t.Fatalf("verify thin did: %v", err)
	}
}

func thinHTTPOperationLog(t *testing.T, did string) []map[string]any {
	t.Helper()

	pub, priv, err := ed25519.GenerateKey(rand.Reader)
	if err != nil {
		t.Fatalf("generate key: %v", err)
	}

	ops := make([]map[string]any, 0, 2)
	prev := ""
	for i := 0; i < 2; i++ {
		unsigned := map[string]any{
			"did":       did,
			"didDoc":    map[string]any{"id": did, "seq": i + 1},
			"publicKey": base64.RawURLEncoding.EncodeToString(pub),
		}
		if prev != "" {
			unsigned["prev"] = prev
		}

		payload, _ := json.Marshal(unsigned)
		canon, _ := types.CanonicalizeJSON(payload)
		sig := ed25519.Sign(priv, canon)

		op := map[string]any{}
		for k, v := range unsigned {
			op[k] = v
		}
		op["sigPayload"] = base64.RawURLEncoding.EncodeToString(canon)
		op["sig"] = base64.RawURLEncoding.EncodeToString(sig)

		raw, _ := json.Marshal(op)
		parsed, err := types.ParseOperation(types.ExportRecord{Seq: uint64(i + 1), DID: did, Operation: raw})
		if err != nil {
			t.Fatalf("parse operation: %v", err)
		}

		prev = parsed.CID
		ops = append(ops, map[string]any{
			"operation": op,
			"cid":       parsed.CID,
			"createdAt": time.Now().UTC().Add(time.Duration(i) * time.Second).Format(time.RFC3339),
			"nullified": false,
		})
	}

	return ops
}