mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git), so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form used by the handful of files that had ours. Shape is now: // Copyright 2026 Hanzo AI, Inc. All rights reserved. // Copyright 2015 The Gogs Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT Ours on top, theirs retained verbatim. Retained because MIT requires it, and separately because they are true — those authors did write that code. Not renamed to "The Git Authors": Git is a different project under a different license, and putting its name here would attribute this code to people who have nothing to do with it. Adding our line is the fix; overwriting theirs would have been both a licence violation and a false statement. gofmt still reports import-order drift in some of these files. It predates this change (verified against a stash) and is left alone rather than burying a one-line-per-file legal change under 3000 files of unrelated reordering. Build clean. Co-authored-by: Hanzo Dev <[email protected]>
72 lines
2.5 KiB
Go
72 lines
2.5 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package integration
|
|
|
|
import (
|
|
"bytes"
|
|
"image/png"
|
|
"io"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/git/models/unittest"
|
|
user_model "github.com/hanzoai/git/models/user"
|
|
"github.com/hanzoai/git/modules/avatar"
|
|
"github.com/hanzoai/git/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)
|
|
}
|