2021-01-21 20:48:59 +00:00
|
|
|
package menus_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-11-07 20:41:48 +00:00
|
|
|
"go.sebtobie.de/httpserver/auth"
|
2021-01-21 20:48:59 +00:00
|
|
|
"go.sebtobie.de/httpserver/menus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type account struct {
|
|
|
|
auth bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *account) Anonymous() bool {
|
|
|
|
return !a.auth
|
|
|
|
}
|
|
|
|
|
2021-11-07 20:41:48 +00:00
|
|
|
func (*account) Get(auth.AccountConstant) interface{} {
|
2021-01-21 20:48:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-07 20:41:48 +00:00
|
|
|
func (*account) List() []auth.AccountConstant {
|
|
|
|
return []auth.AccountConstant{}
|
2021-01-21 20:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-10-25 20:28:39 +00:00
|
|
|
t.Logf("Absent Case, authenticated: %t, seen: %t", authenticated, c.seen)
|
2021-01-21 20:48:59 +00:00
|
|
|
case menus.ReqAuthRequired:
|
|
|
|
c := Case{
|
|
|
|
menu: menus.MenuItem{
|
|
|
|
RequireAuth: require,
|
|
|
|
},
|
|
|
|
authenticated: authenticated,
|
|
|
|
seen: true && authenticated,
|
|
|
|
}
|
|
|
|
cases = append(cases, c)
|
2021-10-25 20:28:39 +00:00
|
|
|
t.Logf("Required Case, authenticated: %t, seen: %t", authenticated, c.seen)
|
2021-01-21 20:48:59 +00:00
|
|
|
case menus.ReqAuthUnspec:
|
|
|
|
c := Case{
|
|
|
|
menu: menus.MenuItem{
|
|
|
|
RequireAuth: require,
|
|
|
|
},
|
|
|
|
authenticated: authenticated,
|
|
|
|
seen: true,
|
|
|
|
}
|
|
|
|
cases = append(cases, c)
|
2021-10-25 20:28:39 +00:00
|
|
|
t.Logf("Unspec Case, authenticated: %t, seen: %t", authenticated, c.seen)
|
2021-01-21 20:48:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|