~bigbes/sr-ht-dolt

ref: ede3b0bb2671ffde35a66527b920c40eaccc096f sr-ht-dolt/authn/ctx_test.go -rw-r--r-- 1.7 KiB
ede3b0bb — Eugene Blikh go.mod: take the shared libraries' current heads 2 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
package authn

import (
	"context"
	"testing"

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

	"sourcecraft.dev/bigbes/sr-ht-dolt/core"
)

func TestCallerFromContext_Anonymous(t *testing.T) {
	if got := CallerFromContext(context.Background()); got != nil {
		t.Fatalf("expected nil caller for empty context, got %+v", got)
	}
}

func TestWithCallerRoundTrip(t *testing.T) {
	ac := &auth.AuthContext{UserID: 7, Username: "bigbes"}
	ctx := WithCaller(context.Background(), ac)
	got := CallerFromContext(ctx)
	if got != ac {
		t.Fatalf("expected to retrieve the stored caller, got %+v", got)
	}
}

func TestAsCoreCaller_Nil(t *testing.T) {
	if got := AsCoreCaller(nil); got != nil {
		t.Fatalf("nil auth context must map to nil (anonymous) caller, got %+v", got)
	}
}

func TestAsCoreCaller_Mapping(t *testing.T) {
	cases := []struct {
		name          string
		userType      string
		wantType      core.UserType
		wantSuspended bool
	}{
		{"user", auth.USER_TYPE_USER, core.UserTypeUser, false},
		{"admin", auth.USER_TYPE_ADMIN, core.UserTypeAdmin, false},
		{"pending", auth.USER_TYPE_PENDING, core.UserTypePending, false},
		{"suspended", auth.USER_TYPE_SUSPENDED, core.UserTypeSuspended, true},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			ac := &auth.AuthContext{UserID: 42, Username: "u", UserType: tc.userType}
			got := AsCoreCaller(ac)
			if got == nil {
				t.Fatal("expected non-nil caller")
			}
			if got.UserID != 42 || got.Username != "u" {
				t.Fatalf("identity not carried through: %+v", got)
			}
			if got.UserType != tc.wantType {
				t.Fatalf("UserType = %q, want %q", got.UserType, tc.wantType)
			}
			if got.Suspended != tc.wantSuspended {
				t.Fatalf("Suspended = %v, want %v", got.Suspended, tc.wantSuspended)
			}
		})
	}
}