-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgetbranchrestrictions_test.go
More file actions
95 lines (86 loc) · 2.72 KB
/
getbranchrestrictions_test.go
File metadata and controls
95 lines (86 loc) · 2.72 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
package stash
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
const branchRestrictionsResponse string = `
{
"size": 1,
"limit": 100,
"isLastPage": true,
"values": [
{
"id": 41,
"type": "BRANCH",
"value": "refs/heads/develop",
"branch": {
"id": "refs/heads/develop",
"displayId": "develop",
"latestChangeset": "d81c71b179c08715eb21251824635ce9a1d7f6f3",
"isDefault": false
}
}
],
"start": 0,
"filter": null
}
`
func TestGetBranchRestrictions(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Fatalf("wanted GET but found %s\n", r.Method)
}
url := *r.URL
if url.Path != "/rest/branch-permissions/1.0/projects/PROJ/repos/slug/restricted" {
t.Fatalf("GetBranchPermissions() URL path expected to be /rest/branch-permissions/1.0/projects/PROJ/repos/slug/restricted but found %s\n", url.Path)
}
if r.Header.Get("Accept") != "application/json" {
t.Fatalf("GetBranchRestrictions() expected request Accept header to be application/json but found %s\n", r.Header.Get("Accept"))
}
if r.Header.Get("Authorization") != "Basic dTpw" {
t.Fatalf("Want Basic dTpw but found %s\n", r.Header.Get("Authorization"))
}
fmt.Fprintln(w, branchRestrictionsResponse)
}))
defer testServer.Close()
url, _ := url.Parse(testServer.URL)
stashClient := NewClient("u", "p", url)
branchRestrictions, err := stashClient.GetBranchRestrictions("PROJ", "slug")
if err != nil {
t.Fatalf("Not expecting error: %v\n", err)
}
// spot checks
if branchRestrictions.BranchRestriction[0].Branch.DisplayID != "develop" {
t.Fatalf("Want develop but got %s\n", branchRestrictions.BranchRestriction[0].Branch.DisplayID)
}
if branchRestrictions.BranchRestriction[0].Id != 41 {
t.Fatalf("Want 41 but got %d\n", branchRestrictions.BranchRestriction[0].Id)
}
}
func TestGetBranchRestrictions404(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
}))
defer testServer.Close()
url, _ := url.Parse(testServer.URL)
stashClient := NewClient("u", "p", url)
_, err := stashClient.GetBranchRestrictions("PROJ", "slug")
if err == nil {
t.Fatalf("Expecting error but did not get one\n")
}
}
func TestGetBranchRestrictions401(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(401)
}))
defer testServer.Close()
url, _ := url.Parse(testServer.URL)
stashClient := NewClient("u", "p", url)
_, err := stashClient.GetBranchRestrictions("PROJ", "slug")
if err == nil {
t.Fatalf("Expecting error but did not get one\n")
}
}