|
| 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 | +} |
0 commit comments