summaryrefslogtreecommitdiff
path: root/packages/taler-harness/src/integrationtests/test-wallet-backup-basic.ts
blob: cb4a50a2b97139e893b15ece8ef35b4119a2a2a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 This file is part of GNU Taler
 (C) 2020 Taler Systems S.A.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

/**
 * Imports.
 */
import { j2s } from "@gnu-taler/taler-util";
import { WalletApiOperation } from "@gnu-taler/taler-wallet-core";
import { GlobalTestState } from "../harness/harness.js";
import {
  createSimpleTestkudosEnvironmentV2,
  createWalletDaemonWithClient,
  withdrawViaBankV2,
} from "../harness/helpers.js";
import { SyncService } from "../harness/sync.js";

/**
 * Run test for basic, bank-integrated withdrawal.
 */
export async function runWalletBackupBasicTest(t: GlobalTestState) {
  // Set up test environment

  const { commonDb, merchant, walletClient, bank, exchange } =
    await createSimpleTestkudosEnvironmentV2(t);

  const sync = await SyncService.create(t, {
    currency: "TESTKUDOS",
    annualFee: "TESTKUDOS:0.5",
    database: commonDb.connStr,
    fulfillmentUrl: "taler://fulfillment-success",
    httpPort: 8089,
    name: "sync1",
    paymentBackendUrl: merchant.makeInstanceBaseUrl(),
    uploadLimitMb: 10,
  });

  await sync.start();
  await sync.pingUntilAvailable();

  await walletClient.call(WalletApiOperation.AddBackupProvider, {
    backupProviderBaseUrl: sync.baseUrl,
    activate: false,
    name: sync.baseUrl,
  });

  {
    const bi = await walletClient.call(WalletApiOperation.GetBackupInfo, {});
    t.assertDeepEqual(bi.providers[0].active, false);
  }

  await walletClient.call(WalletApiOperation.AddBackupProvider, {
    backupProviderBaseUrl: sync.baseUrl,
    activate: true,
    name: sync.baseUrl,
  });

  {
    const bi = await walletClient.call(WalletApiOperation.GetBackupInfo, {});
    t.assertDeepEqual(bi.providers[0].active, true);
  }

  await walletClient.call(WalletApiOperation.RunBackupCycle, {});

  {
    const bi = await walletClient.call(WalletApiOperation.GetBackupInfo, {});
    console.log(bi);
    t.assertDeepEqual(
      bi.providers[0].paymentStatus.type,
      "insufficient-balance",
    );
  }

  await withdrawViaBankV2(t, {
    walletClient,
    bank,
    exchange,
    amount: "TESTKUDOS:10",
  });

  await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {});

  await walletClient.call(WalletApiOperation.RunBackupCycle, {});

  {
    const bi = await walletClient.call(WalletApiOperation.GetBackupInfo, {});
    console.log(bi);
  }

  await withdrawViaBankV2(t, {
    walletClient,
    bank,
    exchange,
    amount: "TESTKUDOS:5",
  });

  await walletClient.call(WalletApiOperation.RunBackupCycle, {});

  {
    const bi = await walletClient.call(WalletApiOperation.GetBackupInfo, {});
    console.log(bi);
  }

  const backupRecovery = await walletClient.call(
    WalletApiOperation.ExportBackupRecovery,
    {},
  );

  const txs = await walletClient.call(WalletApiOperation.GetTransactions, {});
  console.log(`backed up transactions ${j2s(txs)}`);

  const { walletClient: walletClient2 } = await createWalletDaemonWithClient(
    t,
    {
      name: "w2",
    },
  );

  // Check that the second wallet is a fresh wallet.
  {
    const bal = await walletClient2.call(WalletApiOperation.GetBalances, {});
    t.assertTrue(bal.balances.length === 0);
  }

  await walletClient2.call(WalletApiOperation.ImportBackupRecovery, {
    recovery: backupRecovery,
  });

  await walletClient2.call(WalletApiOperation.RunBackupCycle, {});

  // Check that now the old balance is available!
  {
    const bal = await walletClient2.call(WalletApiOperation.GetBalances, {});
    t.assertTrue(bal.balances.length === 1);
    console.log(bal);
  }

  // Now do some basic checks that the restored wallet is still functional
  {
    const txs = await walletClient2.call(
      WalletApiOperation.GetTransactions,
      {},
    );
    console.log(`restored transactions ${j2s(txs)}`);
    const bal1 = await walletClient2.call(WalletApiOperation.GetBalances, {});

    t.assertAmountEquals(bal1.balances[0].available, "TESTKUDOS:14.1");

    await withdrawViaBankV2(t, {
      walletClient: walletClient2,
      bank,
      exchange,
      amount: "TESTKUDOS:10",
    });

    await exchange.runWirewatchOnce();

    await walletClient2.call(
      WalletApiOperation.TestingWaitTransactionsFinal,
      {},
    );

    const txs2 = await walletClient2.call(
      WalletApiOperation.GetTransactions,
      {},
    );
    console.log(`tx after withdraw after restore ${j2s(txs2)}`);

    const bal2 = await walletClient2.call(WalletApiOperation.GetBalances, {});

    t.assertAmountEquals(bal2.balances[0].available, "TESTKUDOS:23.82");
  }
}

runWalletBackupBasicTest.suites = ["wallet", "wallet-backup"];
// See https://bugs.taler.net/n/7598
runWalletBackupBasicTest.experimental = true;