taler-rust

GNU Taler code in Rust. Largely core banking integrations.
Log | Files | Refs | Submodules | README | LICENSE

api.rs (4265B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2025 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::sync::Arc;
     18 
     19 use jiff::{Timestamp, Zoned};
     20 use sqlx::PgPool;
     21 use taler_api::{api::TalerRouter as _, auth::AuthMethod, subject::OutgoingSubject};
     22 use taler_common::{
     23     api_common::ShortHashCode,
     24     api_revenue::RevenueConfig,
     25     api_wire::{OutgoingHistory, TransferState, WireConfig},
     26     types::{amount::amount, payto::payto, url},
     27 };
     28 use taler_magnet_bank::{
     29     api::MagnetApi,
     30     constants::CONFIG_SOURCE,
     31     db::{self, TxOutKind},
     32     magnet_api::types::TxStatus,
     33     magnet_payto,
     34 };
     35 use taler_test_utils::{
     36     Router, db_test_setup,
     37     routine::{admin_add_incoming_routine, revenue_routine, routine_pagination, transfer_routine},
     38     server::TestServer,
     39 };
     40 
     41 async fn setup() -> (Router, PgPool) {
     42     let pool = db_test_setup(CONFIG_SOURCE).await;
     43     let api = Arc::new(
     44         MagnetApi::start(
     45             pool.clone(),
     46             payto("payto://iban/HU02162000031000164800000000?receiver-name=name"),
     47         )
     48         .await,
     49     );
     50     let server = Router::new()
     51         .wire_gateway(api.clone(), AuthMethod::None)
     52         .revenue(api, AuthMethod::None)
     53         .finalize();
     54 
     55     (server, pool)
     56 }
     57 
     58 #[tokio::test]
     59 async fn config() {
     60     let (server, _) = setup().await;
     61     server
     62         .get("/taler-wire-gateway/config")
     63         .await
     64         .assert_ok_json::<WireConfig>();
     65     server
     66         .get("/taler-revenue/config")
     67         .await
     68         .assert_ok_json::<RevenueConfig>();
     69 }
     70 
     71 #[tokio::test]
     72 async fn transfer() {
     73     let (server, _) = setup().await;
     74     transfer_routine(
     75         &server,
     76         TransferState::pending,
     77         &payto("payto://iban/HU02162000031000164800000000?receiver-name=name"),
     78     )
     79     .await;
     80 }
     81 
     82 #[tokio::test]
     83 async fn outgoing_history() {
     84     let (server, pool) = setup().await;
     85     routine_pagination::<OutgoingHistory, _>(
     86         &server,
     87         "/taler-wire-gateway/history/outgoing",
     88         |it| {
     89             it.outgoing_transactions
     90                 .into_iter()
     91                 .map(|it| *it.row_id as i64)
     92                 .collect()
     93         },
     94         |_, i| {
     95             let acquire = pool.acquire();
     96             async move {
     97                 let mut conn = acquire.await.unwrap();
     98                 let now = Zoned::now().date();
     99                 db::register_tx_out(
    100                     &mut *conn,
    101                     &db::TxOut {
    102                         code: i as u64,
    103                         amount: amount("EUR:10"),
    104                         subject: "subject".to_owned(),
    105                         creditor: magnet_payto(
    106                             "payto://iban/HU30162000031000163100000000?receiver-name=name",
    107                         ),
    108                         value_date: now,
    109                         status: TxStatus::Completed,
    110                     },
    111                     &TxOutKind::Talerable(OutgoingSubject(
    112                         ShortHashCode::rand(),
    113                         url("https://exchange.test"),
    114                     )),
    115                     &Timestamp::now(),
    116                 )
    117                 .await
    118                 .unwrap();
    119             }
    120         },
    121     )
    122     .await;
    123 }
    124 
    125 #[tokio::test]
    126 async fn admin_add_incoming() {
    127     let (server, _) = setup().await;
    128     admin_add_incoming_routine(
    129         &server,
    130         &payto("payto://iban/HU02162000031000164800000000?receiver-name=name"),
    131         true,
    132     )
    133     .await;
    134 }
    135 
    136 #[tokio::test]
    137 async fn revenue() {
    138     let (server, _) = setup().await;
    139     revenue_routine(
    140         &server,
    141         &payto("payto://iban/HU02162000031000164800000000?receiver-name=name"),
    142         true,
    143     )
    144     .await;
    145 }