Add admin and customer interfaces, orders, signed activation and leases, protected package delivery, keyring verification, migrations, security controls, tests and deployment documentation. Co-Authored-By: Craft Agent <agents-noreply@craft.do>
96 lines
2.7 KiB
Go
96 lines
2.7 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"database/sql/driver"
|
|
"errors"
|
|
"io"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type readinessDriver struct{}
|
|
type readinessConn struct{ mode string }
|
|
type readinessRows struct {
|
|
mode string
|
|
read bool
|
|
}
|
|
|
|
func (readinessDriver) Open(mode string) (driver.Conn, error) { return readinessConn{mode}, nil }
|
|
func (readinessConn) Prepare(string) (driver.Stmt, error) { return nil, errors.New("not supported") }
|
|
func (readinessConn) Close() error { return nil }
|
|
func (readinessConn) Begin() (driver.Tx, error) { return nil, errors.New("not supported") }
|
|
func (c readinessConn) QueryContext(context.Context, string, []driver.NamedValue) (driver.Rows, error) {
|
|
if c.mode == "unavailable" {
|
|
return nil, errors.New("private connection detail")
|
|
}
|
|
return &readinessRows{mode: c.mode}, nil
|
|
}
|
|
func (*readinessRows) Columns() []string { return []string{"version", "dirty"} }
|
|
func (*readinessRows) Close() error { return nil }
|
|
func (r *readinessRows) Next(values []driver.Value) error {
|
|
if r.read || r.mode == "empty" {
|
|
return io.EOF
|
|
}
|
|
r.read = true
|
|
version := int64(RequiredSchemaVersion)
|
|
if r.mode == "old" {
|
|
version--
|
|
}
|
|
if r.mode == "future" {
|
|
version++
|
|
}
|
|
values[0] = version
|
|
values[1] = r.mode == "dirty"
|
|
return nil
|
|
}
|
|
func init() { sql.Register("readiness-test", readinessDriver{}) }
|
|
func TestReadinessSchemaGate(t *testing.T) {
|
|
for _, mode := range []string{"ready", "old", "future", "dirty", "empty", "unavailable", "nil"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
var db *sql.DB
|
|
if mode != "nil" {
|
|
var err error
|
|
db, err = sql.Open("readiness-test", mode)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
}
|
|
r := gin.New()
|
|
RegisterReadiness(r, db)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, httptest.NewRequest("GET", "/api/v1/health/ready", nil))
|
|
want := 503
|
|
body := `{"status":"not_ready"}`
|
|
if mode == "ready" {
|
|
want = 200
|
|
body = `{"status":"ready"}`
|
|
}
|
|
if w.Code != want || w.Body.String() != body {
|
|
t.Fatalf("%d %s", w.Code, w.Body.String())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
func TestSecureSessionCookie(t *testing.T) {
|
|
t.Setenv("SESSION_COOKIE_SECURE", "true")
|
|
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
c.Request = httptest.NewRequest("GET", "http://localhost/", nil)
|
|
if !secureSessionCookie(c) {
|
|
t.Fatal("forced secure lost")
|
|
}
|
|
t.Setenv("SESSION_COOKIE_SECURE", "false")
|
|
c.Request.Header.Set("X-Forwarded-Proto", "https")
|
|
if secureSessionCookie(c) {
|
|
t.Fatal("untrusted header enabled cookie security")
|
|
}
|
|
c.Request = httptest.NewRequest("GET", "https://localhost/", nil)
|
|
if !secureSessionCookie(c) {
|
|
t.Fatal("direct TLS must be secure")
|
|
}
|
|
}
|