update_handlers_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package api
  2. import (
  3. "context"
  4. "net/http"
  5. "net/http/httptest"
  6. "proxmox-router/internal/auth"
  7. "strings"
  8. "testing"
  9. )
  10. func TestUpdateConfigValidationAndPrivileges(t *testing.T) {
  11. for _, tc := range []struct {
  12. repo, branch string
  13. valid bool
  14. }{
  15. {"http://gogs.example/team/router.git", "master", true},
  16. {"ssh://git@example.com/team/router.git", "release/1", true},
  17. {"file:///etc/passwd", "main", false},
  18. {"https://user:password@example.com/router.git", "main", false},
  19. {"https://example.com/router.git", "../bad", false},
  20. {"https://example.com/router.git", "-option", false},
  21. } {
  22. if got := validUpdateConfig(updateConfig{Repository: tc.repo, Branch: tc.branch}); got != tc.valid {
  23. t.Fatalf("%s %s: got %v", tc.repo, tc.branch, got)
  24. }
  25. }
  26. s := &Server{}
  27. body := `{"repository":"https://example.com/router.git","branch":"main"}`
  28. for _, role := range []auth.Role{auth.RoleRead, auth.RoleWrite} {
  29. r := httptest.NewRequest("POST", "/api/update/config", strings.NewReader(body))
  30. r = r.WithContext(context.WithValue(r.Context(), sessionContextKey, &auth.Session{Role: role}))
  31. w := httptest.NewRecorder()
  32. s.handleUpdateConfig(w, r)
  33. if w.Code != http.StatusForbidden {
  34. t.Fatalf("%s changed update config: %d", role, w.Code)
  35. }
  36. }
  37. }