Added tests for page.

This commit is contained in:
mikestefanello 2021-12-18 17:24:46 -05:00
parent 3525d5d704
commit 8eb8264d6e
2 changed files with 113 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package controller
import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"goweb/config"
"goweb/services"
"github.com/labstack/echo/v4"
)
var (
c *services.Container
)
func TestMain(m *testing.M) {
// Set the environment to test
config.SwitchEnvironment(config.EnvTest)
// Create a new container
c = services.NewContainer()
// Run tests
exitVal := m.Run()
os.Exit(exitVal)
}
func newContext(url string) echo.Context {
req := httptest.NewRequest(http.MethodPost, url, strings.NewReader(""))
return c.Web.NewContext(req, httptest.NewRecorder())
}

78
controller/page_test.go Normal file
View File

@ -0,0 +1,78 @@
package controller
import (
"net/http"
"testing"
"goweb/context"
"goweb/msg"
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
echomw "github.com/labstack/echo/v4/middleware"
"github.com/stretchr/testify/assert"
)
func TestNewPage(t *testing.T) {
ctx := newContext("/")
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)
assert.Equal(t, NewPager(ctx), p.Pager)
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)
ctx = newContext("/abc?def=123")
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) {
ctx := newContext("/")
p := NewPage(ctx)
// Simulate an HTTP request through the session middleware to initiate the session
mw := session.Middleware(sessions.NewCookieStore([]byte("secret")))
handler := mw(echo.NotFoundHandler)
assert.Error(t, handler(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))
}
}
}