aboutsummaryrefslogtreecommitdiffstats
path: root/tests/integration/api_repo_file_create_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'tests/integration/api_repo_file_create_test.go')
-rw-r--r--tests/integration/api_repo_file_create_test.go142
1 files changed, 87 insertions, 55 deletions
diff --git a/tests/integration/api_repo_file_create_test.go b/tests/integration/api_repo_file_create_test.go
index 41ad7211ff..af3bc54680 100644
--- a/tests/integration/api_repo_file_create_test.go
+++ b/tests/integration/api_repo_file_create_test.go
@@ -4,12 +4,11 @@
package integration
import (
- stdCtx "context"
"encoding/base64"
"fmt"
"net/http"
"net/url"
- "path/filepath"
+ "path"
"testing"
"time"
@@ -20,6 +19,7 @@ import (
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
+ "code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/context"
"github.com/stretchr/testify/assert"
@@ -50,28 +50,42 @@ func getCreateFileOptions() api.CreateFileOptions {
}
}
-func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCommitSHA string) *api.FileResponse {
+func normalizeFileContentResponseCommitTime(c *api.ContentsResponse) {
+ // decoded JSON response may contain different timezone from the one parsed by git commit
+ // so we need to normalize the time to UTC to make "assert.Equal" pass
+ c.LastCommitterDate = util.ToPointer(c.LastCommitterDate.UTC())
+ c.LastAuthorDate = util.ToPointer(c.LastAuthorDate.UTC())
+}
+
+type apiFileResponseInfo struct {
+ repoFullName, commitID, treePath, lastCommitSHA string
+ lastCommitterWhen, lastAuthorWhen time.Time
+}
+
+func getExpectedFileResponseForCreate(info apiFileResponseInfo) *api.FileResponse {
sha := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
encoding := "base64"
content := "VGhpcyBpcyBuZXcgdGV4dA=="
- selfURL := setting.AppURL + "api/v1/repos/" + repoFullName + "/contents/" + treePath + "?ref=master"
- htmlURL := setting.AppURL + repoFullName + "/src/branch/master/" + treePath
- gitURL := setting.AppURL + "api/v1/repos/" + repoFullName + "/git/blobs/" + sha
- downloadURL := setting.AppURL + repoFullName + "/raw/branch/master/" + treePath
- return &api.FileResponse{
+ selfURL := setting.AppURL + "api/v1/repos/" + info.repoFullName + "/contents/" + info.treePath + "?ref=master"
+ htmlURL := setting.AppURL + info.repoFullName + "/src/branch/master/" + info.treePath
+ gitURL := setting.AppURL + "api/v1/repos/" + info.repoFullName + "/git/blobs/" + sha
+ downloadURL := setting.AppURL + info.repoFullName + "/raw/branch/master/" + info.treePath
+ ret := &api.FileResponse{
Content: &api.ContentsResponse{
- Name: filepath.Base(treePath),
- Path: treePath,
- SHA: sha,
- LastCommitSHA: latestCommitSHA,
- Size: 16,
- Type: "file",
- Encoding: &encoding,
- Content: &content,
- URL: &selfURL,
- HTMLURL: &htmlURL,
- GitURL: &gitURL,
- DownloadURL: &downloadURL,
+ Name: path.Base(info.treePath),
+ Path: info.treePath,
+ SHA: sha,
+ LastCommitSHA: util.ToPointer(info.lastCommitSHA),
+ LastCommitterDate: util.ToPointer(info.lastCommitterWhen),
+ LastAuthorDate: util.ToPointer(info.lastAuthorWhen),
+ Size: 16,
+ Type: "file",
+ Encoding: &encoding,
+ Content: &content,
+ URL: &selfURL,
+ HTMLURL: &htmlURL,
+ GitURL: &gitURL,
+ DownloadURL: &downloadURL,
Links: &api.FileLinksResponse{
Self: &selfURL,
GitURL: &gitURL,
@@ -80,10 +94,10 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCo
},
Commit: &api.FileCommitResponse{
CommitMeta: api.CommitMeta{
- URL: setting.AppURL + "api/v1/repos/" + repoFullName + "/git/commits/" + commitID,
- SHA: commitID,
+ URL: setting.AppURL + "api/v1/repos/" + info.repoFullName + "/git/commits/" + info.commitID,
+ SHA: info.commitID,
},
- HTMLURL: setting.AppURL + repoFullName + "/commit/" + commitID,
+ HTMLURL: setting.AppURL + info.repoFullName + "/commit/" + info.commitID,
Author: &api.CommitUser{
Identity: api.Identity{
Name: "Anne Doe",
@@ -107,6 +121,8 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCo
Payload: "",
},
}
+ normalizeFileContentResponseCommitTime(ret.Content)
+ return ret
}
func BenchmarkAPICreateFileSmall(b *testing.B) {
@@ -115,7 +131,7 @@ func BenchmarkAPICreateFileSmall(b *testing.B) {
repo1 := unittest.AssertExistsAndLoadBean(b, &repo_model.Repository{ID: 1}) // public repo
b.ResetTimer()
- for n := 0; n < b.N; n++ {
+ for n := 0; b.Loop(); n++ {
treePath := fmt.Sprintf("update/file%d.txt", n)
_, _ = createFileInBranch(user2, repo1, treePath, repo1.DefaultBranch, treePath)
}
@@ -130,7 +146,7 @@ func BenchmarkAPICreateFileMedium(b *testing.B) {
repo1 := unittest.AssertExistsAndLoadBean(b, &repo_model.Repository{ID: 1}) // public repo
b.ResetTimer()
- for n := 0; n < b.N; n++ {
+ for n := 0; b.Loop(); n++ {
treePath := fmt.Sprintf("update/file%d.txt", n)
copy(data, treePath)
_, _ = createFileInBranch(user2, repo1, treePath, repo1.DefaultBranch, treePath)
@@ -167,22 +183,30 @@ func TestAPICreateFile(t *testing.T) {
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo1.Name, treePath), &createFileOptions).
AddTokenAuth(token2)
resp := MakeRequest(t, req, http.StatusCreated)
- gitRepo, _ := gitrepo.OpenRepository(stdCtx.Background(), repo1)
+ gitRepo, _ := gitrepo.OpenRepository(t.Context(), repo1)
+ defer gitRepo.Close()
commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
- latestCommit, _ := gitRepo.GetCommitByPath(treePath)
- expectedFileResponse := getExpectedFileResponseForCreate("user2/repo1", commitID, treePath, latestCommit.ID.String())
+ lastCommit, _ := gitRepo.GetCommitByPath(treePath)
+ expectedFileResponse := getExpectedFileResponseForCreate(apiFileResponseInfo{
+ repoFullName: "user2/repo1",
+ commitID: commitID,
+ treePath: treePath,
+ lastCommitSHA: lastCommit.ID.String(),
+ lastCommitterWhen: lastCommit.Committer.When,
+ lastAuthorWhen: lastCommit.Author.When,
+ })
var fileResponse api.FileResponse
DecodeJSON(t, resp, &fileResponse)
- assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
- assert.EqualValues(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
- assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
- assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email)
- assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name)
- assert.EqualValues(t, expectedFileResponse.Commit.Author.Date, fileResponse.Commit.Author.Date)
- assert.EqualValues(t, expectedFileResponse.Commit.Committer.Email, fileResponse.Commit.Committer.Email)
- assert.EqualValues(t, expectedFileResponse.Commit.Committer.Name, fileResponse.Commit.Committer.Name)
- assert.EqualValues(t, expectedFileResponse.Commit.Committer.Date, fileResponse.Commit.Committer.Date)
- gitRepo.Close()
+ normalizeFileContentResponseCommitTime(fileResponse.Content)
+ assert.Equal(t, expectedFileResponse.Content, fileResponse.Content)
+ assert.Equal(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
+ assert.Equal(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
+ assert.Equal(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email)
+ assert.Equal(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name)
+ assert.Equal(t, expectedFileResponse.Commit.Author.Date, fileResponse.Commit.Author.Date)
+ assert.Equal(t, expectedFileResponse.Commit.Committer.Email, fileResponse.Commit.Committer.Email)
+ assert.Equal(t, expectedFileResponse.Commit.Committer.Name, fileResponse.Commit.Committer.Name)
+ assert.Equal(t, expectedFileResponse.Commit.Committer.Date, fileResponse.Commit.Committer.Date)
}
// Test creating a file in a new branch
@@ -199,10 +223,10 @@ func TestAPICreateFile(t *testing.T) {
expectedSHA := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
expectedHTMLURL := fmt.Sprintf(setting.AppURL+"user2/repo1/src/branch/new_branch/new/file%d.txt", fileID)
expectedDownloadURL := fmt.Sprintf(setting.AppURL+"user2/repo1/raw/branch/new_branch/new/file%d.txt", fileID)
- assert.EqualValues(t, expectedSHA, fileResponse.Content.SHA)
- assert.EqualValues(t, expectedHTMLURL, *fileResponse.Content.HTMLURL)
- assert.EqualValues(t, expectedDownloadURL, *fileResponse.Content.DownloadURL)
- assert.EqualValues(t, createFileOptions.Message+"\n", fileResponse.Commit.Message)
+ assert.Equal(t, expectedSHA, fileResponse.Content.SHA)
+ assert.Equal(t, expectedHTMLURL, *fileResponse.Content.HTMLURL)
+ assert.Equal(t, expectedDownloadURL, *fileResponse.Content.DownloadURL)
+ assert.Equal(t, createFileOptions.Message+"\n", fileResponse.Commit.Message)
// Test creating a file without a message
createFileOptions = getCreateFileOptions()
@@ -214,7 +238,7 @@ func TestAPICreateFile(t *testing.T) {
resp = MakeRequest(t, req, http.StatusCreated)
DecodeJSON(t, resp, &fileResponse)
expectedMessage := "Add " + treePath + "\n"
- assert.EqualValues(t, expectedMessage, fileResponse.Commit.Message)
+ assert.Equal(t, expectedMessage, fileResponse.Commit.Message)
// Test trying to create a file that already exists, should fail
createFileOptions = getCreateFileOptions()
@@ -285,20 +309,28 @@ func TestAPICreateFile(t *testing.T) {
AddTokenAuth(token2)
resp = MakeRequest(t, req, http.StatusCreated)
emptyRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "empty-repo"}) // public repo
- gitRepo, _ := gitrepo.OpenRepository(stdCtx.Background(), emptyRepo)
+ gitRepo, _ := gitrepo.OpenRepository(t.Context(), emptyRepo)
+ defer gitRepo.Close()
commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
latestCommit, _ := gitRepo.GetCommitByPath(treePath)
- expectedFileResponse := getExpectedFileResponseForCreate("user2/empty-repo", commitID, treePath, latestCommit.ID.String())
+ expectedFileResponse := getExpectedFileResponseForCreate(apiFileResponseInfo{
+ repoFullName: "user2/empty-repo",
+ commitID: commitID,
+ treePath: treePath,
+ lastCommitSHA: latestCommit.ID.String(),
+ lastCommitterWhen: latestCommit.Committer.When,
+ lastAuthorWhen: latestCommit.Author.When,
+ })
DecodeJSON(t, resp, &fileResponse)
- assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
- assert.EqualValues(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
- assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
- assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email)
- assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name)
- assert.EqualValues(t, expectedFileResponse.Commit.Author.Date, fileResponse.Commit.Author.Date)
- assert.EqualValues(t, expectedFileResponse.Commit.Committer.Email, fileResponse.Commit.Committer.Email)
- assert.EqualValues(t, expectedFileResponse.Commit.Committer.Name, fileResponse.Commit.Committer.Name)
- assert.EqualValues(t, expectedFileResponse.Commit.Committer.Date, fileResponse.Commit.Committer.Date)
- gitRepo.Close()
+ normalizeFileContentResponseCommitTime(fileResponse.Content)
+ assert.Equal(t, expectedFileResponse.Content, fileResponse.Content)
+ assert.Equal(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
+ assert.Equal(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
+ assert.Equal(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email)
+ assert.Equal(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name)
+ assert.Equal(t, expectedFileResponse.Commit.Author.Date, fileResponse.Commit.Author.Date)
+ assert.Equal(t, expectedFileResponse.Commit.Committer.Email, fileResponse.Commit.Committer.Email)
+ assert.Equal(t, expectedFileResponse.Commit.Committer.Name, fileResponse.Commit.Committer.Name)
+ assert.Equal(t, expectedFileResponse.Commit.Committer.Date, fileResponse.Commit.Committer.Date)
})
}