Skip to content

Commit eed88dc

Browse files
authored
Fix broken avatars since #15301 (#15731)
There was a missing * from the avatars routes in #15301. Fix #15727 Signed-off-by: Andrew Thornton <art27@cantab.net>
1 parent 2bd5408 commit eed88dc

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

integrations/user_avatar_test.go

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"bytes"
9+
"image/png"
10+
"io"
11+
"mime/multipart"
12+
"net/http"
13+
"net/url"
14+
"strings"
15+
"testing"
16+
17+
"code.gitea.io/gitea/models"
18+
"code.gitea.io/gitea/modules/avatar"
19+
"github.com/stretchr/testify/assert"
20+
)
21+
22+
func TestUserAvatar(t *testing.T) {
23+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
24+
user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo3, is an org
25+
26+
seed := user2.Email
27+
if len(seed) == 0 {
28+
seed = user2.Name
29+
}
30+
31+
img, err := avatar.RandomImage([]byte(seed))
32+
if err != nil {
33+
assert.NoError(t, err)
34+
return
35+
}
36+
37+
session := loginUser(t, "user2")
38+
csrf := GetCSRF(t, session, "/user/settings")
39+
40+
imgData := &bytes.Buffer{}
41+
42+
body := &bytes.Buffer{}
43+
44+
//Setup multi-part
45+
writer := multipart.NewWriter(body)
46+
writer.WriteField("source", "local")
47+
part, err := writer.CreateFormFile("avatar", "avatar-for-testuseravatar.png")
48+
if err != nil {
49+
assert.NoError(t, err)
50+
return
51+
}
52+
53+
if err := png.Encode(imgData, img); err != nil {
54+
assert.NoError(t, err)
55+
return
56+
}
57+
58+
if _, err := io.Copy(part, imgData); err != nil {
59+
assert.NoError(t, err)
60+
return
61+
}
62+
63+
if err := writer.Close(); err != nil {
64+
assert.NoError(t, err)
65+
return
66+
}
67+
68+
req := NewRequestWithBody(t, "POST", "/user/settings/avatar", body)
69+
req.Header.Add("X-Csrf-Token", csrf)
70+
req.Header.Add("Content-Type", writer.FormDataContentType())
71+
72+
session.MakeRequest(t, req, http.StatusFound)
73+
74+
user2 = models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo3, is an org
75+
76+
req = NewRequest(t, "GET", user2.AvatarLink())
77+
resp := session.MakeRequest(t, req, http.StatusFound)
78+
location := resp.Header().Get("Location")
79+
if !strings.HasPrefix(location, "/avatars") {
80+
assert.Fail(t, "Avatar location is not local: %s", location)
81+
}
82+
req = NewRequest(t, "GET", location)
83+
session.MakeRequest(t, req, http.StatusOK)
84+
85+
// Can't test if the response matches because the image is regened on upload but checking that this at least doesn't give a 404 should be enough.
86+
})
87+
}

routers/routes/web.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ func WebRoutes() *web.Route {
163163
))
164164

165165
// We use r.Route here over r.Use because this prevents requests that are not for avatars having to go through this additional handler
166-
routes.Route("/avatars", "GET, HEAD", storageHandler(setting.Avatar.Storage, "avatars", storage.Avatars))
167-
routes.Route("/repo-avatars", "GET, HEAD", storageHandler(setting.RepoAvatar.Storage, "repo-avatars", storage.RepoAvatars))
166+
routes.Route("/avatars/*", "GET, HEAD", storageHandler(setting.Avatar.Storage, "avatars", storage.Avatars))
167+
routes.Route("/repo-avatars/*", "GET, HEAD", storageHandler(setting.RepoAvatar.Storage, "repo-avatars", storage.RepoAvatars))
168168

169169
// for health check - doeesn't need to be passed through gzip handler
170170
routes.Head("/", func(w http.ResponseWriter, req *http.Request) {

0 commit comments

Comments
 (0)