aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-harness/src/integrationtests/test-kyc-merchant-deposit.ts
blob: 3a81867a929db265d1cbec2266562ee0e8711988 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 This file is part of GNU Taler
 (C) 2024 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 {
  codecForAccountKycRedirects,
  codecForKycProcessClientInformation,
  codecForQueryInstancesResponse,
  encodeCrock,
  hashPaytoUri,
  j2s,
  Logger,
  MerchantAccountKycRedirectsResponse,
  TalerMerchantApi,
  WireGatewayApiClient,
} from "@gnu-taler/taler-util";
import {
  readResponseJsonOrThrow,
  readSuccessResponseJsonOrThrow,
} from "@gnu-taler/taler-util/http";
import {
  createKycTestkudosEnvironment,
  postAmlDecisionNoRules,
  withdrawViaBankV3,
} from "../harness/environments.js";
import { delayMs, GlobalTestState, harnessHttpLib } from "../harness/harness.js";

const logger = new Logger(`test-kyc-merchant-deposit.ts`);

export async function runKycMerchantDepositTest(t: GlobalTestState) {
  // Set up test environment

  const {
    merchant,
    walletClient,
    bankClient,
    exchange,
    exchangeBankAccount,
    amlKeypair,
  } = await createKycTestkudosEnvironment(t, {
    adjustExchangeConfig(config) {
      config.setString("exchange", "enable_kyc", "yes");

      config.setString("KYC-RULE-R1", "operation_type", "deposit");
      config.setString("KYC-RULE-R1", "enabled", "yes");
      config.setString("KYC-RULE-R1", "exposed", "yes");
      config.setString("KYC-RULE-R1", "is_and_combinator", "yes");
      config.setString("KYC-RULE-R1", "threshold", "TESTKUDOS:0");
      config.setString("KYC-RULE-R1", "timeframe", "1d");
      config.setString("KYC-RULE-R1", "next_measures", "M1");

      config.setString("KYC-MEASURE-M1", "check_name", "C1");
      config.setString("KYC-MEASURE-M1", "context", "{}");
      config.setString("KYC-MEASURE-M1", "program", "P1");

      config.setString("AML-PROGRAM-P1", "command", "/bin/true");
      config.setString("AML-PROGRAM-P1", "enabled", "true");
      config.setString("AML-PROGRAM-P1", "description", "this does nothing");
      config.setString("AML-PROGRAM-P1", "fallback", "M1");

      config.setString("KYC-CHECK-C1", "type", "INFO");
      config.setString("KYC-CHECK-C1", "description", "my check!");
      config.setString("KYC-CHECK-C1", "fallback", "M1");
    },
  });

  let accountPub: string;

  {
    const instanceUrl = new URL("private", merchant.makeInstanceBaseUrl());
    const resp = await harnessHttpLib.fetch(instanceUrl.href);
    const parsedResp = await readSuccessResponseJsonOrThrow(
      resp,
      codecForQueryInstancesResponse(),
    );
    accountPub = parsedResp.merchant_pub;
  }

  const wireGatewayApiClient = new WireGatewayApiClient(
    exchangeBankAccount.wireGatewayApiBaseUrl,
    {
      auth: {
        username: "admin",
        password: "admin-password",
      },
    },
  );

  // Withdraw digital cash into the wallet.

  const wres = await withdrawViaBankV3(t, {
    bankClient,
    amount: "TESTKUDOS:50",
    exchange: exchange,
    walletClient: walletClient,
  });

  await wres.withdrawalFinishedCond;

  let kycRespOne: MerchantAccountKycRedirectsResponse | undefined = undefined;

  while (1) {
    const kycStatusUrl = new URL("private/kyc", merchant.makeInstanceBaseUrl())
      .href;
    logger.info(`requesting GET ${kycStatusUrl}`);
    const resp = await harnessHttpLib.fetch(kycStatusUrl);
    if (resp.status === 200) {
      kycRespOne = await readSuccessResponseJsonOrThrow(
        resp,
        codecForAccountKycRedirects(),
      );
      break;
    }
    // Wait 500ms
    await delayMs(500);
  }

  t.assertTrue(!!kycRespOne);

  logger.info(`mechant kyc status: ${j2s(kycRespOne)}`);

  t.assertDeepEqual(kycRespOne.kyc_data[0].exchange_http_status, 404);

  t.assertTrue(
    (kycRespOne.kyc_data[0].limits?.length ?? 0) > 0,
    "kyc status should contain non-empty limits",
  );

  // Order creation should fail!
  await t.runSpanAsync("order creation should be rejected", async () => {
    let url = new URL("private/orders", merchant.makeInstanceBaseUrl());
    const order = {
      summary: "Test",
      amount: "TESTKUDOS:5",
      fulfillment_url: "taler://fulfillment-success/thx",
    } satisfies TalerMerchantApi.Order;
    const resp = await harnessHttpLib.fetch(url.href, {
      method: "POST",
      body: {
        order,
      },
    });

    logger.info(`order creation status: ${resp.status}`);
    t.assertTrue(resp.status !== 200);
  });

  await bankClient.registerAccountExtended({
    name: "merchant-default",
    password: "merchant-default",
    username: "merchant-default",
    payto_uri: kycRespOne.kyc_data[0].payto_uri, //this bank user needs to have the same payto that the exchange is asking from
  });
  await wireGatewayApiClient.adminAddKycauth({
    amount: "TESTKUDOS:0.1",
    debitAccountPayto: kycRespOne.kyc_data[0].payto_uri,
    accountPub,
  });

  let kycRespTwo: MerchantAccountKycRedirectsResponse | undefined = undefined;

  // We do this in a loop as a work-around.
  // Not exactly the correct behavior from the merchant right now.
  while (true) {
    const kycStatusLongpollUrl = new URL(
      "private/kyc",
      merchant.makeInstanceBaseUrl(),
    );
    kycStatusLongpollUrl.searchParams.set("lpt", "1");
    const resp = await harnessHttpLib.fetch(kycStatusLongpollUrl.href);
    t.assertDeepEqual(resp.status, 200);
    const parsedResp = await readSuccessResponseJsonOrThrow(
      resp,
      codecForAccountKycRedirects(),
    );
    logger.info(`kyc resp 2: ${j2s(parsedResp)}`);
    if (parsedResp.kyc_data[0].payto_kycauths == null) {
      kycRespTwo = parsedResp;
      break;
    }
    // Wait 500ms
    await delayMs(500);
  }

  t.assertTrue(!!kycRespTwo);

  await postAmlDecisionNoRules(t, {
    amlPriv: amlKeypair.priv,
    amlPub: amlKeypair.pub,
    exchangeBaseUrl: exchange.baseUrl,
    paytoHash: encodeCrock(hashPaytoUri(kycRespTwo.kyc_data[0].payto_uri)),
  });

  while (true) {
    // We first POST an order to trigger checking of the KYC status,
    // as no requirement is active.
    let url = new URL("private/orders", merchant.makeInstanceBaseUrl());
    const order = {
      summary: "Test",
      amount: "TESTKUDOS:5",
      fulfillment_url: "taler://fulfillment-success/thx",
    } satisfies TalerMerchantApi.Order;
    const postOrderResp = await harnessHttpLib.fetch(url.href, {
      method: "POST",
      body: {
        order,
      },
    });

    logger.info(`POST /private/orders status: ${postOrderResp.status}`);

    const kycStatusLongpollUrl = new URL(
      "private/kyc",
      merchant.makeInstanceBaseUrl(),
    );
    kycStatusLongpollUrl.searchParams.set("lpt", "3");
    const resp = await harnessHttpLib.fetch(kycStatusLongpollUrl.href);
    t.assertDeepEqual(resp.status, 200);
    const parsedResp = await readSuccessResponseJsonOrThrow(
      resp,
      codecForAccountKycRedirects(),
    );
    logger.info(`kyc resp 3: ${j2s(parsedResp)}`);
    if ((parsedResp.kyc_data[0].limits?.length ?? 0) == 0) {
      break;
    }

    const accessToken = parsedResp.kyc_data[0].access_token;

    t.assertTrue(!!accessToken);

    const infoResp = await harnessHttpLib.fetch(
      new URL(`kyc-info/${accessToken}`, exchange.baseUrl).href,
    );

    const clientInfo = await readResponseJsonOrThrow(
      infoResp,
      codecForKycProcessClientInformation(),
    );

    logger.info(`kyc-info: ${j2s(clientInfo)}`);

    // Wait 500ms
    await delayMs(500);
  }
}

runKycMerchantDepositTest.suites = ["wallet", "merchant", "kyc"];