~bigbes/sr-ht-spec

ref: c6e5d6676fd0a179a022d50362f6a949a95969e2 sr-ht-spec/coreauth/coreauth_test.go -rw-r--r-- 1.9 KiB
c6e5d667 — Eugene Blikh feat(service): review threads and the policy auto-merge gate (spec-by6.3.2) 24 days 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package coreauth

import (
	"context"
	"testing"

	"sourcecraft.dev/bigbes/sr-ht-core/auth"

	"sourcecraft.dev/bigbes/sr-ht-spec/authn"
)

func TestDerive(t *testing.T) {
	const ownerUserID = 42

	tests := []struct {
		name       string
		principal  authn.Principal
		wantMethod string
		wantUserID int
		wantUser   string
	}{
		{
			name:       "owner",
			principal:  authn.Principal{Kind: authn.KindOwner, Owner: "bigbes"},
			wantMethod: auth.AUTH_INTERNAL,
			wantUserID: ownerUserID,
			wantUser:   "bigbes",
		},
		{
			name:       "agent",
			principal:  authn.Principal{Kind: authn.KindAgent, Owner: "bigbes", Agent: "claude-code/spec-writer"},
			wantMethod: auth.AUTH_INTERNAL,
			wantUserID: ownerUserID,
			wantUser:   "bigbes",
		},
		{
			name:       "anonymous",
			principal:  authn.Anonymous(),
			wantMethod: auth.AUTH_ANON_INTERNAL,
			wantUserID: 0,
			wantUser:   "",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ac := Derive(tt.principal, ownerUserID)
			if ac.AuthMethod != tt.wantMethod {
				t.Errorf("AuthMethod = %q, want %q", ac.AuthMethod, tt.wantMethod)
			}
			if ac.UserID != tt.wantUserID {
				t.Errorf("UserID = %d, want %d", ac.UserID, tt.wantUserID)
			}
			if ac.Username != tt.wantUser {
				t.Errorf("Username = %q, want %q", ac.Username, tt.wantUser)
			}
		})
	}
}

func TestContext(t *testing.T) {
	const ownerUserID = 7
	ctx := Context(context.Background(), authn.Principal{Kind: authn.KindOwner, Owner: "bigbes"}, ownerUserID)

	ac := auth.ForContext(ctx)
	if ac == nil {
		t.Fatal("auth.ForContext returned nil; expected an AuthContext")
	}
	if ac.AuthMethod != auth.AUTH_INTERNAL {
		t.Errorf("AuthMethod = %q, want %q", ac.AuthMethod, auth.AUTH_INTERNAL)
	}
	if ac.UserID != ownerUserID {
		t.Errorf("UserID = %d, want %d", ac.UserID, ownerUserID)
	}
	if ac.Username != "bigbes" {
		t.Errorf("Username = %q, want %q", ac.Username, "bigbes")
	}
}