package authmiddleware_test import ( "bytes" "net/http" "testing" "git.sapphic.engineer/roleypoly/v4/authmiddleware" "git.sapphic.engineer/roleypoly/v4/discord" "git.sapphic.engineer/roleypoly/v4/discord/clientmock" "github.com/goccy/go-json" "github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3/middleware/session" ) func TestAnonymous(t *testing.T) { dc := clientmock.NewDiscordClientMock() app := getApp(dc) setSession(app, dc, authmiddleware.Session{ Permissions: authmiddleware.PermAnonymous, }) } func getApp(dc discord.IDiscordClient) *fiber.App { app := fiber.New(fiber.Config{}) sessionMiddleware, sessionStore := session.NewWithStore() sessionStore.RegisterType(authmiddleware.Session{}) app.Use(sessionMiddleware, authmiddleware.New(dc, []string{}, []string{})) app.Get("/", authState) app.Post("/updateSession", updateSession) return app } func setSession(app *fiber.App, dc *clientmock.DiscordClientMock, sess authmiddleware.Session) error { body := bytes.Buffer{} json.NewEncoder(&body).Encode(sess) // do mocks here req, _ := http.NewRequest("POST", "/updateSession", &body) _, err := app.Test(req) return err } func authState(c fiber.Ctx) error { s := authmiddleware.SessionFrom(c) permList := []string{} if s.Permissions >= authmiddleware.PermAnonymous { permList = append(permList, "anonymous") } if s.Permissions >= authmiddleware.PermUser { permList = append(permList, "user") } if s.Permissions >= authmiddleware.PermSupport { permList = append(permList, "support") } if s.Permissions >= authmiddleware.PermSuperuser { permList = append(permList, "superuser") } return c.JSON(permList) } func updateSession(c fiber.Ctx) error { var newSession *authmiddleware.Session c.Bind().JSON(&newSession) sc := session.FromContext(c) sc.Set(authmiddleware.SessionKey, newSession) return c.SendString("ok") }