forked from wavefrontHQ/go-wavefront-management-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_test.go
148 lines (130 loc) · 3.23 KB
/
user_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
package wavefront
import (
"io"
"net/http"
"net/url"
"testing"
asserts "github.com/stretchr/testify/assert"
)
type MockUserClient struct {
Client
T *testing.T
}
type MockCrudUserClient struct {
Client
T *testing.T
method string
}
func (m *MockUserClient) Do(req *http.Request) (io.ReadCloser, error) {
return testDo(m.T, req, "./fixtures/search-user-response.json", "POST", &SearchParams{})
}
func TestUsers_Find(t *testing.T) {
baseurl, _ := url.Parse("http://testing.wavefront.com")
u := &Users{
client: &MockUserClient{
Client: Client{
Config: &Config{Token: "1234-5678-9977"},
BaseURL: baseurl,
httpClient: http.DefaultClient,
debug: true,
},
T: t,
},
}
users, err := u.Find(nil)
if err != nil {
t.Fatal(err)
}
assertEqual(t, 2, len(users))
assertEqual(t, "[email protected]", *users[0].ID)
permissions := []string{
AGENT_MANAGEMENT,
ALERTS_MANAGEMENT,
DASHBOARD_MANAGEMENT,
EMBEDDED_CHARTS_MANAGEMENT,
EVENTS_MANAGEMENT,
EXTERNAL_LINKS_MANAGEMENT,
HOST_TAG_MANAGEMENT,
METRICS_MANAGEMENT,
USER_MANAGEMENT,
INTEGRATIONS_MANAGEMENT,
DIRECT_INGESTION,
BATCH_QUERY_PRIORITY,
DERIVED_METRICS_MANAGEMENT,
}
foundPermission := false
for _, v := range permissions {
for _, p := range (*users[1]).Permissions {
if v == p {
foundPermission = true
break
}
}
if !foundPermission {
t.Errorf("expected to find %s permission on user", v)
}
}
}
func (m *MockCrudUserClient) Do(req *http.Request) (io.ReadCloser, error) {
return testDo(m.T, req, "./fixtures/crud-user-response.json", m.method, &User{})
}
func TestUsers_NilID(t *testing.T) {
assert := asserts.New(t)
baseurl, _ := url.Parse("http://testing.wavefront.com")
u := &Users{
client: &MockCrudUserClient{
Client: Client{
Config: &Config{Token: "1234-5678-9977"},
BaseURL: baseurl,
httpClient: http.DefaultClient,
debug: true,
},
T: t,
method: "PUT",
},
}
var user User
assert.Error(u.Update(&user))
u.client.(*MockCrudUserClient).method = "GET"
assert.Error(u.Get(&user))
u.client.(*MockCrudUserClient).method = "DELETE"
assert.Error(u.Delete(&user))
}
func TestUsers_CreateUpdateDelete(t *testing.T) {
baseurl, _ := url.Parse("http://testing.wavefront.com")
u := &Users{
client: &MockCrudUserClient{
Client: Client{
Config: &Config{Token: "1234-5678-9977"},
BaseURL: baseurl,
httpClient: http.DefaultClient,
debug: true,
},
T: t,
method: "POST",
},
}
emailAddress := "[email protected]"
newUser := &NewUserRequest{
Permissions: []string{},
Groups: UserGroupsWrapper{},
}
user := &User{
Customer: "test",
Permissions: []string{DERIVED_METRICS_MANAGEMENT},
Groups: UserGroupsWrapper{},
}
if err := u.Create(newUser, user, true); err == nil {
t.Errorf("expected error user mising emailAddress")
}
newUser.EmailAddress = emailAddress
var _ = u.Create(newUser, user, true)
assertEqual(t, emailAddress, *user.ID)
u.client.(*MockCrudUserClient).method = "PUT"
user.ID = &emailAddress
var _ = u.Update(user)
assertEqual(t, 1, len(user.Permissions))
u.client.(*MockCrudUserClient).method = "DELETE"
var _ = u.Delete(user)
assertEqual(t, "", *user.ID)
}