Repository navigation
Expand file tree
/
Copy pathaccount_test.go
More file actions
331 lines (283 loc) · 12.9 KB
/
Copy pathaccount_test.go
File metadata and controls
331 lines (283 loc) · 12.9 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Copyright (C) 2025 Nethesis S.r.l.
* http://www.nethesis.it - info@nethesis.it
*
* SPDX-License-Identifier: GPL-2.0-only
*
* author: Giacomo Sanchietti <giacomo.sanchietti@nethesis.it>
*/
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
// TestGetSSHKeys tests retrieving SSH keys for a user.
func TestGetSSHKeys(t *testing.T) {
gin.SetMode(gin.TestMode)
router := setupRouter()
// First, login to get a valid token
var loginResp map[string]interface{}
w := httptest.NewRecorder()
loginBody := `{"username": "admin", "password": "admin"}`
req, _ := http.NewRequest("POST", "/login", bytes.NewBuffer([]byte(loginBody)))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "login should succeed")
json.NewDecoder(w.Body).Decode(&loginResp)
assert.NotNil(t, loginResp["token"], "token should be present in login response")
token := loginResp["token"].(string)
// Call GET /accounts/ssh-keys
w = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/accounts/ssh-keys", nil)
req.Header.Set("Authorization", "Bearer "+token)
router.ServeHTTP(w, req)
// Should return 200 OK (even if no keys exist yet)
assert.Equal(t, http.StatusOK, w.Code, "GetSSHKeys should return 200 OK")
var resp map[string]interface{}
json.NewDecoder(w.Body).Decode(&resp)
assert.Equal(t, float64(200), resp["code"], "response code should be 200")
assert.Equal(t, "success", resp["message"], "response message should be 'success'")
}
// TestAddSSHKeys tests generating a new SSH key pair.
func TestAddSSHKeys(t *testing.T) {
gin.SetMode(gin.TestMode)
router := setupRouter()
// First, login to get a valid token
var loginResp map[string]interface{}
w := httptest.NewRecorder()
loginBody := `{"username": "admin", "password": "admin"}`
req, _ := http.NewRequest("POST", "/login", bytes.NewBuffer([]byte(loginBody)))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "login should succeed")
json.NewDecoder(w.Body).Decode(&loginResp)
token := loginResp["token"].(string)
// Call POST /accounts/ssh-keys with passphrase
w = httptest.NewRecorder()
sshGenBody := `{"passphrase": "test-passphrase"}`
req, _ = http.NewRequest("POST", "/accounts/ssh-keys", bytes.NewBuffer([]byte(sshGenBody)))
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
// SSH key generation might fail if ssh-keygen is not available in test environment
// Only assert on successful response
if w.Code == http.StatusOK {
var resp map[string]interface{}
json.NewDecoder(w.Body).Decode(&resp)
assert.Equal(t, float64(200), resp["code"], "response code should be 200")
assert.Equal(t, "success", resp["message"], "response message should be 'success'")
data := resp["data"].(map[string]interface{})
assert.NotEmpty(t, data["key_pub"], "key_pub should not be empty")
assert.True(t, strings.HasSuffix(data["key_pub"].(string), " admin@controller"), "key_pub comment should be admin@controller")
}
}
// TestDeleteSSHKeys tests deleting SSH keys for a user.
func TestDeleteSSHKeys(t *testing.T) {
gin.SetMode(gin.TestMode)
router := setupRouter()
// First, login to get a valid token
var loginResp map[string]interface{}
w := httptest.NewRecorder()
loginBody := `{"username": "admin", "password": "admin"}`
req, _ := http.NewRequest("POST", "/login", bytes.NewBuffer([]byte(loginBody)))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "login should succeed")
json.NewDecoder(w.Body).Decode(&loginResp)
token := loginResp["token"].(string)
// Call DELETE /accounts/ssh-keys
w = httptest.NewRecorder()
req, _ = http.NewRequest("DELETE", "/accounts/ssh-keys", nil)
req.Header.Set("Authorization", "Bearer "+token)
router.ServeHTTP(w, req)
// Should return 200 OK
assert.Equal(t, http.StatusOK, w.Code, "DeleteSSHKeys should return 200 OK")
var resp map[string]interface{}
json.NewDecoder(w.Body).Decode(&resp)
assert.Equal(t, float64(200), resp["code"], "response code should be 200")
assert.Equal(t, "success", resp["message"], "response message should be 'success'")
}
// TestSSHKeyValidation tests that SSH key operations handle missing keys gracefully.
func TestSSHKeyValidation(t *testing.T) {
gin.SetMode(gin.TestMode)
router := setupRouter()
// First, login to get a valid token
var loginResp map[string]interface{}
w := httptest.NewRecorder()
loginBody := `{"username": "admin", "password": "admin"}`
req, _ := http.NewRequest("POST", "/login", bytes.NewBuffer([]byte(loginBody)))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "login should succeed")
json.NewDecoder(w.Body).Decode(&loginResp)
token := loginResp["token"].(string)
// Delete any existing keys first
w = httptest.NewRecorder()
req, _ = http.NewRequest("DELETE", "/accounts/ssh-keys", nil)
req.Header.Set("Authorization", "Bearer "+token)
router.ServeHTTP(w, req)
// Now try to get keys (should return empty without error)
w = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/accounts/ssh-keys", nil)
req.Header.Set("Authorization", "Bearer "+token)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "GetSSHKeys should return 200 OK even with missing keys")
}
// TestUpdatePassword tests updating user password with correct old password.
func TestUpdatePassword(t *testing.T) {
gin.SetMode(gin.TestMode)
router := setupRouter()
// First, login to get a valid token
var loginResp map[string]interface{}
w := httptest.NewRecorder()
loginBody := `{"username": "admin", "password": "admin"}`
req, _ := http.NewRequest("POST", "/login", bytes.NewBuffer([]byte(loginBody)))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "login should succeed")
json.NewDecoder(w.Body).Decode(&loginResp)
token := loginResp["token"].(string)
// Call PUT /accounts/password with old and new password
w = httptest.NewRecorder()
passChangeBody := `{"old_password": "admin", "new_password": "newpassword123"}`
req, _ = http.NewRequest("PUT", "/accounts/password", bytes.NewBuffer([]byte(passChangeBody)))
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
// Should return 200 OK
assert.Equal(t, http.StatusOK, w.Code, "UpdatePassword should return 200 OK with correct old password")
var resp map[string]interface{}
json.NewDecoder(w.Body).Decode(&resp)
assert.Equal(t, float64(200), resp["code"], "response code should be 200")
assert.Equal(t, "success", resp["message"], "response message should be 'success'")
// Reset password back to "admin" for other tests
w = httptest.NewRecorder()
passChangeBody = `{"old_password": "newpassword123", "new_password": "admin"}`
req, _ = http.NewRequest("PUT", "/accounts/password", bytes.NewBuffer([]byte(passChangeBody)))
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
}
// TestPasswordMismatch tests that password update fails with incorrect old password.
func TestPasswordMismatch(t *testing.T) {
gin.SetMode(gin.TestMode)
router := setupRouter()
// First, login to get a valid token
var loginResp map[string]interface{}
w := httptest.NewRecorder()
loginBody := `{"username": "admin", "password": "admin"}`
req, _ := http.NewRequest("POST", "/login", bytes.NewBuffer([]byte(loginBody)))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "login should succeed")
json.NewDecoder(w.Body).Decode(&loginResp)
token := loginResp["token"].(string)
// Call PUT /accounts/password with wrong old password
w = httptest.NewRecorder()
passChangeBody := `{"old_password": "wrongpassword", "new_password": "newpassword123"}`
req, _ = http.NewRequest("PUT", "/accounts/password", bytes.NewBuffer([]byte(passChangeBody)))
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
// Should return 400 Bad Request
assert.Equal(t, http.StatusBadRequest, w.Code, "UpdatePassword should return 400 with incorrect old password")
var resp map[string]interface{}
json.NewDecoder(w.Body).Decode(&resp)
assert.Equal(t, float64(400), resp["code"], "response code should be 400")
assert.Contains(t, resp["message"].(string), "mismatch", "response should indicate password mismatch")
}
// TestGetAccountsAuthorizationForbidden tests that non-admin users cannot access /accounts endpoint.
func TestGetAccountsAuthorizationForbidden(t *testing.T) {
gin.SetMode(gin.TestMode)
router := setupRouter()
// Create and login with limited user
// First, login as admin to create a limited user account
var adminLoginResp map[string]interface{}
w := httptest.NewRecorder()
adminLoginBody := `{"username": "admin", "password": "admin"}`
req, _ := http.NewRequest("POST", "/login", bytes.NewBuffer([]byte(adminLoginBody)))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "admin login should succeed")
json.NewDecoder(w.Body).Decode(&adminLoginResp)
adminToken := adminLoginResp["token"].(string)
// Create a limited user account
w = httptest.NewRecorder()
addBody := `{"username": "limiteduser", "password": "limited", "display_name": "Limited User", "admin": false}`
req, _ = http.NewRequest("POST", "/accounts", bytes.NewBuffer([]byte(addBody)))
req.Header.Set("Authorization", "Bearer "+adminToken)
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
// Now login as the limited user
var limitedLoginResp map[string]interface{}
w = httptest.NewRecorder()
limitedLoginBody := `{"username": "limiteduser", "password": "limited"}`
req, _ = http.NewRequest("POST", "/login", bytes.NewBuffer([]byte(limitedLoginBody)))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "limited user login should succeed")
json.NewDecoder(w.Body).Decode(&limitedLoginResp)
limitedToken := limitedLoginResp["token"].(string)
// Try to access /accounts endpoint as limited user
w = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/accounts", nil)
req.Header.Set("Authorization", "Bearer "+limitedToken)
router.ServeHTTP(w, req)
// Should return 403 Forbidden
assert.Equal(t, http.StatusForbidden, w.Code, "non-admin user should not access /accounts endpoint")
var resp map[string]interface{}
json.NewDecoder(w.Body).Decode(&resp)
assert.Equal(t, float64(403), resp["code"], "response code should be 403")
}
// TestAddSSHKeysInvalidRequest tests that AddSSHKeys rejects malformed requests.
func TestAddSSHKeysInvalidRequest(t *testing.T) {
gin.SetMode(gin.TestMode)
router := setupRouter()
// First, login to get a valid token
var loginResp map[string]interface{}
w := httptest.NewRecorder()
loginBody := `{"username": "admin", "password": "admin"}`
req, _ := http.NewRequest("POST", "/login", bytes.NewBuffer([]byte(loginBody)))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "login should succeed")
json.NewDecoder(w.Body).Decode(&loginResp)
token := loginResp["token"].(string)
// Call POST /accounts/ssh-keys with malformed JSON
w = httptest.NewRecorder()
req, _ = http.NewRequest("POST", "/accounts/ssh-keys", bytes.NewBuffer([]byte("invalid json")))
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
// Should return 400 Bad Request
assert.Equal(t, http.StatusBadRequest, w.Code, "AddSSHKeys should reject malformed JSON")
}
// TestUpdatePasswordInvalidRequest tests that UpdatePassword rejects malformed requests.
func TestUpdatePasswordInvalidRequest(t *testing.T) {
gin.SetMode(gin.TestMode)
router := setupRouter()
// First, login to get a valid token
var loginResp map[string]interface{}
w := httptest.NewRecorder()
loginBody := `{"username": "admin", "password": "admin"}`
req, _ := http.NewRequest("POST", "/login", bytes.NewBuffer([]byte(loginBody)))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "login should succeed")
json.NewDecoder(w.Body).Decode(&loginResp)
token := loginResp["token"].(string)
// Call PUT /accounts/password with malformed JSON
w = httptest.NewRecorder()
req, _ = http.NewRequest("PUT", "/accounts/password", bytes.NewBuffer([]byte("invalid json")))
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
// Should return 400 Bad Request
assert.Equal(t, http.StatusBadRequest, w.Code, "UpdatePassword should reject malformed JSON")
}