diff --git a/test/auth.go b/test/auth.go new file mode 100644 index 0000000..8ce94d2 --- /dev/null +++ b/test/auth.go @@ -0,0 +1,11 @@ +package test + +import ( + "github.com/gin-gonic/gin" + "github.com/gotify/server/model" +) + +// WithUser fake an authentication for testing. +func WithUser(ctx *gin.Context, userID uint) { + ctx.Set("user", &model.User{ID: userID}) +} diff --git a/test/auth_test.go b/test/auth_test.go new file mode 100644 index 0000000..e217c81 --- /dev/null +++ b/test/auth_test.go @@ -0,0 +1,19 @@ +package test_test + +import ( + "testing" + + "github.com/gin-gonic/gin" + "github.com/gotify/server/auth" + "github.com/gotify/server/mode" + "github.com/gotify/server/test" + "github.com/stretchr/testify/assert" +) + +func TestFakeAuth(t *testing.T) { + mode.Set(mode.TestDev) + + ctx, _ := gin.CreateTestContext(nil) + test.WithUser(ctx, 5) + assert.Equal(t, uint(5), auth.GetUserID(ctx)) +}