@@ -12,7 +12,9 @@ import (
12
12
"strings"
13
13
"testing"
14
14
15
+ auth_model "code.gitea.io/gitea/models/auth"
15
16
"code.gitea.io/gitea/modules/git"
17
+ api "code.gitea.io/gitea/modules/structs"
16
18
"code.gitea.io/gitea/modules/test"
17
19
"code.gitea.io/gitea/tests"
18
20
@@ -83,6 +85,30 @@ func testPullCreateDirectly(t *testing.T, session *TestSession, baseRepoOwner, b
83
85
return resp
84
86
}
85
87
88
+ func testPullCreateFailure (t * testing.T , session * TestSession , baseRepoOwner , baseRepoName , baseBranch , headRepoOwner , headRepoName , headBranch , title string ) * httptest.ResponseRecorder {
89
+ headCompare := headBranch
90
+ if headRepoOwner != "" {
91
+ if headRepoName != "" {
92
+ headCompare = fmt .Sprintf ("%s/%s:%s" , headRepoOwner , headRepoName , headBranch )
93
+ } else {
94
+ headCompare = fmt .Sprintf ("%s:%s" , headRepoOwner , headBranch )
95
+ }
96
+ }
97
+ req := NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/compare/%s...%s" , baseRepoOwner , baseRepoName , baseBranch , headCompare ))
98
+ resp := session .MakeRequest (t , req , http .StatusOK )
99
+
100
+ // Submit the form for creating the pull
101
+ htmlDoc := NewHTMLParser (t , resp .Body )
102
+ link , exists := htmlDoc .doc .Find ("form.ui.form" ).Attr ("action" )
103
+ assert .True (t , exists , "The template has changed" )
104
+ req = NewRequestWithValues (t , "POST" , link , map [string ]string {
105
+ "_csrf" : htmlDoc .GetCSRF (),
106
+ "title" : title ,
107
+ })
108
+ resp = session .MakeRequest (t , req , http .StatusInternalServerError )
109
+ return resp
110
+ }
111
+
86
112
func TestPullCreate (t * testing.T ) {
87
113
onGiteaRun (t , func (t * testing.T , u * url.URL ) {
88
114
session := loginUser (t , "user1" )
@@ -245,3 +271,54 @@ func TestCreateAgitPullWithReadPermission(t *testing.T) {
245
271
})
246
272
})
247
273
}
274
+
275
+ /*
276
+ Setup: user2 has repository, user1 forks it
277
+ ---
278
+
279
+ 1. User2 blocks User1
280
+ 2. User1 adds changes to fork
281
+ 3. User1 attempts to create a pull request
282
+ 4. User1 sees alert that the action is not allowed because of the block
283
+ */
284
+ func TestCreatePullWhenBlocked (t * testing.T ) {
285
+ RepoOwner := "user2"
286
+ ForkOwner := "user1"
287
+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
288
+ // Setup
289
+ // User1 forks repo1 from User2
290
+ sessionFork := loginUser (t , ForkOwner )
291
+ testRepoFork (t , sessionFork , RepoOwner , "repo1" , ForkOwner , "repo1" , "" )
292
+
293
+ // 1. User2 blocks user1
294
+ // sessionBase := loginUser(t, "user2")
295
+ token := getUserToken (t , RepoOwner , auth_model .AccessTokenScopeWriteUser )
296
+
297
+ req := NewRequest (t , "GET" , fmt .Sprintf ("/api/v1/user/blocks/%s" , ForkOwner )).
298
+ AddTokenAuth (token )
299
+ MakeRequest (t , req , http .StatusNotFound )
300
+ req = NewRequest (t , "PUT" , fmt .Sprintf ("/api/v1/user/blocks/%s" , ForkOwner )).
301
+ AddTokenAuth (token )
302
+ MakeRequest (t , req , http .StatusNoContent )
303
+ req = NewRequest (t , "GET" , "/api/v1/user/blocks" ).
304
+ AddTokenAuth (token )
305
+ resp := MakeRequest (t , req , http .StatusOK )
306
+
307
+ var users []api.User
308
+ DecodeJSON (t , resp , & users )
309
+
310
+ assert .Len (t , users , 1 )
311
+
312
+ // 2. User1 adds changes to fork
313
+ testEditFile (t , sessionFork , ForkOwner , "repo1" , "master" , "README.md" , "Hello, World (Edited)\n " )
314
+
315
+ // 3. User1 attempts to create a pull request
316
+ testPullCreateFailure (t , sessionFork , RepoOwner , "repo1" , "master" , ForkOwner , "repo1" , "master" , "This is a pull title" )
317
+
318
+ // Teardown
319
+ // Unblock user
320
+ req = NewRequest (t , "DELETE" , fmt .Sprintf ("/api/v1/user/blocks/%s" , RepoOwner )).
321
+ AddTokenAuth (token )
322
+ MakeRequest (t , req , http .StatusNoContent )
323
+ })
324
+ }
0 commit comments