@@ -6,12 +6,14 @@ import (
66 "encoding/json"
77 "net/http"
88 "net/http/httptest"
9+ "path/filepath"
910 "strings"
1011 "testing"
1112 "time"
1213
1314 "tailscale.com/tsnet"
1415
16+ "github.com/EvalOps/keep/pkg/pki"
1517 "github.com/EvalOps/keep/pkg/retry"
1618)
1719
@@ -28,6 +30,8 @@ const (
2830 testInventoryHost = "test-inventory:8080"
2931 testOPAHost = "test-opa:8181"
3032 testAuthzPort = ":8443"
33+ testRetryAttempts = 2
34+ testZeroLength = 0
3135)
3236
3337// TestServer_healthHandler tests the health endpoint
@@ -489,6 +493,9 @@ func TestServer_lookupDevice(t *testing.T) {
489493 if result ["id" ] != expectedDevice ["id" ] || result ["posture" ] != expectedDevice ["posture" ] {
490494 t .Errorf ("Expected device info %v, got %v" , expectedDevice , result )
491495 }
496+ if score , ok := result ["trust_score" ].(int ); ! ok || score != 0 {
497+ t .Errorf ("Expected trust_score 0 for non-JSON posture, got %v" , result ["trust_score" ])
498+ }
492499 })
493500
494501 t .Run ("handles empty device ID" , func (t * testing.T ) {
@@ -606,6 +613,120 @@ func TestServer_caHandler(t *testing.T) {
606613 // which we avoid in unit tests to keep them lightweight
607614}
608615
616+ func TestNewInitializesServerState (t * testing.T ) {
617+ tmpDir := t .TempDir ()
618+ certPath := filepath .Join (tmpDir , "ca.pem" )
619+ keyPath := filepath .Join (tmpDir , "ca-key.pem" )
620+
621+ t .Setenv ("OTEL_SDK_DISABLED" , "true" )
622+
623+ _ , err := pki .LoadOrCreateCA (certPath , keyPath , "test-ca" , time .Hour )
624+ if err != nil {
625+ t .Fatalf ("Failed to create CA: %v" , err )
626+ }
627+
628+ cfg := Config {
629+ HTTPAddr : "127.0.0.1:0" ,
630+ GoogleClientID : "client-id" ,
631+ OPAURL : "http://opa" ,
632+ InventoryAPI : "http://inventory" ,
633+ RootCAPath : certPath ,
634+ TLSCertPath : certPath ,
635+ TLSKeyPath : keyPath ,
636+ RequestTimeout : time .Second ,
637+ RetryMaxElapsed : defaultInitialInterval ,
638+ RetryMaxAttempts : testRetryAttempts ,
639+ }
640+
641+ srv , err := New (cfg )
642+ if err != nil {
643+ t .Fatalf ("New returned error: %v" , err )
644+ }
645+ t .Cleanup (func () {
646+ _ = srv .httpSrv .Close ()
647+ })
648+
649+ if srv .cfg == nil {
650+ t .Fatal ("server cfg should not be nil" )
651+ }
652+ if srv .retryCfg == nil {
653+ t .Fatal ("server retryCfg should not be nil" )
654+ }
655+ if srv .state .started {
656+ t .Error ("server should not be marked started" )
657+ }
658+ if srv .httpSrv == nil {
659+ t .Fatal ("http server should be initialized" )
660+ }
661+ if ! srv .state .useTLS {
662+ t .Error ("expected TLS to be enabled when cert/key provided" )
663+ }
664+ if len (srv .rootCAPEM ) == testZeroLength {
665+ t .Error ("expected root CA PEM to be cached" )
666+ }
667+ if srv .tsServer != nil {
668+ t .Error ("tailscale server should be nil when auth key not provided" )
669+ }
670+
671+ if srv .cfg .GoogleClientID != cfg .GoogleClientID {
672+ t .Errorf ("expected GoogleClientID %q, got %q" , cfg .GoogleClientID , srv .cfg .GoogleClientID )
673+ }
674+ }
675+
676+ func TestSetupHTTPServerTLSAndPlain (t * testing.T ) {
677+ tmpDir := t .TempDir ()
678+ certPath := filepath .Join (tmpDir , "ca.pem" )
679+ keyPath := filepath .Join (tmpDir , "ca-key.pem" )
680+
681+ ca , err := pki .LoadOrCreateCA (certPath , keyPath , "test-ca" , time .Hour )
682+ if err != nil {
683+ t .Fatalf ("Failed to create CA: %v" , err )
684+ }
685+ handler := http .NewServeMux ()
686+
687+ t .Run ("enables TLS when certs provided" , func (t * testing.T ) {
688+ cfg := Config {
689+ HTTPAddr : "127.0.0.1:0" ,
690+ TLSCertPath : certPath ,
691+ TLSKeyPath : keyPath ,
692+ RootCAPath : certPath ,
693+ }
694+
695+ srv := & Server {}
696+ if err := srv .setupHTTPServer (cfg , handler , ca ); err != nil {
697+ t .Fatalf ("setupHTTPServer returned error: %v" , err )
698+ }
699+ t .Cleanup (func () {
700+ _ = srv .httpSrv .Close ()
701+ })
702+
703+ if srv .httpSrv == nil {
704+ t .Fatal ("http server should be initialized" )
705+ }
706+ if ! srv .state .useTLS {
707+ t .Error ("expected TLS to be enabled" )
708+ }
709+ })
710+
711+ t .Run ("disables TLS when certs missing" , func (t * testing.T ) {
712+ cfg := Config {
713+ HTTPAddr : "127.0.0.1:0" ,
714+ }
715+
716+ srv := & Server {}
717+ if err := srv .setupHTTPServer (cfg , handler , ca ); err != nil {
718+ t .Fatalf ("setupHTTPServer returned error: %v" , err )
719+ }
720+ t .Cleanup (func () {
721+ _ = srv .httpSrv .Close ()
722+ })
723+
724+ if srv .state .useTLS {
725+ t .Error ("expected TLS to remain disabled" )
726+ }
727+ })
728+ }
729+
609730// TestServer_validateTailscaleAccess tests Tailscale network validation
610731func TestServer_validateTailscaleAccess (t * testing.T ) {
611732 server := createTestServer (t )
0 commit comments