|
| 1 | +use std::error::Error; |
| 2 | +use std::fs; |
| 3 | +use std::path::PathBuf; |
| 4 | + |
| 5 | +use clap::Parser; |
| 6 | +use nostr::hashes::{sha256, Hash}; |
| 7 | +use nostr::key::SecretKey; |
| 8 | +use nostr::Keys; |
| 9 | +use nostr_blossom::client::BlossomClient; |
| 10 | + |
| 11 | +/// Integration test for various Blossom operations: upload, check (HEAD), list, download, and delete. |
| 12 | +#[derive(Parser, Debug)] |
| 13 | +#[command(author, version, about = "Run several operations against a Blossom server for demonstration and testing", long_about = None)] |
| 14 | +struct Args { |
| 15 | + /// The Blossom server URL |
| 16 | + #[arg(long)] |
| 17 | + server: String, |
| 18 | + |
| 19 | + /// Optional file path to a blob to upload. If omitted, a small test blob is used. |
| 20 | + #[arg(long)] |
| 21 | + file: Option<PathBuf>, |
| 22 | + |
| 23 | + /// Private key to use for signing (in hex) |
| 24 | + #[arg(long, value_name = "PRIVATE_KEY")] |
| 25 | + private_key: String, |
| 26 | +} |
| 27 | + |
| 28 | +#[tokio::main] |
| 29 | +async fn main() -> Result<(), Box<dyn Error>> { |
| 30 | + // Parse command line arguments. |
| 31 | + let args = Args::parse(); |
| 32 | + |
| 33 | + // Create the Blossom client. |
| 34 | + let client = BlossomClient::new(&args.server); |
| 35 | + // Create signer keys from the provided private key. |
| 36 | + let keys = Keys::new(SecretKey::from_hex(args.private_key.as_str())?); |
| 37 | + |
| 38 | + // Read the blob data from a file if provided, otherwise use default test data. |
| 39 | + let data = if let Some(file_path) = args.file { |
| 40 | + println!("Reading blob data from file: {:?}", file_path); |
| 41 | + fs::read(file_path)? |
| 42 | + } else { |
| 43 | + let default_blob = b"Test blob data from integration_test"; |
| 44 | + println!( |
| 45 | + "No file provided. Using default test data: {:?}", |
| 46 | + String::from_utf8_lossy(default_blob) |
| 47 | + ); |
| 48 | + default_blob.to_vec() |
| 49 | + }; |
| 50 | + |
| 51 | + // Compute SHA256 hash of the blob data. |
| 52 | + let blob_hash = sha256::Hash::hash(&data); |
| 53 | + let blob_hash_hex = blob_hash.to_string(); |
| 54 | + println!("\nBlob SHA256: {}", blob_hash_hex); |
| 55 | + |
| 56 | + // 1. Upload Blob |
| 57 | + println!("\n[1] Uploading blob..."); |
| 58 | + // Use a basic content type |
| 59 | + let content_type = Some("application/octet-stream".to_string()); |
| 60 | + let descriptor = client |
| 61 | + .upload_blob(data.clone(), content_type, None, Some(&keys)) |
| 62 | + .await?; |
| 63 | + println!("Uploaded BlobDescriptor: {:#?}", descriptor); |
| 64 | + |
| 65 | + // 2. Check blob existence using HEAD (has_blob method) |
| 66 | + println!("\n[2] Checking blob existence via HEAD request..."); |
| 67 | + let exists = client.has_blob(blob_hash, None, Some(&keys)).await?; |
| 68 | + println!("has_blob result: {}", exists); |
| 69 | + |
| 70 | + // 3. List blobs for the pubkey |
| 71 | + println!("\n[3] Listing blobs for public key..."); |
| 72 | + let pubkey = keys.public_key(); |
| 73 | + let blobs = client |
| 74 | + .list_blobs::<Keys>(&pubkey, None, None, None, Some(&keys)) |
| 75 | + .await?; |
| 76 | + println!("List Blobs results:"); |
| 77 | + for blob in blobs.iter() { |
| 78 | + println!(" - {:?}", blob); |
| 79 | + } |
| 80 | + |
| 81 | + // 4. Download blob and compare hash |
| 82 | + println!("\n[4] Downloading blob..."); |
| 83 | + |
| 84 | + let downloaded_data: Vec<u8> = client.get_blob(blob_hash, None, None, Some(&keys)).await?; |
| 85 | + let downloaded_hash = sha256::Hash::hash(&downloaded_data); |
| 86 | + println!("Downloaded blob hash: {}", downloaded_hash); |
| 87 | + if downloaded_hash == blob_hash { |
| 88 | + println!("Downloaded blob hash matches the original."); |
| 89 | + } else { |
| 90 | + println!( |
| 91 | + "Hash mismatch! Original: {} Downloaded: {}", |
| 92 | + blob_hash, downloaded_hash |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + // 5. Delete blob |
| 97 | + println!("\n[5] Deleting blob..."); |
| 98 | + client.delete_blob(blob_hash, None, &keys).await?; |
| 99 | + println!("Blob deleted successfully."); |
| 100 | + |
| 101 | + // Final check: verify deletion using HEAD |
| 102 | + let exists_after = client.has_blob(blob_hash, None, Some(&keys)).await?; |
| 103 | + if !exists_after { |
| 104 | + println!("Verified: Blob no longer exists on the server."); |
| 105 | + } else { |
| 106 | + println!("Warning: Blob still exists on the server."); |
| 107 | + } |
| 108 | + |
| 109 | + println!("\nIntegration test complete."); |
| 110 | + Ok(()) |
| 111 | +} |
0 commit comments