-
Notifications
You must be signed in to change notification settings - Fork 7
/
rest_sshuser_test.go
188 lines (168 loc) · 5.99 KB
/
rest_sshuser_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package ne
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"github.com/equinix/ne-go/internal/api"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
func TestSSHUserGet(t *testing.T) {
//given
resp := api.SSHUser{}
if err := readJSONData("./test-fixtures/ne_sshuser_get_resp.json", &resp); err != nil {
assert.Failf(t, "Cannot read test response due to %s", err.Error())
}
userID := "myTestUser"
testHc := setupMockedClient("GET", fmt.Sprintf("%s/ne/v1/sshUsers/%s", baseURL, userID), 200, resp)
defer httpmock.DeactivateAndReset()
//when
c := NewClient(context.Background(), baseURL, testHc)
user, err := c.GetSSHUser(userID)
//then
assert.NotNil(t, user, "Returned user is not nil")
assert.Nil(t, err, "Error is not returned")
verifyUser(t, *user, resp)
}
func TestSSHUsersGet(t *testing.T) {
//Given
var respBody api.SSHUsersResponse
if err := readJSONData("./test-fixtures/ne_sshusers_get.json", &respBody); err != nil {
assert.Failf(t, "cannot read test response due to %s", err.Error())
}
limit := respBody.Pagination.Limit
testHc := &http.Client{}
httpmock.ActivateNonDefault(testHc)
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/ne/v1/sshUsers?limit=%d&verbose=true", baseURL, limit),
func(r *http.Request) (*http.Response, error) {
resp, _ := httpmock.NewJsonResponse(200, respBody)
return resp, nil
},
)
defer httpmock.DeactivateAndReset()
//When
c := NewClient(context.Background(), baseURL, testHc)
c.PageSize = limit
users, err := c.GetSSHUsers()
//Then
assert.Nil(t, err, "Client should not return an error")
assert.NotNil(t, users, "Client should return a response")
assert.Equal(t, len(respBody.Data), len(users))
for i := range users {
verifyUser(t, users[i], respBody.Data[i])
}
}
func TestSSHUserCreate(t *testing.T) {
//given
newUUID := "46eb8aac-a4b9-47af-aa2b-cccfb83ee3d1"
user := SSHUser{
Username: String("myUser"),
Password: String("myPassword"),
DeviceUUIDs: []string{"deviceOne"},
}
req := api.SSHUserRequest{}
testHc := &http.Client{}
httpmock.ActivateNonDefault(testHc)
httpmock.RegisterResponder("POST", fmt.Sprintf("%s/ne/v1/sshUsers", baseURL),
func(r *http.Request) (*http.Response, error) {
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return httpmock.NewStringResponse(400, ""), nil
}
resp := httpmock.NewStringResponse(201, "")
resp.Header.Add("Location", "/ne/v1/sshUsers/"+newUUID)
return resp, nil
},
)
defer httpmock.DeactivateAndReset()
//when
c := NewClient(context.Background(), baseURL, testHc)
uuid, err := c.CreateSSHUser(*user.Username, *user.Password, user.DeviceUUIDs[0])
//then
assert.Nil(t, err, "Error is not returned")
assert.Equal(t, newUUID, *uuid, "UUID matches")
verifyUserRequest(t, user, req)
}
func TestSSHUserUpdate(t *testing.T) {
//given
userID := "myTestUser"
newPass := "myNewPassword"
oldDevices := []string{"Dev1", "Dev2"}
newDevices := []string{"Dev3", "Dev4"}
req := api.SSHUserUpdateRequest{}
testHc := &http.Client{}
httpmock.ActivateNonDefault(testHc)
httpmock.RegisterResponder("PUT", fmt.Sprintf("%s/ne/v1/sshUsers/%s", baseURL, userID),
func(r *http.Request) (*http.Response, error) {
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return httpmock.NewStringResponse(400, ""), nil
}
return httpmock.NewStringResponse(201, ""), nil
},
)
removed, added := diffStringSlices(oldDevices, newDevices)
for _, dev := range added {
httpmock.RegisterResponder("POST", fmt.Sprintf("%s/ne/v1/sshUsers/%s/devices/%s", baseURL, userID, dev),
httpmock.NewStringResponder(201, ""))
}
for _, dev := range removed {
httpmock.RegisterResponder("DELETE", fmt.Sprintf("%s/ne/v1/sshUsers/%s/devices/%s", baseURL, userID, dev),
httpmock.NewStringResponder(200, ""))
}
defer httpmock.DeactivateAndReset()
//when
c := NewClient(context.Background(), baseURL, testHc)
updateReq := c.NewSSHUserUpdateRequest(userID).
WithNewPassword(newPass).
WithDeviceChange(oldDevices, newDevices)
err := updateReq.Execute()
//then
assert.Nil(t, err, "Error is not returned")
verifyUserUpdateRequest(t, updateReq.(*restSSHUserUpdateRequest), req)
for p, c := range httpmock.GetCallCountInfo() {
assert.Equal(t, 1, c, "One request received on mock responder %s", p)
}
}
func TestSSHUserDelete(t *testing.T) {
//given
userID := "myTestUser"
userResp := api.SSHUser{
UUID: String(userID),
Username: String("user"),
DeviceUUIDs: []string{"dev1", "dev2", "dev3"}}
testHc := &http.Client{}
httpmock.ActivateNonDefault(testHc)
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/ne/v1/sshUsers/%s", baseURL, userID),
func(r *http.Request) (*http.Response, error) {
resp, _ := httpmock.NewJsonResponse(200, userResp)
return resp, nil
},
)
for _, dev := range userResp.DeviceUUIDs {
httpmock.RegisterResponder("DELETE", fmt.Sprintf("%s/ne/v1/sshUsers/%s/devices/%s", baseURL, userID, dev),
httpmock.NewStringResponder(200, ""))
}
defer httpmock.DeactivateAndReset()
//when
c := NewClient(context.Background(), baseURL, testHc)
err := c.DeleteSSHUser(userID)
//then
assert.Nil(t, err, "Error is not returned")
for p, c := range httpmock.GetCallCountInfo() {
assert.Equal(t, 1, c, "One request received on mock responder %s", p)
}
}
func verifyUser(t *testing.T, user SSHUser, resp api.SSHUser) {
assert.Equal(t, resp.UUID, user.UUID, "UUID matches")
assert.Equal(t, resp.Username, user.Username, "Username matches")
assert.ElementsMatch(t, resp.DeviceUUIDs, user.DeviceUUIDs, "DeviceUUIDs match")
}
func verifyUserRequest(t *testing.T, user SSHUser, req api.SSHUserRequest) {
assert.Equal(t, user.Username, req.Username, "Username matches")
assert.Equal(t, user.Password, req.Password, "Password matches")
assert.Equal(t, user.DeviceUUIDs[0], StringValue(req.DeviceUUID), "First DeviceUUID matches")
}
func verifyUserUpdateRequest(t *testing.T, updateReq *restSSHUserUpdateRequest, req api.SSHUserUpdateRequest) {
assert.Equal(t, updateReq.newPassword, StringValue(req.Password), "Password matches")
}