code.gitea.io/gitea@v1.22.3/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", &opts). 38 AddTokenAuth(token) 39 MakeRequest(t, req, http.StatusNoContent) 40 41 // Test what happens if you don't have a valid Base64 string 42 opts = api.UpdateUserAvatarOption{ 43 Image: "Invalid", 44 } 45 46 req = NewRequestWithJSON(t, "POST", "/api/v1/orgs/org3/avatar", &opts). 47 AddTokenAuth(token) 48 MakeRequest(t, req, http.StatusBadRequest) 49 50 // Test what happens if you use a file that is not an image 51 text, err := os.ReadFile("tests/integration/README.md") 52 assert.NoError(t, err) 53 if err != nil { 54 assert.FailNow(t, "Unable to open README.md") 55 } 56 57 opts = api.UpdateUserAvatarOption{ 58 Image: base64.StdEncoding.EncodeToString(text), 59 } 60 61 req = NewRequestWithJSON(t, "POST", "/api/v1/orgs/org3/avatar", &opts). 62 AddTokenAuth(token) 63 MakeRequest(t, req, http.StatusInternalServerError) 64 } 65 66 func TestAPIDeleteOrgAvatar(t *testing.T) { 67 defer tests.PrepareTestEnv(t)() 68 69 session := loginUser(t, "user1") 70 71 token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization) 72 73 req := NewRequest(t, "DELETE", "/api/v1/orgs/org3/avatar"). 74 AddTokenAuth(token) 75 MakeRequest(t, req, http.StatusNoContent) 76 }