last from journey
This commit is contained in:
73
internal/api/crl_test.go
Normal file
73
internal/api/crl_test.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/stef/pkiapi/internal/pki"
|
||||
"github.com/stef/pkiapi/internal/storage"
|
||||
)
|
||||
|
||||
func TestGetCRLHandler(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
// Préparer un MemoryStore et l'initialiser dans l'API
|
||||
mem := storage.NewMemoryStore()
|
||||
InitCertificateStore(mem)
|
||||
|
||||
// Générer un certificat de test
|
||||
cert, err := pki.GenerateCertificate("CN=test.example.com,O=Example,C=FR", 365)
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateCertificate error: %v", err)
|
||||
}
|
||||
|
||||
id := uuid.New().String()
|
||||
cert.ID = id
|
||||
|
||||
// Sauvegarder le certificat (non révoqué)
|
||||
if err := mem.SaveCertificate(id, cert); err != nil {
|
||||
t.Fatalf("SaveCertificate error: %v", err)
|
||||
}
|
||||
|
||||
// Révoquer le certificat
|
||||
cert.Revoked = true
|
||||
if err := mem.SaveCertificate(id, cert); err != nil {
|
||||
t.Fatalf("SaveCertificate(revoked) error: %v", err)
|
||||
}
|
||||
|
||||
// Appeler le handler GetCRL
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
req, _ := http.NewRequest("GET", "/api/v1/crl", nil)
|
||||
c.Request = req
|
||||
|
||||
GetCRL(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected status 200, got %d, body: %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
var resp struct {
|
||||
CRL []struct {
|
||||
SerialNumber string `json:"serial_number"`
|
||||
Subject string `json:"subject"`
|
||||
} `json:"crl"`
|
||||
Version int `json:"version"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("unmarshal response: %v", err)
|
||||
}
|
||||
|
||||
if len(resp.CRL) != 1 {
|
||||
t.Fatalf("expected 1 revoked cert in CRL, got %d, body: %s", len(resp.CRL), w.Body.String())
|
||||
}
|
||||
|
||||
if resp.CRL[0].SerialNumber == "" {
|
||||
t.Fatalf("expected serial number present in CRL entry, got empty")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user