This commit is contained in:
+59
-3
@@ -132,9 +132,25 @@ func TestPickupCodePreviewConsumesFinalAccessAndDeletesFile(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
denied := request(t, router, http.MethodGet, "/uf/"+file.Hash, nil, "", nil)
|
||||
if denied.Code != http.StatusForbidden {
|
||||
t.Fatalf("missing code status = %d", denied.Code)
|
||||
missingCode := request(t, router, http.MethodGet, "/api/pickup/"+file.Hash, nil, "", nil)
|
||||
if missingCode.Code != http.StatusForbidden || !strings.Contains(missingCode.Body.String(), "请输入取件码") {
|
||||
t.Fatalf("missing code status = %d, body = %s", missingCode.Code, missingCode.Body.String())
|
||||
}
|
||||
wrongCode := request(t, router, http.MethodGet, "/api/pickup/"+file.Hash+"?code=654321", nil, "", nil)
|
||||
if wrongCode.Code != http.StatusNotFound || !strings.Contains(wrongCode.Body.String(), publicFileOrCodeError) {
|
||||
t.Fatalf("wrong code status = %d, body = %s", wrongCode.Code, wrongCode.Body.String())
|
||||
}
|
||||
missingFile := request(t, router, http.MethodGet, "/api/pickup/notfound?code=654321", nil, "", nil)
|
||||
if missingFile.Code != wrongCode.Code || missingFile.Body.String() != wrongCode.Body.String() {
|
||||
t.Fatalf("missing file and wrong code responses differ: missing = %d %s, wrong = %d %s", missingFile.Code, missingFile.Body.String(), wrongCode.Code, wrongCode.Body.String())
|
||||
}
|
||||
verified := request(t, router, http.MethodGet, "/api/pickup/"+file.Hash+"?code=123456", nil, "", nil)
|
||||
if verified.Code != http.StatusOK {
|
||||
t.Fatalf("verify status = %d, body = %s", verified.Code, verified.Body.String())
|
||||
}
|
||||
var stored SharedFile
|
||||
if err := app.db.Where("hash = ?", file.Hash).First(&stored).Error; err != nil || stored.DownloadCount != 0 || stored.FailedCodeAttempts != 0 {
|
||||
t.Fatalf("verification state: downloads = %d, code failures = %d, err = %v", stored.DownloadCount, stored.FailedCodeAttempts, err)
|
||||
}
|
||||
preview := request(t, router, http.MethodGet, "/uf/"+file.Hash+"?preview=true&code=123456", nil, "", nil)
|
||||
if preview.Code != http.StatusOK || preview.Body.String() != "hello" {
|
||||
@@ -157,6 +173,46 @@ func TestPickupCodePreviewConsumesFinalAccessAndDeletesFile(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFiveConsecutivePickupCodeFailuresDeleteFile(t *testing.T) {
|
||||
app, _ := testApp(t)
|
||||
codeHash, _ := bcrypt.GenerateFromPassword([]byte("123456"), bcrypt.DefaultCost)
|
||||
max := 10
|
||||
file := SharedFile{Hash: "badcode5", OriginalName: "secret.txt", MimeType: "text/plain", OwnerID: 1, MaxDownloads: &max, PickupCodeHash: string(codeHash), Ready: true, Size: 6}
|
||||
if err := app.db.Create(&file).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path := filepath.Join(app.filesDir, file.Hash)
|
||||
if err := os.WriteFile(path, []byte("secret"), 0o640); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
router := app.router()
|
||||
requests := []string{
|
||||
"/api/pickup/" + file.Hash,
|
||||
"/uf/" + file.Hash + "?code=000001",
|
||||
"/api/pickup/" + file.Hash + "?code=000002",
|
||||
"/uf/" + file.Hash + "?code=000003",
|
||||
"/api/pickup/" + file.Hash + "?code=000004",
|
||||
}
|
||||
for index, path := range requests {
|
||||
response := request(t, router, http.MethodGet, path, nil, "", nil)
|
||||
want := http.StatusNotFound
|
||||
if index == 0 {
|
||||
want = http.StatusForbidden
|
||||
}
|
||||
if response.Code != want {
|
||||
t.Fatalf("failure %d status = %d, want %d, body = %s", index+1, response.Code, want, response.Body.String())
|
||||
}
|
||||
}
|
||||
var count int64
|
||||
app.db.Model(&SharedFile{}).Where("id = ?", file.ID).Count(&count)
|
||||
if count != 0 {
|
||||
t.Fatal("file record remains after five code failures")
|
||||
}
|
||||
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||
t.Fatal("file remains on disk after five code failures")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFiniteDownloadCountDeletesAfterMoreThanOneAccess(t *testing.T) {
|
||||
app, _ := testApp(t)
|
||||
max := 3
|
||||
|
||||
Reference in New Issue
Block a user