@@ -5,17 +5,23 @@ package integration
5
5
6
6
import (
7
7
"net/http"
8
+ "net/url"
8
9
"testing"
9
10
10
11
git_model "code.gitea.io/gitea/models/git"
11
12
repo_model "code.gitea.io/gitea/models/repo"
12
13
"code.gitea.io/gitea/models/unittest"
14
+ gitea_context "code.gitea.io/gitea/modules/context"
13
15
"code.gitea.io/gitea/tests"
14
16
15
17
"github.com/stretchr/testify/assert"
16
18
)
17
19
18
20
func TestRenameBranch (t * testing.T ) {
21
+ onGiteaRun (t , testRenameBranch )
22
+ }
23
+
24
+ func testRenameBranch (t * testing.T , u * url.URL ) {
19
25
defer tests .PrepareTestEnv (t )()
20
26
21
27
unittest .AssertExistsAndLoadBean (t , & git_model.Branch {RepoID : 1 , Name : "master" })
@@ -26,25 +32,89 @@ func TestRenameBranch(t *testing.T) {
26
32
resp := session .MakeRequest (t , req , http .StatusOK )
27
33
htmlDoc := NewHTMLParser (t , resp .Body )
28
34
29
- postData := map [string ]string {
35
+ req = NewRequestWithValues ( t , "POST" , "/user2/repo1/settings/rename_branch" , map [string ]string {
30
36
"_csrf" : htmlDoc .GetCSRF (),
31
37
"from" : "master" ,
32
38
"to" : "main" ,
33
- }
34
- req = NewRequestWithValues (t , "POST" , "/user2/repo1/settings/rename_branch" , postData )
39
+ })
35
40
session .MakeRequest (t , req , http .StatusSeeOther )
36
41
37
42
// check new branch link
38
- req = NewRequestWithValues (t , "GET" , "/user2/repo1/src/branch/main/README.md" , postData )
43
+ req = NewRequestWithValues (t , "GET" , "/user2/repo1/src/branch/main/README.md" , nil )
39
44
session .MakeRequest (t , req , http .StatusOK )
40
45
41
46
// check old branch link
42
- req = NewRequestWithValues (t , "GET" , "/user2/repo1/src/branch/master/README.md" , postData )
47
+ req = NewRequestWithValues (t , "GET" , "/user2/repo1/src/branch/master/README.md" , nil )
43
48
resp = session .MakeRequest (t , req , http .StatusSeeOther )
44
49
location := resp .Header ().Get ("Location" )
45
50
assert .Equal (t , "/user2/repo1/src/branch/main/README.md" , location )
46
51
47
52
// check db
48
53
repo1 := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 1 })
49
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/settings/rename_branch" , 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/settings/rename_branch" , 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 )
50
120
}
0 commit comments