|
| 1 | +(** E2E tests for BUD-09 PUT /report and GET / (Terms of Service). *) |
| 2 | + |
| 3 | +let sha256_hex content = |
| 4 | + let hash = Digestif.SHA256.digest_string content in |
| 5 | + Digestif.SHA256.to_hex hash |
| 6 | + |
| 7 | +(** Test: GET / returns 200 and text/plain Terms of Service *) |
| 8 | +let test_get_root_terms_of_service ~sw ~env = |
| 9 | + let base_url = Config.base_url in |
| 10 | + let result = Http_client.get ~sw ~env ~url:base_url () in |
| 11 | + match result with |
| 12 | + | Error e -> failwith ("GET / failed: " ^ e) |
| 13 | + | Ok response -> |
| 14 | + if response.status <> 200 then |
| 15 | + failwith (Printf.sprintf "Expected 200, got %d" response.status); |
| 16 | + let ct = List.find_opt |
| 17 | + (fun (k, _) -> String.lowercase_ascii k = "content-type") response.headers |
| 18 | + in |
| 19 | + (match ct with |
| 20 | + | None -> failwith "Missing Content-Type" |
| 21 | + | Some (_, v) -> |
| 22 | + if not (String.starts_with ~prefix:"text/plain" v) then |
| 23 | + failwith (Printf.sprintf "Expected text/plain, got %s" v)); |
| 24 | + if String.length response.body = 0 then |
| 25 | + failwith "Empty ToS body"; |
| 26 | + (* must mention the report endpoint *) |
| 27 | + let contains s sub = |
| 28 | + try |
| 29 | + let len_sub = String.length sub in |
| 30 | + let len_s = String.length s in |
| 31 | + let rec loop i = |
| 32 | + if i + len_sub > len_s then false |
| 33 | + else if String.sub s i len_sub = sub then true |
| 34 | + else loop (i + 1) |
| 35 | + in loop 0 |
| 36 | + with _ -> false |
| 37 | + in |
| 38 | + if not (contains response.body "report") then |
| 39 | + failwith "ToS should mention reporting" |
| 40 | + |
| 41 | +(** Test: PUT /report with a valid signed report returns 200 *) |
| 42 | +let test_put_report_accepted ~sw ~env = |
| 43 | + let base_url = Config.base_url in |
| 44 | + let clock = Eio.Stdenv.clock env in |
| 45 | + let now = Eio.Time.now clock in |
| 46 | + let keypair = Nostr_signer.generate_keypair () in |
| 47 | + let some_sha = sha256_hex "irrelevant content for report-only test" in |
| 48 | + let event = Nostr_signer.create_report |
| 49 | + ~keypair ~created_at:now |
| 50 | + ~entries:[(some_sha, "spam")] |
| 51 | + ~content:"this is spam" |
| 52 | + () |
| 53 | + in |
| 54 | + let body = Nostr_signer.event_to_json event in |
| 55 | + let result = Http_client.put ~sw ~env ~url:(base_url ^ "/report") |
| 56 | + ~headers:[("Content-Type", "application/json")] ~body () |
| 57 | + in |
| 58 | + match result with |
| 59 | + | Error e -> failwith ("PUT /report failed: " ^ e) |
| 60 | + | Ok response -> |
| 61 | + if response.status <> 200 then |
| 62 | + failwith (Printf.sprintf "Expected 200, got %d (body: %s)" |
| 63 | + response.status response.body) |
| 64 | + |
| 65 | +(** Test: PUT /report with multiple x tags is accepted *) |
| 66 | +let test_put_report_multiple_x_tags ~sw ~env = |
| 67 | + let base_url = Config.base_url in |
| 68 | + let clock = Eio.Stdenv.clock env in |
| 69 | + let now = Eio.Time.now clock in |
| 70 | + let keypair = Nostr_signer.generate_keypair () in |
| 71 | + let sha1 = sha256_hex "blob 1" in |
| 72 | + let sha2 = sha256_hex "blob 2" in |
| 73 | + let event = Nostr_signer.create_report |
| 74 | + ~keypair ~created_at:now |
| 75 | + ~entries:[(sha1, "malware"); (sha2, "illegal")] |
| 76 | + ~content:"" |
| 77 | + () |
| 78 | + in |
| 79 | + let body = Nostr_signer.event_to_json event in |
| 80 | + let result = Http_client.put ~sw ~env ~url:(base_url ^ "/report") |
| 81 | + ~headers:[("Content-Type", "application/json")] ~body () |
| 82 | + in |
| 83 | + match result with |
| 84 | + | Error e -> failwith ("PUT /report failed: " ^ e) |
| 85 | + | Ok response -> |
| 86 | + if response.status <> 200 then |
| 87 | + failwith (Printf.sprintf "Expected 200, got %d (body: %s)" |
| 88 | + response.status response.body) |
| 89 | + |
| 90 | +(** Test: PUT /report rejects an invalid (non-NIP-56) body *) |
| 91 | +let test_put_report_rejects_invalid_body ~sw ~env = |
| 92 | + let base_url = Config.base_url in |
| 93 | + let result = Http_client.put ~sw ~env ~url:(base_url ^ "/report") |
| 94 | + ~headers:[("Content-Type", "application/json")] |
| 95 | + ~body:"{\"not\":\"a report\"}" () |
| 96 | + in |
| 97 | + match result with |
| 98 | + | Error e -> failwith ("Request failed: " ^ e) |
| 99 | + | Ok response -> |
| 100 | + if response.status <> 400 then |
| 101 | + failwith (Printf.sprintf "Expected 400 for invalid body, got %d" response.status) |
| 102 | + |
| 103 | +(** Test: PUT /report rejects an unknown report type *) |
| 104 | +let test_put_report_rejects_unknown_type ~sw ~env = |
| 105 | + let base_url = Config.base_url in |
| 106 | + let clock = Eio.Stdenv.clock env in |
| 107 | + let now = Eio.Time.now clock in |
| 108 | + let keypair = Nostr_signer.generate_keypair () in |
| 109 | + let some_sha = sha256_hex "x" in |
| 110 | + let event = Nostr_signer.create_report |
| 111 | + ~keypair ~created_at:now |
| 112 | + ~entries:[(some_sha, "vandalism")] |
| 113 | + ~content:"" |
| 114 | + () |
| 115 | + in |
| 116 | + let body = Nostr_signer.event_to_json event in |
| 117 | + let result = Http_client.put ~sw ~env ~url:(base_url ^ "/report") |
| 118 | + ~headers:[("Content-Type", "application/json")] ~body () |
| 119 | + in |
| 120 | + match result with |
| 121 | + | Error e -> failwith ("Request failed: " ^ e) |
| 122 | + | Ok response -> |
| 123 | + if response.status <> 400 then |
| 124 | + failwith (Printf.sprintf "Expected 400 for invalid report type, got %d" response.status) |
| 125 | + |
| 126 | +let tests = [ |
| 127 | + ("GET / returns ToS", test_get_root_terms_of_service); |
| 128 | + ("PUT /report accepted", test_put_report_accepted); |
| 129 | + ("PUT /report multiple x tags", test_put_report_multiple_x_tags); |
| 130 | + ("PUT /report rejects invalid body", test_put_report_rejects_invalid_body); |
| 131 | + ("PUT /report rejects unknown type", test_put_report_rejects_unknown_type); |
| 132 | +] |
0 commit comments