2021-02-11 15:43:19 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.mindeco.de/http/render"
|
|
|
|
"go.mindeco.de/http/tester"
|
|
|
|
"go.mindeco.de/logging/logtest"
|
|
|
|
|
|
|
|
"github.com/ssb-ngi-pointer/go-ssb-room/admindb"
|
|
|
|
"github.com/ssb-ngi-pointer/go-ssb-room/admindb/mockdb"
|
|
|
|
"github.com/ssb-ngi-pointer/go-ssb-room/roomstate"
|
|
|
|
"github.com/ssb-ngi-pointer/go-ssb-room/web"
|
|
|
|
"github.com/ssb-ngi-pointer/go-ssb-room/web/router"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testSession struct {
|
|
|
|
Mux *http.ServeMux
|
|
|
|
Client *tester.Tester
|
|
|
|
Router *mux.Router
|
|
|
|
|
|
|
|
AllowListDB *mockdb.FakeAllowListService
|
|
|
|
|
|
|
|
RoomState *roomstate.Manager
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSession(t *testing.T) *testSession {
|
|
|
|
var ts testSession
|
|
|
|
|
|
|
|
// fake dbs
|
|
|
|
ts.AllowListDB = new(mockdb.FakeAllowListService)
|
|
|
|
|
|
|
|
log, _ := logtest.KitLogger("admin", t)
|
|
|
|
ctx := context.TODO()
|
|
|
|
ts.RoomState = roomstate.NewManager(ctx, log)
|
|
|
|
|
|
|
|
ts.Router = router.Admin(nil)
|
|
|
|
|
|
|
|
// setup rendering
|
2021-02-16 09:33:28 +00:00
|
|
|
|
|
|
|
// TODO: make testing utils and move these there
|
2021-02-11 15:43:19 +00:00
|
|
|
testFuncs := web.TemplateFuncs(ts.Router)
|
|
|
|
testFuncs["i18n"] = func(msgID string) string { return msgID }
|
2021-02-16 09:33:28 +00:00
|
|
|
testFuncs["i18npl"] = func(msgID string, count int, _ ...interface{}) string {
|
|
|
|
if count == 1 {
|
|
|
|
return msgID + "Singular"
|
|
|
|
}
|
|
|
|
return msgID + "Plural"
|
|
|
|
}
|
2021-02-11 15:43:19 +00:00
|
|
|
testFuncs["is_logged_in"] = func() *admindb.User { return nil }
|
|
|
|
|
2021-02-22 14:26:51 +00:00
|
|
|
r, err := render.New(web.Templates,
|
2021-02-11 15:43:19 +00:00
|
|
|
render.SetLogger(log),
|
2021-02-22 15:02:55 +00:00
|
|
|
render.BaseTemplates("base.tmpl"),
|
|
|
|
render.AddTemplates(append(HTMLTemplates, "error.tmpl")...),
|
2021-02-22 15:20:26 +00:00
|
|
|
render.ErrorTemplate("error.tmpl"),
|
2021-02-11 15:43:19 +00:00
|
|
|
render.FuncMap(testFuncs),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(errors.Wrap(err, "setup: render init failed"))
|
|
|
|
}
|
|
|
|
|
|
|
|
ts.Mux = http.NewServeMux()
|
|
|
|
ts.Mux.Handle("/", Handler(r, ts.RoomState, ts.AllowListDB))
|
|
|
|
ts.Client = tester.New(ts.Mux, t)
|
|
|
|
|
|
|
|
return &ts
|
|
|
|
}
|