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

     1  // Copyright 2021 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"net/http"
     8  	"testing"
     9  
    10  	git_model "code.gitea.io/gitea/models/git"
    11  	repo_model "code.gitea.io/gitea/models/repo"
    12  	"code.gitea.io/gitea/models/unittest"
    13  	"code.gitea.io/gitea/tests"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestRenameBranch(t *testing.T) {
    19  	defer tests.PrepareTestEnv(t)()
    20  
    21  	unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: 1, Name: "master"})
    22  
    23  	// get branch setting page
    24  	session := loginUser(t, "user2")
    25  	req := NewRequest(t, "GET", "/user2/repo1/settings/branches")
    26  	resp := session.MakeRequest(t, req, http.StatusOK)
    27  	htmlDoc := NewHTMLParser(t, resp.Body)
    28  
    29  	postData := map[string]string{
    30  		"_csrf": htmlDoc.GetCSRF(),
    31  		"from":  "master",
    32  		"to":    "main",
    33  	}
    34  	req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/rename_branch", postData)
    35  	session.MakeRequest(t, req, http.StatusSeeOther)
    36  
    37  	// check new branch link
    38  	req = NewRequestWithValues(t, "GET", "/user2/repo1/src/branch/main/README.md", postData)
    39  	session.MakeRequest(t, req, http.StatusOK)
    40  
    41  	// check old branch link
    42  	req = NewRequestWithValues(t, "GET", "/user2/repo1/src/branch/master/README.md", postData)
    43  	resp = session.MakeRequest(t, req, http.StatusSeeOther)
    44  	location := resp.Header().Get("Location")
    45  	assert.Equal(t, "/user2/repo1/src/branch/main/README.md", location)
    46  
    47  	// check db
    48  	repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
    49  	assert.Equal(t, "main", repo1.DefaultBranch)
    50  }