code.gitea.io/gitea@v1.21.7/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 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^=\"/repo/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^=\"/repo/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  	})
    48  	session.MakeRequest(t, req, http.StatusSeeOther)
    49  
    50  	// Step4: check the existence of the forked repo
    51  	req = NewRequestf(t, "GET", "/%s/%s", forkOwnerName, forkRepoName)
    52  	resp = session.MakeRequest(t, req, http.StatusOK)
    53  
    54  	return resp
    55  }
    56  
    57  func TestRepoFork(t *testing.T) {
    58  	defer tests.PrepareTestEnv(t)()
    59  	session := loginUser(t, "user1")
    60  	testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
    61  }
    62  
    63  func TestRepoForkToOrg(t *testing.T) {
    64  	defer tests.PrepareTestEnv(t)()
    65  	session := loginUser(t, "user2")
    66  	testRepoFork(t, session, "user2", "repo1", "org3", "repo1")
    67  
    68  	// Check that no more forking is allowed as user2 owns repository
    69  	//  and org3 organization that owner user2 is also now has forked this repository
    70  	req := NewRequest(t, "GET", "/user2/repo1")
    71  	resp := session.MakeRequest(t, req, http.StatusOK)
    72  	htmlDoc := NewHTMLParser(t, resp.Body)
    73  	_, exists := htmlDoc.doc.Find("a.ui.button[href^=\"/repo/fork/\"]").Attr("href")
    74  	assert.False(t, exists, "Forking should not be allowed anymore")
    75  }