From 42c5cc15d8e1e2c2a826b5c7d3729dbc1e5a3509 Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Sat, 24 Mar 2018 16:38:57 +0100 Subject: [PATCH] Add auth test util --- test/auth.go | 11 +++++++++++ test/auth_test.go | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 test/auth.go create mode 100644 test/auth_test.go 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)) +}