File size: 2,079 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package permission

import (
	"context"
	"testing"
)

// ExampleSudoContext shows how to use SudoContext.
func ExampleSudoContext() {
	// the original context could be from anywhere (req.Context() in an http.Handler for example)
	ctx := context.Background()
	SudoContext(ctx, func(ctx context.Context) {
		// within this function scope, ctx now has System privileges
	})
	// once the function returns, the elevated context is canceled, but the original ctx is still valid
}

func TestSudoContext(t *testing.T) {
	SudoContext(context.Background(), func(ctx context.Context) {
		if !System(ctx) {
			t.Error("System(ctx) == false; want true")
		}
		err := LimitCheckAny(ctx, System)
		if err != nil {
			t.Errorf("err = %v; want nil", err)
		}
		err = LimitCheckAny(ctx, System, Admin, User)
		if err != nil {
			t.Errorf("err = %v; want nil", err)
		}
	})
}

func TestWithoutAuth(t *testing.T) {
	check := func(ctx context.Context, name string) {
		t.Run(name, func(t *testing.T) {
			ctx = WithoutAuth(ctx)
			if User(ctx) {
				t.Error("User() = true; want false")
			}
			if Admin(ctx) {
				t.Error("Admin() = true; want false")
			}
			if System(ctx) {
				t.Error("System() = true; want false")
			}
			if Service(ctx) {
				t.Error("Service() = true; want false")
			}
			if ServiceID(ctx) != "" {
				t.Errorf("SeriviceID() = %s; want empty string", ServiceID(ctx))
			}
			if UserID(ctx) != "" {
				t.Errorf("UserID() = %s; want empty string", UserID(ctx))
			}
			if SystemComponentName(ctx) != "" {
				t.Errorf("SystemComponentName() = %s; want empty string", SystemComponentName(ctx))
			}
		})
	}
	ctx := context.Background()
	data := []struct {
		ctx  context.Context
		name string
	}{
		{name: "user_role_user", ctx: UserContext(ctx, "bob", RoleUser)},
		{name: "user_role_unknown", ctx: UserContext(ctx, "bob", RoleUnknown)},
		{name: "user_role_admin", ctx: UserContext(ctx, "bob", RoleAdmin)},
		{name: "system", ctx: SystemContext(ctx, "test")},
		{name: "service", ctx: ServiceContext(ctx, "test")},
	}

	for _, d := range data {
		check(d.ctx, d.name)
	}
}