1
0
Fork 0
httpserver/menus/menus_test.go

90 Zeilen
2.0 KiB
Go

package menus_test
import (
"fmt"
"testing"
"github.com/gin-gonic/gin"
"go.sebtobie.de/httpserver/menus"
)
type account struct {
auth bool
}
func (a *account) Anonymous() bool {
return !a.auth
}
func (*account) Get(string) interface{} {
return nil
}
func (*account) List() []string {
return []string{}
}
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, athenticated: %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, athenticated: %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("Uspec Case, athenticated: %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)
}
}
}