tests/taler: authenticate users with tokens

eljamm 1741d8ab e04f8bb9

Changed files
+31 -7
nixos
tests
taler
common
tests
+16 -2
nixos/tests/taler/common/scripts.nix
···
+ command
)
def verify_balance(balanceWanted: str):
"""Compare Taler CLI wallet balance with expected amount"""
···
client.succeed(f"echo Withdraw successfully made. New balance: {balanceWanted}")
-
def verify_conversion(regionalWanted: str):
"""Compare converted Libeufin Nexus funds with expected regional currency"""
# Get transaction details
response = json.loads(
succeed(bank, [
"curl -sSfL",
# TODO: get exchange from config?
-
"-u exchange:exchange",
"http://bank:8082/accounts/exchange/transactions"
])
)
···
+ command
)
+
# https://docs.taler.net/core/api-corebank.html#authentication
+
def create_token(machine, username, password):
+
"""Create a read-write bank access token for a user"""
+
response = succeed(machine, [
+
"curl -X POST",
+
f"-u {username}:{password}",
+
"-H 'Content-Type: application/json'",
+
"""
+
--data '{ "scope": "readwrite" }'
+
""",
+
f"-sSfL 'http://bank:8082/accounts/{username}/token'"
+
])
+
return json.loads(response)["access_token"]
+
def verify_balance(balanceWanted: str):
"""Compare Taler CLI wallet balance with expected amount"""
···
client.succeed(f"echo Withdraw successfully made. New balance: {balanceWanted}")
+
def verify_conversion(regionalWanted: str, accessToken: str):
"""Compare converted Libeufin Nexus funds with expected regional currency"""
# Get transaction details
response = json.loads(
succeed(bank, [
"curl -sSfL",
+
f"-H 'Authorization: Bearer {accessToken}'",
# TODO: get exchange from config?
"http://bank:8082/accounts/exchange/transactions"
])
)
+15 -5
nixos/tests/taler/tests/basic.nix
···
exchange.start()
exchange.wait_for_open_port(8081)
with subtest("Set up exchange"):
exchange.wait_until_succeeds("taler-exchange-offline download sign upload")
···
merchant.start()
merchant.wait_for_open_port(8083)
with subtest("Set up merchant"):
# Create default instance (similar to admin)
succeed(merchant, [
"curl -X POST",
-
"-H 'Authorization: Bearer secret-token:super_secret'",
"""
--data '{
"auth": { "method": "external" },
···
client.succeed("curl -s http://exchange:8081/")
# Make a withdrawal from the CLI wallet
with subtest("Make a withdrawal from the CLI wallet"):
···
withdrawal = json.loads(
succeed(client, [
"curl -X POST",
-
"-u ${TUSER}:${TPASS}",
"-H 'Content-Type: application/json'",
f"""--data '{{"amount": "{balanceWanted}"}}'""", # double brackets escapes them
"-sSfL 'http://bank:8082/accounts/${TUSER}/withdrawals'"
···
wallet_cli(f"withdraw accept-uri {withdrawal["taler_withdraw_uri"]} --exchange http://exchange:8081/")
succeed(client, [
"curl -X POST",
-
"-u ${TUSER}:${TPASS}",
"-H 'Content-Type: application/json'",
f"-sSfL 'http://bank:8082/accounts/${TUSER}/withdrawals/{withdrawal["withdrawal_id"]}/confirm'"
])
···
with subtest("Libeufin Nexus currency conversion"):
regionalWanted = "20"
# Setup Nexus ebics keys
systemd_run(bank, "libeufin-nexus ebics-setup -L debug -c /etc/libeufin/libeufin.conf", "libeufin-nexus")
# Set currency conversion rates (1:1)
succeed(bank, [
"curl -X POST",
"-H 'Content-Type: application/json'",
-
"-u ${AUSER}:${APASS}",
"""
--data '{
"cashin_ratio": "1",
···
systemd_run(bank, f"""libeufin-nexus testing fake-incoming -c ${bankConfig} --amount="${FIAT_CURRENCY}:{regionalWanted}" --subject="{reservePub}" "payto://iban/CH4740123RW4167362694" """, "libeufin-nexus")
wallet_cli("run-until-done")
-
verify_conversion(regionalWanted)
'';
}
)
···
exchange.start()
exchange.wait_for_open_port(8081)
+
# Create access token for exchange
+
accessTokenExchange = create_token(exchange, "exchange", "exchange")
+
with subtest("Set up exchange"):
exchange.wait_until_succeeds("taler-exchange-offline download sign upload")
···
merchant.start()
merchant.wait_for_open_port(8083)
+
# Create access token for merchant
+
accessTokenMerchant = create_token(client, "merchant", "merchant")
with subtest("Set up merchant"):
# Create default instance (similar to admin)
succeed(merchant, [
"curl -X POST",
+
f"-H 'Authorization: Bearer {accessTokenMerchant}'",
"""
--data '{
"auth": { "method": "external" },
···
client.succeed("curl -s http://exchange:8081/")
+
# Create access token for user
+
accessTokenUser = create_token(client, "${TUSER}", "${TPASS}")
# Make a withdrawal from the CLI wallet
with subtest("Make a withdrawal from the CLI wallet"):
···
withdrawal = json.loads(
succeed(client, [
"curl -X POST",
+
f"-H 'Authorization: Bearer {accessTokenUser}'",
"-H 'Content-Type: application/json'",
f"""--data '{{"amount": "{balanceWanted}"}}'""", # double brackets escapes them
"-sSfL 'http://bank:8082/accounts/${TUSER}/withdrawals'"
···
wallet_cli(f"withdraw accept-uri {withdrawal["taler_withdraw_uri"]} --exchange http://exchange:8081/")
succeed(client, [
"curl -X POST",
+
f"-H 'Authorization: Bearer {accessTokenUser}'",
"-H 'Content-Type: application/json'",
f"-sSfL 'http://bank:8082/accounts/${TUSER}/withdrawals/{withdrawal["withdrawal_id"]}/confirm'"
])
···
with subtest("Libeufin Nexus currency conversion"):
regionalWanted = "20"
+
# Create access token
+
accessTokenAdmin = create_token(bank, "${AUSER}", "${APASS}")
+
# Setup Nexus ebics keys
systemd_run(bank, "libeufin-nexus ebics-setup -L debug -c /etc/libeufin/libeufin.conf", "libeufin-nexus")
# Set currency conversion rates (1:1)
succeed(bank, [
"curl -X POST",
+
f"-H 'Authorization: Bearer {accessTokenAdmin}'",
"-H 'Content-Type: application/json'",
"""
--data '{
"cashin_ratio": "1",
···
systemd_run(bank, f"""libeufin-nexus testing fake-incoming -c ${bankConfig} --amount="${FIAT_CURRENCY}:{regionalWanted}" --subject="{reservePub}" "payto://iban/CH4740123RW4167362694" """, "libeufin-nexus")
wallet_cli("run-until-done")
+
verify_conversion(regionalWanted, accessTokenExchange)
'';
}
)