2021-12-21 12:18:17 -08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2021-12-21 17:29:15 -08:00
|
|
|
"fmt"
|
2021-12-21 12:18:17 -08:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2024-07-09 17:57:05 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-12-21 12:18:17 -08:00
|
|
|
"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"
|
2021-12-21 12:18:17 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2021-12-21 12:18:17 -08:00
|
|
|
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)
|
2021-12-21 12:18:17 -08:00
|
|
|
|
|
|
|
// 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())
|
2021-12-22 11:38:00 -08:00
|
|
|
assert.Nil(t, err)
|
2021-12-21 12:18:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequireNoAuthentication(t *testing.T) {
|
|
|
|
ctx, _ := tests.NewContext(c.Web, "/")
|
|
|
|
tests.InitSession(ctx)
|
|
|
|
|
|
|
|
// Not logged in
|
|
|
|
err := tests.ExecuteMiddleware(ctx, RequireNoAuthentication())
|
2021-12-22 11:38:00 -08:00
|
|
|
assert.Nil(t, err)
|
2021-12-21 12:18:17 -08:00
|
|
|
|
|
|
|
// 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)
|
2021-12-21 12:18:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadValidPasswordToken(t *testing.T) {
|
2021-12-21 17:29:15 -08:00
|
|
|
ctx, _ := tests.NewContext(c.Web, "/")
|
|
|
|
tests.InitSession(ctx)
|
2021-12-21 12:18:17 -08:00
|
|
|
|
2021-12-21 17:29:15 -08:00
|
|
|
// Missing user context
|
|
|
|
err := tests.ExecuteMiddleware(ctx, LoadValidPasswordToken(c.Auth))
|
|
|
|
tests.AssertHTTPErrorCode(t, err, http.StatusInternalServerError)
|
|
|
|
|
2022-01-27 05:44:12 -08:00
|
|
|
// 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
|
2022-01-27 05:44:12 -08:00
|
|
|
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
|
2022-01-27 05:44:12 -08:00
|
|
|
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))
|
2021-12-22 11:38:00 -08:00
|
|
|
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)
|
2021-12-21 12:18:17 -08:00
|
|
|
}
|