2019-04-17 19:06:35 +03:00
|
|
|
// Copyright 2019 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.
|
|
|
|
|
2022-09-02 22:18:23 +03:00
|
|
|
package integration
|
2019-04-17 19:06:35 +03:00
|
|
|
|
|
|
|
import (
|
2019-05-11 18:29:17 +03:00
|
|
|
"net/url"
|
2019-04-17 19:06:35 +03:00
|
|
|
"testing"
|
|
|
|
|
2021-12-10 04:27:50 +03:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-17 15:34:35 +03:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2022-01-20 02:26:57 +03:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2019-05-11 13:21:34 +03:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2019-04-17 19:06:35 +03:00
|
|
|
"code.gitea.io/gitea/modules/test"
|
2021-11-24 10:56:24 +03:00
|
|
|
files_service "code.gitea.io/gitea/services/repository/files"
|
2019-04-17 19:06:35 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-12-10 04:27:50 +03:00
|
|
|
func getDeleteRepoFileOptions(repo *repo_model.Repository) *files_service.DeleteRepoFileOptions {
|
2021-11-24 10:56:24 +03:00
|
|
|
return &files_service.DeleteRepoFileOptions{
|
2019-04-17 19:06:35 +03:00
|
|
|
LastCommitID: "",
|
|
|
|
OldBranch: repo.DefaultBranch,
|
|
|
|
NewBranch: repo.DefaultBranch,
|
|
|
|
TreePath: "README.md",
|
|
|
|
Message: "Deletes README.md",
|
|
|
|
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
|
2021-11-24 10:56:24 +03:00
|
|
|
Author: &files_service.IdentityOptions{
|
2019-06-29 18:19:24 +03:00
|
|
|
Name: "Bob Smith",
|
|
|
|
Email: "bob@smith.com",
|
|
|
|
},
|
|
|
|
Committer: nil,
|
2019-04-17 19:06:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 18:29:17 +03:00
|
|
|
func getExpectedDeleteFileResponse(u *url.URL) *api.FileResponse {
|
2019-06-29 18:19:24 +03:00
|
|
|
// Just returns fields that don't change, i.e. fields with commit SHAs and dates can't be determined
|
2019-04-17 19:06:35 +03:00
|
|
|
return &api.FileResponse{
|
|
|
|
Content: nil,
|
|
|
|
Commit: &api.FileCommitResponse{
|
|
|
|
Author: &api.CommitUser{
|
|
|
|
Identity: api.Identity{
|
2019-06-29 18:19:24 +03:00
|
|
|
Name: "Bob Smith",
|
|
|
|
Email: "bob@smith.com",
|
2019-04-17 19:06:35 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Committer: &api.CommitUser{
|
|
|
|
Identity: api.Identity{
|
2019-06-29 18:19:24 +03:00
|
|
|
Name: "Bob Smith",
|
|
|
|
Email: "bob@smith.com",
|
2019-04-17 19:06:35 +03:00
|
|
|
},
|
|
|
|
},
|
2019-06-29 18:19:24 +03:00
|
|
|
Message: "Deletes README.md\n",
|
2019-04-17 19:06:35 +03:00
|
|
|
},
|
|
|
|
Verification: &api.PayloadCommitVerification{
|
|
|
|
Verified: false,
|
2019-10-16 16:42:42 +03:00
|
|
|
Reason: "gpg.error.not_signed_commit",
|
2019-04-17 19:06:35 +03:00
|
|
|
Signature: "",
|
|
|
|
Payload: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteRepoFile(t *testing.T) {
|
2019-05-11 18:29:17 +03:00
|
|
|
onGiteaRun(t, testDeleteRepoFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDeleteRepoFile(t *testing.T, u *url.URL) {
|
2019-04-17 19:06:35 +03:00
|
|
|
// setup
|
2021-11-12 17:36:47 +03:00
|
|
|
unittest.PrepareTestEnv(t)
|
2019-04-17 19:06:35 +03:00
|
|
|
ctx := test.MockContext(t, "user2/repo1")
|
|
|
|
ctx.SetParams(":id", "1")
|
|
|
|
test.LoadRepo(t, ctx, 1)
|
|
|
|
test.LoadRepoCommit(t, ctx)
|
|
|
|
test.LoadUser(t, ctx, 2)
|
|
|
|
test.LoadGitRepo(t, ctx)
|
2019-11-13 10:01:19 +03:00
|
|
|
defer ctx.Repo.GitRepo.Close()
|
2019-04-17 19:06:35 +03:00
|
|
|
repo := ctx.Repo.Repository
|
2022-03-22 10:03:22 +03:00
|
|
|
doer := ctx.Doer
|
2019-04-17 19:06:35 +03:00
|
|
|
opts := getDeleteRepoFileOptions(repo)
|
|
|
|
|
|
|
|
t.Run("Delete README.md file", func(t *testing.T) {
|
2022-01-20 02:26:57 +03:00
|
|
|
fileResponse, err := files_service.DeleteRepoFile(git.DefaultContext, repo, doer, opts)
|
2020-07-19 12:53:40 +03:00
|
|
|
assert.NoError(t, err)
|
2019-05-11 18:29:17 +03:00
|
|
|
expectedFileResponse := getExpectedDeleteFileResponse(u)
|
2019-06-29 18:19:24 +03:00
|
|
|
assert.NotNil(t, fileResponse)
|
|
|
|
assert.Nil(t, fileResponse.Content)
|
|
|
|
assert.EqualValues(t, expectedFileResponse.Commit.Message, fileResponse.Commit.Message)
|
|
|
|
assert.EqualValues(t, expectedFileResponse.Commit.Author.Identity, fileResponse.Commit.Author.Identity)
|
|
|
|
assert.EqualValues(t, expectedFileResponse.Commit.Committer.Identity, fileResponse.Commit.Committer.Identity)
|
|
|
|
assert.EqualValues(t, expectedFileResponse.Verification, fileResponse.Verification)
|
2019-04-17 19:06:35 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Verify README.md has been deleted", func(t *testing.T) {
|
2022-01-20 02:26:57 +03:00
|
|
|
fileResponse, err := files_service.DeleteRepoFile(git.DefaultContext, repo, doer, opts)
|
2019-04-17 19:06:35 +03:00
|
|
|
assert.Nil(t, fileResponse)
|
|
|
|
expectedError := "repository file does not exist [path: " + opts.TreePath + "]"
|
|
|
|
assert.EqualError(t, err, expectedError)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test opts with branch names removed, same results
|
|
|
|
func TestDeleteRepoFileWithoutBranchNames(t *testing.T) {
|
2019-05-11 18:29:17 +03:00
|
|
|
onGiteaRun(t, testDeleteRepoFileWithoutBranchNames)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDeleteRepoFileWithoutBranchNames(t *testing.T, u *url.URL) {
|
2019-04-17 19:06:35 +03:00
|
|
|
// setup
|
2021-11-12 17:36:47 +03:00
|
|
|
unittest.PrepareTestEnv(t)
|
2019-04-17 19:06:35 +03:00
|
|
|
ctx := test.MockContext(t, "user2/repo1")
|
|
|
|
ctx.SetParams(":id", "1")
|
|
|
|
test.LoadRepo(t, ctx, 1)
|
|
|
|
test.LoadRepoCommit(t, ctx)
|
|
|
|
test.LoadUser(t, ctx, 2)
|
|
|
|
test.LoadGitRepo(t, ctx)
|
2019-11-13 10:01:19 +03:00
|
|
|
defer ctx.Repo.GitRepo.Close()
|
|
|
|
|
2019-04-17 19:06:35 +03:00
|
|
|
repo := ctx.Repo.Repository
|
2022-03-22 10:03:22 +03:00
|
|
|
doer := ctx.Doer
|
2019-04-17 19:06:35 +03:00
|
|
|
opts := getDeleteRepoFileOptions(repo)
|
|
|
|
opts.OldBranch = ""
|
|
|
|
opts.NewBranch = ""
|
|
|
|
|
|
|
|
t.Run("Delete README.md without Branch Name", func(t *testing.T) {
|
2022-01-20 02:26:57 +03:00
|
|
|
fileResponse, err := files_service.DeleteRepoFile(git.DefaultContext, repo, doer, opts)
|
2020-07-19 12:53:40 +03:00
|
|
|
assert.NoError(t, err)
|
2019-05-11 18:29:17 +03:00
|
|
|
expectedFileResponse := getExpectedDeleteFileResponse(u)
|
2019-06-29 18:19:24 +03:00
|
|
|
assert.NotNil(t, fileResponse)
|
|
|
|
assert.Nil(t, fileResponse.Content)
|
|
|
|
assert.EqualValues(t, expectedFileResponse.Commit.Message, fileResponse.Commit.Message)
|
|
|
|
assert.EqualValues(t, expectedFileResponse.Commit.Author.Identity, fileResponse.Commit.Author.Identity)
|
|
|
|
assert.EqualValues(t, expectedFileResponse.Commit.Committer.Identity, fileResponse.Commit.Committer.Identity)
|
|
|
|
assert.EqualValues(t, expectedFileResponse.Verification, fileResponse.Verification)
|
2019-04-17 19:06:35 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteRepoFileErrors(t *testing.T) {
|
|
|
|
// setup
|
2021-11-12 17:36:47 +03:00
|
|
|
unittest.PrepareTestEnv(t)
|
2019-04-17 19:06:35 +03:00
|
|
|
ctx := test.MockContext(t, "user2/repo1")
|
|
|
|
ctx.SetParams(":id", "1")
|
|
|
|
test.LoadRepo(t, ctx, 1)
|
|
|
|
test.LoadRepoCommit(t, ctx)
|
|
|
|
test.LoadUser(t, ctx, 2)
|
|
|
|
test.LoadGitRepo(t, ctx)
|
2019-11-13 10:01:19 +03:00
|
|
|
defer ctx.Repo.GitRepo.Close()
|
|
|
|
|
2019-04-17 19:06:35 +03:00
|
|
|
repo := ctx.Repo.Repository
|
2022-03-22 10:03:22 +03:00
|
|
|
doer := ctx.Doer
|
2019-04-17 19:06:35 +03:00
|
|
|
|
|
|
|
t.Run("Bad branch", func(t *testing.T) {
|
|
|
|
opts := getDeleteRepoFileOptions(repo)
|
|
|
|
opts.OldBranch = "bad_branch"
|
2022-01-20 02:26:57 +03:00
|
|
|
fileResponse, err := files_service.DeleteRepoFile(git.DefaultContext, repo, doer, opts)
|
2019-04-17 19:06:35 +03:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, fileResponse)
|
|
|
|
expectedError := "branch does not exist [name: " + opts.OldBranch + "]"
|
|
|
|
assert.EqualError(t, err, expectedError)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Bad SHA", func(t *testing.T) {
|
|
|
|
opts := getDeleteRepoFileOptions(repo)
|
|
|
|
origSHA := opts.SHA
|
|
|
|
opts.SHA = "bad_sha"
|
2022-01-20 02:26:57 +03:00
|
|
|
fileResponse, err := files_service.DeleteRepoFile(git.DefaultContext, repo, doer, opts)
|
2019-04-17 19:06:35 +03:00
|
|
|
assert.Nil(t, fileResponse)
|
|
|
|
assert.Error(t, err)
|
|
|
|
expectedError := "sha does not match [given: " + opts.SHA + ", expected: " + origSHA + "]"
|
|
|
|
assert.EqualError(t, err, expectedError)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("New branch already exists", func(t *testing.T) {
|
|
|
|
opts := getDeleteRepoFileOptions(repo)
|
|
|
|
opts.NewBranch = "develop"
|
2022-01-20 02:26:57 +03:00
|
|
|
fileResponse, err := files_service.DeleteRepoFile(git.DefaultContext, repo, doer, opts)
|
2019-04-17 19:06:35 +03:00
|
|
|
assert.Nil(t, fileResponse)
|
|
|
|
assert.Error(t, err)
|
|
|
|
expectedError := "branch already exists [name: " + opts.NewBranch + "]"
|
|
|
|
assert.EqualError(t, err, expectedError)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("TreePath is empty:", func(t *testing.T) {
|
|
|
|
opts := getDeleteRepoFileOptions(repo)
|
|
|
|
opts.TreePath = ""
|
2022-01-20 02:26:57 +03:00
|
|
|
fileResponse, err := files_service.DeleteRepoFile(git.DefaultContext, repo, doer, opts)
|
2019-04-17 19:06:35 +03:00
|
|
|
assert.Nil(t, fileResponse)
|
|
|
|
assert.Error(t, err)
|
|
|
|
expectedError := "path contains a malformed path component [path: ]"
|
|
|
|
assert.EqualError(t, err, expectedError)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("TreePath is a git directory:", func(t *testing.T) {
|
|
|
|
opts := getDeleteRepoFileOptions(repo)
|
|
|
|
opts.TreePath = ".git"
|
2022-01-20 02:26:57 +03:00
|
|
|
fileResponse, err := files_service.DeleteRepoFile(git.DefaultContext, repo, doer, opts)
|
2019-04-17 19:06:35 +03:00
|
|
|
assert.Nil(t, fileResponse)
|
|
|
|
assert.Error(t, err)
|
|
|
|
expectedError := "path contains a malformed path component [path: " + opts.TreePath + "]"
|
|
|
|
assert.EqualError(t, err, expectedError)
|
|
|
|
})
|
|
|
|
}
|