code.gitea.io/gitea@v1.21.7/tests/integration/api_repo_avatar_test.go (about)

     1  // Copyright 2018 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"encoding/base64"
     8  	"fmt"
     9  	"net/http"
    10  	"os"
    11  	"testing"
    12  
    13  	auth_model "code.gitea.io/gitea/models/auth"
    14  	repo_model "code.gitea.io/gitea/models/repo"
    15  	"code.gitea.io/gitea/models/unittest"
    16  	user_model "code.gitea.io/gitea/models/user"
    17  	api "code.gitea.io/gitea/modules/structs"
    18  	"code.gitea.io/gitea/tests"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func TestAPIUpdateRepoAvatar(t *testing.T) {
    24  	defer tests.PrepareTestEnv(t)()
    25  
    26  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
    27  	user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
    28  	token := getUserToken(t, user2.LowerName, auth_model.AccessTokenScopeWriteRepository)
    29  
    30  	// Test what happens if you use a valid image
    31  	avatar, err := os.ReadFile("tests/integration/avatar.png")
    32  	assert.NoError(t, err)
    33  	if err != nil {
    34  		assert.FailNow(t, "Unable to open avatar.png")
    35  	}
    36  
    37  	opts := api.UpdateRepoAvatarOption{
    38  		Image: base64.StdEncoding.EncodeToString(avatar),
    39  	}
    40  
    41  	req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/avatar?token=%s", repo.OwnerName, repo.Name, token), &opts)
    42  	MakeRequest(t, req, http.StatusNoContent)
    43  
    44  	// Test what happens if you don't have a valid Base64 string
    45  	opts = api.UpdateRepoAvatarOption{
    46  		Image: "Invalid",
    47  	}
    48  
    49  	req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/avatar?token=%s", repo.OwnerName, repo.Name, token), &opts)
    50  	MakeRequest(t, req, http.StatusBadRequest)
    51  
    52  	// Test what happens if you use a file that is not an image
    53  	text, err := os.ReadFile("tests/integration/README.md")
    54  	assert.NoError(t, err)
    55  	if err != nil {
    56  		assert.FailNow(t, "Unable to open README.md")
    57  	}
    58  
    59  	opts = api.UpdateRepoAvatarOption{
    60  		Image: base64.StdEncoding.EncodeToString(text),
    61  	}
    62  
    63  	req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/avatar?token=%s", repo.OwnerName, repo.Name, token), &opts)
    64  	MakeRequest(t, req, http.StatusInternalServerError)
    65  }
    66  
    67  func TestAPIDeleteRepoAvatar(t *testing.T) {
    68  	defer tests.PrepareTestEnv(t)()
    69  
    70  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
    71  	user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
    72  	token := getUserToken(t, user2.LowerName, auth_model.AccessTokenScopeWriteRepository)
    73  
    74  	req := NewRequest(t, "DELETE", fmt.Sprintf("/api/v1/repos/%s/%s/avatar?token=%s", repo.OwnerName, repo.Name, token))
    75  	MakeRequest(t, req, http.StatusNoContent)
    76  }