fix: Fix device response fields and add missing approved confirmation

feat: Improve device test infrastructure with setupDeviceTest helper

refactor: Standardize pending registrations API response field names
This commit is contained in:
2026-02-10 09:31:35 -05:00
parent 36e03f89b6
commit 1413c75b26
3 changed files with 169 additions and 87 deletions
+23 -83
View File
@@ -68,7 +68,7 @@ func TestDeviceRegistrationFlow(t *testing.T) {
// Step 3: Login as user to approve device
loginRequest := map[string]interface{}{
"login": "testuser@example.com",
"password": "testpass123",
"password": "Test@Pass123!",
}
loginBody, _ := json.Marshal(loginRequest)
@@ -115,34 +115,17 @@ func TestDeviceRegistrationFlow(t *testing.T) {
}
func TestListDevices(t *testing.T) {
ts, db, _ := setupTestServer(t)
defer ts.Close()
setup := setupDeviceTest(t)
defer setup.Server.Close()
// Login to get token
token := loginTestUser(t, ts, db)
// Create a device directly in the database
userID := getTestUserID(t, db)
deviceToken := fmt.Sprintf("dev_%s", uuid.New().String())
_, err := db.CreateDevice(context.Background(), database.CreateDeviceParams{
UserID: pgtype.UUID{Bytes: [16]byte(userID), Valid: true},
DeviceName: "Test Device",
DeviceType: "koreader",
DeviceIdentifier: "test-device-123",
AuthToken: deviceToken,
SyncEnabled: pgtype.Bool{Bool: true, Valid: true},
AutoSync: pgtype.Bool{Bool: true, Valid: true},
SyncFrequencyMinutes: pgtype.Int4{Int32: 5, Valid: true},
DeviceMetadata: []byte("{}"),
})
assert.NoError(t, err, "Should create device")
// Create a device using the setup helper
_ = setup.CreateDevice(t, "Test Device", "koreader", "test-device-123")
// List devices
req := httptest.NewRequest("GET", "/api/devices", nil)
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Authorization", "Bearer "+setup.UserToken)
rec := httptest.NewRecorder()
ts.Config.Handler.ServeHTTP(rec, req)
setup.Server.Config.Handler.ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code, "Should list devices")
@@ -160,33 +143,11 @@ func TestListDevices(t *testing.T) {
}
func TestUpdateDevice(t *testing.T) {
ts, db, _ := setupTestServer(t)
defer ts.Close()
setup := setupDeviceTest(t)
defer setup.Server.Close()
// Login to get token
token := loginTestUser(t, ts, db)
// Create a device directly in the database
userID := getTestUserID(t, db)
deviceToken := fmt.Sprintf("dev_%s", uuid.New().String())
device, err := db.CreateDevice(context.Background(), database.CreateDeviceParams{
UserID: pgtype.UUID{Bytes: [16]byte(userID), Valid: true},
DeviceName: "Test Device",
DeviceType: "koreader",
DeviceIdentifier: "test-device-123",
AuthToken: deviceToken,
SyncEnabled: pgtype.Bool{Bool: true, Valid: true},
AutoSync: pgtype.Bool{Bool: true, Valid: true},
SyncFrequencyMinutes: pgtype.Int4{Int32: 5, Valid: true},
DeviceMetadata: []byte("{}"),
})
assert.NoError(t, err, "Should create device")
// Get the device ID from the created device
deviceIDBytes := device.ID.Bytes[0:16]
deviceID, err := uuid.FromBytes(deviceIDBytes)
assert.NoError(t, err, "Should parse device ID")
// Create a device using the setup helper
device := setup.CreateDevice(t, "Test Device", "koreader", "test-device-123")
// Update device
updateRequest := map[string]interface{}{
@@ -196,11 +157,11 @@ func TestUpdateDevice(t *testing.T) {
}
updateBody, _ := json.Marshal(updateRequest)
req := httptest.NewRequest("PUT", fmt.Sprintf("/api/devices/%s", deviceID.String()), bytes.NewReader(updateBody))
req.Header.Set("Authorization", "Bearer "+token)
req := httptest.NewRequest("PUT", fmt.Sprintf("/api/devices/%s", device.ID.String()), bytes.NewReader(updateBody))
req.Header.Set("Authorization", "Bearer "+setup.UserToken)
req.Header.Set("Content-Type", "application/json")
rec := httptest.NewRecorder()
ts.Config.Handler.ServeHTTP(rec, req)
setup.Server.Config.Handler.ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code, "Should update device")
@@ -216,44 +177,23 @@ func TestUpdateDevice(t *testing.T) {
}
func TestDeleteDevice(t *testing.T) {
ts, db, _ := setupTestServer(t)
defer ts.Close()
setup := setupDeviceTest(t)
defer setup.Server.Close()
// Login to get token
token := loginTestUser(t, ts, db)
// Create a device directly in the database
userID := getTestUserID(t, db)
deviceToken := fmt.Sprintf("dev_%s", uuid.New().String())
newDevice, err := db.CreateDevice(context.Background(), database.CreateDeviceParams{
UserID: pgtype.UUID{Bytes: [16]byte(userID), Valid: true},
DeviceName: "Test Device",
DeviceType: "koreader",
DeviceIdentifier: "test-device-123",
AuthToken: deviceToken,
SyncEnabled: pgtype.Bool{Bool: true, Valid: true},
AutoSync: pgtype.Bool{Bool: true, Valid: true},
SyncFrequencyMinutes: pgtype.Int4{Int32: 5, Valid: true},
DeviceMetadata: []byte("{}"),
})
assert.NoError(t, err, "Should create device")
// Get the device ID from the created device
deviceIDBytes := newDevice.ID.Bytes[0:16]
deviceID, err := uuid.FromBytes(deviceIDBytes)
assert.NoError(t, err, "Should parse device ID")
// Create a device using the setup helper
device := setup.CreateDevice(t, "Test Device", "koreader", "test-device-123")
// Delete device
req := httptest.NewRequest("DELETE", fmt.Sprintf("/api/devices/%s", deviceID.String()), nil)
req.Header.Set("Authorization", "Bearer "+token)
req := httptest.NewRequest("DELETE", fmt.Sprintf("/api/devices/%s", device.ID.String()), nil)
req.Header.Set("Authorization", "Bearer "+setup.UserToken)
rec := httptest.NewRecorder()
ts.Config.Handler.ServeHTTP(rec, req)
setup.Server.Config.Handler.ServeHTTP(rec, req)
assert.Equal(t, http.StatusNoContent, rec.Code, "Should delete device")
// Verify device is deleted
_, err = db.GetDevice(context.Background(), newDevice.ID)
pgDeviceID := pgtype.UUID{Bytes: [16]byte(device.ID), Valid: true}
_, err := setup.DB.GetDevice(context.Background(), pgDeviceID)
assert.Error(t, err, "Device should be deleted")
}