~james/core

core/cmd/spindle/ban_cli_test.go -rw-r--r-- 1.7 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
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
package main

import (
	"path/filepath"
	"strings"
	"testing"

	"tangled.org/core/spindle/config"
	"tangled.org/core/spindle/db"
)

// xrpc refuses a banned owner, which leaves the cli as the only way back in
func TestCLIUnbansABannedOwnerStraightOnTheDB(t *testing.T) {
	owner := "did:plc:theowner"
	t.Setenv("SPINDLE_SERVER_OWNER", owner)
	dbPath := filepath.Join(t.TempDir(), "spindle.db")

	if out, code := runCLI(t, dbPath, "ban", owner); code != 0 {
		t.Fatalf("ban = %d: %s", code, out)
	}
	if out, code := runCLI(t, dbPath, "bans"); code != 0 || !strings.Contains(out, owner) {
		t.Fatalf("bans = %d: %q, want the owner listed", code, out)
	}

	d, err := db.Make(t.Context(), dbPath)
	if err != nil {
		t.Fatal(err)
	}
	srv := &config.Server{Owner: owner}
	enrolled, err := db.Enrolls(d, srv, "did:plc:theowner")
	d.Close()
	if err != nil || enrolled {
		t.Fatalf("banned owner enrolls = %v, %v, want a ban to beat being the owner", enrolled, err)
	}

	if out, code := runCLI(t, dbPath, "unban", owner); code != 0 {
		t.Fatalf("unban = %d: %s", code, out)
	}
	if out, code := runCLI(t, dbPath, "bans"); code != 0 || strings.Contains(out, owner) {
		t.Fatalf("bans after unban = %d: %q, want the owner gone", code, out)
	}
	d, err = db.Make(t.Context(), dbPath)
	if err != nil {
		t.Fatal(err)
	}
	defer d.Close()
	enrolled, err = db.Enrolls(d, srv, "did:plc:theowner")
	if err != nil || !enrolled {
		t.Fatalf("unbanned owner enrolls = %v, %v, want the owner served again", enrolled, err)
	}

	if out, code := runCLI(t, dbPath, "unban", owner); code == 0 || !strings.Contains(out, "is not banned") {
		t.Fatalf("unbanning someone not banned = %d: %s, want an error", code, out)
	}
}