@@ -16,6 +16,7 @@ import (
16
16
"code.gitea.io/gitea/modules/process"
17
17
"code.gitea.io/gitea/modules/setting"
18
18
api "code.gitea.io/gitea/modules/structs"
19
+ "code.gitea.io/gitea/modules/test"
19
20
"code.gitea.io/gitea/tests"
20
21
21
22
"github.com/stretchr/testify/assert"
@@ -24,12 +25,7 @@ import (
24
25
)
25
26
26
27
func TestGPGGit (t * testing.T ) {
27
- defer tests .PrepareTestEnv (t )()
28
- username := "user2"
29
-
30
- // OK Set a new GPG home
31
- tmpDir := t .TempDir ()
32
-
28
+ tmpDir := t .TempDir () // use a temp dir to avoid messing with the user's GPG keyring
33
29
err := os .Chmod (tmpDir , 0o700 )
34
30
assert .NoError (t , err )
35
31
@@ -40,31 +36,20 @@ func TestGPGGit(t *testing.T) {
40
36
41
37
// Need to create a root key
42
38
rootKeyPair ,
err := importTestingKey (
tmpDir ,
"gitea" ,
"[email protected] " )
43
- assert .NoError (t , err )
44
- if err != nil {
45
- assert .FailNow (t , "Unable to import rootKeyPair" )
39
+ if ! assert .NoError (t , err , "importTestingKey" ) {
40
+ return
46
41
}
47
42
48
- rootKeyID := rootKeyPair .PrimaryKey .KeyIdShortString ()
43
+ defer test .MockVariableValue (& setting .Repository .Signing .SigningKey , rootKeyPair .PrimaryKey .KeyIdShortString ())()
44
+ defer test .MockVariableValue (& setting .Repository .Signing .SigningName , "gitea" )()
45
+ defer test .
MockVariableValue (
& setting .
Repository .
Signing .
SigningEmail ,
"[email protected] " )()
46
+ defer test .MockVariableValue (& setting .Repository .Signing .InitialCommit , []string {"never" })()
47
+ defer test .MockVariableValue (& setting .Repository .Signing .CRUDActions , []string {"never" })()
49
48
50
- oldKeyID := setting .Repository .Signing .SigningKey
51
- oldName := setting .Repository .Signing .SigningName
52
- oldEmail := setting .Repository .Signing .SigningEmail
53
- defer func () {
54
- setting .Repository .Signing .SigningKey = oldKeyID
55
- setting .Repository .Signing .SigningName = oldName
56
- setting .Repository .Signing .SigningEmail = oldEmail
57
- }()
58
-
59
- setting .Repository .Signing .SigningKey = rootKeyID
60
- setting .Repository .Signing .SigningName = "gitea"
61
- setting .
Repository .
Signing .
SigningEmail = "[email protected] "
49
+ username := "user2"
62
50
user := unittest .AssertExistsAndLoadBean (t , & user_model.User {Name : username })
63
-
64
- setting .Repository .Signing .InitialCommit = []string {"never" }
65
- setting .Repository .Signing .CRUDActions = []string {"never" }
66
-
67
51
baseAPITestContext := NewAPITestContext (t , username , "repo1" )
52
+
68
53
onGiteaRun (t , func (t * testing.T , u * url.URL ) {
69
54
u .Path = baseAPITestContext .GitPath ()
70
55
@@ -87,11 +72,8 @@ func TestGPGGit(t *testing.T) {
87
72
assert .False (t , response .Verification .Verified )
88
73
}))
89
74
})
90
- }, false )
91
- setting .Repository .Signing .CRUDActions = []string {"parentsigned" }
92
- onGiteaRun (t , func (t * testing.T , u * url.URL ) {
93
- u .Path = baseAPITestContext .GitPath ()
94
75
76
+ setting .Repository .Signing .CRUDActions = []string {"parentsigned" }
95
77
t .Run ("Unsigned-Initial-CRUD-ParentSigned" , func (t * testing.T ) {
96
78
defer tests .PrintCurrentTest (t )()
97
79
testCtx := NewAPITestContext (t , username , "initial-unsigned" , auth_model .AccessTokenScopeWriteRepository , auth_model .AccessTokenScopeWriteUser )
@@ -104,11 +86,8 @@ func TestGPGGit(t *testing.T) {
104
86
assert .False (t , response .Verification .Verified )
105
87
}))
106
88
})
107
- }, false )
108
- setting .Repository .Signing .CRUDActions = []string {"never" }
109
- onGiteaRun (t , func (t * testing.T , u * url.URL ) {
110
- u .Path = baseAPITestContext .GitPath ()
111
89
90
+ setting .Repository .Signing .CRUDActions = []string {"never" }
112
91
t .Run ("Unsigned-Initial-CRUD-Never" , func (t * testing.T ) {
113
92
defer tests .PrintCurrentTest (t )()
114
93
testCtx := NewAPITestContext (t , username , "initial-unsigned" , auth_model .AccessTokenScopeWriteRepository , auth_model .AccessTokenScopeWriteUser )
@@ -117,11 +96,8 @@ func TestGPGGit(t *testing.T) {
117
96
assert .False (t , response .Verification .Verified )
118
97
}))
119
98
})
120
- }, false )
121
- setting .Repository .Signing .CRUDActions = []string {"always" }
122
- onGiteaRun (t , func (t * testing.T , u * url.URL ) {
123
- u .Path = baseAPITestContext .GitPath ()
124
99
100
+ setting .Repository .Signing .CRUDActions = []string {"always" }
125
101
t .Run ("Unsigned-Initial-CRUD-Always" , func (t * testing.T ) {
126
102
defer tests .PrintCurrentTest (t )()
127
103
testCtx := NewAPITestContext (t , username , "initial-unsigned" , auth_model .AccessTokenScopeWriteRepository , auth_model .AccessTokenScopeWriteUser )
@@ -154,11 +130,8 @@ func TestGPGGit(t *testing.T) {
154
130
assert .
Equal (
t ,
"[email protected] " ,
response .
Verification .
Signer .
Email )
155
131
}))
156
132
})
157
- }, false )
158
- setting .Repository .Signing .CRUDActions = []string {"parentsigned" }
159
- onGiteaRun (t , func (t * testing.T , u * url.URL ) {
160
- u .Path = baseAPITestContext .GitPath ()
161
133
134
+ setting .Repository .Signing .CRUDActions = []string {"parentsigned" }
162
135
t .Run ("Unsigned-Initial-CRUD-ParentSigned" , func (t * testing.T ) {
163
136
defer tests .PrintCurrentTest (t )()
164
137
testCtx := NewAPITestContext (t , username , "initial-unsigned" , auth_model .AccessTokenScopeWriteRepository , auth_model .AccessTokenScopeWriteUser )
@@ -177,11 +150,8 @@ func TestGPGGit(t *testing.T) {
177
150
assert .
Equal (
t ,
"[email protected] " ,
response .
Verification .
Signer .
Email )
178
151
}))
179
152
})
180
- }, false )
181
- setting .Repository .Signing .InitialCommit = []string {"always" }
182
- onGiteaRun (t , func (t * testing.T , u * url.URL ) {
183
- u .Path = baseAPITestContext .GitPath ()
184
153
154
+ setting .Repository .Signing .InitialCommit = []string {"always" }
185
155
t .Run ("AlwaysSign-Initial" , func (t * testing.T ) {
186
156
defer tests .PrintCurrentTest (t )()
187
157
testCtx := NewAPITestContext (t , username , "initial-always" , auth_model .AccessTokenScopeWriteRepository , auth_model .AccessTokenScopeWriteUser )
@@ -205,11 +175,8 @@ func TestGPGGit(t *testing.T) {
205
175
assert .
Equal (
t ,
"[email protected] " ,
branch .
Commit .
Verification .
Signer .
Email )
206
176
}))
207
177
})
208
- }, false )
209
- setting .Repository .Signing .CRUDActions = []string {"never" }
210
- onGiteaRun (t , func (t * testing.T , u * url.URL ) {
211
- u .Path = baseAPITestContext .GitPath ()
212
178
179
+ setting .Repository .Signing .CRUDActions = []string {"never" }
213
180
t .Run ("AlwaysSign-Initial-CRUD-Never" , func (t * testing.T ) {
214
181
defer tests .PrintCurrentTest (t )()
215
182
testCtx := NewAPITestContext (t , username , "initial-always-never" , auth_model .AccessTokenScopeWriteRepository , auth_model .AccessTokenScopeWriteUser )
@@ -219,10 +186,8 @@ func TestGPGGit(t *testing.T) {
219
186
assert .False (t , response .Verification .Verified )
220
187
}))
221
188
})
222
- }, false )
223
- setting .Repository .Signing .CRUDActions = []string {"parentsigned" }
224
- onGiteaRun (t , func (t * testing.T , u * url.URL ) {
225
- u .Path = baseAPITestContext .GitPath ()
189
+
190
+ setting .Repository .Signing .CRUDActions = []string {"parentsigned" }
226
191
t .Run ("AlwaysSign-Initial-CRUD-ParentSigned-On-Always" , func (t * testing.T ) {
227
192
defer tests .PrintCurrentTest (t )()
228
193
testCtx := NewAPITestContext (t , username , "initial-always-parent" , auth_model .AccessTokenScopeWriteRepository , auth_model .AccessTokenScopeWriteUser )
@@ -237,11 +202,8 @@ func TestGPGGit(t *testing.T) {
237
202
assert .
Equal (
t ,
"[email protected] " ,
response .
Verification .
Signer .
Email )
238
203
}))
239
204
})
240
- }, false )
241
- setting .Repository .Signing .CRUDActions = []string {"always" }
242
- onGiteaRun (t , func (t * testing.T , u * url.URL ) {
243
- u .Path = baseAPITestContext .GitPath ()
244
205
206
+ setting .Repository .Signing .CRUDActions = []string {"always" }
245
207
t .Run ("AlwaysSign-Initial-CRUD-Always" , func (t * testing.T ) {
246
208
defer tests .PrintCurrentTest (t )()
247
209
testCtx := NewAPITestContext (t , username , "initial-always-always" , auth_model .AccessTokenScopeWriteRepository , auth_model .AccessTokenScopeWriteUser )
@@ -256,70 +218,57 @@ func TestGPGGit(t *testing.T) {
256
218
assert .
Equal (
t ,
"[email protected] " ,
response .
Verification .
Signer .
Email )
257
219
}))
258
220
})
259
- }, false )
260
- var pr api.PullRequest
261
- setting .Repository .Signing .Merges = []string {"commitssigned" }
262
- onGiteaRun (t , func (t * testing.T , u * url.URL ) {
263
- u .Path = baseAPITestContext .GitPath ()
264
221
222
+ setting .Repository .Signing .Merges = []string {"commitssigned" }
265
223
t .Run ("UnsignedMerging" , func (t * testing.T ) {
266
224
defer tests .PrintCurrentTest (t )()
267
225
testCtx := NewAPITestContext (t , username , "initial-unsigned" , auth_model .AccessTokenScopeWriteRepository , auth_model .AccessTokenScopeWriteUser )
268
- var err error
269
226
t .Run ("CreatePullRequest" , func (t * testing.T ) {
270
- pr , err = doAPICreatePullRequest (testCtx , testCtx .Username , testCtx .Reponame , "master" , "never2" )(t )
227
+ pr , err : = doAPICreatePullRequest (testCtx , testCtx .Username , testCtx .Reponame , "master" , "never2" )(t )
271
228
assert .NoError (t , err )
229
+ t .Run ("MergePR" , doAPIMergePullRequest (testCtx , testCtx .Username , testCtx .Reponame , pr .Index ))
272
230
})
273
- t .Run ("MergePR" , doAPIMergePullRequest (testCtx , testCtx .Username , testCtx .Reponame , pr .Index ))
274
231
t .Run ("CheckMasterBranchUnsigned" , doAPIGetBranch (testCtx , "master" , func (t * testing.T , branch api.Branch ) {
275
232
assert .NotNil (t , branch .Commit )
276
233
assert .NotNil (t , branch .Commit .Verification )
277
234
assert .False (t , branch .Commit .Verification .Verified )
278
235
assert .Empty (t , branch .Commit .Verification .Signature )
279
236
}))
280
237
})
281
- }, false )
282
- setting .Repository .Signing .Merges = []string {"basesigned" }
283
- onGiteaRun (t , func (t * testing.T , u * url.URL ) {
284
- u .Path = baseAPITestContext .GitPath ()
285
238
239
+ setting .Repository .Signing .Merges = []string {"basesigned" }
286
240
t .Run ("BaseSignedMerging" , func (t * testing.T ) {
287
241
defer tests .PrintCurrentTest (t )()
288
242
testCtx := NewAPITestContext (t , username , "initial-unsigned" , auth_model .AccessTokenScopeWriteRepository , auth_model .AccessTokenScopeWriteUser )
289
- var err error
290
243
t .Run ("CreatePullRequest" , func (t * testing.T ) {
291
- pr , err = doAPICreatePullRequest (testCtx , testCtx .Username , testCtx .Reponame , "master" , "parentsigned2" )(t )
244
+ pr , err : = doAPICreatePullRequest (testCtx , testCtx .Username , testCtx .Reponame , "master" , "parentsigned2" )(t )
292
245
assert .NoError (t , err )
246
+ t .Run ("MergePR" , doAPIMergePullRequest (testCtx , testCtx .Username , testCtx .Reponame , pr .Index ))
293
247
})
294
- t .Run ("MergePR" , doAPIMergePullRequest (testCtx , testCtx .Username , testCtx .Reponame , pr .Index ))
295
248
t .Run ("CheckMasterBranchUnsigned" , doAPIGetBranch (testCtx , "master" , func (t * testing.T , branch api.Branch ) {
296
249
assert .NotNil (t , branch .Commit )
297
250
assert .NotNil (t , branch .Commit .Verification )
298
251
assert .False (t , branch .Commit .Verification .Verified )
299
252
assert .Empty (t , branch .Commit .Verification .Signature )
300
253
}))
301
254
})
302
- }, false )
303
- setting .Repository .Signing .Merges = []string {"commitssigned" }
304
- onGiteaRun (t , func (t * testing.T , u * url.URL ) {
305
- u .Path = baseAPITestContext .GitPath ()
306
255
256
+ setting .Repository .Signing .Merges = []string {"commitssigned" }
307
257
t .Run ("CommitsSignedMerging" , func (t * testing.T ) {
308
258
defer tests .PrintCurrentTest (t )()
309
259
testCtx := NewAPITestContext (t , username , "initial-unsigned" , auth_model .AccessTokenScopeWriteRepository , auth_model .AccessTokenScopeWriteUser )
310
- var err error
311
260
t .Run ("CreatePullRequest" , func (t * testing.T ) {
312
- pr , err = doAPICreatePullRequest (testCtx , testCtx .Username , testCtx .Reponame , "master" , "always-parentsigned" )(t )
261
+ pr , err : = doAPICreatePullRequest (testCtx , testCtx .Username , testCtx .Reponame , "master" , "always-parentsigned" )(t )
313
262
assert .NoError (t , err )
263
+ t .Run ("MergePR" , doAPIMergePullRequest (testCtx , testCtx .Username , testCtx .Reponame , pr .Index ))
314
264
})
315
- t .Run ("MergePR" , doAPIMergePullRequest (testCtx , testCtx .Username , testCtx .Reponame , pr .Index ))
316
265
t .Run ("CheckMasterBranchUnsigned" , doAPIGetBranch (testCtx , "master" , func (t * testing.T , branch api.Branch ) {
317
266
assert .NotNil (t , branch .Commit )
318
267
assert .NotNil (t , branch .Commit .Verification )
319
268
assert .True (t , branch .Commit .Verification .Verified )
320
269
}))
321
270
})
322
- }, false )
271
+ })
323
272
}
324
273
325
274
func crudActionCreateFile (t * testing.T , ctx APITestContext , user * user_model.User , from , to , path string , callback ... func (* testing.T , api.FileResponse )) func (* testing.T ) {
0 commit comments