@@ -6,27 +6,30 @@ package integrations
6
6
7
7
import (
8
8
"net/http"
9
+ "net/http/httptest"
9
10
"strconv"
10
11
"testing"
11
12
12
- "github.com/stretchr/testify/assert"
13
-
14
13
api "code.gitea.io/sdk/gitea"
14
+
15
+ "github.com/stretchr/testify/assert"
15
16
)
16
17
18
+ type makeRequestFunc func (testing.TB , * http.Request , int ) * httptest.ResponseRecorder
19
+
17
20
func TestGPGKeys (t * testing.T ) {
18
21
prepareTestEnv (t )
19
22
session := loginUser (t , "user2" )
20
23
21
24
tt := []struct {
22
- name string
23
- reqBuilder func (testing. TB , * http. Request , int ) * TestResponse
24
- results []int
25
+ name string
26
+ makeRequest makeRequestFunc
27
+ results []int
25
28
}{
26
- {name : "NoLogin" , reqBuilder : MakeRequest ,
29
+ {name : "NoLogin" , makeRequest : MakeRequest ,
27
30
results : []int {http .StatusUnauthorized , http .StatusUnauthorized , http .StatusUnauthorized , http .StatusUnauthorized , http .StatusUnauthorized , http .StatusUnauthorized , http .StatusUnauthorized , http .StatusUnauthorized },
28
31
},
29
- {name : "LoggedAsUser2" , reqBuilder : session .MakeRequest ,
32
+ {name : "LoggedAsUser2" , makeRequest : session .MakeRequest ,
30
33
results : []int {http .StatusOK , http .StatusOK , http .StatusNotFound , http .StatusNoContent , http .StatusInternalServerError , http .StatusInternalServerError , http .StatusCreated , http .StatusCreated }},
31
34
}
32
35
@@ -35,29 +38,29 @@ func TestGPGKeys(t *testing.T) {
35
38
//Basic test on result code
36
39
t .Run (tc .name , func (t * testing.T ) {
37
40
t .Run ("ViewOwnGPGKeys" , func (t * testing.T ) {
38
- testViewOwnGPGKeys (t , tc .reqBuilder , tc .results [0 ])
41
+ testViewOwnGPGKeys (t , tc .makeRequest , tc .results [0 ])
39
42
})
40
43
t .Run ("ViewGPGKeys" , func (t * testing.T ) {
41
- testViewGPGKeys (t , tc .reqBuilder , tc .results [1 ])
44
+ testViewGPGKeys (t , tc .makeRequest , tc .results [1 ])
42
45
})
43
46
t .Run ("GetGPGKey" , func (t * testing.T ) {
44
- testGetGPGKey (t , tc .reqBuilder , tc .results [2 ])
47
+ testGetGPGKey (t , tc .makeRequest , tc .results [2 ])
45
48
})
46
49
t .Run ("DeleteGPGKey" , func (t * testing.T ) {
47
- testDeleteGPGKey (t , tc .reqBuilder , tc .results [3 ])
50
+ testDeleteGPGKey (t , tc .makeRequest , tc .results [3 ])
48
51
})
49
52
50
53
t .Run ("CreateInvalidGPGKey" , func (t * testing.T ) {
51
- testCreateInvalidGPGKey (t , tc .reqBuilder , tc .results [4 ])
54
+ testCreateInvalidGPGKey (t , tc .makeRequest , tc .results [4 ])
52
55
})
53
56
t .Run ("CreateNoneRegistredEmailGPGKey" , func (t * testing.T ) {
54
- testCreateNoneRegistredEmailGPGKey (t , tc .reqBuilder , tc .results [5 ])
57
+ testCreateNoneRegistredEmailGPGKey (t , tc .makeRequest , tc .results [5 ])
55
58
})
56
59
t .Run ("CreateValidGPGKey" , func (t * testing.T ) {
57
- testCreateValidGPGKey (t , tc .reqBuilder , tc .results [6 ])
60
+ testCreateValidGPGKey (t , tc .makeRequest , tc .results [6 ])
58
61
})
59
62
t .Run ("CreateValidSecondaryEmailGPGKey" , func (t * testing.T ) {
60
- testCreateValidSecondaryEmailGPGKey (t , tc .reqBuilder , tc .results [7 ])
63
+ testCreateValidSecondaryEmailGPGKey (t , tc .makeRequest , tc .results [7 ])
61
64
})
62
65
})
63
66
}
@@ -140,39 +143,39 @@ func TestGPGKeys(t *testing.T) {
140
143
})
141
144
}
142
145
143
- func testViewOwnGPGKeys (t * testing.T , reqBuilder func (testing. TB , * http. Request , int ) * TestResponse , expected int ) {
146
+ func testViewOwnGPGKeys (t * testing.T , makeRequest makeRequestFunc , expected int ) {
144
147
req := NewRequest (t , "GET" , "/api/v1/user/gpg_keys" )
145
- reqBuilder (t , req , expected )
148
+ makeRequest (t , req , expected )
146
149
}
147
150
148
- func testViewGPGKeys (t * testing.T , reqBuilder func (testing. TB , * http. Request , int ) * TestResponse , expected int ) {
151
+ func testViewGPGKeys (t * testing.T , makeRequest makeRequestFunc , expected int ) {
149
152
req := NewRequest (t , "GET" , "/api/v1/users/user2/gpg_keys" )
150
- reqBuilder (t , req , expected )
153
+ makeRequest (t , req , expected )
151
154
}
152
155
153
- func testGetGPGKey (t * testing.T , reqBuilder func (testing. TB , * http. Request , int ) * TestResponse , expected int ) {
156
+ func testGetGPGKey (t * testing.T , makeRequest makeRequestFunc , expected int ) {
154
157
req := NewRequest (t , "GET" , "/api/v1/user/gpg_keys/1" )
155
- reqBuilder (t , req , expected )
158
+ makeRequest (t , req , expected )
156
159
}
157
160
158
- func testDeleteGPGKey (t * testing.T , reqBuilder func (testing. TB , * http. Request , int ) * TestResponse , expected int ) {
161
+ func testDeleteGPGKey (t * testing.T , makeRequest makeRequestFunc , expected int ) {
159
162
req := NewRequest (t , "DELETE" , "/api/v1/user/gpg_keys/1" )
160
- reqBuilder (t , req , expected )
163
+ makeRequest (t , req , expected )
161
164
}
162
165
163
- func testCreateGPGKey (t * testing.T , reqBuilder func (testing. TB , * http. Request , int ) * TestResponse , expected int , publicKey string ) {
166
+ func testCreateGPGKey (t * testing.T , makeRequest makeRequestFunc , expected int , publicKey string ) {
164
167
req := NewRequestWithJSON (t , "POST" , "/api/v1/user/gpg_keys" , api.CreateGPGKeyOption {
165
168
ArmoredKey : publicKey ,
166
169
})
167
- reqBuilder (t , req , expected )
170
+ makeRequest (t , req , expected )
168
171
}
169
172
170
- func testCreateInvalidGPGKey (t * testing.T , reqBuilder func (testing. TB , * http. Request , int ) * TestResponse , expected int ) {
171
- testCreateGPGKey (t , reqBuilder , expected , "invalid_key" )
173
+ func testCreateInvalidGPGKey (t * testing.T , makeRequest makeRequestFunc , expected int ) {
174
+ testCreateGPGKey (t , makeRequest , expected , "invalid_key" )
172
175
}
173
176
174
- func testCreateNoneRegistredEmailGPGKey (t * testing.T , reqBuilder func (testing. TB , * http. Request , int ) * TestResponse , expected int ) {
175
- testCreateGPGKey (t , reqBuilder , expected , `-----BEGIN PGP PUBLIC KEY BLOCK-----
177
+ func testCreateNoneRegistredEmailGPGKey (t * testing.T , makeRequest makeRequestFunc , expected int ) {
178
+ testCreateGPGKey (t , makeRequest , expected , `-----BEGIN PGP PUBLIC KEY BLOCK-----
176
179
177
180
mQENBFmGUygBCACjCNbKvMGgp0fd5vyFW9olE1CLCSyyF9gQN2hSuzmZLuAZF2Kh
178
181
dCMCG2T1UwzUB/yWUFWJ2BtCwSjuaRv+cGohqEy6bhEBV90peGA33lHfjx7wP25O
@@ -191,9 +194,9 @@ INx/MmBfmtCq05FqNclvU+sj2R3N1JJOtBOjZrJHQbJhzoILou8AkxeX1A+q9OAz
191
194
-----END PGP PUBLIC KEY BLOCK-----` )
192
195
}
193
196
194
- func testCreateValidGPGKey (t * testing.T , reqBuilder func (testing. TB , * http. Request , int ) * TestResponse , expected int ) {
197
+ func testCreateValidGPGKey (t * testing.T , makeRequest makeRequestFunc , expected int ) {
195
198
//User2 <[email protected] > //primary & activated
196
- testCreateGPGKey (t , reqBuilder , expected , `-----BEGIN PGP PUBLIC KEY BLOCK-----
199
+ testCreateGPGKey (t , makeRequest , expected , `-----BEGIN PGP PUBLIC KEY BLOCK-----
197
200
198
201
mQENBFmGVsMBCACuxgZ7W7rI9xN08Y4M7B8yx/6/I4Slm94+wXf8YNRvAyqj30dW
199
202
VJhyBcnfNRDLKSQp5o/hhfDkCgdqBjLa1PnHlGS3PXJc0hP/FyYPD2BFvNMPpCYS
@@ -225,9 +228,9 @@ uy6MA3VSB99SK9ducGmE1Jv8mcziREroz2TEGr0zPs6h
225
228
-----END PGP PUBLIC KEY BLOCK-----` )
226
229
}
227
230
228
- func testCreateValidSecondaryEmailGPGKey (t * testing.T , reqBuilder func (testing. TB , * http. Request , int ) * TestResponse , expected int ) {
231
+ func testCreateValidSecondaryEmailGPGKey (t * testing.T , makeRequest makeRequestFunc , expected int ) {
229
232
//User2 <[email protected] > //secondary and not activated
230
- testCreateGPGKey (t , reqBuilder , expected , `-----BEGIN PGP PUBLIC KEY BLOCK-----
233
+ testCreateGPGKey (t , makeRequest , expected , `-----BEGIN PGP PUBLIC KEY BLOCK-----
231
234
232
235
mQENBFmGWN4BCAC18V4tVGO65VLCV7p14FuXJlUtZ5CuYMvgEkcOqrvRaBSW9ao4
233
236
PGESOhJpfWpnW3QgJniYndLzPpsmdHEclEER6aZjiNgReWPOjHD5tykWocZAJqXD
0 commit comments