- Add folder to library before scanning in fsnotify integration test - Update API endpoint paths from /items to /media-items - Refactor test server setup to support WebSocket hijacking - Add JobsHandler to test server configuration - Implement proper job status polling instead of fixed delays - Consolidate addFolderToLibrary helper into test_helpers.go - Remove duplicate helper function from media_item_isbn_test.go - Add error logging for search test failures - Improve test robustness with better nil handling and type assertions - Update worker test to use EnqueueJob and poll for completion - Add global worker instance reset in test cleanup - Fix media_scanner_test to initialize folders before testing
186 lines
5.8 KiB
Go
186 lines
5.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestScanSettings_GetSettings(t *testing.T) {
|
|
setup := setupTestServer(t)
|
|
defer setup.Close()
|
|
|
|
t.Run("Get settings as admin", func(t *testing.T) {
|
|
token := setup.Token
|
|
|
|
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/libraries/scan-settings", nil)
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
var response map[string]interface{}
|
|
json.NewDecoder(resp.Body).Decode(&response)
|
|
resp.Body.Close()
|
|
|
|
assert.Contains(t, response, "scan_poll_interval_seconds")
|
|
assert.Contains(t, response, "auto_scan_enabled")
|
|
// Validate scan_poll_interval_seconds is present and valid (don't hardcode value)
|
|
interval, ok := response["scan_poll_interval_seconds"].(float64)
|
|
require.True(t, ok, "scan_poll_interval_seconds must be a number")
|
|
assert.Greater(t, interval, float64(0), "scan_poll_interval_seconds must be positive")
|
|
// Validate auto_scan_enabled is present and a boolean
|
|
autoScan, ok := response["auto_scan_enabled"].(bool)
|
|
require.True(t, ok, "auto_scan_enabled must be a boolean")
|
|
assert.NotNil(t, autoScan, "auto_scan_enabled must not be nil")
|
|
})
|
|
}
|
|
|
|
func TestScanSettings_UpdateSettings(t *testing.T) {
|
|
setup := setupTestServer(t)
|
|
defer setup.Close()
|
|
|
|
t.Run("Update scan_poll_interval_seconds", func(t *testing.T) {
|
|
token := setup.Token
|
|
|
|
updateReq := map[string]interface{}{
|
|
"scan_poll_interval_seconds": 45,
|
|
"auto_scan_enabled": true,
|
|
}
|
|
body, _ := json.Marshal(updateReq)
|
|
|
|
req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/libraries/scan-settings", bytes.NewBuffer(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
var response map[string]interface{}
|
|
json.NewDecoder(resp.Body).Decode(&response)
|
|
resp.Body.Close()
|
|
|
|
assert.Equal(t, float64(45), response["scan_poll_interval_seconds"])
|
|
assert.Equal(t, true, response["auto_scan_enabled"])
|
|
|
|
getReq, _ := http.NewRequest("GET", setup.Server.URL+"/api/libraries/scan-settings", nil)
|
|
getReq.Header.Set("Authorization", "Bearer "+token)
|
|
getResp, _ := client.Do(getReq)
|
|
|
|
var getResponse map[string]interface{}
|
|
json.NewDecoder(getResp.Body).Decode(&getResponse)
|
|
getResp.Body.Close()
|
|
|
|
assert.Equal(t, float64(45), getResponse["scan_poll_interval_seconds"])
|
|
})
|
|
|
|
t.Run("Update auto_scan_enabled to false", func(t *testing.T) {
|
|
token := setup.Token
|
|
|
|
updateReq := map[string]interface{}{
|
|
"scan_poll_interval_seconds": 30,
|
|
"auto_scan_enabled": false,
|
|
}
|
|
body, _ := json.Marshal(updateReq)
|
|
|
|
req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/libraries/scan-settings", bytes.NewBuffer(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
var response map[string]interface{}
|
|
json.NewDecoder(resp.Body).Decode(&response)
|
|
resp.Body.Close()
|
|
|
|
assert.Equal(t, false, response["auto_scan_enabled"])
|
|
})
|
|
|
|
t.Run("Validation rejects values below minimum", func(t *testing.T) {
|
|
token := setup.Token
|
|
|
|
updateReq := map[string]interface{}{
|
|
"scan_poll_interval_seconds": 0,
|
|
"auto_scan_enabled": true,
|
|
}
|
|
body, _ := json.Marshal(updateReq)
|
|
|
|
req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/libraries/scan-settings", bytes.NewBuffer(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
|
|
})
|
|
|
|
t.Run("Validation rejects values above maximum", func(t *testing.T) {
|
|
token := setup.Token
|
|
|
|
updateReq := map[string]interface{}{
|
|
"scan_poll_interval_seconds": 4000,
|
|
"auto_scan_enabled": true,
|
|
}
|
|
body, _ := json.Marshal(updateReq)
|
|
|
|
req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/libraries/scan-settings", bytes.NewBuffer(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
|
|
})
|
|
}
|
|
|
|
func TestScanSettings_RequireAdmin(t *testing.T) {
|
|
setup := setupTestServer(t)
|
|
defer setup.Close()
|
|
|
|
t.Run("Get settings without auth returns 401", func(t *testing.T) {
|
|
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/libraries/scan-settings", nil)
|
|
client := &http.Client{}
|
|
resp, _ := client.Do(req)
|
|
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
|
})
|
|
|
|
t.Run("Get settings as non-admin returns 403", func(t *testing.T) {
|
|
token := setup.RegularToken
|
|
|
|
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/libraries/scan-settings", nil)
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
|
|
client := &http.Client{}
|
|
resp, _ := client.Do(req)
|
|
assert.Equal(t, http.StatusForbidden, resp.StatusCode)
|
|
})
|
|
|
|
t.Run("Update settings without auth returns 401", func(t *testing.T) {
|
|
updateReq := map[string]interface{}{
|
|
"scan_poll_interval_seconds": 30,
|
|
"auto_scan_enabled": true,
|
|
}
|
|
body, _ := json.Marshal(updateReq)
|
|
|
|
req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/libraries/scan-settings", bytes.NewBuffer(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
client := &http.Client{}
|
|
resp, _ := client.Do(req)
|
|
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
|
})
|
|
}
|