dal-license-server/internal/middleware/auth_test.go
djuka dc0114e4b7 Inicijalni commit: kompletna implementacija + dokumentacija + testovi
- Kompletna Go implementacija licencnog servera (19 Go fajlova)
- Klijentski API: activate, deactivate, validate
- Admin API: CRUD licence, stats, audit log
- Admin dashboard: htmx + Go templates
- RSA-2048 potpisivanje licencnih podataka
- Rate limiting i API key autentifikacija
- MySQL migracije i seed podaci (ESIR, ARV, LIGHT_TICKET)
- Unit testovi: keygen, crypto, model, middleware (24 testa)
- Dokumentacija: SPEC.md, ARCHITECTURE.md, SETUP.md, API.md, TESTING.md, README.md

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-04 07:42:25 +00:00

66 lines
1.5 KiB
Go

package middleware
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestAPIKeyAuth_ValidKey(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})
mw := APIKeyAuth("test-api-key-12345")
wrapped := mw(handler)
req := httptest.NewRequest("GET", "/api/v1/admin/licenses", nil)
req.Header.Set("X-API-Key", "test-api-key-12345")
w := httptest.NewRecorder()
wrapped.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("validan kljuc mora proci, dobijen status %d", w.Code)
}
}
func TestAPIKeyAuth_MissingKey(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
mw := APIKeyAuth("test-api-key-12345")
wrapped := mw(handler)
req := httptest.NewRequest("GET", "/api/v1/admin/licenses", nil)
// No X-API-Key header
w := httptest.NewRecorder()
wrapped.ServeHTTP(w, req)
if w.Code != http.StatusUnauthorized {
t.Errorf("bez kljuca mora biti 401, dobijen %d", w.Code)
}
}
func TestAPIKeyAuth_WrongKey(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
mw := APIKeyAuth("correct-key")
wrapped := mw(handler)
req := httptest.NewRequest("GET", "/api/v1/admin/licenses", nil)
req.Header.Set("X-API-Key", "wrong-key")
w := httptest.NewRecorder()
wrapped.ServeHTTP(w, req)
if w.Code != http.StatusUnauthorized {
t.Errorf("pogresan kljuc mora biti 401, dobijen %d", w.Code)
}
}