-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathauthorize_test.go
More file actions
74 lines (69 loc) · 2 KB
/
authorize_test.go
File metadata and controls
74 lines (69 loc) · 2 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
package confluent
import (
"errors"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
var (
testPrincipals = []UserPrincipalAction{
{
Scope: Scope{
Clusters: AuthorClusters{
KafkaCluster: clusterId,
},
},
ResourceName: "Testing-Principal",
ResourceType: "Cluster",
Operation: "DefaultSaramaClusterAdmin",
},
}
)
func TestAuthorize_CreatePrincipalSuccess(t *testing.T) {
mock := MockHttpClient{}
mk := MockKafkaClient{}
mock.DoRequestFn = func(method string, uri string, reqBody io.Reader) (responseBody []byte, statusCode int, status string, err error) {
assert.Equal(t, http.MethodPut, method, "Expected method 'PUT', got %s", method)
assert.Equal(t, "/security/1.0/authorize", uri)
return []byte(`
[
"ALLOWED",
"DENIED"
]
`), 200, "200", nil
}
clusterAdmin, _ := mk.NewSaramaClusterAdmin()
c := NewClient(&mock, &mk, clusterAdmin)
newPrincipal, err := c.CreatePrincipal("User:testing", testPrincipals)
assert.NoError(t, err)
assert.Equal(t, "Testing-Principal", newPrincipal.Actions[0].ResourceName)
}
func TestAuthorize_CreatePrincipalFail(t *testing.T) {
mock := MockHttpClient{}
mk := MockKafkaClient{}
mock.DoRequestFn = func(method string, uri string, reqBody io.Reader) (responseBody []byte, statusCode int, status string, err error) {
assert.Equal(t, http.MethodPut, method, "Expected method 'PUT', got %s", method)
assert.Equal(t, "/security/1.0/authorize", uri)
return []byte(`
{
"status_code": 400,
"error_code": 0,
"type": "INVALID REQUEST DATA",
"message": "Bad request",
"errors": [
{
"error_type": "string",
"message": "INVALID REQUEST DATA"
}
]
}
`), 400, "400 Bad Request", nil
}
clusterAdmin, _ := mk.NewSaramaClusterAdmin()
c := NewClient(&mock, &mk, clusterAdmin)
newPrincipal, err := c.CreatePrincipal("User:testing", testPrincipals)
assert.NotNil(t, err)
assert.Nil(t, newPrincipal)
assert.Equal(t, errors.New("error with status: 400 Bad Request INVALID REQUEST DATA"), err)
}