-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeys_test.go
More file actions
97 lines (90 loc) · 2.14 KB
/
keys_test.go
File metadata and controls
97 lines (90 loc) · 2.14 KB
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
package auth
import (
"context"
"github.com/bottledcode/durable-php/cli/appcontext"
"github.com/bottledcode/durable-php/cli/config"
"testing"
)
func TestGetActiveKey(t *testing.T) {
testCases := []struct {
name string
config *config.Config
wantErr bool
}{
{
name: "Should return error when no secrets are available",
config: &config.Config{
Extensions: config.ExtensionsConfig{
Authz: config.AuthzConfig{
Secrets: []string{},
},
},
},
wantErr: true,
},
{
name: "Should return error when secret encoding fails",
config: &config.Config{
Extensions: config.ExtensionsConfig{
Authz: config.AuthzConfig{
Secrets: []string{"invalidSecret"},
},
},
},
wantErr: true,
},
{
name: "Should return key when valid secret",
config: &config.Config{
Extensions: config.ExtensionsConfig{
Authz: config.AuthzConfig{
Secrets: []string{"SGVsbG8sIHdvcmxkIQ=="}, // base64 encoded string of "Hello, world!"
},
},
},
wantErr: false,
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
_, err := getActiveKey(tt.config)
if (err != nil) != tt.wantErr {
t.Errorf("getActiveKey() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
func TestDecorateContextWithUser(t *testing.T) {
var tests = []struct {
desc string
input *User
}{
{
desc: "decorating with nil user",
input: nil,
},
{
desc: "decorating with valid user",
input: &User{UserId: UserId("user"), Roles: []Role{"admin"}},
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
ctx := context.Background()
ctx = DecorateContextWithUser(ctx, tt.input)
if user, ok := ctx.Value(appcontext.CurrentUserKey).(*User); ok {
if user.UserId != tt.input.UserId {
t.Errorf("Expected User ID: %v, got: %v", tt.input.UserId, user.UserId)
}
for i, role := range user.Roles {
if role != tt.input.Roles[i] {
t.Errorf("Expected User Role: %v, got: %v", tt.input.Roles[i], role)
}
}
} else if tt.input != nil {
t.Error("Expected to find an User, but none was found.")
}
})
}
}