mirror of
https://github.com/pocket-id/pocket-id.git
synced 2026-09-30 06:49:06 +02:00
fix: serve bundled logo if no custom logo is set (#1748)
This commit is contained in:
@@ -3,11 +3,11 @@ package controller
|
||||
import (
|
||||
"net/http"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
kitutils "github.com/italypaleale/go-kit/utils"
|
||||
|
||||
"github.com/pocket-id/pocket-id/backend/internal/apperror"
|
||||
_ "github.com/pocket-id/pocket-id/backend/internal/dto"
|
||||
@@ -52,6 +52,7 @@ type AppImagesController struct {
|
||||
// @Description Get the logo image for the application
|
||||
// @Tags Application Images
|
||||
// @Param light query boolean false "Light mode logo (true) or dark mode logo (false)"
|
||||
// @Param default query boolean false "Return the bundled default logo if no custom logo is set (default true)"
|
||||
// @Produce image/png
|
||||
// @Produce image/jpeg
|
||||
// @Produce image/svg+xml
|
||||
@@ -155,7 +156,7 @@ func (c *AppImagesController) deleteLogoHandler(ctx *gin.Context) error {
|
||||
}
|
||||
|
||||
func logoImageName(ctx *gin.Context) string {
|
||||
lightLogo, _ := strconv.ParseBool(ctx.DefaultQuery("light", "true"))
|
||||
lightLogo := kitutils.IsTruthy(ctx.DefaultQuery("light", "true"))
|
||||
if lightLogo {
|
||||
return "logoLight"
|
||||
}
|
||||
@@ -261,7 +262,12 @@ func (c *AppImagesController) updateFaviconHandler(ctx *gin.Context) error {
|
||||
}
|
||||
|
||||
func (c *AppImagesController) getImage(ctx *gin.Context, name string) error {
|
||||
reader, size, mimeType, err := c.appImagesService.GetImage(ctx.Request.Context(), name)
|
||||
getImage := c.appImagesService.GetImage
|
||||
if kitutils.IsTruthy(ctx.DefaultQuery("default", "true")) {
|
||||
getImage = c.appImagesService.GetImageWithDefault
|
||||
}
|
||||
|
||||
reader, size, mimeType, err := getImage(ctx.Request.Context(), name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/pocket-id/pocket-id/backend/internal/apperror"
|
||||
"github.com/pocket-id/pocket-id/backend/internal/service"
|
||||
"github.com/pocket-id/pocket-id/backend/internal/storage"
|
||||
)
|
||||
|
||||
func TestAppImagesControllerGetLogo(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
store, err := storage.NewFilesystemStorage(t.TempDir())
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
require.NoError(t, store.Close())
|
||||
})
|
||||
|
||||
extensions := map[string]string{}
|
||||
appImagesController := &AppImagesController{
|
||||
appImagesService: service.NewAppImagesService(extensions, store),
|
||||
}
|
||||
|
||||
t.Run("returns the bundled logo if no custom logo is set", func(t *testing.T) {
|
||||
res, err := getLogo(t, appImagesController, "/api/application-images/logo")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, res.Code)
|
||||
assert.Equal(t, "image/svg+xml", res.Header().Get("Content-Type"))
|
||||
assert.Contains(t, res.Body.String(), `fill="#000"`)
|
||||
})
|
||||
|
||||
t.Run("returns the bundled dark mode logo if no custom logo is set", func(t *testing.T) {
|
||||
res, err := getLogo(t, appImagesController, "/api/application-images/logo?light=false")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, res.Code)
|
||||
assert.Contains(t, res.Body.String(), `fill="#fff"`)
|
||||
})
|
||||
|
||||
t.Run("returns not found if the bundled logo is skipped", func(t *testing.T) {
|
||||
_, err := getLogo(t, appImagesController, "/api/application-images/logo?default=false")
|
||||
require.Error(t, err)
|
||||
assert.True(t, apperror.IsCode(err, apperror.CodeImageNotFound))
|
||||
})
|
||||
|
||||
t.Run("returns the custom logo if one is set", func(t *testing.T) {
|
||||
err := store.Save(t.Context(), path.Join("application-images", "logoLight.png"), bytes.NewReader([]byte("custom")))
|
||||
require.NoError(t, err)
|
||||
extensions["logoLight"] = "png"
|
||||
t.Cleanup(func() {
|
||||
delete(extensions, "logoLight")
|
||||
})
|
||||
|
||||
for _, target := range []string{"/api/application-images/logo", "/api/application-images/logo?default=false"} {
|
||||
res, err := getLogo(t, appImagesController, target)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, res.Code)
|
||||
assert.Equal(t, "image/png", res.Header().Get("Content-Type"))
|
||||
assert.Equal(t, "custom", res.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func getLogo(t *testing.T, appImagesController *AppImagesController, target string) (*httptest.ResponseRecorder, error) {
|
||||
t.Helper()
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
ctx, _ := gin.CreateTestContext(res)
|
||||
ctx.Request = httptest.NewRequestWithContext(t.Context(), http.MethodGet, target, http.NoBody)
|
||||
|
||||
err := appImagesController.getLogoHandler(ctx)
|
||||
return res, err
|
||||
}
|
||||
Reference in New Issue
Block a user