You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

546 lines
20 KiB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "math/rand"
  9. "net/http"
  10. "net/url"
  11. "os"
  12. "path"
  13. "path/filepath"
  14. "strconv"
  15. "testing"
  16. "time"
  17. "code.gitea.io/gitea/models"
  18. "code.gitea.io/gitea/modules/git"
  19. "code.gitea.io/gitea/modules/setting"
  20. api "code.gitea.io/gitea/modules/structs"
  21. "github.com/stretchr/testify/assert"
  22. )
  23. const (
  24. littleSize = 1024 //1ko
  25. bigSize = 128 * 1024 * 1024 //128Mo
  26. )
  27. func TestGit(t *testing.T) {
  28. onGiteaRun(t, testGit)
  29. }
  30. func testGit(t *testing.T, u *url.URL) {
  31. username := "user2"
  32. baseAPITestContext := NewAPITestContext(t, username, "repo1")
  33. u.Path = baseAPITestContext.GitPath()
  34. forkedUserCtx := NewAPITestContext(t, "user4", "repo1")
  35. t.Run("HTTP", func(t *testing.T) {
  36. defer PrintCurrentTest(t)()
  37. ensureAnonymousClone(t, u)
  38. httpContext := baseAPITestContext
  39. httpContext.Reponame = "repo-tmp-17"
  40. forkedUserCtx.Reponame = httpContext.Reponame
  41. dstPath, err := ioutil.TempDir("", httpContext.Reponame)
  42. assert.NoError(t, err)
  43. defer os.RemoveAll(dstPath)
  44. t.Run("CreateRepoInDifferentUser", doAPICreateRepository(forkedUserCtx, false))
  45. t.Run("AddUserAsCollaborator", doAPIAddCollaborator(forkedUserCtx, httpContext.Username, models.AccessModeRead))
  46. t.Run("ForkFromDifferentUser", doAPIForkRepository(httpContext, forkedUserCtx.Username))
  47. u.Path = httpContext.GitPath()
  48. u.User = url.UserPassword(username, userPassword)
  49. t.Run("Clone", doGitClone(dstPath, u))
  50. little, big := standardCommitAndPushTest(t, dstPath)
  51. littleLFS, bigLFS := lfsCommitAndPushTest(t, dstPath)
  52. rawTest(t, &httpContext, little, big, littleLFS, bigLFS)
  53. mediaTest(t, &httpContext, little, big, littleLFS, bigLFS)
  54. t.Run("BranchProtectMerge", doBranchProtectPRMerge(&httpContext, dstPath))
  55. t.Run("MergeFork", func(t *testing.T) {
  56. defer PrintCurrentTest(t)()
  57. t.Run("CreatePRAndMerge", doMergeFork(httpContext, forkedUserCtx, "master", httpContext.Username+":master"))
  58. rawTest(t, &forkedUserCtx, little, big, littleLFS, bigLFS)
  59. mediaTest(t, &forkedUserCtx, little, big, littleLFS, bigLFS)
  60. })
  61. t.Run("PushCreate", doPushCreate(httpContext, u))
  62. })
  63. t.Run("SSH", func(t *testing.T) {
  64. defer PrintCurrentTest(t)()
  65. sshContext := baseAPITestContext
  66. sshContext.Reponame = "repo-tmp-18"
  67. keyname := "my-testing-key"
  68. forkedUserCtx.Reponame = sshContext.Reponame
  69. t.Run("CreateRepoInDifferentUser", doAPICreateRepository(forkedUserCtx, false))
  70. t.Run("AddUserAsCollaborator", doAPIAddCollaborator(forkedUserCtx, sshContext.Username, models.AccessModeRead))
  71. t.Run("ForkFromDifferentUser", doAPIForkRepository(sshContext, forkedUserCtx.Username))
  72. //Setup key the user ssh key
  73. withKeyFile(t, keyname, func(keyFile string) {
  74. t.Run("CreateUserKey", doAPICreateUserKey(sshContext, "test-key", keyFile))
  75. //Setup remote link
  76. //TODO: get url from api
  77. sshURL := createSSHUrl(sshContext.GitPath(), u)
  78. //Setup clone folder
  79. dstPath, err := ioutil.TempDir("", sshContext.Reponame)
  80. assert.NoError(t, err)
  81. defer os.RemoveAll(dstPath)
  82. t.Run("Clone", doGitClone(dstPath, sshURL))
  83. little, big := standardCommitAndPushTest(t, dstPath)
  84. littleLFS, bigLFS := lfsCommitAndPushTest(t, dstPath)
  85. rawTest(t, &sshContext, little, big, littleLFS, bigLFS)
  86. mediaTest(t, &sshContext, little, big, littleLFS, bigLFS)
  87. t.Run("BranchProtectMerge", doBranchProtectPRMerge(&sshContext, dstPath))
  88. t.Run("MergeFork", func(t *testing.T) {
  89. defer PrintCurrentTest(t)()
  90. t.Run("CreatePRAndMerge", doMergeFork(sshContext, forkedUserCtx, "master", sshContext.Username+":master"))
  91. rawTest(t, &forkedUserCtx, little, big, littleLFS, bigLFS)
  92. mediaTest(t, &forkedUserCtx, little, big, littleLFS, bigLFS)
  93. })
  94. t.Run("PushCreate", doPushCreate(sshContext, sshURL))
  95. })
  96. })
  97. }
  98. func ensureAnonymousClone(t *testing.T, u *url.URL) {
  99. dstLocalPath, err := ioutil.TempDir("", "repo1")
  100. assert.NoError(t, err)
  101. defer os.RemoveAll(dstLocalPath)
  102. t.Run("CloneAnonymous", doGitClone(dstLocalPath, u))
  103. }
  104. func standardCommitAndPushTest(t *testing.T, dstPath string) (little, big string) {
  105. t.Run("Standard", func(t *testing.T) {
  106. defer PrintCurrentTest(t)()
  107. little, big = commitAndPushTest(t, dstPath, "data-file-")
  108. })
  109. return
  110. }
  111. func lfsCommitAndPushTest(t *testing.T, dstPath string) (littleLFS, bigLFS string) {
  112. t.Run("LFS", func(t *testing.T) {
  113. defer PrintCurrentTest(t)()
  114. setting.CheckLFSVersion()
  115. if !setting.LFS.StartServer {
  116. t.Skip()
  117. return
  118. }
  119. prefix := "lfs-data-file-"
  120. _, err := git.NewCommand("lfs").AddArguments("install").RunInDir(dstPath)
  121. assert.NoError(t, err)
  122. _, err = git.NewCommand("lfs").AddArguments("track", prefix+"*").RunInDir(dstPath)
  123. assert.NoError(t, err)
  124. err = git.AddChanges(dstPath, false, ".gitattributes")
  125. assert.NoError(t, err)
  126. err = git.CommitChangesWithArgs(dstPath, allowLFSFilters(), git.CommitChangesOptions{
  127. Committer: &git.Signature{
  128. Email: "user2@example.com",
  129. Name: "User Two",
  130. When: time.Now(),
  131. },
  132. Author: &git.Signature{
  133. Email: "user2@example.com",
  134. Name: "User Two",
  135. When: time.Now(),
  136. },
  137. Message: fmt.Sprintf("Testing commit @ %v", time.Now()),
  138. })
  139. assert.NoError(t, err)
  140. littleLFS, bigLFS = commitAndPushTest(t, dstPath, prefix)
  141. t.Run("Locks", func(t *testing.T) {
  142. defer PrintCurrentTest(t)()
  143. lockTest(t, dstPath)
  144. })
  145. })
  146. return
  147. }
  148. func commitAndPushTest(t *testing.T, dstPath, prefix string) (little, big string) {
  149. t.Run("PushCommit", func(t *testing.T) {
  150. defer PrintCurrentTest(t)()
  151. t.Run("Little", func(t *testing.T) {
  152. defer PrintCurrentTest(t)()
  153. little = doCommitAndPush(t, littleSize, dstPath, prefix)
  154. })
  155. t.Run("Big", func(t *testing.T) {
  156. if testing.Short() {
  157. t.Skip("Skipping test in short mode.")
  158. return
  159. }
  160. defer PrintCurrentTest(t)()
  161. big = doCommitAndPush(t, bigSize, dstPath, prefix)
  162. })
  163. })
  164. return
  165. }
  166. func rawTest(t *testing.T, ctx *APITestContext, little, big, littleLFS, bigLFS string) {
  167. t.Run("Raw", func(t *testing.T) {
  168. defer PrintCurrentTest(t)()
  169. username := ctx.Username
  170. reponame := ctx.Reponame
  171. session := loginUser(t, username)
  172. // Request raw paths
  173. req := NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", little))
  174. resp := session.MakeRequest(t, req, http.StatusOK)
  175. assert.Equal(t, littleSize, resp.Body.Len())
  176. setting.CheckLFSVersion()
  177. if setting.LFS.StartServer {
  178. req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", littleLFS))
  179. resp = session.MakeRequest(t, req, http.StatusOK)
  180. assert.NotEqual(t, littleSize, resp.Body.Len())
  181. assert.Contains(t, resp.Body.String(), models.LFSMetaFileIdentifier)
  182. }
  183. if !testing.Short() {
  184. req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", big))
  185. resp = session.MakeRequest(t, req, http.StatusOK)
  186. assert.Equal(t, bigSize, resp.Body.Len())
  187. if setting.LFS.StartServer {
  188. req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", bigLFS))
  189. resp = session.MakeRequest(t, req, http.StatusOK)
  190. assert.NotEqual(t, bigSize, resp.Body.Len())
  191. assert.Contains(t, resp.Body.String(), models.LFSMetaFileIdentifier)
  192. }
  193. }
  194. })
  195. }
  196. func mediaTest(t *testing.T, ctx *APITestContext, little, big, littleLFS, bigLFS string) {
  197. t.Run("Media", func(t *testing.T) {
  198. defer PrintCurrentTest(t)()
  199. username := ctx.Username
  200. reponame := ctx.Reponame
  201. session := loginUser(t, username)
  202. // Request media paths
  203. req := NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", little))
  204. resp := session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
  205. assert.Equal(t, littleSize, resp.Length)
  206. setting.CheckLFSVersion()
  207. if setting.LFS.StartServer {
  208. req = NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", littleLFS))
  209. resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
  210. assert.Equal(t, littleSize, resp.Length)
  211. }
  212. if !testing.Short() {
  213. req = NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", big))
  214. resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
  215. assert.Equal(t, bigSize, resp.Length)
  216. if setting.LFS.StartServer {
  217. req = NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", bigLFS))
  218. resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
  219. assert.Equal(t, bigSize, resp.Length)
  220. }
  221. }
  222. })
  223. }
  224. func lockTest(t *testing.T, repoPath string) {
  225. lockFileTest(t, "README.md", repoPath)
  226. }
  227. func lockFileTest(t *testing.T, filename, repoPath string) {
  228. _, err := git.NewCommand("lfs").AddArguments("locks").RunInDir(repoPath)
  229. assert.NoError(t, err)
  230. _, err = git.NewCommand("lfs").AddArguments("lock", filename).RunInDir(repoPath)
  231. assert.NoError(t, err)
  232. _, err = git.NewCommand("lfs").AddArguments("locks").RunInDir(repoPath)
  233. assert.NoError(t, err)
  234. _, err = git.NewCommand("lfs").AddArguments("unlock", filename).RunInDir(repoPath)
  235. assert.NoError(t, err)
  236. }
  237. func doCommitAndPush(t *testing.T, size int, repoPath, prefix string) string {
  238. name, err := generateCommitWithNewData(size, repoPath, "user2@example.com", "User Two", prefix)
  239. assert.NoError(t, err)
  240. _, err = git.NewCommand("push", "origin", "master").RunInDir(repoPath) //Push
  241. assert.NoError(t, err)
  242. return name
  243. }
  244. func generateCommitWithNewData(size int, repoPath, email, fullName, prefix string) (string, error) {
  245. //Generate random file
  246. bufSize := 4 * 1024
  247. if bufSize > size {
  248. bufSize = size
  249. }
  250. buffer := make([]byte, bufSize)
  251. tmpFile, err := ioutil.TempFile(repoPath, prefix)
  252. if err != nil {
  253. return "", err
  254. }
  255. defer tmpFile.Close()
  256. written := 0
  257. for written < size {
  258. n := size - written
  259. if n > bufSize {
  260. n = bufSize
  261. }
  262. _, err := rand.Read(buffer[:n])
  263. if err != nil {
  264. return "", err
  265. }
  266. n, err = tmpFile.Write(buffer[:n])
  267. if err != nil {
  268. return "", err
  269. }
  270. written += n
  271. }
  272. if err != nil {
  273. return "", err
  274. }
  275. //Commit
  276. // Now here we should explicitly allow lfs filters to run
  277. globalArgs := allowLFSFilters()
  278. err = git.AddChangesWithArgs(repoPath, globalArgs, false, filepath.Base(tmpFile.Name()))
  279. if err != nil {
  280. return "", err
  281. }
  282. err = git.CommitChangesWithArgs(repoPath, globalArgs, git.CommitChangesOptions{
  283. Committer: &git.Signature{
  284. Email: email,
  285. Name: fullName,
  286. When: time.Now(),
  287. },
  288. Author: &git.Signature{
  289. Email: email,
  290. Name: fullName,
  291. When: time.Now(),
  292. },
  293. Message: fmt.Sprintf("Testing commit @ %v", time.Now()),
  294. })
  295. return filepath.Base(tmpFile.Name()), err
  296. }
  297. func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *testing.T) {
  298. return func(t *testing.T) {
  299. defer PrintCurrentTest(t)()
  300. t.Run("CreateBranchProtected", doGitCreateBranch(dstPath, "protected"))
  301. t.Run("PushProtectedBranch", doGitPushTestRepository(dstPath, "origin", "protected"))
  302. ctx := NewAPITestContext(t, baseCtx.Username, baseCtx.Reponame)
  303. t.Run("ProtectProtectedBranchNoWhitelist", doProtectBranch(ctx, "protected", ""))
  304. t.Run("GenerateCommit", func(t *testing.T) {
  305. _, err := generateCommitWithNewData(littleSize, dstPath, "user2@example.com", "User Two", "branch-data-file-")
  306. assert.NoError(t, err)
  307. })
  308. t.Run("FailToPushToProtectedBranch", doGitPushTestRepositoryFail(dstPath, "origin", "protected"))
  309. t.Run("PushToUnprotectedBranch", doGitPushTestRepository(dstPath, "origin", "protected:unprotected"))
  310. var pr api.PullRequest
  311. var err error
  312. t.Run("CreatePullRequest", func(t *testing.T) {
  313. pr, err = doAPICreatePullRequest(ctx, baseCtx.Username, baseCtx.Reponame, "protected", "unprotected")(t)
  314. assert.NoError(t, err)
  315. })
  316. t.Run("GenerateCommit", func(t *testing.T) {
  317. _, err := generateCommitWithNewData(littleSize, dstPath, "user2@example.com", "User Two", "branch-data-file-")
  318. assert.NoError(t, err)
  319. })
  320. t.Run("PushToUnprotectedBranch", doGitPushTestRepository(dstPath, "origin", "protected:unprotected-2"))
  321. var pr2 api.PullRequest
  322. t.Run("CreatePullRequest", func(t *testing.T) {
  323. pr2, err = doAPICreatePullRequest(ctx, baseCtx.Username, baseCtx.Reponame, "unprotected", "unprotected-2")(t)
  324. assert.NoError(t, err)
  325. })
  326. t.Run("MergePR2", doAPIMergePullRequest(ctx, baseCtx.Username, baseCtx.Reponame, pr2.Index))
  327. t.Run("MergePR", doAPIMergePullRequest(ctx, baseCtx.Username, baseCtx.Reponame, pr.Index))
  328. t.Run("PullProtected", doGitPull(dstPath, "origin", "protected"))
  329. t.Run("ProtectProtectedBranchWhitelist", doProtectBranch(ctx, "protected", baseCtx.Username))
  330. t.Run("CheckoutMaster", doGitCheckoutBranch(dstPath, "master"))
  331. t.Run("CreateBranchForced", doGitCreateBranch(dstPath, "toforce"))
  332. t.Run("GenerateCommit", func(t *testing.T) {
  333. _, err := generateCommitWithNewData(littleSize, dstPath, "user2@example.com", "User Two", "branch-data-file-")
  334. assert.NoError(t, err)
  335. })
  336. t.Run("FailToForcePushToProtectedBranch", doGitPushTestRepositoryFail(dstPath, "-f", "origin", "toforce:protected"))
  337. t.Run("MergeProtectedToToforce", doGitMerge(dstPath, "protected"))
  338. t.Run("PushToProtectedBranch", doGitPushTestRepository(dstPath, "origin", "toforce:protected"))
  339. t.Run("CheckoutMasterAgain", doGitCheckoutBranch(dstPath, "master"))
  340. }
  341. }
  342. func doProtectBranch(ctx APITestContext, branch string, userToWhitelist string) func(t *testing.T) {
  343. // We are going to just use the owner to set the protection.
  344. return func(t *testing.T) {
  345. csrf := GetCSRF(t, ctx.Session, fmt.Sprintf("/%s/%s/settings/branches", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)))
  346. if userToWhitelist == "" {
  347. // Change branch to protected
  348. 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{
  349. "_csrf": csrf,
  350. "protected": "on",
  351. })
  352. ctx.Session.MakeRequest(t, req, http.StatusFound)
  353. } else {
  354. user, err := models.GetUserByName(userToWhitelist)
  355. assert.NoError(t, err)
  356. // Change branch to protected
  357. 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{
  358. "_csrf": csrf,
  359. "protected": "on",
  360. "enable_push": "whitelist",
  361. "enable_whitelist": "on",
  362. "whitelist_users": strconv.FormatInt(user.ID, 10),
  363. })
  364. ctx.Session.MakeRequest(t, req, http.StatusFound)
  365. }
  366. // Check if master branch has been locked successfully
  367. flashCookie := ctx.Session.GetCookie("macaron_flash")
  368. assert.NotNil(t, flashCookie)
  369. assert.EqualValues(t, "success%3DBranch%2Bprotection%2Bfor%2Bbranch%2B%2527"+url.QueryEscape(branch)+"%2527%2Bhas%2Bbeen%2Bupdated.", flashCookie.Value)
  370. }
  371. }
  372. func doMergeFork(ctx, baseCtx APITestContext, baseBranch, headBranch string) func(t *testing.T) {
  373. return func(t *testing.T) {
  374. defer PrintCurrentTest(t)()
  375. var pr api.PullRequest
  376. var err error
  377. // Create a test pullrequest
  378. t.Run("CreatePullRequest", func(t *testing.T) {
  379. pr, err = doAPICreatePullRequest(ctx, baseCtx.Username, baseCtx.Reponame, baseBranch, headBranch)(t)
  380. assert.NoError(t, err)
  381. })
  382. // Ensure the PR page works
  383. t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr))
  384. // Then get the diff string
  385. var diffStr string
  386. t.Run("GetDiff", func(t *testing.T) {
  387. req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d.diff", url.PathEscape(baseCtx.Username), url.PathEscape(baseCtx.Reponame), pr.Index))
  388. resp := ctx.Session.MakeRequest(t, req, http.StatusOK)
  389. diffStr = resp.Body.String()
  390. })
  391. // Now: Merge the PR & make sure that doesn't break the PR page or change its diff
  392. t.Run("MergePR", doAPIMergePullRequest(baseCtx, baseCtx.Username, baseCtx.Reponame, pr.Index))
  393. t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr))
  394. t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffStr))
  395. // Then: Delete the head branch & make sure that doesn't break the PR page or change its diff
  396. t.Run("DeleteHeadBranch", doBranchDelete(baseCtx, baseCtx.Username, baseCtx.Reponame, headBranch))
  397. t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr))
  398. t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffStr))
  399. // Delete the head repository & make sure that doesn't break the PR page or change its diff
  400. t.Run("DeleteHeadRepository", doAPIDeleteRepository(ctx))
  401. t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr))
  402. t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffStr))
  403. }
  404. }
  405. func doEnsureCanSeePull(ctx APITestContext, pr api.PullRequest) func(t *testing.T) {
  406. return func(t *testing.T) {
  407. req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), pr.Index))
  408. ctx.Session.MakeRequest(t, req, http.StatusOK)
  409. req = NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d/files", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), pr.Index))
  410. ctx.Session.MakeRequest(t, req, http.StatusOK)
  411. req = NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d/commits", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), pr.Index))
  412. ctx.Session.MakeRequest(t, req, http.StatusOK)
  413. }
  414. }
  415. func doEnsureDiffNoChange(ctx APITestContext, pr api.PullRequest, diffStr string) func(t *testing.T) {
  416. return func(t *testing.T) {
  417. req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d.diff", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), pr.Index))
  418. resp := ctx.Session.MakeRequest(t, req, http.StatusOK)
  419. assert.Equal(t, diffStr, resp.Body.String())
  420. }
  421. }
  422. func doPushCreate(ctx APITestContext, u *url.URL) func(t *testing.T) {
  423. return func(t *testing.T) {
  424. defer PrintCurrentTest(t)()
  425. // create a context for a currently non-existent repository
  426. ctx.Reponame = fmt.Sprintf("repo-tmp-push-create-%s", u.Scheme)
  427. u.Path = ctx.GitPath()
  428. // Create a temporary directory
  429. tmpDir, err := ioutil.TempDir("", ctx.Reponame)
  430. assert.NoError(t, err)
  431. defer os.RemoveAll(tmpDir)
  432. // Now create local repository to push as our test and set its origin
  433. t.Run("InitTestRepository", doGitInitTestRepository(tmpDir))
  434. t.Run("AddRemote", doGitAddRemote(tmpDir, "origin", u))
  435. // Disable "Push To Create" and attempt to push
  436. setting.Repository.EnablePushCreateUser = false
  437. t.Run("FailToPushAndCreateTestRepository", doGitPushTestRepositoryFail(tmpDir, "origin", "master"))
  438. // Enable "Push To Create"
  439. setting.Repository.EnablePushCreateUser = true
  440. // Assert that cloning from a non-existent repository does not create it and that it definitely wasn't create above
  441. t.Run("FailToCloneFromNonExistentRepository", doGitCloneFail(u))
  442. // Then "Push To Create"x
  443. t.Run("SuccessfullyPushAndCreateTestRepository", doGitPushTestRepository(tmpDir, "origin", "master"))
  444. // Finally, fetch repo from database and ensure the correct repository has been created
  445. repo, err := models.GetRepositoryByOwnerAndName(ctx.Username, ctx.Reponame)
  446. assert.NoError(t, err)
  447. assert.False(t, repo.IsEmpty)
  448. assert.True(t, repo.IsPrivate)
  449. // Now add a remote that is invalid to "Push To Create"
  450. invalidCtx := ctx
  451. invalidCtx.Reponame = fmt.Sprintf("invalid/repo-tmp-push-create-%s", u.Scheme)
  452. u.Path = invalidCtx.GitPath()
  453. t.Run("AddInvalidRemote", doGitAddRemote(tmpDir, "invalid", u))
  454. // Fail to "Push To Create" the invalid
  455. t.Run("FailToPushAndCreateInvalidTestRepository", doGitPushTestRepositoryFail(tmpDir, "invalid", "master"))
  456. }
  457. }
  458. func doBranchDelete(ctx APITestContext, owner, repo, branch string) func(*testing.T) {
  459. return func(t *testing.T) {
  460. csrf := GetCSRF(t, ctx.Session, fmt.Sprintf("/%s/%s/branches", url.PathEscape(owner), url.PathEscape(repo)))
  461. req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/branches/delete?name=%s", url.PathEscape(owner), url.PathEscape(repo), url.QueryEscape(branch)), map[string]string{
  462. "_csrf": csrf,
  463. })
  464. ctx.Session.MakeRequest(t, req, http.StatusOK)
  465. }
  466. }