v4/testing/testing.go

128 lines
3.9 KiB
Go

package testing
import (
"log"
"github.com/gofiber/fiber/v3"
"git.sapphic.engineer/roleypoly/v4/auth/authmiddleware"
"git.sapphic.engineer/roleypoly/v4/discord"
"git.sapphic.engineer/roleypoly/v4/templates/components"
"git.sapphic.engineer/roleypoly/v4/types"
"git.sapphic.engineer/roleypoly/v4/types/fixtures"
)
type TestingController struct {
Guilds *discord.GuildService
}
func (t *TestingController) Routes(r fiber.Router) {
r.Get("/picker/:version?", t.Picker)
r.Get("/index", t.Index)
r.Get("/t/:which", t.TestTemplate)
r.Get("/m/:server/:user", t.GetMember)
r.Get("/g/:server", t.GetGuild)
r.Get("/auth", t.AuthState, authmiddleware.New(t.Guilds.Client(), []string{}, []string{}))
}
func (t *TestingController) Index(c fiber.Ctx) error {
return c.Render("index", fiber.Map{})
}
func (t *TestingController) Picker(c fiber.Ctx) error {
version := c.Params("version", "main")
return c.Render("picker/"+version, fiber.Map{})
}
func (t *TestingController) TestTemplate(c fiber.Ctx) error {
which := c.Params("which")
cat1 := fixtures.Category(fixtures.CategoryMulti)
cat2 := fixtures.Category(fixtures.CategorySingle)
cat3 := fixtures.Category(fixtures.CategoryMulti)
cat4 := fixtures.Category(fixtures.CategorySingle)
return c.Render("tests/"+which, fiber.Map{
// multi
"Category1": []components.RoleTemplateData{
components.Role(cat1, &fixtures.RoleWithDarkColor, true, true),
components.Role(cat1, &fixtures.RoleWithDarkMediumColor, true, true),
components.Role(cat1, &fixtures.RoleWithLightMediumColor, true, true),
components.Role(cat1, &fixtures.RoleWithLightColor, true, true),
},
// single
"Category2": []components.RoleTemplateData{
components.Role(cat2, &fixtures.RoleWithDarkColor, true, true),
components.Role(cat2, &fixtures.RoleWithDarkMediumColor, true, true),
components.Role(cat2, &fixtures.RoleWithLightMediumColor, true, true),
components.Role(cat2, &fixtures.RoleWithLightColor, true, true),
},
// unsafe
"Category3": []components.RoleTemplateData{
components.Role(cat3, &fixtures.RoleWithDarkColor, true, false),
components.Role(cat3, &fixtures.RoleWithDarkMediumColor, false, false),
components.Role(cat3, &fixtures.RoleWithLightMediumColor, false, false),
components.Role(cat3, &fixtures.RoleWithLightColor, true, false),
},
// unselected
"Category4": []components.RoleTemplateData{
components.Role(cat4, &fixtures.RoleWithDarkColor, true, true),
components.Role(cat4, &fixtures.RoleWithDarkMediumColor, false, true),
components.Role(cat4, &fixtures.RoleWithLightMediumColor, false, true),
components.Role(cat4, &fixtures.RoleWithLightColor, false, true),
},
}, "layouts/main")
}
func (t *TestingController) GetMember(c fiber.Ctx) error {
serverID := c.Params("server")
userID := c.Params("user")
g, err := t.Guilds.GetGuild(serverID)
if err != nil {
log.Println("testing/get guild: ", err)
types.NewAPIError(500, err.Error()).Send(c)
}
m, err := g.GetMember(userID)
if err != nil {
log.Println("testing/get member: ", err)
types.NewAPIError(500, err.Error()).Send(c)
}
return c.JSON(m.DiscordMember)
}
func (t *TestingController) GetGuild(c fiber.Ctx) error {
serverID := c.Params("server")
g, err := t.Guilds.GetGuild(serverID)
if err != nil {
log.Println("testing/get guild: ", err)
types.NewAPIError(500, err.Error()).Send(c)
}
return c.JSON(g.DiscordGuild)
}
func (t *TestingController) 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)
}