start_fake_auth_mint.rs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //! Binary for starting a fake mint with authentication for testing
  2. //!
  3. //! This binary provides a programmatic way to start a fake mint instance with authentication for testing purposes:
  4. //! 1. Sets up a fake mint instance with authentication using the cdk-mintd library
  5. //! 2. Configures OpenID Connect authentication settings
  6. //! 3. Waits for the mint to be ready and responsive
  7. //! 4. Keeps it running until interrupted (Ctrl+C)
  8. //! 5. Gracefully shuts down on receiving shutdown signal
  9. //!
  10. //! This approach offers better control and integration compared to external scripts,
  11. //! making it easier to run authentication integration tests with consistent configuration.
  12. use std::path::Path;
  13. use std::sync::Arc;
  14. use anyhow::Result;
  15. use bip39::Mnemonic;
  16. use cdk_integration_tests::cli::CommonArgs;
  17. use cdk_integration_tests::shared;
  18. use cdk_mintd::config::AuthType;
  19. use clap::Parser;
  20. use tokio::sync::Notify;
  21. use tokio_util::sync::CancellationToken;
  22. #[derive(Parser)]
  23. #[command(name = "start-fake-auth-mint")]
  24. #[command(about = "Start a fake mint with authentication for testing", long_about = None)]
  25. struct Args {
  26. #[command(flatten)]
  27. common: CommonArgs,
  28. /// Database type (sqlite)
  29. database_type: String,
  30. /// Working directory path
  31. work_dir: String,
  32. /// OpenID discovery URL
  33. openid_discovery: String,
  34. /// Port to listen on (default: 8087)
  35. #[arg(default_value_t = 8087)]
  36. port: u16,
  37. }
  38. /// Start a fake mint with authentication using the library
  39. async fn start_fake_auth_mint(
  40. temp_dir: &Path,
  41. database: &str,
  42. port: u16,
  43. openid_discovery: String,
  44. shutdown: Arc<Notify>,
  45. ) -> Result<tokio::task::JoinHandle<()>> {
  46. println!("Starting fake auth mintd on port {port}");
  47. // Create settings struct for fake mint with auth using shared function
  48. let fake_wallet_config = cdk_mintd::config::FakeWallet {
  49. supported_units: vec![cdk::nuts::CurrencyUnit::Sat, cdk::nuts::CurrencyUnit::Usd],
  50. fee_percent: 0.0,
  51. reserve_fee_min: cdk::Amount::from(1),
  52. min_delay_time: 1,
  53. max_delay_time: 3,
  54. };
  55. let mut settings = shared::create_fake_wallet_settings(
  56. port,
  57. database,
  58. Some(Mnemonic::generate(12)?.to_string()),
  59. None,
  60. Some(fake_wallet_config),
  61. );
  62. // Enable authentication
  63. settings.auth = Some(cdk_mintd::config::Auth {
  64. auth_enabled: true,
  65. openid_discovery,
  66. openid_client_id: "cashu-client".to_string(),
  67. mint_max_bat: 50,
  68. mint: AuthType::Blind,
  69. get_mint_quote: AuthType::Blind,
  70. check_mint_quote: AuthType::Blind,
  71. melt: AuthType::Blind,
  72. get_melt_quote: AuthType::Blind,
  73. check_melt_quote: AuthType::Blind,
  74. swap: AuthType::Blind,
  75. restore: AuthType::Blind,
  76. check_proof_state: AuthType::Blind,
  77. });
  78. // Set description for the mint
  79. settings.mint_info.description = "fake test mint with auth".to_string();
  80. let temp_dir = temp_dir.to_path_buf();
  81. let shutdown_clone = shutdown.clone();
  82. // Run the mint in a separate task
  83. let handle = tokio::spawn(async move {
  84. // Create a future that resolves when the shutdown signal is received
  85. let shutdown_future = async move {
  86. shutdown_clone.notified().await;
  87. println!("Fake auth mint shutdown signal received");
  88. };
  89. match cdk_mintd::run_mintd_with_shutdown(&temp_dir, &settings, shutdown_future, None, None)
  90. .await
  91. {
  92. Ok(_) => println!("Fake auth mint exited normally"),
  93. Err(e) => eprintln!("Fake auth mint exited with error: {e}"),
  94. }
  95. });
  96. Ok(handle)
  97. }
  98. #[tokio::main]
  99. async fn main() -> Result<()> {
  100. let args = Args::parse();
  101. // Initialize logging based on CLI arguments
  102. shared::setup_logging(&args.common);
  103. let temp_dir = shared::init_working_directory(&args.work_dir)?;
  104. // Start fake auth mint
  105. let shutdown = shared::create_shutdown_handler();
  106. let shutdown_clone = shutdown.clone();
  107. let handle = start_fake_auth_mint(
  108. &temp_dir,
  109. &args.database_type,
  110. args.port,
  111. args.openid_discovery.clone(),
  112. shutdown_clone,
  113. )
  114. .await?;
  115. let cancel_token = Arc::new(CancellationToken::new());
  116. // Wait for fake auth mint to be ready
  117. if let Err(e) = shared::wait_for_mint_ready_with_shutdown(args.port, 100, cancel_token).await {
  118. eprintln!("Error waiting for fake auth mint: {e}");
  119. return Err(e);
  120. }
  121. println!("Fake auth mint started successfully!");
  122. println!("Fake auth mint: http://127.0.0.1:{}", args.port);
  123. println!("Temp directory: {temp_dir:?}");
  124. println!("Database type: {}", args.database_type);
  125. println!("OpenID Discovery: {}", args.openid_discovery);
  126. println!();
  127. println!("Environment variables needed for tests:");
  128. println!(" CDK_TEST_OIDC_USER=<username>");
  129. println!(" CDK_TEST_OIDC_PASSWORD=<password>");
  130. println!();
  131. println!("You can now run auth integration tests with:");
  132. println!(" cargo test -p cdk-integration-tests --test fake_auth");
  133. println!();
  134. println!("Press Ctrl+C to stop the mint...");
  135. // Wait for Ctrl+C signal
  136. shared::wait_for_shutdown_signal(shutdown).await;
  137. println!("\nReceived Ctrl+C, shutting down mint...");
  138. // Wait for mint to finish gracefully
  139. if let Err(e) = handle.await {
  140. eprintln!("Error waiting for mint to shut down: {e}");
  141. }
  142. println!("Mint shut down successfully");
  143. Ok(())
  144. }