feat: add device management and queue management routes
- Add /devices route for device management interface - Add /conflicts route for sync conflict resolution - Add /queue route for sync queue management - Add comprehensive tests for device cap management - Add test suite for queue management
This commit is contained in:
@@ -288,3 +288,102 @@ func TestDeviceAuthentication(t *testing.T) {
|
||||
// Device auth is for sync endpoints only
|
||||
assert.Equal(t, http.StatusUnauthorized, rec.Code, "Should require user auth for device management")
|
||||
}
|
||||
|
||||
func TestListPendingRegistrations(t *testing.T) {
|
||||
ts, db, _, _ := setupTestServer(t)
|
||||
defer ts.Close()
|
||||
|
||||
token := loginTestUser(t, ts, db)
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/devices/pending", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
rec := httptest.NewRecorder()
|
||||
ts.Config.Handler.ServeHTTP(rec, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, rec.Code, "Should list pending registrations")
|
||||
|
||||
var response map[string]interface{}
|
||||
json.Unmarshal(rec.Body.Bytes(), &response)
|
||||
|
||||
pending, ok := response["pending_registrations"].([]interface{})
|
||||
assert.True(t, ok, "Should have pending_registrations array")
|
||||
assert.NotNil(t, pending, "Pending registrations should not be nil")
|
||||
}
|
||||
|
||||
func TestApproveDeviceRegistration(t *testing.T) {
|
||||
ts, db, _, _ := setupTestServer(t)
|
||||
defer ts.Close()
|
||||
|
||||
token := loginTestUser(t, ts, db)
|
||||
|
||||
regRequest := map[string]interface{}{
|
||||
"device_name": "Test Device for Approval",
|
||||
"device_type": "koreader",
|
||||
"device_identifier": "test-approval-12345",
|
||||
}
|
||||
regBody, _ := json.Marshal(regRequest)
|
||||
|
||||
req := httptest.NewRequest("POST", "/api/devices/register", bytes.NewReader(regBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rec := httptest.NewRecorder()
|
||||
ts.Config.Handler.ServeHTTP(rec, req)
|
||||
|
||||
assert.Equal(t, http.StatusCreated, rec.Code, "Should initiate device registration")
|
||||
|
||||
var regResponse map[string]interface{}
|
||||
json.Unmarshal(rec.Body.Bytes(), ®Response)
|
||||
|
||||
registrationID, ok := regResponse["registration_id"].(string)
|
||||
assert.True(t, ok, "Should have registration_id")
|
||||
|
||||
req = httptest.NewRequest("GET", fmt.Sprintf("/api/devices/approve/%s", registrationID), nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
rec = httptest.NewRecorder()
|
||||
ts.Config.Handler.ServeHTTP(rec, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, rec.Code, "Should approve device registration")
|
||||
|
||||
var approveResponse map[string]interface{}
|
||||
json.Unmarshal(rec.Body.Bytes(), &approveResponse)
|
||||
|
||||
assert.True(t, approveResponse["approved"].(bool), "Should confirm approval")
|
||||
}
|
||||
|
||||
func TestRejectDeviceRegistration(t *testing.T) {
|
||||
ts, db, _, _ := setupTestServer(t)
|
||||
defer ts.Close()
|
||||
|
||||
token := loginTestUser(t, ts, db)
|
||||
|
||||
regRequest := map[string]interface{}{
|
||||
"device_name": "Test Device for Rejection",
|
||||
"device_type": "koreader",
|
||||
"device_identifier": "test-rejection-12345",
|
||||
}
|
||||
regBody, _ := json.Marshal(regRequest)
|
||||
|
||||
req := httptest.NewRequest("POST", "/api/devices/register", bytes.NewReader(regBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rec := httptest.NewRecorder()
|
||||
ts.Config.Handler.ServeHTTP(rec, req)
|
||||
|
||||
assert.Equal(t, http.StatusCreated, rec.Code, "Should initiate device registration")
|
||||
|
||||
var regResponse map[string]interface{}
|
||||
json.Unmarshal(rec.Body.Bytes(), ®Response)
|
||||
|
||||
registrationID, ok := regResponse["registration_id"].(string)
|
||||
assert.True(t, ok, "Should have registration_id")
|
||||
|
||||
req = httptest.NewRequest("POST", fmt.Sprintf("/api/devices/reject/%s", registrationID), nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
rec = httptest.NewRecorder()
|
||||
ts.Config.Handler.ServeHTTP(rec, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, rec.Code, "Should reject device registration")
|
||||
|
||||
var rejectResponse map[string]interface{}
|
||||
json.Unmarshal(rec.Body.Bytes(), &rejectResponse)
|
||||
|
||||
assert.True(t, rejectResponse["rejected"].(bool), "Should confirm rejection")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user