Refactor reqctx package and remove unnecessary files

This commit is contained in:
Luis Eduardo Jeréz Girón
2024-07-22 22:59:29 -06:00
parent 2986ae7f8c
commit 5d3821e05b
7 changed files with 49 additions and 127 deletions

View File

@@ -1,5 +0,0 @@
# reqctx
This package provides a helper functions and types for echo request context.
Can be used to store request-scoped data like user data, etc.

View File

@@ -1,20 +0,0 @@
package reqctx
import (
"github.com/labstack/echo/v4"
)
// GetAllCtx returns ALL the values from an
// echo request context.
//
// It includes AuthCtx, TenantCtx, etc.
func GetAllCtx(c echo.Context) AllCtx {
authCtx := GetAuthCtx(c)
return AllCtx{
Auth: authCtx,
IsAuthed: authCtx.IsAuthed,
UserID: authCtx.User.ID,
}
}

View File

@@ -1,40 +0,0 @@
package reqctx
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
func TestAllCtxFuncs(t *testing.T) {
testUser := dbgen.User{
ID: uuid.New(),
Email: "user@example.com",
Name: "John",
}
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
t.Run("Create authentication values in context", func(t *testing.T) {
SetAuthCtx(c, AuthCtx{
IsAuthed: true,
User: testUser,
})
ctx := GetAllCtx(c)
assert.True(t, ctx.Auth.IsAuthed)
assert.Equal(t, testUser, ctx.Auth.User)
assert.Equal(t, testUser.Email, ctx.Auth.User.Email)
assert.Equal(t, testUser.ID, ctx.UserID)
assert.Equal(t, testUser.ID, ctx.Auth.User.ID)
})
}

View File

@@ -1,32 +0,0 @@
package reqctx
import (
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
"github.com/labstack/echo/v4"
)
// SetAuthCtx inserts the authentication values of
// a user into a echo request context.
func SetAuthCtx(c echo.Context, auth AuthCtx) {
c.Set("isAuthed", auth.IsAuthed)
c.Set("user", auth.User)
}
// GetAuthCtx returns the authentication values of
// a user from a echo request context.
func GetAuthCtx(c echo.Context) AuthCtx {
var isAuthed bool
var user dbgen.User
if ia, ok := c.Get("isAuthed").(bool); ok {
isAuthed = ia
}
if au, ok := c.Get("user").(dbgen.User); ok {
user = au
}
return AuthCtx{
IsAuthed: isAuthed,
User: user,
}
}

View File

@@ -0,0 +1,42 @@
package reqctx
import (
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
"github.com/labstack/echo/v4"
)
// Context keys to avoid typos
const (
isAuthedKey = "isAuthed"
userKey = "user"
)
// Ctx represents the values passed through a single request context.
type Ctx struct {
IsAuthed bool
User dbgen.User
}
// SetCtx inserts values into the Echo request context.
func SetCtx(c echo.Context, ctx Ctx) {
c.Set(isAuthedKey, ctx.IsAuthed)
c.Set(userKey, ctx.User)
}
// GetCtx retrieves values from the Echo request context.
func GetCtx(c echo.Context) Ctx {
var isAuthed bool
var user dbgen.User
if ia, ok := c.Get(isAuthedKey).(bool); ok {
isAuthed = ia
}
if au, ok := c.Get(userKey).(dbgen.User); ok {
user = au
}
return Ctx{
IsAuthed: isAuthed,
User: user,
}
}

View File

@@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/assert"
)
func TestAuthCtxFuncs(t *testing.T) {
func TestCtxFuncs(t *testing.T) {
testUser := dbgen.User{
ID: uuid.New(),
Email: "user@example.com",
@@ -24,13 +24,13 @@ func TestAuthCtxFuncs(t *testing.T) {
c := e.NewContext(req, rec)
t.Run("Create authentication values in context", func(t *testing.T) {
authData := AuthCtx{
authData := Ctx{
IsAuthed: true,
User: testUser,
}
SetAuthCtx(c, authData)
auth := GetAuthCtx(c)
SetCtx(c, authData)
auth := GetCtx(c)
assert.True(t, auth.IsAuthed)
assert.Equal(t, testUser, auth.User)
@@ -38,12 +38,12 @@ func TestAuthCtxFuncs(t *testing.T) {
})
t.Run("Create authentication values in context with only IsAuthed", func(t *testing.T) {
authData := AuthCtx{
authData := Ctx{
IsAuthed: true,
}
SetAuthCtx(c, authData)
auth := GetAuthCtx(c)
SetCtx(c, authData)
auth := GetCtx(c)
assert.True(t, auth.IsAuthed)
assert.Empty(t, auth.User)

View File

@@ -1,23 +0,0 @@
package reqctx
import (
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
"github.com/google/uuid"
)
// AllCtx represents the full context of a request.
//
// ⚠️ SHOULD have with all the other *Ctx structs.
type AllCtx struct {
Auth AuthCtx
IsAuthed bool
UserID uuid.UUID
}
// AuthCtx represents the authentication values of a user
// in the context of a request.
type AuthCtx struct {
IsAuthed bool
User dbgen.User
}