package menus_test import ( "fmt" "testing" "github.com/gin-gonic/gin" "go.sebtobie.de/httpserver/constants" "go.sebtobie.de/httpserver/menus" ) type account struct { auth bool } func (a *account) Anonymous() bool { return !a.auth } func (*account) Get(constants.AccountConstant) any { return nil } func (*account) List() []constants.AccountConstant { return []constants.AccountConstant{} } func (*account) Redirect(*gin.Context) { } type Case struct { menu menus.MenuItem authenticated bool seen bool } func (c *Case) String() string { return fmt.Sprintf("%v ,authenticated: %t,seen: %t", c.menu.RequireAuth.String(), c.authenticated, c.seen) } func gencases(t *testing.T) []Case { cases := make([]Case, 0, 6) for _, authenticated := range []bool{true, false} { for _, require := range []menus.Rauth{menus.ReqAuthAbsent, menus.ReqAuthRequired, menus.ReqAuthUnspec} { switch require { case menus.ReqAuthAbsent: c := Case{ menu: menus.MenuItem{ RequireAuth: require, }, authenticated: authenticated, seen: true && !authenticated, } cases = append(cases, c) t.Logf("Absent Case, authenticated: %t, seen: %t", authenticated, c.seen) case menus.ReqAuthRequired: c := Case{ menu: menus.MenuItem{ RequireAuth: require, }, authenticated: authenticated, seen: true && authenticated, } cases = append(cases, c) t.Logf("Required Case, authenticated: %t, seen: %t", authenticated, c.seen) case menus.ReqAuthUnspec: c := Case{ menu: menus.MenuItem{ RequireAuth: require, }, authenticated: authenticated, seen: true, } cases = append(cases, c) t.Logf("Unspec Case, authenticated: %t, seen: %t", authenticated, c.seen) } } } t.Log(cap(cases), len(cases)) return cases } func TestMenuAuth(t *testing.T) { cases := gencases(t) for _, input := range cases { if s := input.menu.Enabled(&account{input.authenticated}); s != input.seen { t.Errorf("Test failed. %s, is seen: %t", input.String(), s) } } }