This commit is contained in:
1 parent
b4f5f51c0e
commit
ce8285f7cc
7 files changed
+343
-1
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -84,3 +85,46 @@ func TestPasswordBusyResponsePreservesOtherErrors(t *testing.T) {
|
||||
t.Fatal("包装后的繁忙错误未被识别")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublicRecoveryBusyHTTPAllowsRetry(t *testing.T) {
|
||||
enablePublicRecoveryForTest(t)
|
||||
recoveryService, _, _, user, _ := newPublicSecurityFixture(t)
|
||||
var token string
|
||||
recoveryService.SetPublicLinkDelivery(func(_ string, issued string) error { token = issued; return nil })
|
||||
if err := recoveryService.BeginPublicLink(user.Account); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
previous := currentSecurityRuntime()
|
||||
ConfigureSecurityRuntime(previous.verification, previous.external, recoveryService, previous.credentials)
|
||||
t.Cleanup(func() {
|
||||
ConfigureSecurityRuntime(previous.verification, previous.external, previous.recovery, previous.credentials)
|
||||
})
|
||||
body, err := json.Marshal(map[string]string{"token": token, "new_password": "NewPass123"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
invoke := func() *httptest.ResponseRecorder {
|
||||
request := httptest.NewRequest(http.MethodPost, "https://example.com/rest/api/recovery/complete", strings.NewReader(string(body)))
|
||||
recorder := httptest.NewRecorder()
|
||||
CompletePublicLinkPasswordRecovery(recorder, request)
|
||||
return recorder
|
||||
}
|
||||
release := passwordtest.Hold(t)
|
||||
for index := 0; index < 12; index++ {
|
||||
recorder := invoke()
|
||||
var result map[string]bool
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &result); err != nil || len(result) != 1 || result["reset"] {
|
||||
t.Fatal("匿名恢复繁忙响应泄露额外状态或错误报告成功")
|
||||
}
|
||||
if recorder.Code != http.StatusServiceUnavailable || recorder.Header().Get("Retry-After") != "1" || recorder.Header().Get("Cache-Control") != "no-store" {
|
||||
t.Fatalf("匿名恢复繁忙响应不可重试:%d", recorder.Code)
|
||||
}
|
||||
}
|
||||
release()
|
||||
if recorder := invoke(); recorder.Code != http.StatusOK {
|
||||
t.Fatalf("繁忙结束后链接无法重试:%d", recorder.Code)
|
||||
}
|
||||
if recorder := invoke(); recorder.Code != http.StatusBadRequest {
|
||||
t.Fatalf("恢复链接可被重复使用:%d", recorder.Code)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user