forked from xanzy/go-gitlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotected_branches_test.go
173 lines (164 loc) · 5.62 KB
/
protected_branches_test.go
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
package gitlab
import (
"fmt"
"net/http"
"reflect"
"testing"
)
func TestListProtectedBranches(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/projects/1/protected_branches", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1,
"name":"master",
"push_access_levels":[{
"access_level":40,
"access_level_description":"Maintainers"
}],
"merge_access_levels":[{
"access_level":40,
"access_level_description":"Maintainers"
}],
"code_owner_approval_required":false
}]`)
})
opt := &ListProtectedBranchesOptions{}
protectedBranches, _, err := client.ProtectedBranches.ListProtectedBranches("1", opt)
if err != nil {
t.Errorf("ProtectedBranches.ListProtectedBranches returned error: %v", err)
}
want := []*ProtectedBranch{
{
ID: 1,
Name: "master",
PushAccessLevels: []*BranchAccessDescription{
{
AccessLevel: 40,
AccessLevelDescription: "Maintainers",
},
},
MergeAccessLevels: []*BranchAccessDescription{
{
AccessLevel: 40,
AccessLevelDescription: "Maintainers",
},
},
CodeOwnerApprovalRequired: false,
},
}
if !reflect.DeepEqual(want, protectedBranches) {
t.Errorf("ProtectedBranches.ListProtectedBranches returned %+v, want %+v", protectedBranches, want)
}
}
func TestListProtectedBranchesWithoutCodeOwnerApproval(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/projects/1/protected_branches", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1,
"name":"master",
"push_access_levels":[{
"access_level":40,
"access_level_description":"Maintainers"
}],
"merge_access_levels":[{
"access_level":40,
"access_level_description":"Maintainers"
}]
}]`)
})
opt := &ListProtectedBranchesOptions{}
protectedBranches, _, err := client.ProtectedBranches.ListProtectedBranches("1", opt)
if err != nil {
t.Errorf("ProtectedBranches.ListProtectedBranches returned error: %v", err)
}
want := []*ProtectedBranch{
{
ID: 1,
Name: "master",
PushAccessLevels: []*BranchAccessDescription{
{
AccessLevel: 40,
AccessLevelDescription: "Maintainers",
},
},
MergeAccessLevels: []*BranchAccessDescription{
{
AccessLevel: 40,
AccessLevelDescription: "Maintainers",
},
},
CodeOwnerApprovalRequired: false,
},
}
if !reflect.DeepEqual(want, protectedBranches) {
t.Errorf("Projects.ListProjects returned %+v, want %+v", protectedBranches, want)
}
}
func TestProtectRepositoryBranches(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/projects/1/protected_branches", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
fmt.Fprint(w, `{"id":1,
"name":"master",
"push_access_levels":[{
"access_level":40,
"access_level_description":"Maintainers"
}],
"merge_access_levels":[{
"access_level":40,
"access_level_description":"Maintainers"
}],
"code_owner_approval_required":true
}`)
})
opt := &ProtectRepositoryBranchesOptions{
Name: String("master"),
PushAccessLevel: AccessLevel(MaintainerPermissions),
MergeAccessLevel: AccessLevel(MaintainerPermissions),
CodeOwnerApprovalRequired: Bool(true),
}
projects, _, err := client.ProtectedBranches.ProtectRepositoryBranches("1", opt)
if err != nil {
t.Errorf("ProtectedBranches.ProtectRepositoryBranches returned error: %v", err)
}
want := &ProtectedBranch{
Name: "master",
PushAccessLevels: []*BranchAccessDescription{
{
AccessLevel: 40,
AccessLevelDescription: "Maintainers",
},
},
MergeAccessLevels: []*BranchAccessDescription{
{
AccessLevel: 40,
AccessLevelDescription: "Maintainers",
},
},
CodeOwnerApprovalRequired: true,
}
if !reflect.DeepEqual(want, projects) {
t.Errorf("Projects.ListProjects returned %+v, want %+v", projects, want)
}
}
func TestUpdateRepositoryBranches(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/projects/1/protected_branches/master", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PATCH")
codeApprovalQueryParam := r.URL.Query().Get("code_owner_approval_required")
if codeApprovalQueryParam != "true" {
t.Errorf("query param code_owner_approval_required should be true but was %s", codeApprovalQueryParam)
}
})
opt := &RequireCodeOwnerApprovalsOptions{
CodeOwnerApprovalRequired: Bool(true),
}
_, err := client.ProtectedBranches.RequireCodeOwnerApprovals("1", "master", opt)
if err != nil {
t.Errorf("ProtectedBranches.UpdateRepositoryBranchesOptions returned error: %v", err)
}
}