shiori/internal/http/routes/api/v1/bookmarks_test.go
Felipe Martin db313f5c62
fix: authentication flow should abort early (#888)
* fix: finish connection once we send auth response

* removed interface for now

* handle authentication in each route group

* tags api tests

* typo

* testutil improvements

* bookmarks api auth

* cache update requires owner
2024-04-13 19:45:03 +02:00

47 lines
1.3 KiB
Go

package api_v1
import (
"context"
"net/http"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/go-shiori/shiori/internal/http/middleware"
"github.com/go-shiori/shiori/internal/model"
"github.com/go-shiori/shiori/internal/testutil"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
func TestUpdateBookmarkCache(t *testing.T) {
logger := logrus.New()
ctx := context.TODO()
g := gin.New()
_, deps := testutil.GetTestConfigurationAndDependencies(t, ctx, logger)
g.Use(middleware.AuthMiddleware(deps))
router := NewBookmarksAPIRoutes(logger, deps)
router.Setup(g.Group("/"))
account := model.Account{
Username: "test",
Password: "test",
Owner: false,
}
require.NoError(t, deps.Database.SaveAccount(ctx, account))
token, err := deps.Domains.Auth.CreateTokenForAccount(&account, time.Now().Add(time.Minute))
require.NoError(t, err)
t.Run("require authentication", func(t *testing.T) {
w := testutil.PerformRequest(g, "PUT", "/cache")
require.Equal(t, http.StatusUnauthorized, w.Code)
})
t.Run("require owner", func(t *testing.T) {
w := testutil.PerformRequest(g, "PUT", "/cache", testutil.WithHeader(model.AuthorizationHeader, model.AuthorizationTokenType+" "+token))
require.Equal(t, http.StatusForbidden, w.Code)
})
}