Some checks failed
Build and Release / Create Release (push) Successful in 0s
Trigger Vault Plugin Rebuild / Trigger Vault Rebuild (push) Successful in 0s
Build and Release / Integration Tests (PostgreSQL) (push) Successful in 2m48s
Build and Release / Lint (push) Failing after 5m2s
Build and Release / Build Binaries (amd64, windows, windows-latest) (push) Has been skipped
Build and Release / Build Binaries (amd64, darwin, linux-latest) (push) Has been skipped
Build and Release / Build Binaries (amd64, linux, linux-latest) (push) Has been skipped
Build and Release / Build Binaries (arm64, darwin, linux-latest) (push) Has been skipped
Build and Release / Build Binaries (arm64, linux, linux-latest) (push) Has been skipped
Build and Release / Unit Tests (push) Successful in 5m37s
Go's semantic import versioning requires v2+ modules to include the major version in the module path. This enables using proper version tags (v3.x.x) instead of pseudo-versions. Updated module path: code.gitcaddy.com/server/v3
67 lines
2.0 KiB
Go
67 lines
2.0 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package user
|
|
|
|
import (
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
|
|
"code.gitcaddy.com/server/v3/models/unittest"
|
|
"code.gitcaddy.com/server/v3/modules/setting"
|
|
"code.gitcaddy.com/server/v3/modules/storage"
|
|
"code.gitcaddy.com/server/v3/modules/test"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestUserAvatarLink(t *testing.T) {
|
|
defer test.MockVariableValue(&setting.AppURL, "https://localhost/")()
|
|
defer test.MockVariableValue(&setting.AppSubURL, "")()
|
|
|
|
u := &User{ID: 1, Avatar: "avatar.png"}
|
|
link := u.AvatarLink(t.Context())
|
|
assert.Equal(t, "https://localhost/avatars/avatar.png", link)
|
|
|
|
setting.AppURL = "https://localhost/sub-path/"
|
|
setting.AppSubURL = "/sub-path"
|
|
link = u.AvatarLink(t.Context())
|
|
assert.Equal(t, "https://localhost/sub-path/avatars/avatar.png", link)
|
|
}
|
|
|
|
func TestUserAvatarGenerate(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
var err error
|
|
tmpDir := t.TempDir()
|
|
storage.Avatars, err = storage.NewLocalStorage(t.Context(), &setting.Storage{Path: tmpDir})
|
|
require.NoError(t, err)
|
|
|
|
u := unittest.AssertExistsAndLoadBean(t, &User{ID: 2})
|
|
|
|
// there was no avatar, generate a new one
|
|
assert.Empty(t, u.Avatar)
|
|
err = GenerateRandomAvatar(t.Context(), u)
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, u.Avatar)
|
|
|
|
// make sure the generated one exists
|
|
oldAvatarPath := u.CustomAvatarRelativePath()
|
|
_, err = storage.Avatars.Stat(u.CustomAvatarRelativePath())
|
|
require.NoError(t, err)
|
|
// and try to change its content
|
|
_, err = storage.Avatars.Save(u.CustomAvatarRelativePath(), strings.NewReader("abcd"), 4)
|
|
require.NoError(t, err)
|
|
|
|
// try to generate again
|
|
err = GenerateRandomAvatar(t.Context(), u)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, oldAvatarPath, u.CustomAvatarRelativePath())
|
|
f, err := storage.Avatars.Open(u.CustomAvatarRelativePath())
|
|
require.NoError(t, err)
|
|
defer f.Close()
|
|
content, _ := io.ReadAll(f)
|
|
assert.Equal(t, "abcd", string(content))
|
|
}
|