-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
60 lines (50 loc) · 1.61 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
package main
import (
"encoding/json"
"github.com/aws/aws-lambda-go/events"
"github.com/flow-lab/dlog"
"github.com/stretchr/testify/assert"
"io/ioutil"
"testing"
)
const requestID = "1-581cf771-a006649127e371903a2de979"
// Toke will expire ...
// TODO [grokrz]: mock ?
//func TestValid(t *testing.T) {
// os.Setenv("COGNITO_IDS", "eu-west-1_yubvj7uwx,eu-west-1_yubvj7uwx")
// os.Setenv("AWS_REGION", "eu-west-1")
// var inputJSON = readFile("token-valid.json")
// var event events.APIGatewayCustomAuthorizerRequest
// if err := json.Unmarshal(inputJSON, &event); err != nil {
// assert.FailNow(t, "unable to deserialize", inputJSON)
// }
// output, err := Process(event, dlog.NewRequestLogger(requestID, "test"))
//
// assert.Nil(t, err)
// assert.NotNil(t, output.PrincipalID)
//}
func TestExpired(t *testing.T) {
var inputJSON = readFile("token-expired.json")
var event events.APIGatewayCustomAuthorizerRequest
if err := json.Unmarshal(inputJSON, &event); err != nil {
assert.FailNow(t, "unable to deserialize", inputJSON)
}
_, err := Process(event, dlog.NewRequestLogger(requestID, "test"))
assert.Equal(t, "unauthorized", err.Error())
}
func TestInvalid(t *testing.T) {
var inputJSON = readFile("token-invalid.json")
var event events.APIGatewayCustomAuthorizerRequest
if err := json.Unmarshal(inputJSON, &event); err != nil {
assert.FailNow(t, "unable to deserialize", inputJSON)
}
_, err := Process(event, dlog.NewRequestLogger(requestID, "test"))
assert.Equal(t, "unauthorized", err.Error())
}
func readFile(path string) []byte {
f, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
return f
}