Files
gitea/modules/markup/camo_test.go
T
silverwind 98c613d943 test: fix unit tests failing on repeated runs (#39915)
`go test -count=2` failed in 12 packages because tests leaked state into
later runs.

- Reset SQLite sequences after each fixture load, like PostgreSQL
already does
- Prepare the database in tests that relied on earlier tests doing it
- Restore mutated globals with `test.MockVariableValue`
2026-10-22 01:27:02 +00:00

48 lines
1.5 KiB
Go

// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package markup
import (
"testing"
"gitea.dev/modules/setting"
testModule "gitea.dev/modules/test"
"github.com/stretchr/testify/assert"
)
func TestCamoHandleLink(t *testing.T) {
defer testModule.MockVariableValue(&setting.AppURL, "https://gitea.com")()
defer testModule.MockVariableValue(&setting.Camo)()
// Test media proxy
setting.Camo.Enabled = true
setting.Camo.Always = false
setting.Camo.ServerURL = "https://image.proxy"
setting.Camo.HMACKey = "geheim"
dataURL := "data:image/png;base64,i//V"
assert.Equal(t, dataURL, camoHandleLink(dataURL))
assert.Equal(t,
"https://gitea.com/img.jpg",
camoHandleLink("https://gitea.com/img.jpg"))
assert.Equal(t,
"https://testimages.org/img.jpg",
camoHandleLink("https://testimages.org/img.jpg"))
assert.Equal(t,
"https://image.proxy/eivin43gJwGVIjR9MiYYtFIk0mw/aHR0cDovL3Rlc3RpbWFnZXMub3JnL2ltZy5qcGc",
camoHandleLink("http://testimages.org/img.jpg"))
setting.Camo.Always = true
assert.Equal(t, dataURL, camoHandleLink(dataURL))
assert.Equal(t,
"https://gitea.com/img.jpg",
camoHandleLink("https://gitea.com/img.jpg"))
assert.Equal(t,
"https://image.proxy/tkdlvmqpbIr7SjONfHNgEU622y0/aHR0cHM6Ly90ZXN0aW1hZ2VzLm9yZy9pbWcuanBn",
camoHandleLink("https://testimages.org/img.jpg"))
assert.Equal(t,
"https://image.proxy/eivin43gJwGVIjR9MiYYtFIk0mw/aHR0cDovL3Rlc3RpbWFnZXMub3JnL2ltZy5qcGc",
camoHandleLink("http://testimages.org/img.jpg"))
}