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.

repo_fork_test.go 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. "net/http"
  8. "testing"
  9. "code.gitea.io/gitea/models"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func testRepoFork(t *testing.T, session *TestSession, ownerName, repoName, forkOwnerName, forkRepoName string) *TestResponse {
  13. forkOwner := models.AssertExistsAndLoadBean(t, &models.User{Name: forkOwnerName}).(*models.User)
  14. // Step0: check the existence of the to-fork repo
  15. req := NewRequestf(t, "GET", "/%s/%s", forkOwnerName, forkRepoName)
  16. resp := session.MakeRequest(t, req, http.StatusNotFound)
  17. // Step1: go to the main page of repo
  18. req = NewRequestf(t, "GET", "/%s/%s", ownerName, repoName)
  19. resp = session.MakeRequest(t, req, http.StatusOK)
  20. // Step2: click the fork button
  21. htmlDoc := NewHTMLParser(t, resp.Body)
  22. link, exists := htmlDoc.doc.Find("a.ui.button[href^=\"/repo/fork/\"]").Attr("href")
  23. assert.True(t, exists, "The template has changed")
  24. req = NewRequest(t, "GET", link)
  25. resp = session.MakeRequest(t, req, http.StatusOK)
  26. // Step3: fill the form of the forking
  27. htmlDoc = NewHTMLParser(t, resp.Body)
  28. link, exists = htmlDoc.doc.Find("form.ui.form[action^=\"/repo/fork/\"]").Attr("action")
  29. assert.True(t, exists, "The template has changed")
  30. _, exists = htmlDoc.doc.Find(fmt.Sprintf(".owner.dropdown .item[data-value=\"%d\"]", forkOwner.ID)).Attr("data-value")
  31. assert.True(t, exists, fmt.Sprintf("Fork owner '%s' is not present in select box", forkOwnerName))
  32. req = NewRequestWithValues(t, "POST", link, map[string]string{
  33. "_csrf": htmlDoc.GetCSRF(),
  34. "uid": fmt.Sprintf("%d", forkOwner.ID),
  35. "repo_name": forkRepoName,
  36. })
  37. resp = session.MakeRequest(t, req, http.StatusFound)
  38. // Step4: check the existence of the forked repo
  39. req = NewRequestf(t, "GET", "/%s/%s", forkOwnerName, forkRepoName)
  40. resp = session.MakeRequest(t, req, http.StatusOK)
  41. return resp
  42. }
  43. func TestRepoFork(t *testing.T) {
  44. prepareTestEnv(t)
  45. session := loginUser(t, "user1")
  46. testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
  47. }
  48. func TestRepoForkToOrg(t *testing.T) {
  49. prepareTestEnv(t)
  50. session := loginUser(t, "user2")
  51. testRepoFork(t, session, "user2", "repo1", "user3", "repo1")
  52. // Check that no more forking is allowed as user2 owns repository
  53. // and user3 organization that owner user2 is also now has forked this repository
  54. req := NewRequest(t, "GET", "/user2/repo1")
  55. resp := session.MakeRequest(t, req, http.StatusOK)
  56. htmlDoc := NewHTMLParser(t, resp.Body)
  57. _, exists := htmlDoc.doc.Find("a.ui.button[href^=\"/repo/fork/\"]").Attr("href")
  58. assert.False(t, exists, "Forking should not be allowed anymore")
  59. }