code.gitea.io/gitea@v1.21.7/tests/integration/repo_generate_test.go (about)

     1  // Copyright 2019 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  	"strings"
    11  	"testing"
    12  
    13  	"code.gitea.io/gitea/models/unittest"
    14  	user_model "code.gitea.io/gitea/models/user"
    15  	"code.gitea.io/gitea/modules/setting"
    16  	"code.gitea.io/gitea/tests"
    17  
    18  	"github.com/stretchr/testify/assert"
    19  )
    20  
    21  func testRepoGenerate(t *testing.T, session *TestSession, templateID, templateOwnerName, templateRepoName, generateOwnerName, generateRepoName string) *httptest.ResponseRecorder {
    22  	generateOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: generateOwnerName})
    23  
    24  	// Step0: check the existence of the generated repo
    25  	req := NewRequestf(t, "GET", "/%s/%s", generateOwnerName, generateRepoName)
    26  	session.MakeRequest(t, req, http.StatusNotFound)
    27  
    28  	// Step1: go to the main page of template repo
    29  	req = NewRequestf(t, "GET", "/%s/%s", templateOwnerName, templateRepoName)
    30  	resp := session.MakeRequest(t, req, http.StatusOK)
    31  
    32  	// Step2: click the "Use this template" button
    33  	htmlDoc := NewHTMLParser(t, resp.Body)
    34  	link, exists := htmlDoc.doc.Find("a.ui.button[href^=\"/repo/create\"]").Attr("href")
    35  	assert.True(t, exists, "The template has changed")
    36  	req = NewRequest(t, "GET", link)
    37  	resp = session.MakeRequest(t, req, http.StatusOK)
    38  
    39  	// Step3: fill the form of the create
    40  	htmlDoc = NewHTMLParser(t, resp.Body)
    41  	link, exists = htmlDoc.doc.Find("form.ui.form[action^=\"/repo/create\"]").Attr("action")
    42  	assert.True(t, exists, "The template has changed")
    43  	_, exists = htmlDoc.doc.Find(fmt.Sprintf(".owner.dropdown .item[data-value=\"%d\"]", generateOwner.ID)).Attr("data-value")
    44  	assert.True(t, exists, fmt.Sprintf("Generate owner '%s' is not present in select box", generateOwnerName))
    45  	req = NewRequestWithValues(t, "POST", link, map[string]string{
    46  		"_csrf":         htmlDoc.GetCSRF(),
    47  		"uid":           fmt.Sprintf("%d", generateOwner.ID),
    48  		"repo_name":     generateRepoName,
    49  		"repo_template": templateID,
    50  		"git_content":   "true",
    51  	})
    52  	session.MakeRequest(t, req, http.StatusSeeOther)
    53  
    54  	// Step4: check the existence of the generated repo
    55  	req = NewRequestf(t, "GET", "/%s/%s", generateOwnerName, generateRepoName)
    56  	session.MakeRequest(t, req, http.StatusOK)
    57  
    58  	// Step5: check substituted values in Readme
    59  	req = NewRequestf(t, "GET", "/%s/%s/raw/branch/master/README.md", generateOwnerName, generateRepoName)
    60  	resp = session.MakeRequest(t, req, http.StatusOK)
    61  	body := fmt.Sprintf(`# %s Readme
    62  Owner: %s
    63  Link: /%s/%s
    64  Clone URL: %s%s/%s.git`,
    65  		generateRepoName,
    66  		strings.ToUpper(generateOwnerName),
    67  		generateOwnerName,
    68  		generateRepoName,
    69  		setting.AppURL,
    70  		generateOwnerName,
    71  		generateRepoName)
    72  	assert.Equal(t, body, resp.Body.String())
    73  
    74  	// Step6: check substituted values in substituted file path ${REPO_NAME}
    75  	req = NewRequestf(t, "GET", "/%s/%s/raw/branch/master/%s.log", generateOwnerName, generateRepoName, generateRepoName)
    76  	resp = session.MakeRequest(t, req, http.StatusOK)
    77  	assert.Equal(t, generateRepoName, resp.Body.String())
    78  
    79  	return resp
    80  }
    81  
    82  func TestRepoGenerate(t *testing.T) {
    83  	defer tests.PrepareTestEnv(t)()
    84  	session := loginUser(t, "user1")
    85  	testRepoGenerate(t, session, "44", "user27", "template1", "user1", "generated1")
    86  }
    87  
    88  func TestRepoGenerateToOrg(t *testing.T) {
    89  	defer tests.PrepareTestEnv(t)()
    90  	session := loginUser(t, "user2")
    91  	testRepoGenerate(t, session, "44", "user27", "template1", "user2", "generated2")
    92  }