|
@@ -219,18 +219,22 @@ func TestCheckAPIAuth_EmptyVerifiedChainsFallsThrough(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// TestCheckAPIAuth_RejectsUnauthenticated characterizes the reject paths: no
|
|
|
|
|
-// bearer token and no session yields 401 for XHR callers and 404 otherwise.
|
|
|
|
|
|
|
+// TestCheckAPIAuth_RejectsUnauthenticated characterizes the reject paths:
|
|
|
|
|
+// no credential → 404 (masking); XHR or a presented (but invalid) Bearer → 401
|
|
|
|
|
+// so script authors can tell auth failure from a wrong base path.
|
|
|
func TestCheckAPIAuth_RejectsUnauthenticated(t *testing.T) {
|
|
func TestCheckAPIAuth_RejectsUnauthenticated(t *testing.T) {
|
|
|
engine, _ := newAPIAuthTestEngine(t)
|
|
engine, _ := newAPIAuthTestEngine(t)
|
|
|
|
|
|
|
|
cases := []struct {
|
|
cases := []struct {
|
|
|
- name string
|
|
|
|
|
- xhr bool
|
|
|
|
|
- want int
|
|
|
|
|
|
|
+ name string
|
|
|
|
|
+ xhr bool
|
|
|
|
|
+ bearer string // empty = omit Authorization header
|
|
|
|
|
+ want int
|
|
|
}{
|
|
}{
|
|
|
- {"xhr gets 401", true, http.StatusUnauthorized},
|
|
|
|
|
- {"non-xhr gets 404", false, http.StatusNotFound},
|
|
|
|
|
|
|
+ {"xhr gets 401", true, "", http.StatusUnauthorized},
|
|
|
|
|
+ {"non-xhr gets 404", false, "", http.StatusNotFound},
|
|
|
|
|
+ {"invalid bearer gets 401", false, "definitely-not-a-token", http.StatusUnauthorized},
|
|
|
|
|
+ {"invalid bearer xhr gets 401", true, "definitely-not-a-token", http.StatusUnauthorized},
|
|
|
}
|
|
}
|
|
|
for _, c := range cases {
|
|
for _, c := range cases {
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
@@ -238,6 +242,9 @@ func TestCheckAPIAuth_RejectsUnauthenticated(t *testing.T) {
|
|
|
if c.xhr {
|
|
if c.xhr {
|
|
|
req.Header.Set("X-Requested-With", "XMLHttpRequest")
|
|
req.Header.Set("X-Requested-With", "XMLHttpRequest")
|
|
|
}
|
|
}
|
|
|
|
|
+ if c.bearer != "" {
|
|
|
|
|
+ req.Header.Set("Authorization", "Bearer "+c.bearer)
|
|
|
|
|
+ }
|
|
|
w := httptest.NewRecorder()
|
|
w := httptest.NewRecorder()
|
|
|
engine.ServeHTTP(w, req)
|
|
engine.ServeHTTP(w, req)
|
|
|
if w.Code != c.want {
|
|
if w.Code != c.want {
|