-
Notifications
You must be signed in to change notification settings - Fork 2
/
permissions_test.go
100 lines (78 loc) · 3.5 KB
/
permissions_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
package gocd_test
import (
_ "embed"
"net/http"
"testing"
"github.com/nikhilsbhat/gocd-sdk-go"
"github.com/stretchr/testify/assert"
)
//go:embed internal/fixtures/permissions.json
var permissionsJSON string
func Test_client_GetPermissions(t *testing.T) {
correctPermissionHeader := map[string]string{"Accept": gocd.HeaderVersionOne}
t.Run("should be able to fetch permissions information successfully", func(t *testing.T) {
server := mockServer([]byte(permissionsJSON), http.StatusOK,
correctPermissionHeader, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
expected := gocd.Permission{
Environment: gocd.EntityPermissions{
View: []string{"DEV", "UAT", "QA", "PROD"},
Administer: []string{"DEV"},
},
ConfigRepo: gocd.EntityPermissions{
View: []string{"dev-pipelines-repo", "prod-pipelines-repo"},
Administer: []string{"dev-pipelines-repo"},
},
ClusterProfile: gocd.EntityPermissions{
View: []string{"dev-cluster", "prod-cluster"},
Administer: []string{"dev-cluster"},
},
ElasticAgentProfile: gocd.EntityPermissions{
View: []string{"build-agent", "deploy-agent"},
Administer: []string{"build-agent"},
},
}
actual, err := client.GetPermissions(nil)
assert.NoError(t, err)
assert.Equal(t, expected, actual)
})
t.Run("should error out while fetching permissions info from GoCD due to wrong headers", func(t *testing.T) {
server := mockServer([]byte(permissionsJSON), http.StatusOK,
map[string]string{"Accept": gocd.HeaderVersionTwo}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
expected := gocd.Permission{}
actual, err := client.GetPermissions(nil)
assert.EqualError(t, err, "got 404 from GoCD while making GET call for "+server.URL+
"/api/auth/permissions\nwith BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
assert.Equal(t, expected, actual)
})
t.Run("should error out while fetching permissions info from GoCD due to missing headers", func(t *testing.T) {
server := mockServer([]byte(permissionsJSON), http.StatusOK,
nil, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
expected := gocd.Permission{}
actual, err := client.GetPermissions(nil)
assert.EqualError(t, err, "got 404 from GoCD while making GET call for "+server.URL+
"/api/auth/permissions\nwith BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
assert.Equal(t, expected, actual)
})
t.Run("should error out while fetching permissions info from GoCD as server returned malformed response", func(t *testing.T) {
server := mockServer([]byte("permissionsJSON"), http.StatusOK, correctPermissionHeader,
false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
expected := gocd.Permission{}
actual, err := client.GetPermissions(nil)
assert.EqualError(t, err, "reading response body errored with: invalid character 'p' looking for beginning of value")
assert.Equal(t, expected, actual)
})
t.Run("should error out while fetching permissions info from GoCD as server is not reachable", func(t *testing.T) {
client := gocd.NewClient("http://localhost:8156/go", auth, "info", nil)
client.SetRetryCount(1)
client.SetRetryWaitTime(1)
expected := gocd.Permission{}
actual, err := client.GetPermissions(nil)
assert.EqualError(t, err, "call made to get permissions errored with: "+
"Get \"http://localhost:8156/go/api/auth/permissions\": dial tcp [::1]:8156: connect: connection refused")
assert.Equal(t, expected, actual)
})
}