forked from anthonydahanne/stash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createbranchrestriction_test.go
86 lines (77 loc) · 2.56 KB
/
createbranchrestriction_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
package stash
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
const createBranchRestrictionsResponse string = `
{
"id": 41,
"type": "BRANCH",
"value": "refs/heads/develop",
"branch": {
"id": "refs/heads/develop",
"displayId": "develop",
"latestChangeset": "d81c71b179c08715eb21251824635ce9a1d7f6f3",
"isDefault": false
}
}
`
func TestCreateBranchRestriction(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Fatalf("wanted POST but found %s\n", r.Method)
}
url := *r.URL
if url.Path != "/rest/branch-permissions/1.0/projects/PROJ/repos/slug/restricted" {
t.Fatalf("CreateBranchPermissions() 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("CreateBranchPermissions() 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, createBranchRestrictionsResponse)
}))
defer testServer.Close()
url, _ := url.Parse(testServer.URL)
stashClient := NewClient("u", "p", url)
branchRestriction, err := stashClient.CreateBranchRestriction("PROJ", "slug", "develop", "user")
if err != nil {
t.Fatalf("Not expecting error: %v\n", err)
}
// spot checks
if branchRestriction.Branch.DisplayID != "develop" {
t.Fatalf("Want develop but got %s\n", branchRestriction.Branch.DisplayID)
}
if branchRestriction.Id != 41 {
t.Fatalf("Want 41 but got %d\n", branchRestriction.Id)
}
}
func TestCreateBranchRestriction404(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.CreateBranchRestriction("PROJ", "slug", "develop", "user")
if err == nil {
t.Fatalf("Expecting error but did not get one\n")
}
}
func TestCreateBranchRestriction401(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.CreateBranchRestriction("PROJ", "slug", "develop", "user")
if err == nil {
t.Fatalf("Expecting error but did not get one\n")
}
}