disable_signup_test.go (1203B)
1 package internal 2 3 import ( 4 "bytes" 5 "io" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 ) 10 11 func TestDisableSignup_RemovesRouteAndInjectsConfig(t *testing.T) { 12 t.Parallel() 13 db, err := OpenAtPath(t.Context(), t.TempDir()+"/nosign.db") 14 if err != nil { 15 t.Fatalf("open: %v", err) 16 } 17 t.Cleanup(func() { _ = db.Close() }) 18 19 h, err := NewHandlerWithConfig(db, HandlerConfig{DisableSignup: true}) 20 if err != nil { 21 t.Fatalf("handler: %v", err) 22 } 23 srv := httptest.NewServer(h) 24 defer srv.Close() 25 26 // POST /signup should not be available 27 req, _ := http.NewRequest(http.MethodPost, srv.URL+"/signup", bytes.NewReader([]byte(`{"username":"x","password":"y"}`))) 28 req.Header.Set("Content-Type", "application/json") 29 resp, _ := http.DefaultClient.Do(req) 30 if resp.StatusCode != http.StatusNotFound { 31 t.Fatalf("signup status: got %d want 404", resp.StatusCode) 32 } 33 resp.Body.Close() 34 35 // Index should include injected config snippet 36 r2, _ := http.Get(srv.URL + "/") 37 if r2.StatusCode != 200 { 38 t.Fatalf("index %d", r2.StatusCode) 39 } 40 b, _ := io.ReadAll(r2.Body) 41 r2.Body.Close() 42 if !bytes.Contains(b, []byte("window.MARKY_CFG={disableSignup:true}")) { 43 t.Fatalf("expected config injection in index") 44 } 45 }