9a5720e8ad
Universal: PR Check / Branch Policy (pull_request) Successful in 1s
Branch Policy Check / Verify merge target (pull_request) Successful in 1s
PR RC Release / Build RC Release (pull_request) Successful in 3s
Universal: PR Check / Validate PR (pull_request) Failing after 6s
Branch Cleanup / Delete merged branch (pull_request) Successful in 1s
Universal: PR Check / Build RC Package (pull_request) Has been cancelled
Full namespace migration: update the Go module path and all import statements from git.mokoconsulting.tech to code.mokoconsulting.tech. Also updates all URL references in templates, workflows, configs, tests, and documentation. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
71 lines
2.5 KiB
Go
71 lines
2.5 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package integration
|
|
|
|
import (
|
|
"bytes"
|
|
"image/png"
|
|
"io"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"code.mokoconsulting.tech/MokoConsulting/MokoGitea/models/unittest"
|
|
user_model "code.mokoconsulting.tech/MokoConsulting/MokoGitea/models/user"
|
|
"code.mokoconsulting.tech/MokoConsulting/MokoGitea/modules/avatar"
|
|
"code.mokoconsulting.tech/MokoConsulting/MokoGitea/tests"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestUserAvatar(t *testing.T) {
|
|
defer tests.PrepareTestEnv(t)()
|
|
session := loginUser(t, "user2")
|
|
|
|
img := avatar.RandomImageDefaultSize([]byte("any-random-image-seed"))
|
|
originAvatarData := &bytes.Buffer{}
|
|
require.NoError(t, png.Encode(originAvatarData, img))
|
|
|
|
// setup multipart form to upload avatar
|
|
body := &bytes.Buffer{}
|
|
writer := multipart.NewWriter(body)
|
|
_ = writer.WriteField("source", "local")
|
|
part, err := writer.CreateFormFile("avatar", "avatar-for-testuseravatar.png")
|
|
require.NoError(t, err)
|
|
_, _ = io.Copy(part, bytes.NewReader(originAvatarData.Bytes()))
|
|
require.NoError(t, writer.Close())
|
|
|
|
// upload avatar
|
|
req := NewRequestWithBody(t, "POST", "/user/settings/avatar", body)
|
|
req.Header.Add("Content-Type", writer.FormDataContentType())
|
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
// check user2's avatar can be accessed
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
req = NewRequest(t, "GET", user2.AvatarLinkWithSize(t.Context(), 0))
|
|
_ = session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
req = NewRequest(t, "GET", "/user2.png")
|
|
resp := MakeRequest(t, req, http.StatusSeeOther)
|
|
avatarRedirect := resp.Header().Get("Location")
|
|
assert.Equal(t, "/avatars/"+user2.Avatar, avatarRedirect)
|
|
|
|
// check the content of the avatar is correct
|
|
resp = MakeRequest(t, NewRequest(t, "GET", avatarRedirect), http.StatusOK)
|
|
assert.Equal(t, "image/png", resp.Header().Get("Content-Type"))
|
|
avatarData, _ := io.ReadAll(resp.Body)
|
|
assert.Equal(t, originAvatarData.Bytes(), avatarData)
|
|
|
|
// for non-existing avatar, it should return a random one with proper cache control headers
|
|
resp = MakeRequest(t, NewRequest(t, "GET", "/avatars/no-such-avatar"), http.StatusOK)
|
|
assert.Equal(t, "image/png", resp.Header().Get("Content-Type"))
|
|
assert.NotEmpty(t, resp.Header().Get("ETag"))
|
|
assert.NotEmpty(t, resp.Header().Get("Last-Modified"))
|
|
assert.Contains(t, resp.Header().Get("Cache-Control"), "public")
|
|
avatarData, _ = io.ReadAll(resp.Body)
|
|
_, err = png.Decode(bytes.NewReader(avatarData))
|
|
require.NoError(t, err)
|
|
}
|