40 lines
1.3 KiB
Go
40 lines
1.3 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"database/sql"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestIntegrationTrust(t *testing.T) {
|
|
const public = "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAESr8/VFFwt70o7fwa8HiRNyxWtZEu\nUqzCBfYRG72DazEgeOaSVAdlTv49OujtggXSJzFLOluMENJzaRmp9+iJ0Q==\n-----END PUBLIC KEY-----\n"
|
|
for _, configured := range []bool{false, true} {
|
|
a := &BusinessAPI{}
|
|
if configured {
|
|
a.keyring = &Keyring{Current: "prod-test", Keys: []VerificationKey{{KeyID: "prod-test", PublicKeyPEM: public}, {KeyID: "old", PublicKeyPEM: public, Revoked: true}}}
|
|
}
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
a.integrationTrust(c)
|
|
if !configured {
|
|
if w.Code != 503 {
|
|
t.Fatal(w.Code)
|
|
}
|
|
continue
|
|
}
|
|
body := w.Body.String()
|
|
if w.Code != 200 || !strings.Contains(body, "22056bfacc8fe8a240dd2125b629e60ba9f4a5ea0041a1efe034f376a258d562") || !strings.Contains(body, `"revoked":true`) || strings.Contains(body, "PRIVATE KEY") || w.Header().Get("Cache-Control") != "no-store" {
|
|
t.Fatal("invalid trust response")
|
|
}
|
|
}
|
|
r := gin.New()
|
|
RegisterBusiness(r, &sql.DB{})
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, httptest.NewRequest("GET", "/api/v1/integration-trust", nil))
|
|
if w.Code != 401 {
|
|
t.Fatal("unauthenticated trust access")
|
|
}
|
|
}
|