code.gitea.io/gitea@v1.22.3/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 "net/url" 9 "testing" 10 11 git_model "code.gitea.io/gitea/models/git" 12 repo_model "code.gitea.io/gitea/models/repo" 13 "code.gitea.io/gitea/models/unittest" 14 gitea_context "code.gitea.io/gitea/services/context" 15 "code.gitea.io/gitea/tests" 16 17 "github.com/stretchr/testify/assert" 18 ) 19 20 func TestRenameBranch(t *testing.T) { 21 onGiteaRun(t, testRenameBranch) 22 } 23 24 func testRenameBranch(t *testing.T, u *url.URL) { 25 defer tests.PrepareTestEnv(t)() 26 27 unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: 1, Name: "master"}) 28 29 // get branch setting page 30 session := loginUser(t, "user2") 31 req := NewRequest(t, "GET", "/user2/repo1/branches") 32 resp := session.MakeRequest(t, req, http.StatusOK) 33 htmlDoc := NewHTMLParser(t, resp.Body) 34 35 req = NewRequestWithValues(t, "POST", "/user2/repo1/branches/rename", map[string]string{ 36 "_csrf": htmlDoc.GetCSRF(), 37 "from": "master", 38 "to": "main", 39 }) 40 session.MakeRequest(t, req, http.StatusSeeOther) 41 42 // check new branch link 43 req = NewRequestWithValues(t, "GET", "/user2/repo1/src/branch/main/README.md", nil) 44 session.MakeRequest(t, req, http.StatusOK) 45 46 // check old branch link 47 req = NewRequestWithValues(t, "GET", "/user2/repo1/src/branch/master/README.md", nil) 48 resp = session.MakeRequest(t, req, http.StatusSeeOther) 49 location := resp.Header().Get("Location") 50 assert.Equal(t, "/user2/repo1/src/branch/main/README.md", location) 51 52 // check db 53 repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 54 assert.Equal(t, "main", repo1.DefaultBranch) 55 56 // create branch1 57 csrf := GetCSRF(t, session, "/user2/repo1/src/branch/main") 58 59 req = NewRequestWithValues(t, "POST", "/user2/repo1/branches/_new/branch/main", map[string]string{ 60 "_csrf": csrf, 61 "new_branch_name": "branch1", 62 }) 63 session.MakeRequest(t, req, http.StatusSeeOther) 64 65 branch1 := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "branch1"}) 66 assert.Equal(t, "branch1", branch1.Name) 67 68 // create branch2 69 req = NewRequestWithValues(t, "POST", "/user2/repo1/branches/_new/branch/main", map[string]string{ 70 "_csrf": csrf, 71 "new_branch_name": "branch2", 72 }) 73 session.MakeRequest(t, req, http.StatusSeeOther) 74 75 branch2 := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "branch2"}) 76 assert.Equal(t, "branch2", branch2.Name) 77 78 // rename branch2 to branch1 79 req = NewRequestWithValues(t, "POST", "/user2/repo1/branches/rename", map[string]string{ 80 "_csrf": htmlDoc.GetCSRF(), 81 "from": "branch2", 82 "to": "branch1", 83 }) 84 session.MakeRequest(t, req, http.StatusSeeOther) 85 flashCookie := session.GetCookie(gitea_context.CookieNameFlash) 86 assert.NotNil(t, flashCookie) 87 assert.Contains(t, flashCookie.Value, "error") 88 89 branch2 = unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "branch2"}) 90 assert.Equal(t, "branch2", branch2.Name) 91 branch1 = unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "branch1"}) 92 assert.Equal(t, "branch1", branch1.Name) 93 94 // delete branch1 95 req = NewRequestWithValues(t, "POST", "/user2/repo1/branches/delete", map[string]string{ 96 "_csrf": htmlDoc.GetCSRF(), 97 "name": "branch1", 98 }) 99 session.MakeRequest(t, req, http.StatusOK) 100 branch2 = unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "branch2"}) 101 assert.Equal(t, "branch2", branch2.Name) 102 branch1 = unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "branch1"}) 103 assert.True(t, branch1.IsDeleted) // virtual deletion 104 105 // rename branch2 to branch1 again 106 req = NewRequestWithValues(t, "POST", "/user2/repo1/branches/rename", map[string]string{ 107 "_csrf": htmlDoc.GetCSRF(), 108 "from": "branch2", 109 "to": "branch1", 110 }) 111 session.MakeRequest(t, req, http.StatusSeeOther) 112 113 flashCookie = session.GetCookie(gitea_context.CookieNameFlash) 114 assert.NotNil(t, flashCookie) 115 assert.Contains(t, flashCookie.Value, "success") 116 117 unittest.AssertNotExistsBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "branch2"}) 118 branch1 = unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "branch1"}) 119 assert.Equal(t, "branch1", branch1.Name) 120 }