2017-11-02 10:51:03 -07:00
|
|
|
// Copyright 2017 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 integrations
|
|
|
|
|
|
|
|
import (
|
2018-01-16 04:07:47 -07:00
|
|
|
"fmt"
|
2017-11-02 10:51:03 -07:00
|
|
|
"io/ioutil"
|
2020-04-27 04:20:09 -07:00
|
|
|
"math/rand"
|
2019-02-12 08:09:43 -07:00
|
|
|
"net/http"
|
2017-12-08 05:21:37 -07:00
|
|
|
"net/url"
|
2017-11-02 10:51:03 -07:00
|
|
|
"os"
|
2019-02-12 08:09:43 -07:00
|
|
|
"path"
|
2017-11-02 10:51:03 -07:00
|
|
|
"path/filepath"
|
2019-05-31 03:12:15 -07:00
|
|
|
"strconv"
|
2017-11-02 10:51:03 -07:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-02-12 08:09:43 -07:00
|
|
|
"code.gitea.io/gitea/models"
|
2019-03-27 02:33:00 -07:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2019-10-11 17:13:27 -07:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-05-31 03:12:15 -07:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2017-11-02 10:51:03 -07:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2018-01-16 04:07:47 -07:00
|
|
|
const (
|
|
|
|
littleSize = 1024 //1ko
|
|
|
|
bigSize = 128 * 1024 * 1024 //128Mo
|
|
|
|
)
|
|
|
|
|
2019-02-03 16:56:53 -07:00
|
|
|
func TestGit(t *testing.T) {
|
|
|
|
onGiteaRun(t, testGit)
|
|
|
|
}
|
2017-11-02 10:51:03 -07:00
|
|
|
|
2019-02-03 16:56:53 -07:00
|
|
|
func testGit(t *testing.T, u *url.URL) {
|
|
|
|
username := "user2"
|
|
|
|
baseAPITestContext := NewAPITestContext(t, username, "repo1")
|
2017-11-02 10:51:03 -07:00
|
|
|
|
2019-02-03 16:56:53 -07:00
|
|
|
u.Path = baseAPITestContext.GitPath()
|
2017-11-02 10:51:03 -07:00
|
|
|
|
2019-06-22 10:35:34 -07:00
|
|
|
forkedUserCtx := NewAPITestContext(t, "user4", "repo1")
|
|
|
|
|
2019-02-03 16:56:53 -07:00
|
|
|
t.Run("HTTP", func(t *testing.T) {
|
2019-11-25 16:21:37 -07:00
|
|
|
defer PrintCurrentTest(t)()
|
2019-06-22 10:35:34 -07:00
|
|
|
ensureAnonymousClone(t, u)
|
2019-02-03 16:56:53 -07:00
|
|
|
httpContext := baseAPITestContext
|
|
|
|
httpContext.Reponame = "repo-tmp-17"
|
2019-06-22 10:35:34 -07:00
|
|
|
forkedUserCtx.Reponame = httpContext.Reponame
|
2017-11-02 10:51:03 -07:00
|
|
|
|
2019-02-03 16:56:53 -07:00
|
|
|
dstPath, err := ioutil.TempDir("", httpContext.Reponame)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.RemoveAll(dstPath)
|
2019-02-12 08:09:43 -07:00
|
|
|
|
2019-06-22 10:35:34 -07:00
|
|
|
t.Run("CreateRepoInDifferentUser", doAPICreateRepository(forkedUserCtx, false))
|
|
|
|
t.Run("AddUserAsCollaborator", doAPIAddCollaborator(forkedUserCtx, httpContext.Username, models.AccessModeRead))
|
|
|
|
|
|
|
|
t.Run("ForkFromDifferentUser", doAPIForkRepository(httpContext, forkedUserCtx.Username))
|
2019-02-12 08:09:43 -07:00
|
|
|
|
2019-05-31 03:12:15 -07:00
|
|
|
u.Path = httpContext.GitPath()
|
|
|
|
u.User = url.UserPassword(username, userPassword)
|
2019-02-12 08:09:43 -07:00
|
|
|
|
2019-05-31 03:12:15 -07:00
|
|
|
t.Run("Clone", doGitClone(dstPath, u))
|
2019-05-28 03:32:41 -07:00
|
|
|
|
2019-05-31 03:12:15 -07:00
|
|
|
little, big := standardCommitAndPushTest(t, dstPath)
|
|
|
|
littleLFS, bigLFS := lfsCommitAndPushTest(t, dstPath)
|
|
|
|
rawTest(t, &httpContext, little, big, littleLFS, bigLFS)
|
|
|
|
mediaTest(t, &httpContext, little, big, littleLFS, bigLFS)
|
2019-02-12 08:09:43 -07:00
|
|
|
|
2019-05-31 03:12:15 -07:00
|
|
|
t.Run("BranchProtectMerge", doBranchProtectPRMerge(&httpContext, dstPath))
|
2019-06-22 10:35:34 -07:00
|
|
|
t.Run("MergeFork", func(t *testing.T) {
|
2020-04-27 04:20:09 -07:00
|
|
|
defer PrintCurrentTest(t)()
|
2019-06-22 10:35:34 -07:00
|
|
|
t.Run("CreatePRAndMerge", doMergeFork(httpContext, forkedUserCtx, "master", httpContext.Username+":master"))
|
|
|
|
rawTest(t, &forkedUserCtx, little, big, littleLFS, bigLFS)
|
|
|
|
mediaTest(t, &forkedUserCtx, little, big, littleLFS, bigLFS)
|
|
|
|
})
|
2019-12-14 19:49:52 -07:00
|
|
|
|
|
|
|
t.Run("PushCreate", doPushCreate(httpContext, u))
|
2019-02-03 16:56:53 -07:00
|
|
|
})
|
|
|
|
t.Run("SSH", func(t *testing.T) {
|
2019-11-25 16:21:37 -07:00
|
|
|
defer PrintCurrentTest(t)()
|
2019-02-03 16:56:53 -07:00
|
|
|
sshContext := baseAPITestContext
|
|
|
|
sshContext.Reponame = "repo-tmp-18"
|
|
|
|
keyname := "my-testing-key"
|
2019-06-22 10:35:34 -07:00
|
|
|
forkedUserCtx.Reponame = sshContext.Reponame
|
|
|
|
t.Run("CreateRepoInDifferentUser", doAPICreateRepository(forkedUserCtx, false))
|
|
|
|
t.Run("AddUserAsCollaborator", doAPIAddCollaborator(forkedUserCtx, sshContext.Username, models.AccessModeRead))
|
|
|
|
t.Run("ForkFromDifferentUser", doAPIForkRepository(sshContext, forkedUserCtx.Username))
|
|
|
|
|
2019-02-03 16:56:53 -07:00
|
|
|
//Setup key the user ssh key
|
|
|
|
withKeyFile(t, keyname, func(keyFile string) {
|
|
|
|
t.Run("CreateUserKey", doAPICreateUserKey(sshContext, "test-key", keyFile))
|
2018-01-16 04:07:47 -07:00
|
|
|
|
2019-02-03 16:56:53 -07:00
|
|
|
//Setup remote link
|
2019-05-31 03:12:15 -07:00
|
|
|
//TODO: get url from api
|
2019-02-03 16:56:53 -07:00
|
|
|
sshURL := createSSHUrl(sshContext.GitPath(), u)
|
2018-01-16 04:07:47 -07:00
|
|
|
|
|
|
|
//Setup clone folder
|
2019-02-03 16:56:53 -07:00
|
|
|
dstPath, err := ioutil.TempDir("", sshContext.Reponame)
|
2018-01-16 04:07:47 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.RemoveAll(dstPath)
|
2019-02-03 16:56:53 -07:00
|
|
|
|
2019-05-31 03:12:15 -07:00
|
|
|
t.Run("Clone", doGitClone(dstPath, sshURL))
|
|
|
|
|
|
|
|
little, big := standardCommitAndPushTest(t, dstPath)
|
|
|
|
littleLFS, bigLFS := lfsCommitAndPushTest(t, dstPath)
|
|
|
|
rawTest(t, &sshContext, little, big, littleLFS, bigLFS)
|
|
|
|
mediaTest(t, &sshContext, little, big, littleLFS, bigLFS)
|
|
|
|
|
|
|
|
t.Run("BranchProtectMerge", doBranchProtectPRMerge(&sshContext, dstPath))
|
2019-06-22 10:35:34 -07:00
|
|
|
t.Run("MergeFork", func(t *testing.T) {
|
2020-04-27 04:20:09 -07:00
|
|
|
defer PrintCurrentTest(t)()
|
2019-06-22 10:35:34 -07:00
|
|
|
t.Run("CreatePRAndMerge", doMergeFork(sshContext, forkedUserCtx, "master", sshContext.Username+":master"))
|
|
|
|
rawTest(t, &forkedUserCtx, little, big, littleLFS, bigLFS)
|
|
|
|
mediaTest(t, &forkedUserCtx, little, big, littleLFS, bigLFS)
|
|
|
|
})
|
2019-12-14 19:49:52 -07:00
|
|
|
|
|
|
|
t.Run("PushCreate", doPushCreate(sshContext, sshURL))
|
2017-12-08 05:21:37 -07:00
|
|
|
})
|
2017-11-02 10:51:03 -07:00
|
|
|
})
|
|
|
|
}
|
2018-01-16 04:07:47 -07:00
|
|
|
|
2019-02-03 16:56:53 -07:00
|
|
|
func ensureAnonymousClone(t *testing.T, u *url.URL) {
|
|
|
|
dstLocalPath, err := ioutil.TempDir("", "repo1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.RemoveAll(dstLocalPath)
|
|
|
|
t.Run("CloneAnonymous", doGitClone(dstLocalPath, u))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-31 03:12:15 -07:00
|
|
|
func standardCommitAndPushTest(t *testing.T, dstPath string) (little, big string) {
|
|
|
|
t.Run("Standard", func(t *testing.T) {
|
2019-11-25 16:21:37 -07:00
|
|
|
defer PrintCurrentTest(t)()
|
2019-05-31 03:12:15 -07:00
|
|
|
little, big = commitAndPushTest(t, dstPath, "data-file-")
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func lfsCommitAndPushTest(t *testing.T, dstPath string) (littleLFS, bigLFS string) {
|
|
|
|
t.Run("LFS", func(t *testing.T) {
|
2019-11-25 16:21:37 -07:00
|
|
|
defer PrintCurrentTest(t)()
|
2019-10-11 17:13:27 -07:00
|
|
|
setting.CheckLFSVersion()
|
|
|
|
if !setting.LFS.StartServer {
|
|
|
|
t.Skip()
|
|
|
|
return
|
|
|
|
}
|
2019-05-31 03:12:15 -07:00
|
|
|
prefix := "lfs-data-file-"
|
|
|
|
_, err := git.NewCommand("lfs").AddArguments("install").RunInDir(dstPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = git.NewCommand("lfs").AddArguments("track", prefix+"*").RunInDir(dstPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = git.AddChanges(dstPath, false, ".gitattributes")
|
|
|
|
assert.NoError(t, err)
|
2019-11-26 17:35:52 -07:00
|
|
|
|
|
|
|
err = git.CommitChangesWithArgs(dstPath, allowLFSFilters(), git.CommitChangesOptions{
|
2019-10-11 17:13:27 -07:00
|
|
|
Committer: &git.Signature{
|
|
|
|
Email: "user2@example.com",
|
|
|
|
Name: "User Two",
|
|
|
|
When: time.Now(),
|
|
|
|
},
|
|
|
|
Author: &git.Signature{
|
|
|
|
Email: "user2@example.com",
|
|
|
|
Name: "User Two",
|
|
|
|
When: time.Now(),
|
|
|
|
},
|
|
|
|
Message: fmt.Sprintf("Testing commit @ %v", time.Now()),
|
|
|
|
})
|
2019-11-16 11:21:39 -07:00
|
|
|
assert.NoError(t, err)
|
2019-05-31 03:12:15 -07:00
|
|
|
|
|
|
|
littleLFS, bigLFS = commitAndPushTest(t, dstPath, prefix)
|
|
|
|
|
|
|
|
t.Run("Locks", func(t *testing.T) {
|
2019-11-25 16:21:37 -07:00
|
|
|
defer PrintCurrentTest(t)()
|
2019-05-31 03:12:15 -07:00
|
|
|
lockTest(t, dstPath)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func commitAndPushTest(t *testing.T, dstPath, prefix string) (little, big string) {
|
|
|
|
t.Run("PushCommit", func(t *testing.T) {
|
2019-11-25 16:21:37 -07:00
|
|
|
defer PrintCurrentTest(t)()
|
2019-05-31 03:12:15 -07:00
|
|
|
t.Run("Little", func(t *testing.T) {
|
2019-11-25 16:21:37 -07:00
|
|
|
defer PrintCurrentTest(t)()
|
2019-05-31 03:12:15 -07:00
|
|
|
little = doCommitAndPush(t, littleSize, dstPath, prefix)
|
|
|
|
})
|
|
|
|
t.Run("Big", func(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping test in short mode.")
|
|
|
|
return
|
|
|
|
}
|
2019-11-25 16:21:37 -07:00
|
|
|
defer PrintCurrentTest(t)()
|
2019-05-31 03:12:15 -07:00
|
|
|
big = doCommitAndPush(t, bigSize, dstPath, prefix)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func rawTest(t *testing.T, ctx *APITestContext, little, big, littleLFS, bigLFS string) {
|
|
|
|
t.Run("Raw", func(t *testing.T) {
|
2019-11-25 16:21:37 -07:00
|
|
|
defer PrintCurrentTest(t)()
|
2019-05-31 03:12:15 -07:00
|
|
|
username := ctx.Username
|
|
|
|
reponame := ctx.Reponame
|
|
|
|
|
|
|
|
session := loginUser(t, username)
|
|
|
|
|
|
|
|
// Request raw paths
|
|
|
|
req := NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", little))
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
assert.Equal(t, littleSize, resp.Body.Len())
|
|
|
|
|
2019-10-11 17:13:27 -07:00
|
|
|
setting.CheckLFSVersion()
|
|
|
|
if setting.LFS.StartServer {
|
|
|
|
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", littleLFS))
|
|
|
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
assert.NotEqual(t, littleSize, resp.Body.Len())
|
|
|
|
assert.Contains(t, resp.Body.String(), models.LFSMetaFileIdentifier)
|
|
|
|
}
|
2019-05-31 03:12:15 -07:00
|
|
|
|
|
|
|
if !testing.Short() {
|
|
|
|
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", big))
|
|
|
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
assert.Equal(t, bigSize, resp.Body.Len())
|
|
|
|
|
2019-10-11 17:13:27 -07:00
|
|
|
if setting.LFS.StartServer {
|
|
|
|
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", bigLFS))
|
|
|
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
assert.NotEqual(t, bigSize, resp.Body.Len())
|
|
|
|
assert.Contains(t, resp.Body.String(), models.LFSMetaFileIdentifier)
|
|
|
|
}
|
2019-05-31 03:12:15 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func mediaTest(t *testing.T, ctx *APITestContext, little, big, littleLFS, bigLFS string) {
|
|
|
|
t.Run("Media", func(t *testing.T) {
|
2019-11-25 16:21:37 -07:00
|
|
|
defer PrintCurrentTest(t)()
|
2019-05-31 03:12:15 -07:00
|
|
|
|
|
|
|
username := ctx.Username
|
|
|
|
reponame := ctx.Reponame
|
|
|
|
|
|
|
|
session := loginUser(t, username)
|
|
|
|
|
|
|
|
// Request media paths
|
|
|
|
req := NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", little))
|
|
|
|
resp := session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
|
|
|
|
assert.Equal(t, littleSize, resp.Length)
|
|
|
|
|
2019-10-11 17:13:27 -07:00
|
|
|
setting.CheckLFSVersion()
|
|
|
|
if setting.LFS.StartServer {
|
|
|
|
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", littleLFS))
|
|
|
|
resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
|
|
|
|
assert.Equal(t, littleSize, resp.Length)
|
|
|
|
}
|
2019-05-31 03:12:15 -07:00
|
|
|
|
|
|
|
if !testing.Short() {
|
|
|
|
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", big))
|
|
|
|
resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
|
|
|
|
assert.Equal(t, bigSize, resp.Length)
|
|
|
|
|
2019-10-11 17:13:27 -07:00
|
|
|
if setting.LFS.StartServer {
|
|
|
|
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", bigLFS))
|
|
|
|
resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
|
|
|
|
assert.Equal(t, bigSize, resp.Length)
|
|
|
|
}
|
2019-05-31 03:12:15 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func lockTest(t *testing.T, repoPath string) {
|
2019-05-28 03:32:41 -07:00
|
|
|
lockFileTest(t, "README.md", repoPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func lockFileTest(t *testing.T, filename, repoPath string) {
|
|
|
|
_, err := git.NewCommand("lfs").AddArguments("locks").RunInDir(repoPath)
|
2018-01-16 04:07:47 -07:00
|
|
|
assert.NoError(t, err)
|
2019-05-28 03:32:41 -07:00
|
|
|
_, err = git.NewCommand("lfs").AddArguments("lock", filename).RunInDir(repoPath)
|
2018-01-16 04:07:47 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = git.NewCommand("lfs").AddArguments("locks").RunInDir(repoPath)
|
|
|
|
assert.NoError(t, err)
|
2019-05-28 03:32:41 -07:00
|
|
|
_, err = git.NewCommand("lfs").AddArguments("unlock", filename).RunInDir(repoPath)
|
2018-01-16 04:07:47 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2019-05-31 03:12:15 -07:00
|
|
|
func doCommitAndPush(t *testing.T, size int, repoPath, prefix string) string {
|
|
|
|
name, err := generateCommitWithNewData(size, repoPath, "user2@example.com", "User Two", prefix)
|
2018-01-16 04:07:47 -07:00
|
|
|
assert.NoError(t, err)
|
2019-05-31 03:12:15 -07:00
|
|
|
_, err = git.NewCommand("push", "origin", "master").RunInDir(repoPath) //Push
|
2018-01-16 04:07:47 -07:00
|
|
|
assert.NoError(t, err)
|
2019-02-12 08:09:43 -07:00
|
|
|
return name
|
2018-01-16 04:07:47 -07:00
|
|
|
}
|
|
|
|
|
2019-05-31 03:12:15 -07:00
|
|
|
func generateCommitWithNewData(size int, repoPath, email, fullName, prefix string) (string, error) {
|
2018-01-16 04:07:47 -07:00
|
|
|
//Generate random file
|
2020-04-27 04:20:09 -07:00
|
|
|
bufSize := 4 * 1024
|
|
|
|
if bufSize > size {
|
|
|
|
bufSize = size
|
2018-01-16 04:07:47 -07:00
|
|
|
}
|
2020-04-27 04:20:09 -07:00
|
|
|
|
|
|
|
buffer := make([]byte, bufSize)
|
|
|
|
|
2019-05-31 03:12:15 -07:00
|
|
|
tmpFile, err := ioutil.TempFile(repoPath, prefix)
|
2018-01-16 04:07:47 -07:00
|
|
|
if err != nil {
|
2019-02-12 08:09:43 -07:00
|
|
|
return "", err
|
2018-01-16 04:07:47 -07:00
|
|
|
}
|
|
|
|
defer tmpFile.Close()
|
2020-04-27 04:20:09 -07:00
|
|
|
written := 0
|
|
|
|
for written < size {
|
|
|
|
n := size - written
|
|
|
|
if n > bufSize {
|
|
|
|
n = bufSize
|
|
|
|
}
|
|
|
|
_, err := rand.Read(buffer[:n])
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
n, err = tmpFile.Write(buffer[:n])
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
written += n
|
|
|
|
}
|
2018-01-16 04:07:47 -07:00
|
|
|
if err != nil {
|
2019-02-12 08:09:43 -07:00
|
|
|
return "", err
|
2018-01-16 04:07:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//Commit
|
2019-10-11 17:13:27 -07:00
|
|
|
// Now here we should explicitly allow lfs filters to run
|
2019-11-26 17:35:52 -07:00
|
|
|
globalArgs := allowLFSFilters()
|
|
|
|
err = git.AddChangesWithArgs(repoPath, globalArgs, false, filepath.Base(tmpFile.Name()))
|
2018-01-16 04:07:47 -07:00
|
|
|
if err != nil {
|
2019-02-12 08:09:43 -07:00
|
|
|
return "", err
|
2018-01-16 04:07:47 -07:00
|
|
|
}
|
2019-11-26 17:35:52 -07:00
|
|
|
err = git.CommitChangesWithArgs(repoPath, globalArgs, git.CommitChangesOptions{
|
2018-01-16 04:07:47 -07:00
|
|
|
Committer: &git.Signature{
|
|
|
|
Email: email,
|
|
|
|
Name: fullName,
|
|
|
|
When: time.Now(),
|
|
|
|
},
|
|
|
|
Author: &git.Signature{
|
|
|
|
Email: email,
|
|
|
|
Name: fullName,
|
|
|
|
When: time.Now(),
|
|
|
|
},
|
|
|
|
Message: fmt.Sprintf("Testing commit @ %v", time.Now()),
|
|
|
|
})
|
2019-02-12 08:09:43 -07:00
|
|
|
return filepath.Base(tmpFile.Name()), err
|
2018-01-16 04:07:47 -07:00
|
|
|
}
|
2019-05-31 03:12:15 -07:00
|
|
|
|
|
|
|
func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
2019-11-25 16:21:37 -07:00
|
|
|
defer PrintCurrentTest(t)()
|
2019-05-31 03:12:15 -07:00
|
|
|
t.Run("CreateBranchProtected", doGitCreateBranch(dstPath, "protected"))
|
|
|
|
t.Run("PushProtectedBranch", doGitPushTestRepository(dstPath, "origin", "protected"))
|
|
|
|
|
|
|
|
ctx := NewAPITestContext(t, baseCtx.Username, baseCtx.Reponame)
|
|
|
|
t.Run("ProtectProtectedBranchNoWhitelist", doProtectBranch(ctx, "protected", ""))
|
|
|
|
t.Run("GenerateCommit", func(t *testing.T) {
|
|
|
|
_, err := generateCommitWithNewData(littleSize, dstPath, "user2@example.com", "User Two", "branch-data-file-")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
t.Run("FailToPushToProtectedBranch", doGitPushTestRepositoryFail(dstPath, "origin", "protected"))
|
|
|
|
t.Run("PushToUnprotectedBranch", doGitPushTestRepository(dstPath, "origin", "protected:unprotected"))
|
|
|
|
var pr api.PullRequest
|
|
|
|
var err error
|
|
|
|
t.Run("CreatePullRequest", func(t *testing.T) {
|
|
|
|
pr, err = doAPICreatePullRequest(ctx, baseCtx.Username, baseCtx.Reponame, "protected", "unprotected")(t)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
2020-02-21 11:18:13 -07:00
|
|
|
t.Run("GenerateCommit", func(t *testing.T) {
|
|
|
|
_, err := generateCommitWithNewData(littleSize, dstPath, "user2@example.com", "User Two", "branch-data-file-")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
t.Run("PushToUnprotectedBranch", doGitPushTestRepository(dstPath, "origin", "protected:unprotected-2"))
|
|
|
|
var pr2 api.PullRequest
|
|
|
|
t.Run("CreatePullRequest", func(t *testing.T) {
|
|
|
|
pr2, err = doAPICreatePullRequest(ctx, baseCtx.Username, baseCtx.Reponame, "unprotected", "unprotected-2")(t)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
t.Run("MergePR2", doAPIMergePullRequest(ctx, baseCtx.Username, baseCtx.Reponame, pr2.Index))
|
2019-05-31 03:12:15 -07:00
|
|
|
t.Run("MergePR", doAPIMergePullRequest(ctx, baseCtx.Username, baseCtx.Reponame, pr.Index))
|
|
|
|
t.Run("PullProtected", doGitPull(dstPath, "origin", "protected"))
|
|
|
|
t.Run("ProtectProtectedBranchWhitelist", doProtectBranch(ctx, "protected", baseCtx.Username))
|
|
|
|
|
|
|
|
t.Run("CheckoutMaster", doGitCheckoutBranch(dstPath, "master"))
|
|
|
|
t.Run("CreateBranchForced", doGitCreateBranch(dstPath, "toforce"))
|
|
|
|
t.Run("GenerateCommit", func(t *testing.T) {
|
|
|
|
_, err := generateCommitWithNewData(littleSize, dstPath, "user2@example.com", "User Two", "branch-data-file-")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
t.Run("FailToForcePushToProtectedBranch", doGitPushTestRepositoryFail(dstPath, "-f", "origin", "toforce:protected"))
|
|
|
|
t.Run("MergeProtectedToToforce", doGitMerge(dstPath, "protected"))
|
|
|
|
t.Run("PushToProtectedBranch", doGitPushTestRepository(dstPath, "origin", "toforce:protected"))
|
|
|
|
t.Run("CheckoutMasterAgain", doGitCheckoutBranch(dstPath, "master"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func doProtectBranch(ctx APITestContext, branch string, userToWhitelist string) func(t *testing.T) {
|
|
|
|
// We are going to just use the owner to set the protection.
|
|
|
|
return func(t *testing.T) {
|
|
|
|
csrf := GetCSRF(t, ctx.Session, fmt.Sprintf("/%s/%s/settings/branches", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)))
|
|
|
|
|
|
|
|
if userToWhitelist == "" {
|
|
|
|
// Change branch to protected
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings/branches/%s", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), url.PathEscape(branch)), map[string]string{
|
|
|
|
"_csrf": csrf,
|
|
|
|
"protected": "on",
|
|
|
|
})
|
|
|
|
ctx.Session.MakeRequest(t, req, http.StatusFound)
|
|
|
|
} else {
|
|
|
|
user, err := models.GetUserByName(userToWhitelist)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// Change branch to protected
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings/branches/%s", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), url.PathEscape(branch)), map[string]string{
|
|
|
|
"_csrf": csrf,
|
|
|
|
"protected": "on",
|
2019-12-03 18:08:56 -07:00
|
|
|
"enable_push": "whitelist",
|
2019-05-31 03:12:15 -07:00
|
|
|
"enable_whitelist": "on",
|
|
|
|
"whitelist_users": strconv.FormatInt(user.ID, 10),
|
|
|
|
})
|
|
|
|
ctx.Session.MakeRequest(t, req, http.StatusFound)
|
|
|
|
}
|
|
|
|
// Check if master branch has been locked successfully
|
|
|
|
flashCookie := ctx.Session.GetCookie("macaron_flash")
|
|
|
|
assert.NotNil(t, flashCookie)
|
|
|
|
assert.EqualValues(t, "success%3DBranch%2Bprotection%2Bfor%2Bbranch%2B%2527"+url.QueryEscape(branch)+"%2527%2Bhas%2Bbeen%2Bupdated.", flashCookie.Value)
|
|
|
|
}
|
|
|
|
}
|
2019-06-22 10:35:34 -07:00
|
|
|
|
|
|
|
func doMergeFork(ctx, baseCtx APITestContext, baseBranch, headBranch string) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
2020-04-27 04:20:09 -07:00
|
|
|
defer PrintCurrentTest(t)()
|
2019-06-22 10:35:34 -07:00
|
|
|
var pr api.PullRequest
|
|
|
|
var err error
|
2020-04-28 01:32:23 -07:00
|
|
|
|
|
|
|
// Create a test pullrequest
|
2019-06-22 10:35:34 -07:00
|
|
|
t.Run("CreatePullRequest", func(t *testing.T) {
|
|
|
|
pr, err = doAPICreatePullRequest(ctx, baseCtx.Username, baseCtx.Reponame, baseBranch, headBranch)(t)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
2020-04-28 01:32:23 -07:00
|
|
|
|
|
|
|
// Ensure the PR page works
|
|
|
|
t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr))
|
|
|
|
|
|
|
|
// Then get the diff string
|
2020-04-03 06:21:41 -07:00
|
|
|
var diffStr string
|
|
|
|
t.Run("GetDiff", func(t *testing.T) {
|
|
|
|
req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d.diff", url.PathEscape(baseCtx.Username), url.PathEscape(baseCtx.Reponame), pr.Index))
|
|
|
|
resp := ctx.Session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
diffStr = resp.Body.String()
|
|
|
|
})
|
2020-04-28 01:32:23 -07:00
|
|
|
|
|
|
|
// Now: Merge the PR & make sure that doesn't break the PR page or change its diff
|
2019-06-22 10:35:34 -07:00
|
|
|
t.Run("MergePR", doAPIMergePullRequest(baseCtx, baseCtx.Username, baseCtx.Reponame, pr.Index))
|
2020-04-28 01:32:23 -07:00
|
|
|
t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr))
|
|
|
|
t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffStr))
|
|
|
|
|
|
|
|
// Then: Delete the head branch & make sure that doesn't break the PR page or change its diff
|
2020-04-03 06:21:41 -07:00
|
|
|
t.Run("DeleteHeadBranch", doBranchDelete(baseCtx, baseCtx.Username, baseCtx.Reponame, headBranch))
|
2020-04-28 01:32:23 -07:00
|
|
|
t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr))
|
|
|
|
t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffStr))
|
|
|
|
|
|
|
|
// Delete the head repository & make sure that doesn't break the PR page or change its diff
|
|
|
|
t.Run("DeleteHeadRepository", doAPIDeleteRepository(ctx))
|
|
|
|
t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr))
|
|
|
|
t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffStr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func doEnsureCanSeePull(ctx APITestContext, pr api.PullRequest) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), pr.Index))
|
|
|
|
ctx.Session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
req = NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d/files", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), pr.Index))
|
|
|
|
ctx.Session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
req = NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d/commits", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), pr.Index))
|
|
|
|
ctx.Session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func doEnsureDiffNoChange(ctx APITestContext, pr api.PullRequest, diffStr string) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d.diff", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), pr.Index))
|
|
|
|
resp := ctx.Session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
assert.Equal(t, diffStr, resp.Body.String())
|
2019-06-22 10:35:34 -07:00
|
|
|
}
|
|
|
|
}
|
2019-12-14 19:49:52 -07:00
|
|
|
|
|
|
|
func doPushCreate(ctx APITestContext, u *url.URL) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
defer PrintCurrentTest(t)()
|
2020-04-28 01:32:23 -07:00
|
|
|
|
|
|
|
// create a context for a currently non-existent repository
|
2019-12-14 19:49:52 -07:00
|
|
|
ctx.Reponame = fmt.Sprintf("repo-tmp-push-create-%s", u.Scheme)
|
|
|
|
u.Path = ctx.GitPath()
|
|
|
|
|
2020-04-28 01:32:23 -07:00
|
|
|
// Create a temporary directory
|
2019-12-14 19:49:52 -07:00
|
|
|
tmpDir, err := ioutil.TempDir("", ctx.Reponame)
|
|
|
|
assert.NoError(t, err)
|
2020-04-28 01:32:23 -07:00
|
|
|
defer os.RemoveAll(tmpDir)
|
2019-12-14 19:49:52 -07:00
|
|
|
|
2020-04-28 01:32:23 -07:00
|
|
|
// Now create local repository to push as our test and set its origin
|
|
|
|
t.Run("InitTestRepository", doGitInitTestRepository(tmpDir))
|
|
|
|
t.Run("AddRemote", doGitAddRemote(tmpDir, "origin", u))
|
2019-12-14 19:49:52 -07:00
|
|
|
|
2020-04-28 01:32:23 -07:00
|
|
|
// Disable "Push To Create" and attempt to push
|
2019-12-14 19:49:52 -07:00
|
|
|
setting.Repository.EnablePushCreateUser = false
|
2020-04-28 01:32:23 -07:00
|
|
|
t.Run("FailToPushAndCreateTestRepository", doGitPushTestRepositoryFail(tmpDir, "origin", "master"))
|
2019-12-14 19:49:52 -07:00
|
|
|
|
2020-04-28 01:32:23 -07:00
|
|
|
// Enable "Push To Create"
|
2019-12-14 19:49:52 -07:00
|
|
|
setting.Repository.EnablePushCreateUser = true
|
2020-02-05 02:40:35 -07:00
|
|
|
|
2020-04-28 01:32:23 -07:00
|
|
|
// Assert that cloning from a non-existent repository does not create it and that it definitely wasn't create above
|
|
|
|
t.Run("FailToCloneFromNonExistentRepository", doGitCloneFail(u))
|
2020-02-05 02:40:35 -07:00
|
|
|
|
2020-04-28 01:32:23 -07:00
|
|
|
// Then "Push To Create"x
|
|
|
|
t.Run("SuccessfullyPushAndCreateTestRepository", doGitPushTestRepository(tmpDir, "origin", "master"))
|
2019-12-14 19:49:52 -07:00
|
|
|
|
2020-04-28 01:32:23 -07:00
|
|
|
// Finally, fetch repo from database and ensure the correct repository has been created
|
2019-12-14 19:49:52 -07:00
|
|
|
repo, err := models.GetRepositoryByOwnerAndName(ctx.Username, ctx.Reponame)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, repo.IsEmpty)
|
|
|
|
assert.True(t, repo.IsPrivate)
|
2020-04-28 01:32:23 -07:00
|
|
|
|
|
|
|
// Now add a remote that is invalid to "Push To Create"
|
|
|
|
invalidCtx := ctx
|
|
|
|
invalidCtx.Reponame = fmt.Sprintf("invalid/repo-tmp-push-create-%s", u.Scheme)
|
|
|
|
u.Path = invalidCtx.GitPath()
|
|
|
|
t.Run("AddInvalidRemote", doGitAddRemote(tmpDir, "invalid", u))
|
|
|
|
|
|
|
|
// Fail to "Push To Create" the invalid
|
|
|
|
t.Run("FailToPushAndCreateInvalidTestRepository", doGitPushTestRepositoryFail(tmpDir, "invalid", "master"))
|
2019-12-14 19:49:52 -07:00
|
|
|
}
|
|
|
|
}
|
2020-04-03 06:21:41 -07:00
|
|
|
|
|
|
|
func doBranchDelete(ctx APITestContext, owner, repo, branch string) func(*testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
csrf := GetCSRF(t, ctx.Session, fmt.Sprintf("/%s/%s/branches", url.PathEscape(owner), url.PathEscape(repo)))
|
|
|
|
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/branches/delete?name=%s", url.PathEscape(owner), url.PathEscape(repo), url.QueryEscape(branch)), map[string]string{
|
|
|
|
"_csrf": csrf,
|
|
|
|
})
|
|
|
|
ctx.Session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
}
|
|
|
|
}
|