2020-04-01 03:39:20 +01:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gitea
|
|
|
|
|
|
|
|
import (
|
2021-02-16 20:07:44 +00:00
|
|
|
"bytes"
|
2020-04-01 03:39:20 +01:00
|
|
|
"encoding/base64"
|
|
|
|
"log"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFileCreateUpdateGet(t *testing.T) {
|
2020-04-03 13:55:16 +01:00
|
|
|
log.Println("== TestFileCRUD ==")
|
2020-04-01 03:39:20 +01:00
|
|
|
c := newTestClient()
|
|
|
|
|
|
|
|
repo, err := createTestRepo(t, "ChangeFiles", c)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, repo)
|
|
|
|
|
2022-04-22 08:52:45 +01:00
|
|
|
raw, _, err := c.GetFile(repo.Owner.UserName, repo.Name, "main", "README.md")
|
2020-04-01 03:39:20 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, "IyBDaGFuZ2VGaWxlcwoKQSB0ZXN0IFJlcG86IENoYW5nZUZpbGVz", base64.StdEncoding.EncodeToString(raw))
|
|
|
|
|
2021-02-16 20:07:44 +00:00
|
|
|
testFileName := "A+#&ä"
|
|
|
|
newFile, _, err := c.CreateFile(repo.Owner.UserName, repo.Name, testFileName, CreateFileOptions{
|
2020-04-01 03:39:20 +01:00
|
|
|
FileOptions: FileOptions{
|
2021-02-16 20:07:44 +00:00
|
|
|
Message: "create file " + testFileName,
|
2020-04-01 03:39:20 +01:00
|
|
|
},
|
|
|
|
Content: "ZmlsZUEK",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2022-04-22 08:52:45 +01:00
|
|
|
raw, _, _ = c.GetFile(repo.Owner.UserName, repo.Name, "main", testFileName)
|
2020-04-01 03:39:20 +01:00
|
|
|
assert.EqualValues(t, "ZmlsZUEK", base64.StdEncoding.EncodeToString(raw))
|
|
|
|
|
2021-02-16 20:07:44 +00:00
|
|
|
updatedFile, _, err := c.UpdateFile(repo.Owner.UserName, repo.Name, testFileName, UpdateFileOptions{
|
2020-05-21 11:13:32 +01:00
|
|
|
FileOptions: FileOptions{
|
|
|
|
Message: "add a new line",
|
2020-04-01 03:39:20 +01:00
|
|
|
},
|
2020-05-21 11:13:32 +01:00
|
|
|
SHA: newFile.Content.SHA,
|
2020-04-01 03:39:20 +01:00
|
|
|
Content: "ZmlsZUEKCmFuZCBhIG5ldyBsaW5lCg==",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, updatedFile)
|
|
|
|
|
2022-04-22 08:52:45 +01:00
|
|
|
file, _, err := c.GetContents(repo.Owner.UserName, repo.Name, "main", testFileName)
|
2020-04-01 03:39:20 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, updatedFile.Content.SHA, file.SHA)
|
|
|
|
assert.EqualValues(t, &updatedFile.Content.Content, &file.Content)
|
2020-04-03 13:55:16 +01:00
|
|
|
|
2021-02-16 20:07:44 +00:00
|
|
|
_, err = c.DeleteFile(repo.Owner.UserName, repo.Name, testFileName, DeleteFileOptions{
|
2020-04-03 13:55:16 +01:00
|
|
|
FileOptions: FileOptions{
|
2021-02-16 20:07:44 +00:00
|
|
|
Message: "Delete File " + testFileName,
|
2020-04-03 13:55:16 +01:00
|
|
|
},
|
|
|
|
SHA: updatedFile.Content.SHA,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2022-04-22 08:52:45 +01:00
|
|
|
_, resp, err := c.GetFile(repo.Owner.UserName, repo.Name, "main", testFileName)
|
2021-02-16 22:47:21 +00:00
|
|
|
assert.Error(t, err)
|
2021-02-16 20:07:44 +00:00
|
|
|
assert.EqualValues(t, 404, resp.StatusCode)
|
|
|
|
|
|
|
|
licence, _, err := c.GetContents(repo.Owner.UserName, repo.Name, "", "LICENSE")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
licenceRaw, _, err := c.GetFile(repo.Owner.UserName, repo.Name, "", "LICENSE")
|
|
|
|
testContent := "Tk9USElORyBJUyBIRVJFIEFOWU1PUkUKSUYgWU9VIExJS0UgVE8gRklORCBTT01FVEhJTkcKV0FJVCBGT1IgVEhFIEZVVFVSRQo="
|
|
|
|
updatedFile, _, err = c.UpdateFile(repo.Owner.UserName, repo.Name, "LICENSE", UpdateFileOptions{
|
|
|
|
FileOptions: FileOptions{
|
|
|
|
Message: "Overwrite",
|
2022-04-22 08:52:45 +01:00
|
|
|
BranchName: "main",
|
2021-02-16 20:07:44 +00:00
|
|
|
NewBranchName: "overwrite-a+/&licence",
|
|
|
|
},
|
|
|
|
SHA: licence.SHA,
|
|
|
|
Content: testContent,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, updatedFile)
|
|
|
|
licenceRawNew, _, err := c.GetFile(repo.Owner.UserName, repo.Name, "overwrite-a+/&licence", "LICENSE")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, licence)
|
|
|
|
assert.False(t, bytes.Equal(licenceRaw, licenceRawNew))
|
|
|
|
assert.EqualValues(t, testContent, base64.StdEncoding.EncodeToString(licenceRawNew))
|
2021-02-16 22:47:21 +00:00
|
|
|
|
|
|
|
// ListContents in root dir of default branch
|
|
|
|
dir, resp, err := c.ListContents(repo.Owner.UserName, repo.Name, "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, dir, 3)
|
|
|
|
assert.NotNil(t, resp)
|
|
|
|
|
|
|
|
// ListContents in not existing dir of default branch
|
|
|
|
_, resp, err = c.ListContents(repo.Owner.UserName, repo.Name, "", "/hehe/")
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.EqualValues(t, 404, resp.StatusCode)
|
|
|
|
// ListContents in root dir of not existing branch
|
|
|
|
_, resp, err = c.ListContents(repo.Owner.UserName, repo.Name, "no-ref-at-all", "")
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.EqualValues(t, 404, resp.StatusCode)
|
|
|
|
|
|
|
|
// ListContents try to get file as dir
|
|
|
|
dir, resp, err = c.ListContents(repo.Owner.UserName, repo.Name, "", "LICENSE")
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.EqualValues(t, "expect directory, got file", err.Error())
|
|
|
|
}
|
|
|
|
assert.Nil(t, dir)
|
|
|
|
assert.EqualValues(t, 200, resp.StatusCode)
|
|
|
|
|
|
|
|
// GetContents try to get dir as file
|
|
|
|
file, resp, err = c.GetContents(repo.Owner.UserName, repo.Name, "", "")
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.EqualValues(t, "expect file, got directory", err.Error())
|
|
|
|
}
|
|
|
|
assert.Nil(t, file)
|
|
|
|
assert.EqualValues(t, 200, resp.StatusCode)
|
2020-04-01 03:39:20 +01:00
|
|
|
}
|