Add auth test util

This commit is contained in:
Jannis Mattheis 2018-03-24 16:38:57 +01:00 committed by Jannis Mattheis
parent 34f8cc6b77
commit 42c5cc15d8
2 changed files with 30 additions and 0 deletions

11
test/auth.go Normal file
View File

@ -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})
}

19
test/auth_test.go Normal file
View File

@ -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))
}