forked from orktes/go-alexa-smarthome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
94 lines (80 loc) · 1.78 KB
/
handler_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
package smarthome
import (
"encoding/json"
"testing"
)
func TestHandleAuth(t *testing.T) {
reqJSON := `
{
"directive": {
"header": {
"namespace": "Alexa.Authorization",
"name": "AcceptGrant",
"payloadVersion": "3",
"messageId": "1bd5d003-31b9-476f-ad03-71d471922820",
"correlationToken": "dFMb0z+PgpgdDmluhJ1LddFvSqZ/jCc8ptlAKulUj90jSqg=="
},
"payload": {
"grant": {
"type": "OAuth2.AuthorizationCode",
"code": "ANUbUKCJqlBOpMhwYWxU"
},
"grantee": {
"type": "BearerToken",
"token": "access-token-from-skill"
}
}
}
}
`
authFN := AuthorizationFunc(func(req AcceptGrantRequest) error {
if req.Grant.Code != "ANUbUKCJqlBOpMhwYWxU" {
t.Error("Wrong code received")
}
if req.Grantee.Token != "access-token-from-skill" {
t.Error("Fox bauh")
}
return nil
})
sh := New(authFN)
req := &Request{}
json.Unmarshal([]byte(reqJSON), req)
res := sh.Handle(req)
if res.Event.Header.Name != "AcceptGrant.Response" {
t.Error("Failed")
}
}
func TestHandlePowerControllerTurnOn(t *testing.T) {
reqJSON := `
{
"directive": {
"header": {
"namespace": "Alexa.PowerController",
"name": "TurnOn",
"payloadVersion": "3",
"messageId": "1bd5d003-31b9-476f-ad03-71d471922820",
"correlationToken": "dFMb0z+PgpgdDmluhJ1LddFvSqZ/jCc8ptlAKulUj90jSqg=="
},
"endpoint": {
"scope": {
"type": "BearerToken",
"token": "access-token-from-skill"
},
"endpointId": "endpoint-001",
"cookie": {}
},
"payload": {}
}
}
`
sh := New(nil)
req := &Request{}
json.Unmarshal([]byte(reqJSON), req)
res := sh.Handle(req)
if res.Event.Header.Name != "Response" {
t.Error("Failed")
}
if len(res.Context.(EndpointResponse).Properties) != 0 {
t.Error("Nothing should be returned")
}
}