interactions yay!!
This commit is contained in:
parent
b9a05bedf9
commit
f60033a3e4
30 changed files with 716 additions and 44 deletions
93
interactions/utils_test.go
Normal file
93
interactions/utils_test.go
Normal file
|
@ -0,0 +1,93 @@
|
|||
package interactions_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto"
|
||||
"crypto/ed25519"
|
||||
"encoding/hex"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.sapphic.engineer/roleypoly/v4/discord"
|
||||
"git.sapphic.engineer/roleypoly/v4/interactions"
|
||||
"github.com/goccy/go-json"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func makeInteractions(t *testing.T) (*interactions.Interactions, *discord.GuildServiceMock, ed25519.PrivateKey) {
|
||||
pub, priv, err := ed25519.GenerateKey(nil)
|
||||
if err != nil {
|
||||
t.Errorf("makeInteractions: failed to generate key: %v", err)
|
||||
}
|
||||
|
||||
gs := &discord.GuildServiceMock{}
|
||||
|
||||
i := &interactions.Interactions{
|
||||
PublicKey: pub,
|
||||
Guilds: gs,
|
||||
Router: interactions.NewInteractionRouter(),
|
||||
OK: true,
|
||||
}
|
||||
|
||||
return i, gs, priv
|
||||
}
|
||||
|
||||
func cmdHelloWorld(ix interactions.Interaction) (interactions.InteractionResponse, error) {
|
||||
return interactions.String("Hello world!", true)
|
||||
}
|
||||
|
||||
func sign(t *testing.T, key ed25519.PrivateKey, body []byte) (string, string) {
|
||||
timestamp := time.Now().UTC().Format(time.RFC3339)
|
||||
|
||||
payloadBuf := bytes.Buffer{}
|
||||
payloadBuf.WriteString(timestamp)
|
||||
payloadBuf.Write(body)
|
||||
|
||||
signature, err := key.Sign(nil, payloadBuf.Bytes(), crypto.Hash(0))
|
||||
if err != nil {
|
||||
t.Errorf("signing failed: %v", err)
|
||||
}
|
||||
|
||||
sigHex := hex.EncodeToString(signature)
|
||||
|
||||
return sigHex, timestamp
|
||||
}
|
||||
|
||||
func sendInteraction(t *testing.T, i *interactions.Interactions, key ed25519.PrivateKey, ix interactions.Interaction) (ir interactions.InteractionResponse) {
|
||||
app := fiber.New()
|
||||
i.Routes(app)
|
||||
|
||||
body, err := json.Marshal(ix)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
sig, ts := sign(t, key, body)
|
||||
|
||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(body))
|
||||
req.Header.Add("X-Signature-Ed25519", sig)
|
||||
req.Header.Add("X-Signature-Timestamp", ts)
|
||||
|
||||
resp, err := app.Test(req)
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
err = json.NewDecoder(resp.Body).Decode(&ir)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func mkInteraction(name string) interactions.Interaction {
|
||||
return interactions.Interaction{
|
||||
Type: interactions.TypeApplicationCommand,
|
||||
Data: interactions.InteractionData{
|
||||
Name: name,
|
||||
},
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue