forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbranchrestrictions_test.go
More file actions
340 lines (286 loc) · 9.71 KB
/
branchrestrictions_test.go
File metadata and controls
340 lines (286 loc) · 9.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package bitbucket
import (
"encoding/json"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestBranchRestrictionsGets_Success(t *testing.T) {
t.Parallel()
var receivedPath string
var receivedMethod string
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
receivedPath = r.URL.Path
receivedMethod = r.Method
respondJSON(w, http.StatusOK, paginatedResponse([]interface{}{
map[string]interface{}{"id": 1, "kind": "push", "pattern": "main"},
}))
})
defer server.Close()
opts := &BranchRestrictionsOptions{Owner: "owner", RepoSlug: "repo"}
result, err := client.Repositories.BranchRestrictions.Gets(opts)
require.NoError(t, err)
assert.Equal(t, "GET", receivedMethod)
assert.Equal(t, "/2.0/repositories/owner/repo/branch-restrictions", receivedPath)
require.NotNil(t, result)
resultMap, ok := result.(map[string]interface{})
require.True(t, ok, "result should be a map")
values, ok := resultMap["values"].([]interface{})
require.True(t, ok, "result should contain values array")
assert.Len(t, values, 1)
firstItem := values[0].(map[string]interface{})
assert.Equal(t, float64(1), firstItem["id"])
assert.Equal(t, "push", firstItem["kind"])
assert.Equal(t, "main", firstItem["pattern"])
}
func TestBranchRestrictionsGets_Error(t *testing.T) {
t.Parallel()
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})
defer server.Close()
opts := &BranchRestrictionsOptions{Owner: "owner", RepoSlug: "repo"}
result, err := client.Repositories.BranchRestrictions.Gets(opts)
assert.Error(t, err)
assert.Nil(t, result)
}
func TestBranchRestrictionsCreate_Success(t *testing.T) {
t.Parallel()
var receivedMethod string
var receivedBody map[string]interface{}
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
receivedMethod = r.Method
bodyBytes, _ := io.ReadAll(r.Body)
_ = json.Unmarshal(bodyBytes, &receivedBody)
respondJSON(w, http.StatusCreated, map[string]interface{}{
"id": 1, "kind": "push", "pattern": "main",
})
})
defer server.Close()
opts := &BranchRestrictionsOptions{
Owner: "owner",
RepoSlug: "repo",
Kind: "push",
Pattern: "main",
Users: []string{"user1"},
Groups: map[string]string{"group1": "group1"},
}
result, err := client.Repositories.BranchRestrictions.Create(opts)
require.NoError(t, err)
assert.Equal(t, "POST", receivedMethod)
assert.Equal(t, 1, result.ID)
assert.Equal(t, "push", result.Kind)
assert.Equal(t, "main", result.Pattern)
// Verify the serialized request body
assert.Equal(t, "push", receivedBody["kind"])
assert.Equal(t, "main", receivedBody["pattern"])
users := receivedBody["users"].([]interface{})
require.Len(t, users, 1)
assert.Equal(t, "user1", users[0].(map[string]interface{})["username"])
groups := receivedBody["groups"].([]interface{})
require.Len(t, groups, 1)
assert.Equal(t, "group1", groups[0].(map[string]interface{})["slug"])
}
func TestBranchRestrictionsCreate_Error(t *testing.T) {
t.Parallel()
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
})
defer server.Close()
opts := &BranchRestrictionsOptions{
Owner: "owner",
RepoSlug: "repo",
Kind: "push",
Pattern: "main",
}
result, err := client.Repositories.BranchRestrictions.Create(opts)
assert.Error(t, err)
assert.Nil(t, result)
}
func TestBranchRestrictionsGet_Success(t *testing.T) {
t.Parallel()
var receivedPath string
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
receivedPath = r.URL.Path
respondJSON(w, http.StatusOK, map[string]interface{}{
"id": 1, "kind": "push", "pattern": "main",
})
})
defer server.Close()
opts := &BranchRestrictionsOptions{Owner: "owner", RepoSlug: "repo", ID: "1"}
result, err := client.Repositories.BranchRestrictions.Get(opts)
require.NoError(t, err)
assert.Equal(t, "/2.0/repositories/owner/repo/branch-restrictions/1", receivedPath)
assert.Equal(t, "push", result.Kind)
}
func TestBranchRestrictionsUpdate_Success(t *testing.T) {
t.Parallel()
var receivedMethod string
var receivedPath string
var receivedBody map[string]interface{}
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
receivedMethod = r.Method
receivedPath = r.URL.Path
bodyBytes, _ := io.ReadAll(r.Body)
_ = json.Unmarshal(bodyBytes, &receivedBody)
respondJSON(w, http.StatusOK, map[string]interface{}{
"id": 1, "kind": "push", "pattern": "develop",
})
})
defer server.Close()
opts := &BranchRestrictionsOptions{
Owner: "owner",
RepoSlug: "repo",
ID: "1",
Kind: "push",
Pattern: "develop",
}
result, err := client.Repositories.BranchRestrictions.Update(opts)
require.NoError(t, err)
assert.Equal(t, "PUT", receivedMethod)
assert.Equal(t, "/2.0/repositories/owner/repo/branch-restrictions/1", receivedPath)
assert.Equal(t, "push", receivedBody["kind"])
assert.Equal(t, "develop", receivedBody["pattern"])
// Update returns interface{}, but it's decoded via decodeBranchRestriction
brResult, ok := result.(*BranchRestrictions)
require.True(t, ok, "result should be *BranchRestrictions")
assert.Equal(t, 1, brResult.ID)
assert.Equal(t, "push", brResult.Kind)
assert.Equal(t, "develop", brResult.Pattern)
}
func TestBranchRestrictionsUpdate_Error(t *testing.T) {
t.Parallel()
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
defer server.Close()
opts := &BranchRestrictionsOptions{
Owner: "owner",
RepoSlug: "repo",
ID: "999",
Kind: "push",
Pattern: "develop",
}
result, err := client.Repositories.BranchRestrictions.Update(opts)
assert.Error(t, err)
assert.Nil(t, result)
}
func TestBranchRestrictionsDelete_Success(t *testing.T) {
t.Parallel()
var receivedMethod string
var receivedPath string
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
receivedMethod = r.Method
receivedPath = r.URL.Path
w.WriteHeader(http.StatusNoContent)
})
defer server.Close()
opts := &BranchRestrictionsOptions{Owner: "owner", RepoSlug: "repo", ID: "1"}
result, err := client.Repositories.BranchRestrictions.Delete(opts)
require.NoError(t, err)
assert.Equal(t, "DELETE", receivedMethod)
assert.Equal(t, "/2.0/repositories/owner/repo/branch-restrictions/1", receivedPath)
// Delete with 204 No Content returns nil body
assert.Nil(t, result)
}
func TestBranchRestrictionsDelete_Error(t *testing.T) {
t.Parallel()
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
defer server.Close()
opts := &BranchRestrictionsOptions{Owner: "owner", RepoSlug: "repo", ID: "999"}
result, err := client.Repositories.BranchRestrictions.Delete(opts)
assert.Error(t, err)
assert.Nil(t, result)
}
func TestBranchRestrictionsGet_Error(t *testing.T) {
t.Parallel()
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
defer server.Close()
opts := &BranchRestrictionsOptions{Owner: "owner", RepoSlug: "repo", ID: "999"}
result, err := client.Repositories.BranchRestrictions.Get(opts)
assert.Error(t, err)
assert.Nil(t, result)
}
func TestBuildBranchRestrictionsBody(t *testing.T) {
t.Parallel()
br := &BranchRestrictions{}
opts := &BranchRestrictionsOptions{
Kind: "push",
Pattern: "main",
Users: []string{"user1", "user2"},
Groups: map[string]string{"group1": "group1", "group2": "group2"},
Value: 42,
}
data, err := br.buildBranchRestrictionsBody(opts)
require.NoError(t, err)
var body map[string]interface{}
err = json.Unmarshal([]byte(data), &body)
require.NoError(t, err)
assert.Equal(t, "push", body["kind"])
assert.Equal(t, "main", body["pattern"])
assert.Equal(t, float64(42), body["value"])
// Verify Users array: string[] -> struct[] with username field
users := body["users"].([]interface{})
require.Len(t, users, 2)
user1 := users[0].(map[string]interface{})
assert.Equal(t, "user1", user1["username"])
user2 := users[1].(map[string]interface{})
assert.Equal(t, "user2", user2["username"])
// Verify Groups array: map[string]string -> struct[] with slug field
groups := body["groups"].([]interface{})
require.Len(t, groups, 2)
groupSlugs := []string{}
for _, g := range groups {
groupSlugs = append(groupSlugs, g.(map[string]interface{})["slug"].(string))
}
assert.Contains(t, groupSlugs, "group1")
assert.Contains(t, groupSlugs, "group2")
}
func TestBuildBranchRestrictionsBody_Empty(t *testing.T) {
t.Parallel()
br := &BranchRestrictions{}
opts := &BranchRestrictionsOptions{
Kind: "push",
Pattern: "main",
}
data, err := br.buildBranchRestrictionsBody(opts)
require.NoError(t, err)
var body map[string]interface{}
err = json.Unmarshal([]byte(data), &body)
require.NoError(t, err)
assert.Equal(t, "push", body["kind"])
assert.Equal(t, "main", body["pattern"])
// Users and Groups should be null when not provided
assert.Nil(t, body["users"])
assert.Nil(t, body["groups"])
}
func TestDecodeBranchRestriction_Success(t *testing.T) {
t.Parallel()
response := map[string]interface{}{
"id": float64(1),
"kind": "push",
"pattern": "main",
}
result, err := decodeBranchRestriction(response)
require.NoError(t, err)
assert.Equal(t, 1, result.ID)
assert.Equal(t, "push", result.Kind)
assert.Equal(t, "main", result.Pattern)
}
func TestDecodeBranchRestriction_ErrorType(t *testing.T) {
t.Parallel()
response := map[string]interface{}{
"type": "error",
"error": map[string]interface{}{
"message": "not found",
},
}
_, err := decodeBranchRestriction(response)
assert.Error(t, err)
}