114 lines
2.7 KiB
Go
114 lines
2.7 KiB
Go
package interactions_test
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/ed25519"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"git.sapphic.engineer/roleypoly/v4/auth/authmiddleware"
|
|
"git.sapphic.engineer/roleypoly/v4/discord"
|
|
"git.sapphic.engineer/roleypoly/v4/interactions"
|
|
"git.sapphic.engineer/roleypoly/v4/roleypoly"
|
|
"github.com/gofiber/fiber/v3"
|
|
"github.com/gofiber/fiber/v3/middleware/session"
|
|
"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, "http://roleypoly.local", 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 := roleypoly.CreateFiberApp()
|
|
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 TestPostHandlerIntegration(t *testing.T) {
|
|
i, dsm, key := makeInteractions(t)
|
|
app := roleypoly.CreateFiberApp()
|
|
|
|
// Tests that everything works including auth middleware
|
|
sessionMiddleware, sessionStore := session.NewWithStore()
|
|
sessionStore.RegisterType(authmiddleware.Session{})
|
|
app.Use(sessionMiddleware)
|
|
app.Use(authmiddleware.New(dsm, []string{}, []string{}))
|
|
|
|
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)
|
|
}
|