2021-12-18 14:24:46 -08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"goweb/context"
|
|
|
|
"goweb/msg"
|
2021-12-21 12:18:17 -08:00
|
|
|
"goweb/tests"
|
2021-12-18 14:24:46 -08:00
|
|
|
|
|
|
|
echomw "github.com/labstack/echo/v4/middleware"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewPage(t *testing.T) {
|
2021-12-21 12:18:17 -08:00
|
|
|
ctx, _ := tests.NewContext(c.Web, "/")
|
2021-12-18 14:24:46 -08:00
|
|
|
p := NewPage(ctx)
|
|
|
|
assert.Same(t, ctx, p.Context)
|
|
|
|
assert.NotNil(t, p.ToURL)
|
|
|
|
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)
|
|
|
|
|
2021-12-21 12:18:17 -08:00
|
|
|
ctx, _ = tests.NewContext(c.Web, "/abc?def=123")
|
2021-12-18 14:24:46 -08:00
|
|
|
ctx.Set(context.AuthenticatedUserKey, 1)
|
|
|
|
ctx.Set(echomw.DefaultCSRFConfig.ContextKey, "csrf")
|
|
|
|
p = NewPage(ctx)
|
|
|
|
assert.Equal(t, "/abc", p.Path)
|
|
|
|
assert.Equal(t, "/abc?def=123", p.URL)
|
|
|
|
assert.False(t, p.IsHome)
|
|
|
|
assert.True(t, p.IsAuth)
|
|
|
|
assert.Equal(t, "csrf", p.CSRF)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPage_GetMessages(t *testing.T) {
|
2021-12-21 12:18:17 -08:00
|
|
|
ctx, _ := tests.NewContext(c.Web, "/")
|
|
|
|
tests.InitSession(ctx)
|
2021-12-18 14:24:46 -08:00
|
|
|
p := NewPage(ctx)
|
|
|
|
|
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|