@@ -7,36 +7,36 @@ import (
7
7
"fmt"
8
8
"net/http"
9
9
"net/http/httptest"
10
+ "net/url"
10
11
"testing"
11
12
13
+ "code.gitea.io/gitea/models/db"
14
+ repo_model "code.gitea.io/gitea/models/repo"
12
15
"code.gitea.io/gitea/models/unittest"
13
16
user_model "code.gitea.io/gitea/models/user"
17
+ repo_service "code.gitea.io/gitea/services/repository"
14
18
"code.gitea.io/gitea/tests"
15
19
16
20
"github.com/stretchr/testify/assert"
17
21
)
18
22
19
23
func testRepoFork (t * testing.T , session * TestSession , ownerName , repoName , forkOwnerName , forkRepoName string ) * httptest.ResponseRecorder {
24
+ t .Helper ()
25
+
20
26
forkOwner := unittest .AssertExistsAndLoadBean (t , & user_model.User {Name : forkOwnerName })
21
27
22
28
// Step0: check the existence of the to-fork repo
23
29
req := NewRequestf (t , "GET" , "/%s/%s" , forkOwnerName , forkRepoName )
24
30
session .MakeRequest (t , req , http .StatusNotFound )
25
31
26
- // Step1: go to the main page of repo
27
- req = NewRequestf (t , "GET" , "/%s/%s" , ownerName , repoName )
32
+ // Step1: visit the /fork page
33
+ forkURL := fmt .Sprintf ("/%s/%s/fork" , ownerName , repoName )
34
+ req = NewRequest (t , "GET" , forkURL )
28
35
resp := session .MakeRequest (t , req , http .StatusOK )
29
36
30
- // Step2: click the fork button
37
+ // Step2: fill the form of the forking
31
38
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" )
39
+ link , exists := htmlDoc .doc .Find (fmt .Sprintf ("form.ui.form[action=\" %s\" ]" , forkURL )).Attr ("action" )
40
40
assert .True (t , exists , "The template has changed" )
41
41
_ , exists = htmlDoc .doc .Find (fmt .Sprintf (".owner.dropdown .item[data-value=\" %d\" ]" , forkOwner .ID )).Attr ("data-value" )
42
42
assert .True (t , exists , fmt .Sprintf ("Fork owner '%s' is not present in select box" , forkOwnerName ))
@@ -47,29 +47,108 @@ func testRepoFork(t *testing.T, session *TestSession, ownerName, repoName, forkO
47
47
})
48
48
session .MakeRequest (t , req , http .StatusSeeOther )
49
49
50
- // Step4 : check the existence of the forked repo
50
+ // Step3 : check the existence of the forked repo
51
51
req = NewRequestf (t , "GET" , "/%s/%s" , forkOwnerName , forkRepoName )
52
52
resp = session .MakeRequest (t , req , http .StatusOK )
53
53
54
54
return resp
55
55
}
56
56
57
+ func testRepoForkLegacyRedirect (t * testing.T , session * TestSession , ownerName , repoName string ) {
58
+ t .Helper ()
59
+
60
+ owner := unittest .AssertExistsAndLoadBean (t , & user_model.User {Name : ownerName })
61
+ repo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {OwnerID : owner .ID , Name : repoName })
62
+
63
+ // Visit the /repo/fork/:id url
64
+ req := NewRequestf (t , "GET" , "/repo/fork/%d" , repo .ID )
65
+ resp := session .MakeRequest (t , req , http .StatusMovedPermanently )
66
+
67
+ assert .Equal (t , repo .Link ()+ "/fork" , resp .Header ().Get ("Location" ))
68
+ }
69
+
57
70
func TestRepoFork (t * testing.T ) {
58
- defer tests .PrepareTestEnv (t )()
59
- session := loginUser (t , "user1" )
60
- testRepoFork (t , session , "user2" , "repo1" , "user1" , "repo1" )
71
+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
72
+ user5 := unittest .AssertExistsAndLoadBean (t , & user_model.User {Name : "user5" })
73
+ session := loginUser (t , user5 .Name )
74
+
75
+ t .Run ("by name" , func (t * testing.T ) {
76
+ defer tests .PrintCurrentTest (t )()
77
+ defer func () {
78
+ repo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {OwnerID : user5 .ID , Name : "repo1" })
79
+ repo_service .DeleteRepository (db .DefaultContext , user5 , repo , false )
80
+ }()
81
+ testRepoFork (t , session , "user2" , "repo1" , "user5" , "repo1" )
82
+ })
83
+
84
+ t .Run ("legacy redirect" , func (t * testing.T ) {
85
+ defer tests .PrintCurrentTest (t )()
86
+ testRepoForkLegacyRedirect (t , session , "user2" , "repo1" )
87
+
88
+ t .Run ("private 404" , func (t * testing.T ) {
89
+ defer tests .PrintCurrentTest (t )()
90
+
91
+ // Make sure the repo we try to fork is private
92
+ repo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 31 , IsPrivate : true })
93
+
94
+ // user5 does not have access to user2/repo20
95
+ req := NewRequestf (t , "GET" , "/repo/fork/%d" , repo .ID ) // user2/repo20
96
+ session .MakeRequest (t , req , http .StatusNotFound )
97
+ })
98
+ t .Run ("authenticated private redirect" , func (t * testing.T ) {
99
+ defer tests .PrintCurrentTest (t )()
100
+
101
+ // Make sure the repo we try to fork is private
102
+ repo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 31 , IsPrivate : true })
103
+
104
+ // user1 has access to user2/repo20
105
+ session := loginUser (t , "user1" )
106
+ req := NewRequestf (t , "GET" , "/repo/fork/%d" , repo .ID ) // user2/repo20
107
+ session .MakeRequest (t , req , http .StatusMovedPermanently )
108
+ })
109
+ t .Run ("no code unit" , func (t * testing.T ) {
110
+ defer tests .PrintCurrentTest (t )()
111
+
112
+ // Make sure the repo we try to fork is private.
113
+ // We're also choosing user15/big_test_private_2, becase it has the Code unit disabled.
114
+ repo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 20 , IsPrivate : true })
115
+
116
+ // user1, even though an admin, can't fork a repo without a code unit.
117
+ session := loginUser (t , "user1" )
118
+ req := NewRequestf (t , "GET" , "/repo/fork/%d" , repo .ID ) // user15/big_test_private_2
119
+ session .MakeRequest (t , req , http .StatusNotFound )
120
+ })
121
+ })
122
+ })
61
123
}
62
124
63
125
func TestRepoForkToOrg (t * testing.T ) {
64
- defer tests . PrepareTestEnv ( t )()
65
- session := loginUser (t , "user2" )
66
- testRepoFork (t , session , "user2" , "repo1" , " org3", "repo1" )
126
+ onGiteaRun ( t , func ( t * testing. T , u * url. URL ) {
127
+ session := loginUser (t , "user2" )
128
+ org3 := unittest . AssertExistsAndLoadBean (t , & user_model. User { Name : " org3"} )
67
129
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" )
130
+ t .Run ("by name" , func (t * testing.T ) {
131
+ defer tests .PrintCurrentTest (t )()
132
+ defer func () {
133
+ repo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {OwnerID : org3 .ID , Name : "repo1" })
134
+ repo_service .DeleteRepository (db .DefaultContext , org3 , repo , false )
135
+ }()
136
+
137
+ testRepoFork (t , session , "user2" , "repo1" , "org3" , "repo1" )
138
+
139
+ // Check that no more forking is allowed as user2 owns repository
140
+ // and org3 organization that owner user2 is also now has forked this repository
141
+ req := NewRequest (t , "GET" , "/user2/repo1" )
142
+ resp := session .MakeRequest (t , req , http .StatusOK )
143
+ htmlDoc := NewHTMLParser (t , resp .Body )
144
+ _ , exists := htmlDoc .doc .Find ("a.ui.button[href^=\" /fork\" ]" ).Attr ("href" )
145
+ assert .False (t , exists , "Forking should not be allowed anymore" )
146
+ })
147
+
148
+ t .Run ("legacy redirect" , func (t * testing.T ) {
149
+ defer tests .PrintCurrentTest (t )()
150
+
151
+ testRepoForkLegacyRedirect (t , session , "user2" , "repo1" )
152
+ })
153
+ })
75
154
}
0 commit comments