saasitone/pkg/middleware/auth_test.go

111 lines
3.4 KiB
Go
Raw Permalink Normal View History

package middleware
import (
2021-12-21 17:29:15 -08:00
"fmt"
"net/http"
"testing"
2024-07-09 17:57:05 -07:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2024-07-09 17:57:05 -07:00
"git.grosinger.net/tgrosinger/saasitone/pkg/context"
"git.grosinger.net/tgrosinger/saasitone/pkg/tests"
2024-07-11 21:09:15 -07:00
"git.grosinger.net/tgrosinger/saasitone/pkg/models/sqlc"
)
func TestLoadAuthenticatedUser(t *testing.T) {
ctx, _ := tests.NewContext(c.Web, "/")
tests.InitSession(ctx)
mw := LoadAuthenticatedUser(c.Auth)
// Not authenticated
_ = tests.ExecuteMiddleware(ctx, mw)
assert.Nil(t, ctx.Get(context.AuthenticatedUserKey))
// Login
err := c.Auth.Login(ctx, usr.ID)
require.NoError(t, err)
// Verify the midldeware returns the authenticated user
_ = tests.ExecuteMiddleware(ctx, mw)
require.NotNil(t, ctx.Get(context.AuthenticatedUserKey))
2024-07-11 21:09:15 -07:00
ctxUsr, ok := ctx.Get(context.AuthenticatedUserKey).(*sqlc.User)
require.True(t, ok)
assert.Equal(t, usr.ID, ctxUsr.ID)
}
func TestRequireAuthentication(t *testing.T) {
ctx, _ := tests.NewContext(c.Web, "/")
tests.InitSession(ctx)
// Not logged in
err := tests.ExecuteMiddleware(ctx, RequireAuthentication())
2021-12-21 17:29:15 -08:00
tests.AssertHTTPErrorCode(t, err, http.StatusUnauthorized)
// Login
err = c.Auth.Login(ctx, usr.ID)
require.NoError(t, err)
_ = tests.ExecuteMiddleware(ctx, LoadAuthenticatedUser(c.Auth))
// Logged in
err = tests.ExecuteMiddleware(ctx, RequireAuthentication())
assert.Nil(t, err)
}
func TestRequireNoAuthentication(t *testing.T) {
ctx, _ := tests.NewContext(c.Web, "/")
tests.InitSession(ctx)
// Not logged in
err := tests.ExecuteMiddleware(ctx, RequireNoAuthentication())
assert.Nil(t, err)
// Login
err = c.Auth.Login(ctx, usr.ID)
require.NoError(t, err)
_ = tests.ExecuteMiddleware(ctx, LoadAuthenticatedUser(c.Auth))
// Logged in
err = tests.ExecuteMiddleware(ctx, RequireNoAuthentication())
2021-12-21 17:29:15 -08:00
tests.AssertHTTPErrorCode(t, err, http.StatusForbidden)
}
func TestLoadValidPasswordToken(t *testing.T) {
2021-12-21 17:29:15 -08:00
ctx, _ := tests.NewContext(c.Web, "/")
tests.InitSession(ctx)
2021-12-21 17:29:15 -08:00
// Missing user context
err := tests.ExecuteMiddleware(ctx, LoadValidPasswordToken(c.Auth))
tests.AssertHTTPErrorCode(t, err, http.StatusInternalServerError)
// Add user and password token context but no token and expect a redirect
ctx.SetParamNames("user", "password_token")
ctx.SetParamValues(fmt.Sprintf("%d", usr.ID), "1")
2024-07-11 21:09:15 -07:00
_ = tests.ExecuteMiddleware(ctx, LoadUser(c.DB))
2021-12-21 17:29:15 -08:00
err = tests.ExecuteMiddleware(ctx, LoadValidPasswordToken(c.Auth))
assert.NoError(t, err)
assert.Equal(t, http.StatusFound, ctx.Response().Status)
// Add user context and invalid password token and expect a redirect
ctx.SetParamNames("user", "password_token", "token")
ctx.SetParamValues(fmt.Sprintf("%d", usr.ID), "1", "faketoken")
2024-07-11 21:09:15 -07:00
_ = tests.ExecuteMiddleware(ctx, LoadUser(c.DB))
2021-12-21 17:29:15 -08:00
err = tests.ExecuteMiddleware(ctx, LoadValidPasswordToken(c.Auth))
assert.NoError(t, err)
assert.Equal(t, http.StatusFound, ctx.Response().Status)
// Create a valid token
token, pt, err := c.Auth.GeneratePasswordResetToken(ctx, usr.ID)
require.NoError(t, err)
// Add user and valid password token
ctx.SetParamNames("user", "password_token", "token")
ctx.SetParamValues(fmt.Sprintf("%d", usr.ID), fmt.Sprintf("%d", pt.ID), token)
2024-07-11 21:09:15 -07:00
_ = tests.ExecuteMiddleware(ctx, LoadUser(c.DB))
2021-12-21 17:29:15 -08:00
err = tests.ExecuteMiddleware(ctx, LoadValidPasswordToken(c.Auth))
assert.Nil(t, err)
2024-07-11 21:09:15 -07:00
ctxPt, ok := ctx.Get(context.PasswordTokenKey).(*sqlc.PasswordToken)
2021-12-21 17:29:15 -08:00
require.True(t, ok)
assert.Equal(t, pt.ID, ctxPt.ID)
}