api.rs (4308B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2025, 2026 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 use std::{str::FromStr, sync::Arc}; 18 19 use compact_str::CompactString; 20 use jiff::Timestamp; 21 use sqlx::PgPool; 22 use taler_api::{api::TalerRouter as _, auth::AuthMethod, subject::OutgoingSubject}; 23 use taler_common::{ 24 api_revenue::RevenueConfig, 25 api_wire::{OutgoingHistory, TransferState, WireConfig}, 26 types::{ 27 amount::{Currency, decimal}, 28 payto::payto, 29 }, 30 }; 31 use taler_cyclos::{ 32 api::CyclosApi, 33 constants::CONFIG_SOURCE, 34 db::{self, TxOutKind}, 35 }; 36 use taler_test_utils::{ 37 Router, 38 db::db_test_setup, 39 routine::{admin_add_incoming_routine, revenue_routine, routine_pagination, transfer_routine}, 40 server::TestServer, 41 }; 42 43 async fn setup() -> (Router, PgPool) { 44 let pool = db_test_setup(CONFIG_SOURCE).await; 45 let api = Arc::new( 46 CyclosApi::start( 47 pool.clone(), 48 CompactString::const_new("localhost"), 49 payto("payto://iban/HU02162000031000164800000000?receiver-name=name"), 50 Currency::from_str("TEST").unwrap(), 51 ) 52 .await, 53 ); 54 let server = Router::new() 55 .wire_gateway(api.clone(), AuthMethod::None) 56 .revenue(api, AuthMethod::None) 57 .finalize(); 58 59 (server, pool) 60 } 61 62 #[tokio::test] 63 async fn config() { 64 let (server, _) = setup().await; 65 server 66 .get("/taler-wire-gateway/config") 67 .await 68 .assert_ok_json::<WireConfig>(); 69 server 70 .get("/taler-revenue/config") 71 .await 72 .assert_ok_json::<RevenueConfig>(); 73 } 74 75 #[tokio::test] 76 async fn transfer() { 77 let (server, _) = setup().await; 78 transfer_routine( 79 &server, 80 TransferState::pending, 81 &payto("payto://cyclos/localhost/7762070814178012479?receiver-name=name"), 82 ) 83 .await; 84 } 85 86 #[tokio::test] 87 async fn outgoing_history() { 88 let (server, pool) = setup().await; 89 routine_pagination::<OutgoingHistory, _>( 90 &server, 91 "/taler-wire-gateway/history/outgoing", 92 |it| { 93 it.outgoing_transactions 94 .into_iter() 95 .map(|it| *it.row_id as i64) 96 .collect() 97 }, 98 |_, i| { 99 let acquire = pool.acquire(); 100 async move { 101 let mut conn = acquire.await.unwrap(); 102 db::register_tx_out( 103 &mut *conn, 104 &db::TxOut { 105 transfer_id: i as i64, 106 tx_id: if i % 2 == 0 { 107 Some((i % 2) as i64) 108 } else { 109 None 110 }, 111 amount: decimal("10"), 112 subject: "subject".to_owned(), 113 creditor_id: 31000163100000000, 114 creditor_name: "Name".to_string(), 115 valued_at: Timestamp::now(), 116 }, 117 &TxOutKind::Talerable(OutgoingSubject::rand()), 118 &Timestamp::now(), 119 ) 120 .await 121 .unwrap(); 122 } 123 }, 124 ) 125 .await; 126 } 127 128 #[tokio::test] 129 async fn admin_add_incoming() { 130 let (server, _) = setup().await; 131 admin_add_incoming_routine( 132 &server, 133 &payto("payto://cyclos/localhost/7762070814178012479?receiver-name=name"), 134 true, 135 ) 136 .await; 137 } 138 139 #[tokio::test] 140 async fn revenue() { 141 let (server, _) = setup().await; 142 revenue_routine( 143 &server, 144 &payto("payto://cyclos/localhost/7762070814178012479?receiver-name=name"), 145 true, 146 ) 147 .await; 148 }