Skip to content

Commit a0ee58d

Browse files
committed
restapi: handler validation tests for the new endpoints
httptest-based tests exercising the request-validation branches (which run before any device I/O): missing/invalid params across files, mobilegestalt, wifi, mdm, crashes, devmode, and the job endpoints (erase confirm-gate, missing bundle/ports, job-not-found 404). Closes the biggest coverage gap for the parity endpoints.
1 parent a4406e1 commit a0ee58d

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package api
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"strings"
7+
"testing"
8+
9+
"github.com/danielpaulus/go-ios/ios"
10+
"github.com/gin-gonic/gin"
11+
)
12+
13+
// newHandlerCtx builds a gin test context with a device already in context, so a
14+
// handler's request-validation branches (which run before any device I/O) can be
15+
// exercised without a real device.
16+
func newHandlerCtx(method, target, body string) (*httptest.ResponseRecorder, *gin.Context) {
17+
gin.SetMode(gin.TestMode)
18+
w := httptest.NewRecorder()
19+
c, _ := gin.CreateTestContext(w)
20+
var r *http.Request
21+
if body != "" {
22+
r = httptest.NewRequest(method, target, strings.NewReader(body))
23+
r.Header.Set("Content-Type", "application/json")
24+
} else {
25+
r = httptest.NewRequest(method, target, nil)
26+
}
27+
c.Request = r
28+
c.Set(IOS_KEY, ios.DeviceEntry{})
29+
return w, c
30+
}
31+
32+
func TestValidationRejections(t *testing.T) {
33+
cases := []struct {
34+
name string
35+
method string
36+
target string
37+
body string
38+
handler gin.HandlerFunc
39+
want int
40+
}{
41+
{"erase without confirm", "POST", "/erase", "", Erase, http.StatusBadRequest},
42+
{"mobilegestalt without key", "GET", "/mobilegestalt", "", GetMobileGestalt, http.StatusBadRequest},
43+
{"files without domain", "GET", "/files", "", ListFiles, http.StatusBadRequest},
44+
{"files unknown domain", "GET", "/files?domain=bogus", "", ListFiles, http.StatusBadRequest},
45+
{"pull without remote", "GET", "/files/pull?domain=temp", "", PullFile, http.StatusBadRequest},
46+
{"push without remote", "POST", "/files/push?domain=temp", "", PushFile, http.StatusBadRequest},
47+
{"forward without ports", "POST", "/jobs/forward", `{}`, StartForward, http.StatusBadRequest},
48+
{"runtest without bundle", "POST", "/jobs/runtest", `{}`, StartRunTest, http.StatusBadRequest},
49+
{"wifi without ssid", "PUT", "/wifi", `{"password":"x"}`, SetWifi, http.StatusBadRequest},
50+
{"remove wifi without ssid", "DELETE", "/wifi", "", RemoveWifi, http.StatusBadRequest},
51+
{"clear-passcode without token", "POST", "/mdm/clear-passcode", "", MdmClearPasscode, http.StatusBadRequest},
52+
{"remove crashes without args", "DELETE", "/crashes", "", RemoveCrashes, http.StatusBadRequest},
53+
{"set devmode bad action", "POST", "/devmode", `{"action":"bogus"}`, SetDevMode, http.StatusBadRequest},
54+
}
55+
for _, tc := range cases {
56+
t.Run(tc.name, func(t *testing.T) {
57+
w, c := newHandlerCtx(tc.method, tc.target, tc.body)
58+
tc.handler(c)
59+
if w.Code != tc.want {
60+
t.Fatalf("%s: got %d, want %d (body=%s)", tc.name, w.Code, tc.want, w.Body.String())
61+
}
62+
if !strings.Contains(w.Body.String(), `"error"`) {
63+
t.Fatalf("%s: expected an error envelope, got %q", tc.name, w.Body.String())
64+
}
65+
})
66+
}
67+
}
68+
69+
func TestJobNotFoundReturns404(t *testing.T) {
70+
w, c := newHandlerCtx("GET", "/jobs/nope-1", "")
71+
c.Params = gin.Params{{Key: "udid", Value: "UDID-X"}, {Key: "id", Value: "nope-1"}}
72+
GetJob(c)
73+
if w.Code != http.StatusNotFound {
74+
t.Fatalf("got %d, want 404", w.Code)
75+
}
76+
}

0 commit comments

Comments
 (0)