forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbranchrestrictions_test.go
174 lines (150 loc) · 3.73 KB
/
branchrestrictions_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
174
package tests
import (
"errors"
"fmt"
"os"
"strconv"
"testing"
"github.com/diggerhq/go-bitbucket"
)
var (
user = os.Getenv("BITBUCKET_TEST_USERNAME")
pass = os.Getenv("BITBUCKET_TEST_PASSWORD")
owner = os.Getenv("BITBUCKET_TEST_OWNER")
repo = os.Getenv("BITBUCKET_TEST_REPOSLUG")
)
func setup(t *testing.T) *bitbucket.Client {
if user == "" {
t.Error("BITBUCKET_TEST_USERNAME is empty.")
}
if pass == "" {
t.Error("BITBUCKET_TEST_PASSWORD is empty.")
}
if owner == "" {
t.Error("BITBUCKET_TEST_OWNER is empty.")
}
if repo == "" {
t.Error("BITBUCKET_TEST_REPOSLUG is empty.")
}
c := bitbucket.NewBasicAuth(user, pass)
return c
}
func TestBranchRestrictionsKindPush(t *testing.T) {
c := setup(t)
var pushRestrictionID int
t.Run("create", func(t *testing.T) {
opt := &bitbucket.BranchRestrictionsOptions{
Owner: owner,
Pattern: "develop",
RepoSlug: repo,
Kind: "push",
Users: []string{user},
}
res, err := c.Repositories.BranchRestrictions.Create(opt)
if err != nil {
t.Error(err)
}
if res.Kind != "push" {
t.Error("did not match branchrestriction kind")
}
pushRestrictionID = res.ID
})
t.Run("delete", func(t *testing.T) {
opt := &bitbucket.BranchRestrictionsOptions{
Owner: owner,
RepoSlug: repo,
ID: strconv.Itoa(pushRestrictionID),
}
_, err := c.Repositories.BranchRestrictions.Delete(opt)
if err != nil {
t.Error(err)
}
})
}
func TestBranchRestrictionsKindRequirePassingBuilds(t *testing.T) {
c := setup(t)
var pushRestrictionID int
t.Run("create", func(t *testing.T) {
opt := &bitbucket.BranchRestrictionsOptions{
Owner: owner,
Pattern: "master",
RepoSlug: repo,
Kind: "require_passing_builds_to_merge",
Value: 2,
}
res, err := c.Repositories.BranchRestrictions.Create(opt)
if err != nil {
t.Error(err)
}
if res.Kind != "require_passing_builds_to_merge" {
t.Error("did not match branchrestriction kind")
}
pushRestrictionID = res.ID
})
t.Run("delete", func(t *testing.T) {
opt := &bitbucket.BranchRestrictionsOptions{
Owner: owner,
RepoSlug: repo,
ID: strconv.Itoa(pushRestrictionID),
}
_, err := c.Repositories.BranchRestrictions.Delete(opt)
if err != nil {
t.Error(err)
}
})
}
func TestBranchRestrictionsGets(t *testing.T) {
c := setup(t)
t.Run("gets", func(t *testing.T) {
const expectedNumberOfRestrictions = 20
var restrictionIDs []int
defer func() {
for i := 0; i < len(restrictionIDs); i++ {
opt := &bitbucket.BranchRestrictionsOptions{
Owner: owner,
RepoSlug: repo,
ID: strconv.Itoa(restrictionIDs[i]),
}
_, err := c.Repositories.BranchRestrictions.Delete(opt)
if err != nil {
t.Error(err)
}
}
}()
for i := 0; i < expectedNumberOfRestrictions; i++ {
opt := &bitbucket.BranchRestrictionsOptions{
Owner: owner,
Pattern: fmt.Sprintf("branch-restrictions-gets-%d", i),
RepoSlug: repo,
Kind: "push",
Users: []string{user},
}
res, err := c.Repositories.BranchRestrictions.Create(opt)
if err != nil {
t.Error(err)
return
}
restrictionIDs = append(restrictionIDs, res.ID)
}
c.Pagelen = 5
opt := &bitbucket.BranchRestrictionsOptions{
Owner: owner,
RepoSlug: repo,
}
res, err := c.Repositories.BranchRestrictions.Gets(opt)
if err != nil {
t.Error(err)
return
}
responseMap, ok := res.(map[string]interface{})
if !ok {
t.Error(errors.New("response could not be decoded"))
return
}
values := responseMap["values"].([]interface{})
if len(values) != expectedNumberOfRestrictions {
t.Error(fmt.Errorf("Expected %d branch restrictions but got %d. Response: %v", expectedNumberOfRestrictions, len(values), res))
return
}
})
}