code.gitea.io/gitea@v1.21.7/tests/integration/api_org_avatar_test.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package integration 5 6 import ( 7 "encoding/base64" 8 "net/http" 9 "os" 10 "testing" 11 12 auth_model "code.gitea.io/gitea/models/auth" 13 api "code.gitea.io/gitea/modules/structs" 14 "code.gitea.io/gitea/tests" 15 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestAPIUpdateOrgAvatar(t *testing.T) { 20 defer tests.PrepareTestEnv(t)() 21 22 session := loginUser(t, "user1") 23 24 token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization) 25 26 // Test what happens if you use a valid image 27 avatar, err := os.ReadFile("tests/integration/avatar.png") 28 assert.NoError(t, err) 29 if err != nil { 30 assert.FailNow(t, "Unable to open avatar.png") 31 } 32 33 opts := api.UpdateUserAvatarOption{ 34 Image: base64.StdEncoding.EncodeToString(avatar), 35 } 36 37 req := NewRequestWithJSON(t, "POST", "/api/v1/orgs/org3/avatar?token="+token, &opts) 38 MakeRequest(t, req, http.StatusNoContent) 39 40 // Test what happens if you don't have a valid Base64 string 41 opts = api.UpdateUserAvatarOption{ 42 Image: "Invalid", 43 } 44 45 req = NewRequestWithJSON(t, "POST", "/api/v1/orgs/org3/avatar?token="+token, &opts) 46 MakeRequest(t, req, http.StatusBadRequest) 47 48 // Test what happens if you use a file that is not an image 49 text, err := os.ReadFile("tests/integration/README.md") 50 assert.NoError(t, err) 51 if err != nil { 52 assert.FailNow(t, "Unable to open README.md") 53 } 54 55 opts = api.UpdateUserAvatarOption{ 56 Image: base64.StdEncoding.EncodeToString(text), 57 } 58 59 req = NewRequestWithJSON(t, "POST", "/api/v1/orgs/org3/avatar?token="+token, &opts) 60 MakeRequest(t, req, http.StatusInternalServerError) 61 } 62 63 func TestAPIDeleteOrgAvatar(t *testing.T) { 64 defer tests.PrepareTestEnv(t)() 65 66 session := loginUser(t, "user1") 67 68 token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization) 69 70 req := NewRequest(t, "DELETE", "/api/v1/orgs/org3/avatar?token="+token) 71 MakeRequest(t, req, http.StatusNoContent) 72 }