2024-06-15 12:34:24 -07:00
|
|
|
package page
|
2021-12-18 14:24:46 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2024-06-15 12:34:24 -07:00
|
|
|
"github.com/labstack/echo/v4"
|
2021-12-18 14:24:46 -08:00
|
|
|
echomw "github.com/labstack/echo/v4/middleware"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-09 17:57:05 -07:00
|
|
|
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/context"
|
2024-07-11 21:09:15 -07:00
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/models/sqlc"
|
2024-07-09 17:57:05 -07:00
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/msg"
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/tests"
|
2021-12-18 14:24:46 -08:00
|
|
|
)
|
|
|
|
|
2024-06-15 12:34:24 -07:00
|
|
|
func TestNew(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
ctx, _ := tests.NewContext(e, "/")
|
|
|
|
p := New(ctx)
|
2021-12-18 14:24:46 -08:00
|
|
|
assert.Same(t, ctx, p.Context)
|
|
|
|
assert.Equal(t, "/", p.Path)
|
|
|
|
assert.Equal(t, "/", p.URL)
|
|
|
|
assert.Equal(t, http.StatusOK, p.StatusCode)
|
2021-12-18 15:08:04 -08:00
|
|
|
assert.Equal(t, NewPager(ctx, DefaultItemsPerPage), p.Pager)
|
2021-12-18 14:24:46 -08:00
|
|
|
assert.Empty(t, p.Headers)
|
|
|
|
assert.True(t, p.IsHome)
|
|
|
|
assert.False(t, p.IsAuth)
|
|
|
|
assert.Empty(t, p.CSRF)
|
|
|
|
assert.Empty(t, p.RequestID)
|
|
|
|
assert.False(t, p.Cache.Enabled)
|
|
|
|
|
2024-06-15 12:34:24 -07:00
|
|
|
ctx, _ = tests.NewContext(e, "/abc?def=123")
|
2024-07-11 21:09:15 -07:00
|
|
|
usr := &sqlc.User{
|
2024-06-15 12:34:24 -07:00
|
|
|
ID: 1,
|
|
|
|
}
|
2021-12-27 07:09:46 -08:00
|
|
|
ctx.Set(context.AuthenticatedUserKey, usr)
|
2021-12-18 14:24:46 -08:00
|
|
|
ctx.Set(echomw.DefaultCSRFConfig.ContextKey, "csrf")
|
2024-06-15 12:34:24 -07:00
|
|
|
p = New(ctx)
|
2021-12-18 14:24:46 -08:00
|
|
|
assert.Equal(t, "/abc", p.Path)
|
|
|
|
assert.Equal(t, "/abc?def=123", p.URL)
|
|
|
|
assert.False(t, p.IsHome)
|
|
|
|
assert.True(t, p.IsAuth)
|
2021-12-27 07:09:46 -08:00
|
|
|
assert.Equal(t, usr, p.AuthUser)
|
2021-12-18 14:24:46 -08:00
|
|
|
assert.Equal(t, "csrf", p.CSRF)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPage_GetMessages(t *testing.T) {
|
2024-06-15 12:34:24 -07:00
|
|
|
ctx, _ := tests.NewContext(echo.New(), "/")
|
2021-12-21 12:18:17 -08:00
|
|
|
tests.InitSession(ctx)
|
2024-06-15 12:34:24 -07:00
|
|
|
p := New(ctx)
|
2021-12-18 14:24:46 -08:00
|
|
|
|
|
|
|
// Set messages
|
|
|
|
msgTests := make(map[msg.Type][]string)
|
|
|
|
msgTests[msg.TypeWarning] = []string{
|
|
|
|
"abc",
|
|
|
|
"def",
|
|
|
|
}
|
|
|
|
msgTests[msg.TypeInfo] = []string{
|
|
|
|
"123",
|
|
|
|
"456",
|
|
|
|
}
|
|
|
|
for typ, values := range msgTests {
|
|
|
|
for _, value := range values {
|
|
|
|
msg.Set(ctx, typ, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the messages
|
|
|
|
for typ, values := range msgTests {
|
|
|
|
msgs := p.GetMessages(typ)
|
|
|
|
|
|
|
|
for i, message := range msgs {
|
|
|
|
assert.Equal(t, values[i], string(message))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|