update_handlers_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. } {
  20. if got := validUpdateConfig(updateConfig{Repository: tc.repo, Branch: tc.branch}); got != tc.valid {
  21. t.Fatalf("%s %s: got %v", tc.repo, tc.branch, got)
  22. }
  23. }
  24. s := &Server{}
  25. body := `{"repository":"https://example.com/router.git","branch":"main"}`
  26. for _, role := range []auth.Role{auth.RoleRead, auth.RoleWrite} {
  27. r := httptest.NewRequest("POST", "/api/update/config", strings.NewReader(body))
  28. r = r.WithContext(context.WithValue(r.Context(), sessionContextKey, &auth.Session{Role: role}))
  29. w := httptest.NewRecorder()
  30. s.handleUpdateConfig(w, r)
  31. if w.Code != http.StatusForbidden {
  32. t.Fatalf("%s changed update config: %d", role, w.Code)
  33. }
  34. }
  35. }