Files
software-station/internal/api/handlers_test.go
Sudo-Ivan d8748bba77
All checks were successful
CI / build (push) Successful in 1m8s
renovate / renovate (push) Successful in 1m42s
Update asset caching and documentation features
- Updated the API server to support asset caching with a new flag for enabling/disabling caching.
- Implemented asset caching logic in the DownloadProxyHandler to store and retrieve assets efficiently.
- Added tests for asset caching functionality, ensuring proper behavior for cache hits and misses.
- Introduced new documentation files for software, including multi-language support.
- Enhanced the SoftwareCard component to display documentation links for software with available docs.
- Updated the Software model to include a flag indicating the presence of documentation.
- Improved the user interface for documentation navigation and search functionality.
2025-12-27 19:08:36 -06:00

184 lines
5.0 KiB
Go

package api
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"software-station/internal/models"
"software-station/internal/stats"
"strings"
"testing"
"time"
)
func TestHandlers(t *testing.T) {
os.Setenv("ALLOW_LOOPBACK", "true")
defer os.Unsetenv("ALLOW_LOOPBACK")
tempHashes := "test_handlers_hashes.json"
defer os.Remove(tempHashes)
os.RemoveAll(".cache")
statsService := stats.NewService(tempHashes)
initialSoftware := []models.Software{
{
Name: "test-app",
Releases: []models.Release{
{
TagName: "v1.0.0",
Assets: []models.Asset{
{Name: "test.exe", URL: "http://example.com/test.exe"},
},
},
},
AvatarURL: "http://example.com/logo.png",
},
}
server := NewServer("token", initialSoftware, statsService, true)
t.Run("APISoftwareHandler", func(t *testing.T) {
req := httptest.NewRequest("GET", "/api/software", nil)
rr := httptest.NewRecorder()
server.APISoftwareHandler(rr, req)
if rr.Code != http.StatusOK {
t.Errorf("expected 200, got %d", rr.Code)
}
var sw []models.Software
if err := json.Unmarshal(rr.Body.Bytes(), &sw); err != nil {
t.Fatal(err)
}
if len(sw) != 1 || sw[0].Name != "test-app" {
t.Errorf("unexpected response: %v", sw)
}
if !strings.Contains(sw[0].AvatarURL, "/api/avatar?id=") {
t.Errorf("AvatarURL not proxied: %s", sw[0].AvatarURL)
}
})
t.Run("AvatarHandler", func(t *testing.T) {
// Mock upstream
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/png")
w.Write([]byte("fake-image"))
}))
defer upstream.Close()
hash := server.RegisterURL(upstream.URL)
req := httptest.NewRequest("GET", "/api/avatar?id="+hash, nil)
rr := httptest.NewRecorder()
server.AvatarHandler(rr, req)
if rr.Code != http.StatusOK {
t.Errorf("expected 200, got %d", rr.Code)
}
if rr.Header().Get("Content-Type") != "image/png" {
t.Errorf("expected image/png, got %s", rr.Header().Get("Content-Type"))
}
})
t.Run("DownloadProxyHandler", func(t *testing.T) {
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("fake-binary"))
}))
defer upstream.Close()
hash := server.RegisterURL(upstream.URL)
req := httptest.NewRequest("GET", "/api/download?id="+hash, nil)
rr := httptest.NewRecorder()
server.DownloadProxyHandler(rr, req)
if rr.Code != http.StatusOK {
t.Errorf("expected 200, got %d", rr.Code)
}
})
t.Run("AssetCaching", func(t *testing.T) {
content := []byte("cache-me-if-you-can")
callCount := 0
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
callCount++
w.Write(content)
}))
defer upstream.Close()
hash := server.RegisterURL(upstream.URL)
// First call - should go to upstream
req := httptest.NewRequest("GET", "/api/download?id="+hash, nil)
rr := httptest.NewRecorder()
server.DownloadProxyHandler(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("first call: expected 200, got %d", rr.Code)
}
if callCount != 1 {
t.Errorf("first call: expected call count 1, got %d", callCount)
}
// Verify file exists in cache
cachePath := filepath.Join(AssetCacheDir, hash)
if _, err := os.Stat(cachePath); os.IsNotExist(err) {
t.Error("asset was not cached to disk")
}
// Second call - should be served from cache
req = httptest.NewRequest("GET", "/api/download?id="+hash, nil)
rr = httptest.NewRecorder()
server.DownloadProxyHandler(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("second call: expected 200, got %d", rr.Code)
}
if callCount != 1 {
t.Errorf("second call: expected call count 1 (from cache), got %d", callCount)
}
if rr.Body.String() != string(content) {
t.Errorf("second call: expected content %q, got %q", string(content), rr.Body.String())
}
})
t.Run("CacheCleanup", func(t *testing.T) {
cacheDir := "test_cleanup_cache"
os.MkdirAll(cacheDir, 0750)
defer os.RemoveAll(cacheDir)
// Create some files
f1 := filepath.Join(cacheDir, "old")
f2 := filepath.Join(cacheDir, "new")
os.WriteFile(f1, make([]byte, 100), 0600)
time.Sleep(10 * time.Millisecond) // Ensure different mod times
os.WriteFile(f2, make([]byte, 100), 0600)
// Set mod times explicitly
now := time.Now()
os.Chtimes(f1, now.Add(-1*time.Hour), now.Add(-1*time.Hour))
os.Chtimes(f2, now, now)
// Clean up with limit that only allows one file
server.cleanupCacheDir(cacheDir, 150)
if _, err := os.Stat(f1); err == nil {
t.Error("expected old file to be removed")
}
if _, err := os.Stat(f2); err != nil {
t.Error("expected new file to be kept")
}
})
t.Run("RSSHandler", func(t *testing.T) {
req := httptest.NewRequest("GET", "/api/rss", nil)
rr := httptest.NewRecorder()
server.RSSHandler(rr, req)
if rr.Code != http.StatusOK {
t.Errorf("expected 200, got %d", rr.Code)
}
if !strings.Contains(rr.Body.String(), "<rss") {
t.Error("missing rss tag")
}
})
}