-
Notifications
You must be signed in to change notification settings - Fork 1
/
policy_test.go
122 lines (90 loc) · 2.59 KB
/
policy_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
package g8
import (
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)
func TestHasMethodsEmpty(t *testing.T) {
// Given:
c := APIGatewayCustomAuthorizerContext{}
// Then:
assert.False(t, c.hasAtLeastOneAllowedMethod) // <-- default value is false
}
func TestHasMethodsNonEmptyButContainsAllDenies(t *testing.T) {
// Given:
c := APIGatewayCustomAuthorizerContext{}
// When
c.DenyAllMethods()
// Then:
assert.False(t, c.hasAtLeastOneAllowedMethod)
}
func TestHasMethodsAllowsAllMethods(t *testing.T) {
// Given:
c := APIGatewayCustomAuthorizerContext{}
// When
c.AllowAllMethods()
// Then:
assert.True(t, c.hasAtLeastOneAllowedMethod)
}
func TestHasMethodsHasMixedAllowAndDenyMethods(t *testing.T) {
// Given:
c := APIGatewayCustomAuthorizerContext{}
// When
c.DenyMethod(http.MethodPost, "/pets/*")
c.DenyMethod(http.MethodDelete, "/cars/*")
c.AllowMethod(http.MethodGet, "/users/*") // <-- !!!
c.DenyMethod(http.MethodPost, "/picture/update")
c.DenyMethod(http.MethodPost, "/picture/assign")
c.DenyMethod(http.MethodPut, "/users/new")
// Then:
assert.True(t, c.hasAtLeastOneAllowedMethod)
}
func TestHasMethodsAllDenyMethods(t *testing.T) {
// Given:
c := APIGatewayCustomAuthorizerContext{}
// When
c.DenyMethod(http.MethodPost, "/pets/*")
c.DenyMethod(http.MethodDelete, "/cars/*")
c.DenyMethod(http.MethodPost, "/picture/update")
c.DenyMethod(http.MethodPost, "/picture/assign")
c.DenyMethod(http.MethodPut, "/users/new")
// Then:
assert.False(t, c.hasAtLeastOneAllowedMethod, "No methods allowed")
}
func TestBuildResourceArn(t *testing.T) {
// Given:
m := methodARN{
Region: "eu-west-1",
AccountID: "aws-account-id",
APIID: "*",
Stage: "*",
}
// When
resourceARN := m.buildResourceARN(http.MethodPost, "/pets/*")
// Then:
assert.Equal(t, "arn:aws:execute-api:eu-west-1:aws-account-id:*/*/POST/pets/*", resourceARN)
}
func TestBuildResourceArnAllowAll(t *testing.T) {
// Given:
m := methodARN{
Region: "*",
AccountID: "aws-account-id",
APIID: "*",
Stage: "*",
}
// When
resourceARN := m.buildResourceARN(All, "*")
// Then:
assert.Equal(t, "arn:aws:execute-api:*:aws-account-id:*/*/*/*", resourceARN)
}
func TestParseMethodARN(t *testing.T) {
// Given:
strMethodARN := "arn:aws:execute-api:eu-west-1:123456789012:oy1e34abcd/main/GET/test-endpoint"
// When:
methodARN := parseFromMethodARN(strMethodARN)
// Then:
assert.Equal(t, "eu-west-1", methodARN.Region)
assert.Equal(t, "123456789012", methodARN.AccountID)
assert.Equal(t, "oy1e34abcd", methodARN.APIID)
assert.Equal(t, "main", methodARN.Stage)
}