-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparser_test.go
159 lines (151 loc) · 3.57 KB
/
parser_test.go
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package atlas_claims
import (
"context"
"fmt"
"testing"
"time"
"google.golang.org/grpc/metadata"
)
func TestGetAccountID(t *testing.T) {
var accountIDTests = []struct {
claims *Claims
expected string
err error
}{
{
claims: &Claims{
AccountId: "id-abc-123",
},
expected: "id-abc-123",
err: nil,
},
{
claims: &Claims{
AccountId: "id-abc-123",
CompartmentID: "cmp-1",
},
expected: "id-abc-123",
err: nil,
},
{
claims: &Claims{},
expected: "",
err: errMissingField,
},
}
for _, test := range accountIDTests {
token := makeToken(test.claims, t)
ctx := contextWithToken(token, DefaultSubjectAuthType)
actual, err := GetAccountID(ctx)
if err != test.err {
t.Errorf("Invalid error value: %v - expected %v", err, test.err)
}
if actual != test.expected {
t.Errorf("Invalid AccountID: %v - expected %v", actual, test.expected)
}
}
}
func TestGetCompartmentID(t *testing.T) {
var compartmentIDTests = []struct {
claims *Claims
expected string
}{
{
claims: &Claims{
AccountId: "id-abc-123",
CompartmentID: "",
},
expected: "",
},
{
claims: &Claims{
AccountId: "id-abc-123",
CompartmentID: "cmp-1",
},
expected: "cmp-1",
},
{
claims: &Claims{},
expected: "",
},
}
for _, test := range compartmentIDTests {
token := makeToken(test.claims, t)
ctx := contextWithToken(token, DefaultSubjectAuthType)
actual, _ := GetCompartmentID(ctx)
if actual != test.expected {
t.Errorf("Invalid CompartmentID: %v - expected %v", actual, test.expected)
}
}
}
func TestGetAccountAndCompartmentID(t *testing.T) {
var accountIDTests = []struct {
claims *Claims
expectedAccount string
expectedCompartment string
err error
}{
{
claims: &Claims{
AccountId: "id-abc-123",
},
expectedAccount: "id-abc-123",
expectedCompartment: "",
err: nil,
},
{
claims: &Claims{
AccountId: "id-abc-123",
CompartmentID: "",
},
expectedAccount: "id-abc-123",
expectedCompartment: "",
err: nil,
},
{
claims: &Claims{
AccountId: "id-abc-123",
CompartmentID: "cmp-1",
},
expectedAccount: "id-abc-123",
expectedCompartment: "cmp-1",
err: nil,
},
{
claims: &Claims{},
expectedAccount: "",
expectedCompartment: "",
err: errMissingField,
},
}
for _, test := range accountIDTests {
token := makeToken(test.claims, t)
ctx := contextWithToken(token, DefaultSubjectAuthType)
account, compartment, err := GetAccountAndCompartmentID(ctx)
if err != test.err {
t.Errorf("Invalid error value: %v - expected %v", err, test.err)
}
if account != test.expectedAccount {
t.Errorf("Invalid AccountID: %v - expected %v", account, test.expectedAccount)
}
if compartment != test.expectedCompartment {
t.Errorf("Invalid CompartmentID: %v - expected %v", compartment, test.expectedCompartment)
}
}
}
// contextWithToken creates a context with a JWT
func contextWithToken(token, tokenType string) context.Context {
md := metadata.Pairs(
"authorization", fmt.Sprintf("%s %s", tokenType, token),
)
return metadata.NewIncomingContext(context.Background(), md)
}
// makeToken creates a JWT
func makeToken(claims *Claims, t *testing.T) string {
cfgStandardClaimsExpires := time.Hour * 24 * 365
testToken, err := BuildJwt(claims, "hmackey", cfgStandardClaimsExpires)
if err != nil {
t.Fatalf("Error when building token: %v", err)
}
return testToken
}