-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
117 lines (93 loc) · 2.07 KB
/
main_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
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetRequiredEnv(t *testing.T) {
// arrange
varName := "ENV_VAR_NAME"
varValue := "ENV_VAR_VALUE"
os.Setenv(varName, varValue)
// act
value := getRequiredEnv(varName)
// assert
assert.Equal(t, varValue, value)
}
func TestGetRequiredEnvWithMissingEnvironment(t *testing.T) {
// act & assert
assert.Panics(t, func() {
getRequiredEnv("MISSING_ENV_VAR_NAME")
})
}
func TestNewAuthenticator(t *testing.T) {
// arrange
issuer, sampleJwks := setupFakeAuthServer()
// act
config := newAuthenticatorConfig()
// assert
assert.Equal(t, []byte(sampleJwks), config.KeySetJSON)
assert.Equal(t, issuer, config.Issuer)
}
func TestNewAuthenticatorWithInvalidTokenUrl(t *testing.T) {
// arrange
setupFakeAuthServer()
os.Setenv(AUTH_JWKS_LOCATION, "invalid_url")
// act
assert.Panics(t, func() {
newAuthenticatorConfig()
})
}
func TestConfigureGin(t *testing.T) {
// arrange
setupFakeAuthServer()
os.Setenv(CURRCONV_API_KEY, "fake_key")
// act
r := configureGin()
// assert
assert.NotNil(t, r)
assert.Len(t, r.RouterGroup.Handlers, 4)
}
func TestGetRoot(t *testing.T) {
// arrange
setupFakeAuthServer()
os.Setenv(CURRCONV_API_KEY, "fake_key")
r := configureGin()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
// act
r.ServeHTTP(w, req)
// assert
assert.Equal(t, w.Code, http.StatusOK)
}
func setupFakeAuthServer() (string, string) {
issuer := "https://cognito-idp.$AWS_REGION.amazonaws.com/$POOL_ID"
sampleJwks := `
{
"keys": [{
"kid": "1234example=",
"alg": "RS256",
"kty": "RSA",
"e": "AQAB",
"n": "1234567890",
"use": "sig"
}, {
"kid": "5678example=",
"alg": "RS256",
"kty": "RSA",
"e": "AQAB",
"n": "987654321",
"use": "sig"
}]
}`
svr := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, sampleJwks)
}))
os.Setenv(AUTH_TOKEN_ISS, issuer)
os.Setenv(AUTH_JWKS_LOCATION, svr.URL)
return issuer, sampleJwks
}