-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractions_test.go
More file actions
152 lines (126 loc) · 3.98 KB
/
interactions_test.go
File metadata and controls
152 lines (126 loc) · 3.98 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package interactions
import (
"bytes"
"context"
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/Postcord/objects"
)
func PrepareTest() (*App, ed25519.PrivateKey, ed25519.PublicKey) {
// Generate a keypair for use in testing
pub, priv, err := ed25519.GenerateKey(nil)
if err != nil {
panic(err)
}
app, err := New(&Config{
PublicKey: hex.EncodeToString(pub),
})
if err != nil {
panic(err)
}
return app, priv, pub
}
// Generates a valid request for the given interaction
func generateValid(i *objects.Interaction, priv ed25519.PrivateKey) *http.Request {
buf := bytes.Buffer{}
enc := json.NewEncoder(&buf)
enc.Encode(i)
req := httptest.NewRequest("POST", "/", &buf)
timestamp := time.Now().Format(time.RFC3339)
req.Header.Set("X-Signature-Timestamp", timestamp)
signature := ed25519.Sign(priv, append([]byte(timestamp), buf.Bytes()...))
req.Header.Set("X-Signature-Ed25519", hex.EncodeToString(signature))
return req
}
func Test_HTTPHandler(t *testing.T) {
// Generate a keypair for use in testing
app, priv, _ := PrepareTest()
req := generateValid(&objects.Interaction{
DiscordBaseObject: objects.DiscordBaseObject{ID: objects.Snowflake(1234)},
Type: objects.InteractionRequestPing,
Version: 1,
}, priv)
w := httptest.NewRecorder()
app.HTTPHandler()(w, req)
if w.Code != 200 {
t.Errorf("Expected 200, got %d", w.Code)
}
}
func Test_HTTPHandler_InvalidSignature(t *testing.T) {
// Generate a keypair for use in testing
app, _, _ := PrepareTest()
buf := bytes.Buffer{}
enc := json.NewEncoder(&buf)
enc.Encode(objects.Interaction{
DiscordBaseObject: objects.DiscordBaseObject{ID: objects.Snowflake(1234)},
Type: objects.InteractionRequestPing,
Version: 1,
})
req := httptest.NewRequest("POST", "/", &buf)
timestamp := time.Now().Format(time.RFC3339)
req.Header.Set("X-Signature-Timestamp", timestamp)
req.Header.Set("X-Signature-Ed25519", "invalid")
w := httptest.NewRecorder()
app.HTTPHandler()(w, req)
if w.Code != 401 {
t.Errorf("Expected 401, got %d", w.Code)
}
}
// Test a full interaction and ensure it is properly handled, and returns a valid response
func Test_HTTPHandler_FullEvent(t *testing.T) {
// Generate a keypair for use in testing
app, priv, _ := PrepareTest()
app.CommandHandler(func(context.Context, *objects.Interaction) *objects.InteractionResponse {
return &objects.InteractionResponse{
Type: objects.ResponseChannelMessageWithSource,
Data: &objects.InteractionApplicationCommandCallbackData{
Content: "Success",
},
}
})
data, err := json.Marshal(&objects.ApplicationCommandInteractionData{
DiscordBaseObject: objects.DiscordBaseObject{ID: objects.Snowflake(1234)},
Name: "test",
Type: objects.CommandTypeChatInput,
})
if err != nil {
t.Errorf("Failed to marshal test interaction: %s", err)
}
req := generateValid(&objects.Interaction{
DiscordBaseObject: objects.DiscordBaseObject{ID: objects.Snowflake(1234)},
Type: objects.InteractionApplicationCommand,
ApplicationID: objects.Snowflake(1234),
Data: data,
GuildID: objects.Snowflake(1234),
ChannelID: objects.Snowflake(1234),
Member: &objects.GuildMember{
User: &objects.User{
DiscordBaseObject: objects.DiscordBaseObject{ID: objects.Snowflake(1234)},
Username: "Test",
Discriminator: "1234",
},
},
Version: 1,
}, priv)
w := httptest.NewRecorder()
app.HTTPHandler()(w, req)
if w.Code != 200 {
t.Errorf("Expected 200, got %d", w.Code)
}
if w.Header().Get("Content-Type") != "application/json" {
t.Errorf("Expected application/json, got %s", w.Header().Get("Content-Type"))
}
var resp objects.InteractionResponse
err = json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil {
t.Errorf("Expected valid response, got %v", err)
}
if resp.Data.Content != "Success" {
t.Errorf("Expected 'Success', got '%s'", resp.Data.Content)
}
}