-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_test.go
137 lines (128 loc) · 5.64 KB
/
http_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
package escalation
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
"google.golang.org/api/cloudresourcemanager/v1"
)
type policyManagerMock struct {
getPolicyFunc func(ctx context.Context, crmService *cloudresourcemanager.Service, projectID string) (*cloudresourcemanager.Policy, error)
setPolicyFunc func(ctx context.Context, crmService *cloudresourcemanager.Service, projectID string, policy *cloudresourcemanager.Policy, member, role string) ([]*cloudresourcemanager.Binding, error)
}
func (pmm *policyManagerMock) getPolicy(ctx context.Context, crmService *cloudresourcemanager.Service, projectID string) (*cloudresourcemanager.Policy, error) {
return pmm.getPolicyFunc(ctx, crmService, projectID)
}
func (pmm *policyManagerMock) setPolicy(ctx context.Context, crmService *cloudresourcemanager.Service, projectID string, policy *cloudresourcemanager.Policy, member, role string) ([]*cloudresourcemanager.Binding, error) {
return pmm.setPolicyFunc(ctx, crmService, projectID, policy, member, role)
}
func Test_AddMember(t *testing.T) {
member := "[email protected]"
roleName := "roles/product.rolename"
tests := []struct {
name string
policyManager *policyManagerMock
expectedNumberOfBindings int
}{
{
name: "no bindings - create new binding",
policyManager: &policyManagerMock{
getPolicyFunc: func(ctx context.Context, crmService *cloudresourcemanager.Service, projectID string) (*cloudresourcemanager.Policy, error) {
return &cloudresourcemanager.Policy{}, nil
},
setPolicyFunc: func(ctx context.Context, crmService *cloudresourcemanager.Service, projectID string, policy *cloudresourcemanager.Policy, member, role string) ([]*cloudresourcemanager.Binding, error) {
return policy.Bindings, nil
},
},
expectedNumberOfBindings: 1,
},
{
name: "no binding for role - create new binding",
policyManager: &policyManagerMock{
getPolicyFunc: func(ctx context.Context, crmService *cloudresourcemanager.Service, projectID string) (*cloudresourcemanager.Policy, error) {
return &cloudresourcemanager.Policy{
Bindings: []*cloudresourcemanager.Binding{},
}, nil
},
setPolicyFunc: func(ctx context.Context, crmService *cloudresourcemanager.Service, projectID string, policy *cloudresourcemanager.Policy, member, role string) ([]*cloudresourcemanager.Binding, error) {
return policy.Bindings, nil
},
},
expectedNumberOfBindings: 1,
},
{
name: "existing binding for role with same condition title - override exisiting binding",
policyManager: &policyManagerMock{
getPolicyFunc: func(ctx context.Context, crmService *cloudresourcemanager.Service, projectID string) (*cloudresourcemanager.Policy, error) {
return &cloudresourcemanager.Policy{
Bindings: []*cloudresourcemanager.Binding{
{
Role: roleName,
Condition: &cloudresourcemanager.Expr{
Title: generateTitle(member),
},
},
},
}, nil
},
setPolicyFunc: func(ctx context.Context, crmService *cloudresourcemanager.Service, projectID string, policy *cloudresourcemanager.Policy, member, role string) ([]*cloudresourcemanager.Binding, error) {
return policy.Bindings, nil
},
},
expectedNumberOfBindings: 1,
},
{
name: "existing binding for role with no condition - create new binding and dont touch existing one",
policyManager: &policyManagerMock{
getPolicyFunc: func(ctx context.Context, crmService *cloudresourcemanager.Service, projectID string) (*cloudresourcemanager.Policy, error) {
return &cloudresourcemanager.Policy{
Bindings: []*cloudresourcemanager.Binding{
{
Role: roleName,
},
},
}, nil
},
setPolicyFunc: func(ctx context.Context, crmService *cloudresourcemanager.Service, projectID string, policy *cloudresourcemanager.Policy, member, role string) ([]*cloudresourcemanager.Binding, error) {
return policy.Bindings, nil
},
},
expectedNumberOfBindings: 2,
},
{
name: "existing binding for role with existing unrecognised condition - create new binding and dont touch existing one",
policyManager: &policyManagerMock{
getPolicyFunc: func(ctx context.Context, crmService *cloudresourcemanager.Service, projectID string) (*cloudresourcemanager.Policy, error) {
return &cloudresourcemanager.Policy{
Bindings: []*cloudresourcemanager.Binding{
{
Role: roleName,
Condition: &cloudresourcemanager.Expr{
Title: "unregonised condition",
},
},
},
}, nil
},
setPolicyFunc: func(ctx context.Context, crmService *cloudresourcemanager.Service, projectID string, policy *cloudresourcemanager.Policy, member, role string) ([]*cloudresourcemanager.Binding, error) {
return policy.Bindings, nil
},
},
expectedNumberOfBindings: 2,
},
}
for _, test := range tests {
tc := test
t.Run(tc.name, func(t *testing.T) {
bindings, err := addMember(context.Background(), tc.policyManager, nil, "random-project-id", member, roleName, time.Now())
require.NoError(t, err)
require.Len(t, bindings, tc.expectedNumberOfBindings)
})
}
}
func Test_GetMemberFromJWT(t *testing.T) {
sampleJWT := `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJpYXQiOjE2MDIxOTU1NDcsImV4cCI6MTYzMzczMTU0NywiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsImhkIjoieW91cm9yZy5jb20iLCJlbWFpbCI6InJhbmRvbS5ndXlAeW91cm9yZy5jb20iLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZX0.NK9DPyqIdMpFgoc1Qm1BJvhibcbOXjeZek4j0pDMwDg`
email, err := getEmailFromJWT(sampleJWT)
require.NoError(t, err)
require.Equal(t, "[email protected]", *email)
}