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.

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