79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
package interactions_test
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/ed25519"
|
|
"encoding/hex"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"git.sapphic.engineer/roleypoly/v4/discord"
|
|
"git.sapphic.engineer/roleypoly/v4/interactions"
|
|
"git.sapphic.engineer/roleypoly/v4/roleypoly"
|
|
"github.com/goccy/go-json"
|
|
"github.com/gofiber/fiber/v3"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestNewInteractions(t *testing.T) {
|
|
pub, _, _ := ed25519.GenerateKey(nil)
|
|
key := hex.EncodeToString(pub)
|
|
|
|
gs := &discord.GuildService{}
|
|
|
|
i := interactions.NewInteractions(key, gs)
|
|
assert.True(t, i.OK, "interactions OK")
|
|
assert.Equal(t, string(i.PublicKey), key, "public key accepted from args")
|
|
}
|
|
|
|
func TestPostHandler(t *testing.T) {
|
|
i, _, key := makeInteractions(t)
|
|
app := fiber.New()
|
|
i.Routes(app)
|
|
|
|
body, err := json.Marshal(map[string]any{
|
|
"type": 1,
|
|
})
|
|
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)
|
|
|
|
res, err := app.Test(req, fiber.TestConfig{})
|
|
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
|
}
|
|
|
|
func TestPostHandlerSigFail(t *testing.T) {
|
|
i, _, _ := makeInteractions(t)
|
|
app := roleypoly.CreateFiberApp()
|
|
i.Routes(app)
|
|
|
|
body, err := json.Marshal(map[string]any{
|
|
"type": 1,
|
|
})
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
badPub, _, _ := ed25519.GenerateKey(nil)
|
|
|
|
sig, ts := sign(t, ed25519.PrivateKey(hex.EncodeToString(badPub)), body)
|
|
|
|
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(body))
|
|
req.Header.Set("X-Signature-Ed25519", sig)
|
|
req.Header.Set("X-Signature-Timestamp", ts)
|
|
|
|
res, err := app.Test(req, fiber.TestConfig{})
|
|
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, http.StatusUnauthorized, res.StatusCode)
|
|
}
|