code.gitea.io/gitea@v1.22.3/tests/integration/repo_fork_test.go (about) 1 // Copyright 2017 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package integration 5 6 import ( 7 "fmt" 8 "net/http" 9 "net/http/httptest" 10 "testing" 11 12 "code.gitea.io/gitea/models/unittest" 13 user_model "code.gitea.io/gitea/models/user" 14 "code.gitea.io/gitea/tests" 15 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func testRepoFork(t *testing.T, session *TestSession, ownerName, repoName, forkOwnerName, forkRepoName, forkBranch string) *httptest.ResponseRecorder { 20 forkOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: forkOwnerName}) 21 22 // Step0: check the existence of the to-fork repo 23 req := NewRequestf(t, "GET", "/%s/%s", forkOwnerName, forkRepoName) 24 session.MakeRequest(t, req, http.StatusNotFound) 25 26 // Step1: go to the main page of repo 27 req = NewRequestf(t, "GET", "/%s/%s", ownerName, repoName) 28 resp := session.MakeRequest(t, req, http.StatusOK) 29 30 // Step2: click the fork button 31 htmlDoc := NewHTMLParser(t, resp.Body) 32 link, exists := htmlDoc.doc.Find(`a.ui.button[href*="/fork"]`).Attr("href") 33 assert.True(t, exists, "The template has changed") 34 req = NewRequest(t, "GET", link) 35 resp = session.MakeRequest(t, req, http.StatusOK) 36 37 // Step3: fill the form of the forking 38 htmlDoc = NewHTMLParser(t, resp.Body) 39 link, exists = htmlDoc.doc.Find(`form.ui.form[action*="/fork"]`).Attr("action") 40 assert.True(t, exists, "The template has changed") 41 _, exists = htmlDoc.doc.Find(fmt.Sprintf(".owner.dropdown .item[data-value=\"%d\"]", forkOwner.ID)).Attr("data-value") 42 assert.True(t, exists, fmt.Sprintf("Fork owner '%s' is not present in select box", forkOwnerName)) 43 req = NewRequestWithValues(t, "POST", link, map[string]string{ 44 "_csrf": htmlDoc.GetCSRF(), 45 "uid": fmt.Sprintf("%d", forkOwner.ID), 46 "repo_name": forkRepoName, 47 "fork_single_branch": forkBranch, 48 }) 49 session.MakeRequest(t, req, http.StatusSeeOther) 50 51 // Step4: check the existence of the forked repo 52 req = NewRequestf(t, "GET", "/%s/%s", forkOwnerName, forkRepoName) 53 resp = session.MakeRequest(t, req, http.StatusOK) 54 55 return resp 56 } 57 58 func TestRepoFork(t *testing.T) { 59 defer tests.PrepareTestEnv(t)() 60 session := loginUser(t, "user1") 61 testRepoFork(t, session, "user2", "repo1", "user1", "repo1", "") 62 } 63 64 func TestRepoForkToOrg(t *testing.T) { 65 defer tests.PrepareTestEnv(t)() 66 session := loginUser(t, "user2") 67 testRepoFork(t, session, "user2", "repo1", "org3", "repo1", "") 68 69 // Check that no more forking is allowed as user2 owns repository 70 // and org3 organization that owner user2 is also now has forked this repository 71 req := NewRequest(t, "GET", "/user2/repo1") 72 resp := session.MakeRequest(t, req, http.StatusOK) 73 htmlDoc := NewHTMLParser(t, resp.Body) 74 _, exists := htmlDoc.doc.Find(`a.ui.button[href*="/fork"]`).Attr("href") 75 assert.False(t, exists, "Forking should not be allowed anymore") 76 }